Home Assistant Unofficial Reference 2024.12.1
lawn_mower.py
Go to the documentation of this file.
1 """Ecovacs mower entity."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from deebot_client.capabilities import Capabilities, DeviceType
8 from deebot_client.device import Device
9 from deebot_client.events import StateEvent
10 from deebot_client.models import CleanAction, State
11 
13  LawnMowerActivity,
14  LawnMowerEntity,
15  LawnMowerEntityEntityDescription,
16  LawnMowerEntityFeature,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import EcovacsConfigEntry
22 from .entity import EcovacsEntity
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 _STATE_TO_MOWER_STATE = {
28  State.IDLE: LawnMowerActivity.PAUSED,
29  State.CLEANING: LawnMowerActivity.MOWING,
30  State.RETURNING: LawnMowerActivity.RETURNING,
31  State.DOCKED: LawnMowerActivity.DOCKED,
32  State.ERROR: LawnMowerActivity.ERROR,
33  State.PAUSED: LawnMowerActivity.PAUSED,
34 }
35 
36 
38  hass: HomeAssistant,
39  config_entry: EcovacsConfigEntry,
40  async_add_entities: AddEntitiesCallback,
41 ) -> None:
42  """Set up the Ecovacs mowers."""
43  controller = config_entry.runtime_data
44  mowers: list[EcovacsMower] = [
45  EcovacsMower(device)
46  for device in controller.devices
47  if device.capabilities.device_type is DeviceType.MOWER
48  ]
49  _LOGGER.debug("Adding Ecovacs Mowers to Home Assistant: %s", mowers)
50  async_add_entities(mowers)
51 
52 
54  EcovacsEntity[Capabilities],
55  LawnMowerEntity,
56 ):
57  """Ecovacs Mower."""
58 
59  _attr_supported_features = (
60  LawnMowerEntityFeature.DOCK
61  | LawnMowerEntityFeature.PAUSE
62  | LawnMowerEntityFeature.START_MOWING
63  )
64 
65  entity_description = LawnMowerEntityEntityDescription(key="mower", name=None)
66 
67  def __init__(self, device: Device) -> None:
68  """Initialize the mower."""
69  super().__init__(device, device.capabilities)
70 
71  async def async_added_to_hass(self) -> None:
72  """Set up the event listeners now that hass is ready."""
73  await super().async_added_to_hass()
74 
75  async def on_status(event: StateEvent) -> None:
76  self._attr_activity_attr_activity = _STATE_TO_MOWER_STATE[event.state]
77  self.async_write_ha_stateasync_write_ha_state()
78 
79  self._subscribe_subscribe(self._capability_capability.state.event, on_status)
80 
81  async def _clean_command(self, action: CleanAction) -> None:
82  await self._device_device.execute_command(
83  self._capability_capability.clean.action.command(action)
84  )
85 
86  async def async_start_mowing(self) -> None:
87  """Resume schedule."""
88  await self._clean_command_clean_command(CleanAction.START)
89 
90  async def async_pause(self) -> None:
91  """Pauses the mower."""
92  await self._clean_command_clean_command(CleanAction.PAUSE)
93 
94  async def async_dock(self) -> None:
95  """Parks the mower until next schedule."""
96  await self._device_device.execute_command(self._capability_capability.charge.execute())
None _subscribe(self, type[EventT] event_type, Callable[[EventT], Coroutine[Any, Any, None]] callback)
Definition: entity.py:87
None async_setup_entry(HomeAssistant hass, EcovacsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: lawn_mower.py:41