1 """Platform for Husqvarna Automower base entity."""
4 from collections.abc
import Awaitable, Callable, Coroutine
7 from typing
import TYPE_CHECKING, Any
9 from aioautomower.exceptions
import ApiException
10 from aioautomower.model
import MowerActivities, MowerAttributes, MowerStates, WorkArea
17 from .
import AutomowerDataUpdateCoordinator
18 from .const
import DOMAIN, EXECUTION_TIME_DELAY
20 _LOGGER = logging.getLogger(__name__)
23 MowerActivities.STOPPED_IN_GARDEN,
24 MowerActivities.UNKNOWN,
25 MowerActivities.NOT_APPLICABLE,
28 MowerStates.FATAL_ERROR,
30 MowerStates.ERROR_AT_POWER_UP,
31 MowerStates.NOT_APPLICABLE,
40 """Check if the mower has any errors."""
42 mower_attributes.mower.state
not in ERROR_STATES
43 or mower_attributes.mower.activity
not in ERROR_ACTIVITIES
49 """Return the translation key."""
51 return f
"my_lawn_{key}"
52 return f
"work_area_{key}"
56 poll_after_sending: bool =
False,
58 [Callable[..., Awaitable[Any]]], Callable[..., Coroutine[Any, Any,
None]]
60 """Handle exceptions while sending a command and optionally refresh coordinator."""
63 func: Callable[..., Awaitable[Any]],
64 ) -> Callable[..., Coroutine[Any, Any,
None]]:
65 @functools.wraps(func)
66 async
def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
68 await func(self, *args, **kwargs)
69 except ApiException
as exception:
71 translation_domain=DOMAIN,
72 translation_key=
"command_send_failed",
73 translation_placeholders={
"exception":
str(exception)},
76 if poll_after_sending:
79 await asyncio.sleep(EXECUTION_TIME_DELAY)
80 await self.coordinator.async_request_refresh()
88 """Defining the Automower base Entity."""
90 _attr_has_entity_name =
True
95 coordinator: AutomowerDataUpdateCoordinator,
97 """Initialize AutomowerEntity."""
101 identifiers={(DOMAIN, mower_id)},
102 manufacturer=
"Husqvarna",
105 ).removeprefix(
"Husqvarna "),
108 suggested_area=
"Garden",
113 """Get the mower attributes of the current mower."""
114 return self.coordinator.data[self.
mower_idmower_id]
118 """Replies available when the mower is connected."""
122 """Return True if the device is available."""
123 return super().available
and self.
mower_attributesmower_attributes.metadata.connected
127 """Replies available when the mower is connected and not in error state."""
131 """Return True if the device is available."""
136 """Base entity for work work areas."""
141 coordinator: AutomowerDataUpdateCoordinator,
144 """Initialize AutomowerEntity."""
145 super().
__init__(mower_id, coordinator)
150 """Get the work areas from the mower attributes."""
157 """Get the work area attributes of the current work area."""
162 """Return True if the work area is available and the mower has no errors."""
167 """Base entity work work areas with control function."""
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator)
MowerAttributes mower_attributes(self)
dict[int, WorkArea] work_areas(self)
WorkArea work_area_attributes(self)
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator, int work_area_id)
str _work_area_translation_key(int work_area_id, str key)
bool _check_error_free(MowerAttributes mower_attributes)
Callable[[Callable[..., Awaitable[Any]]], Callable[..., Coroutine[Any, Any, None]]] handle_sending_exception(bool poll_after_sending=False)