Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #267 from Thomas55555/number_entity-for-park-and-s…
Browse files Browse the repository at this point in the history
…tart

number_entity for park and start
  • Loading branch information
Thomas55555 authored Jun 29, 2022
2 parents 4615164 + e57dc74 commit 4ab5a85
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion custom_components/husqvarna_automower/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
CONF_PASSWORD,
CONF_TOKEN,
CONF_USERNAME,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
from homeassistant.helpers.typing import ConfigType
from homeassistant.const import Platform

from . import config_flow
from .const import (
Expand Down
18 changes: 8 additions & 10 deletions custom_components/husqvarna_automower/camera.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
"""Platform for Husqvarna Automower camera integration."""

import logging
import io
import logging
import math
import numpy as np
from typing import Optional

from PIL import Image, ImageDraw
from typing import Optional
import numpy as np

from homeassistant.components.camera import SUPPORT_ON_OFF, Camera
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.camera import Camera, SUPPORT_ON_OFF

from .entity import AutomowerEntity
from .vacuum import HusqvarnaAutomowerStateMixin

from .const import (
DOMAIN,
ENABLE_CAMERA,
GPS_TOP_LEFT,
GPS_BOTTOM_RIGHT,
MOWER_IMG_PATH,
GPS_TOP_LEFT,
MAP_IMG_PATH,
MOWER_IMG_PATH,
)

from .entity import AutomowerEntity
from .vacuum import HusqvarnaAutomowerStateMixin

GpsPoint = tuple[float, float]
ImgPoint = tuple[int, int]
Expand Down
10 changes: 4 additions & 6 deletions custom_components/husqvarna_automower/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import voluptuous as vol

from aioautomower import GetAccessToken, GetMowerData, TokenError
from homeassistant import data_entry_flow, config_entries


from homeassistant import config_entries, data_entry_flow
from homeassistant.const import (
ATTR_CREDENTIALS,
CONF_ACCESS_TOKEN,
Expand All @@ -18,19 +16,19 @@
CONF_TOKEN,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.network import get_url
from homeassistant.core import callback

from .const import (
CONF_PROVIDER,
CONF_TOKEN_TYPE,
DOMAIN,
ENABLE_CAMERA,
GPS_TOP_LEFT,
GPS_BOTTOM_RIGHT,
MOWER_IMG_PATH,
GPS_TOP_LEFT,
MAP_IMG_PATH,
MOWER_IMG_PATH,
)

_LOGGER = logging.getLogger(__name__)
Expand Down
59 changes: 58 additions & 1 deletion custom_components/husqvarna_automower/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import json
import logging

from homeassistant.components.number import NumberEntity
from aiohttp import ClientResponseError

from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TIME_MINUTES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -27,6 +30,29 @@ async def async_setup_entry(
for idx, ent in enumerate(session.data["data"])
if "4" in session.data["data"][idx]["attributes"]["system"]["model"]
)
async_add_entities(
AutomowerParkStartNumberEntity(session, idx, description)
for idx, ent in enumerate(session.data["data"])
for description in NUMBER_SENSOR_TYPES
)


NUMBER_SENSOR_TYPES: tuple[NumberEntityDescription, ...] = (
NumberEntityDescription(
key="Park",
name="Park for",
icon="mdi:clock-outline",
entity_registry_enabled_default=True,
native_unit_of_measurement=TIME_MINUTES,
),
NumberEntityDescription(
key="Start",
name="Mow for",
icon="mdi:clock-outline",
entity_registry_enabled_default=True,
native_unit_of_measurement=TIME_MINUTES,
),
)


class AutomowerNumber(NumberEntity, AutomowerEntity):
Expand Down Expand Up @@ -68,3 +94,34 @@ async def async_set_native_value(self, value: float) -> None:
await self.session.action(self.mower_id, payload, command_type)
except Exception as exception:
raise UpdateFailed(exception) from exception


class AutomowerParkStartNumberEntity(NumberEntity, AutomowerEntity):
"""Defining the AutomowerParkStartNumberEntity."""

_attr_native_value: float = 1
_attr_native_min_value: float = 1
_attr_native_max_value: float = 60480
_attr_native_step: float = 1

def __init__(self, session, idx, description: NumberEntityDescription):
super().__init__(session, idx)
self.description = description
self.entity_description = description
self._attr_unique_id = f"{self.mower_id}_{description.key}"

async def async_set_native_value(self, value: float) -> None:
"""Change the value."""
command_type = "actions"
duration = int(value)
string = {
"data": {
"type": self.entity_description.key,
"attributes": {"duration": duration},
}
}
payload = json.dumps(string)
try:
await self.session.action(self.mower_id, payload, command_type)
except ClientResponseError as exception:
_LOGGER.error("Command couldn't be sent to the command que: %s", exception)
3 changes: 3 additions & 0 deletions custom_components/husqvarna_automower/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ async def async_return_to_base(self, **kwargs) -> None:

async def async_park_and_start(self, command, duration, **kwargs) -> None:
"""Sends a custom command to the mower."""
_LOGGER.warning(
"The service `park_and_start` is depracated. Please use the number entites `number.park_for` or `number.mow_for` instead"
)
command_type = "actions"
string = {
"data": {
Expand Down

0 comments on commit 4ab5a85

Please sign in to comment.