1 """Creates a switch entity for the mower."""
4 from typing
import TYPE_CHECKING, Any
6 from aioautomower.model
import MowerModes, StayOutZones, Zone
13 from .
import AutomowerConfigEntry
14 from .coordinator
import AutomowerDataUpdateCoordinator
16 AutomowerControlEntity,
17 WorkAreaControlEntity,
18 _work_area_translation_key,
19 handle_sending_exception,
24 _LOGGER = logging.getLogger(__name__)
29 entry: AutomowerConfigEntry,
30 async_add_entities: AddEntitiesCallback,
32 """Set up switch platform."""
33 coordinator = entry.runtime_data
34 current_work_areas: dict[str, set[int]] = {}
35 current_stay_out_zones: dict[str, set[str]] = {}
39 for mower_id
in coordinator.data
42 def _async_work_area_listener() -> None:
43 """Listen for new work areas and add switch entities if they did not exist.
45 Listening for deletable work areas is managed in the number platform.
47 for mower_id
in coordinator.data:
49 coordinator.data[mower_id].capabilities.work_areas
50 and (_work_areas := coordinator.data[mower_id].work_areas)
is not None
52 received_work_areas = set(_work_areas.keys())
53 new_work_areas = received_work_areas - current_work_areas.get(
57 current_work_areas.setdefault(mower_id, set()).
update(
62 for work_area_id
in new_work_areas
65 def _remove_stay_out_zone_entities(
66 removed_stay_out_zones: set, mower_id: str
68 """Remove all unused stay-out zones for all platforms."""
69 entity_reg = er.async_get(hass)
70 for entity_entry
in er.async_entries_for_config_entry(
71 entity_reg, entry.entry_id
73 for stay_out_zone_uid
in removed_stay_out_zones:
74 if entity_entry.unique_id.startswith(f
"{mower_id}_{stay_out_zone_uid}"):
75 entity_reg.async_remove(entity_entry.entity_id)
77 def _async_stay_out_zone_listener() -> None:
78 """Listen for new stay-out zones and add/remove switch entities if they did not exist."""
79 for mower_id
in coordinator.data:
81 coordinator.data[mower_id].capabilities.stay_out_zones
82 and (_stay_out_zones := coordinator.data[mower_id].stay_out_zones)
85 received_stay_out_zones = set(_stay_out_zones.zones)
86 current_stay_out_zones_set = current_stay_out_zones.get(mower_id, set())
87 new_stay_out_zones = (
88 received_stay_out_zones - current_stay_out_zones_set
90 removed_stay_out_zones = (
91 current_stay_out_zones_set - received_stay_out_zones
93 if new_stay_out_zones:
94 current_stay_out_zones.setdefault(mower_id, set()).
update(
99 coordinator, mower_id, stay_out_zone_uid
101 for stay_out_zone_uid
in new_stay_out_zones
103 if removed_stay_out_zones:
104 _remove_stay_out_zone_entities(removed_stay_out_zones, mower_id)
106 coordinator.async_add_listener(_async_work_area_listener)
107 coordinator.async_add_listener(_async_stay_out_zone_listener)
108 _async_work_area_listener()
109 _async_stay_out_zone_listener()
113 """Defining the Automower schedule switch."""
115 _attr_translation_key =
"enable_schedule"
120 coordinator: AutomowerDataUpdateCoordinator,
122 """Set up Automower switch."""
123 super().
__init__(mower_id, coordinator)
128 """Return the state of the switch."""
131 @handle_sending_exception()
133 """Turn the entity off."""
134 await self.coordinator.api.commands.park_until_further_notice(self.
mower_idmower_id)
136 @handle_sending_exception()
138 """Turn the entity on."""
139 await self.coordinator.api.commands.resume_schedule(self.
mower_idmower_id)
143 """Defining the Automower stay out zone switch."""
145 _attr_translation_key =
"stay_out_zones"
149 coordinator: AutomowerDataUpdateCoordinator,
151 stay_out_zone_uid: str,
153 """Set up Automower switch."""
154 super().
__init__(mower_id, coordinator)
158 f
"{self.mower_id}_{stay_out_zone_uid}_{self._attr_translation_key}"
164 """Return all stay out zones."""
171 """Return the specific stay out zone."""
176 """Return the state of the switch."""
181 """Return True if the device is available and the zones are not `dirty`."""
182 return super().available
and not self.
stay_out_zonesstay_out_zones.dirty
184 @handle_sending_exception(poll_after_sending=True)
186 """Turn the switch off."""
187 await self.
coordinatorcoordinator.api.commands.switch_stay_out_zone(
191 @handle_sending_exception(poll_after_sending=True)
193 """Turn the switch on."""
194 await self.
coordinatorcoordinator.api.commands.switch_stay_out_zone(
200 """Defining the Automower work area switch."""
204 coordinator: AutomowerDataUpdateCoordinator,
208 """Set up Automower switch."""
209 super().
__init__(mower_id, coordinator, work_area_id)
222 """Return the state of the switch."""
225 @handle_sending_exception(poll_after_sending=True)
227 """Turn the switch off."""
228 await self.coordinator.api.commands.workarea_settings(
232 @handle_sending_exception(poll_after_sending=True)
234 """Turn the switch on."""
235 await self.coordinator.api.commands.workarea_settings(
MowerAttributes mower_attributes(self)
WorkArea work_area_attributes(self)
None async_turn_off(self, **Any kwargs)
None __init__(self, str mower_id, AutomowerDataUpdateCoordinator coordinator)
None async_turn_on(self, **Any kwargs)
None __init__(self, AutomowerDataUpdateCoordinator coordinator, str mower_id, str stay_out_zone_uid)
None async_turn_off(self, **Any kwargs)
_attr_translation_placeholders
None async_turn_on(self, **Any kwargs)
StayOutZones stay_out_zones(self)
_attr_translation_placeholders
None __init__(self, AutomowerDataUpdateCoordinator coordinator, str mower_id, int work_area_id)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
str _work_area_translation_key(int work_area_id, str key)
None async_setup_entry(HomeAssistant hass, AutomowerConfigEntry entry, AddEntitiesCallback async_add_entities)
IssData update(pyiss.ISS iss)