1 """Creates the number entities for the mower."""
3 from collections.abc
import Awaitable, Callable
4 from dataclasses
import dataclass
6 from typing
import TYPE_CHECKING, Any
8 from aioautomower.model
import MowerAttributes, WorkArea
9 from aioautomower.session
import AutomowerSession
16 from .
import AutomowerConfigEntry, remove_work_area_entities
17 from .coordinator
import AutomowerDataUpdateCoordinator
19 AutomowerControlEntity,
20 WorkAreaControlEntity,
21 _work_area_translation_key,
22 handle_sending_exception,
25 _LOGGER = logging.getLogger(__name__)
32 """Return the cutting height."""
35 assert data.settings.cutting_height
is not None
36 return data.settings.cutting_height
40 coordinator: AutomowerDataUpdateCoordinator,
45 """Set cutting height for work area."""
46 await coordinator.api.commands.workarea_settings(
47 mower_id,
int(cheight), work_area_id
52 session: AutomowerSession,
56 """Set cutting height."""
57 await session.commands.set_cutting_height(mower_id,
int(cheight))
60 @dataclass(frozen=True, kw_only=True)
62 """Describes Automower number entity."""
64 exists_fn: Callable[[MowerAttributes], bool] =
lambda _:
True
65 value_fn: Callable[[MowerAttributes], int]
66 set_value_fn: Callable[[AutomowerSession, str, float], Awaitable[Any]]
69 MOWER_NUMBER_TYPES: tuple[AutomowerNumberEntityDescription, ...] = (
72 translation_key=
"cutting_height",
73 entity_registry_enabled_default=
False,
74 entity_category=EntityCategory.CONFIG,
77 exists_fn=
lambda data: data.settings.cutting_height
is not None,
78 value_fn=_async_get_cutting_height,
79 set_value_fn=async_set_cutting_height,
84 @dataclass(frozen=True, kw_only=True)
86 """Describes Automower work area number entity."""
88 value_fn: Callable[[WorkArea], int]
89 translation_key_fn: Callable[[int, str], str]
90 set_value_fn: Callable[
91 [AutomowerDataUpdateCoordinator, str, float, int], Awaitable[Any]
95 WORK_AREA_NUMBER_TYPES: tuple[WorkAreaNumberEntityDescription, ...] = (
97 key=
"cutting_height_work_area",
98 translation_key_fn=_work_area_translation_key,
99 entity_category=EntityCategory.CONFIG,
100 native_unit_of_measurement=PERCENTAGE,
101 value_fn=
lambda data: data.cutting_height,
102 set_value_fn=async_set_work_area_cutting_height,
109 entry: AutomowerConfigEntry,
110 async_add_entities: AddEntitiesCallback,
112 """Set up number platform."""
113 coordinator = entry.runtime_data
114 current_work_areas: dict[str, set[int]] = {}
118 for mower_id
in coordinator.data
119 for description
in MOWER_NUMBER_TYPES
120 if description.exists_fn(coordinator.data[mower_id])
123 def _async_work_area_listener() -> None:
124 """Listen for new work areas and add/remove entities as needed."""
125 for mower_id
in coordinator.data:
127 coordinator.data[mower_id].capabilities.work_areas
128 and (_work_areas := coordinator.data[mower_id].work_areas)
is not None
130 received_work_areas = set(_work_areas.keys())
131 current_work_area_set = current_work_areas.setdefault(mower_id, set())
133 new_work_areas = received_work_areas - current_work_area_set
134 removed_work_areas = current_work_area_set - received_work_areas
137 current_work_area_set.update(new_work_areas)
140 mower_id, coordinator, description, work_area_id
142 for description
in WORK_AREA_NUMBER_TYPES
143 for work_area_id
in new_work_areas
146 if removed_work_areas:
148 current_work_area_set.difference_update(removed_work_areas)
150 coordinator.async_add_listener(_async_work_area_listener)
151 _async_work_area_listener()
155 """Defining the AutomowerNumberEntity with AutomowerNumberEntityDescription."""
157 entity_description: AutomowerNumberEntityDescription
162 coordinator: AutomowerDataUpdateCoordinator,
163 description: AutomowerNumberEntityDescription,
165 """Set up AutomowerNumberEntity."""
166 super().
__init__(mower_id, coordinator)
172 """Return the state of the number."""
175 @handle_sending_exception()
177 """Change to new number value."""
179 self.coordinator.api, self.
mower_idmower_id, value
184 """Defining the WorkAreaNumberEntity with WorkAreaNumberEntityDescription."""
186 entity_description: WorkAreaNumberEntityDescription
191 coordinator: AutomowerDataUpdateCoordinator,
192 description: WorkAreaNumberEntityDescription,
195 """Set up AutomowerNumberEntity."""
196 super().
__init__(mower_id, coordinator, work_area_id)
205 """Return the translation key of the work area."""
212 """Return the state of the number."""
215 @handle_sending_exception(poll_after_sending=True)
217 """Change to new number value."""
MowerAttributes mower_attributes(self)
WorkArea work_area_attributes(self)
None async_set_native_value(self, float value)
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator, AutomowerNumberEntityDescription description)
str translation_key(self)
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator, WorkAreaNumberEntityDescription description, int work_area_id)
_attr_translation_placeholders
None async_set_native_value(self, float value)
int _async_get_cutting_height(MowerAttributes data)
None async_set_cutting_height(AutomowerSession session, str mower_id, float cheight)
None async_setup_entry(HomeAssistant hass, AutomowerConfigEntry entry, AddEntitiesCallback async_add_entities)
None async_set_work_area_cutting_height(AutomowerDataUpdateCoordinator coordinator, str mower_id, float cheight, int work_area_id)
None remove_work_area_entities(HomeAssistant hass, ConfigEntry config_entry, set[int] removed_work_areas, str mower_id)