1 """Ecovacs sensor module."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from typing
import Any, Generic
9 from deebot_client.capabilities
import CapabilityEvent, CapabilityLifeSpan
10 from deebot_client.events
import (
20 from sucks
import VacBot
25 SensorEntityDescription,
40 from .
import EcovacsConfigEntry
41 from .const
import LEGACY_SUPPORTED_LIFESPANS, SUPPORTED_LIFESPANS
43 EcovacsCapabilityEntityDescription,
44 EcovacsDescriptionEntity,
49 from .util
import get_supported_entitites
52 @dataclass(kw_only=True, frozen=True)
54 EcovacsCapabilityEntityDescription,
55 SensorEntityDescription,
58 """Ecovacs sensor entity description."""
60 value_fn: Callable[[EventT], StateType]
63 ENTITY_DESCRIPTIONS: tuple[EcovacsSensorEntityDescription, ...] = (
65 EcovacsSensorEntityDescription[StatsEvent](
67 capability_fn=
lambda caps: caps.stats.clean,
68 value_fn=
lambda e: e.area,
69 translation_key=
"stats_area",
70 native_unit_of_measurement=UnitOfArea.SQUARE_METERS,
72 EcovacsSensorEntityDescription[StatsEvent](
74 capability_fn=
lambda caps: caps.stats.clean,
75 value_fn=
lambda e: e.time,
76 translation_key=
"stats_time",
77 device_class=SensorDeviceClass.DURATION,
78 native_unit_of_measurement=UnitOfTime.SECONDS,
79 suggested_unit_of_measurement=UnitOfTime.MINUTES,
82 EcovacsSensorEntityDescription[TotalStatsEvent](
83 capability_fn=
lambda caps: caps.stats.total,
84 value_fn=
lambda e: e.area,
85 key=
"total_stats_area",
86 translation_key=
"total_stats_area",
87 native_unit_of_measurement=UnitOfArea.SQUARE_METERS,
88 state_class=SensorStateClass.TOTAL_INCREASING,
90 EcovacsSensorEntityDescription[TotalStatsEvent](
91 capability_fn=
lambda caps: caps.stats.total,
92 value_fn=
lambda e: e.time,
93 key=
"total_stats_time",
94 translation_key=
"total_stats_time",
95 device_class=SensorDeviceClass.DURATION,
96 native_unit_of_measurement=UnitOfTime.SECONDS,
97 suggested_unit_of_measurement=UnitOfTime.HOURS,
98 state_class=SensorStateClass.TOTAL_INCREASING,
100 EcovacsSensorEntityDescription[TotalStatsEvent](
101 capability_fn=
lambda caps: caps.stats.total,
102 value_fn=
lambda e: e.cleanings,
103 key=
"total_stats_cleanings",
104 translation_key=
"total_stats_cleanings",
105 state_class=SensorStateClass.TOTAL_INCREASING,
107 EcovacsSensorEntityDescription[BatteryEvent](
108 capability_fn=
lambda caps: caps.battery,
109 value_fn=
lambda e: e.value,
110 key=ATTR_BATTERY_LEVEL,
111 native_unit_of_measurement=PERCENTAGE,
112 device_class=SensorDeviceClass.BATTERY,
113 entity_category=EntityCategory.DIAGNOSTIC,
115 EcovacsSensorEntityDescription[NetworkInfoEvent](
116 capability_fn=
lambda caps: caps.network,
117 value_fn=
lambda e: e.ip,
119 translation_key=
"network_ip",
120 entity_registry_enabled_default=
False,
121 entity_category=EntityCategory.DIAGNOSTIC,
123 EcovacsSensorEntityDescription[NetworkInfoEvent](
124 capability_fn=
lambda caps: caps.network,
125 value_fn=
lambda e: e.rssi,
127 translation_key=
"network_rssi",
128 entity_registry_enabled_default=
False,
129 entity_category=EntityCategory.DIAGNOSTIC,
131 EcovacsSensorEntityDescription[NetworkInfoEvent](
132 capability_fn=
lambda caps: caps.network,
133 value_fn=
lambda e: e.ssid,
135 translation_key=
"network_ssid",
136 entity_registry_enabled_default=
False,
137 entity_category=EntityCategory.DIAGNOSTIC,
142 @dataclass(kw_only=True, frozen=True)
144 """Ecovacs lifespan sensor entity description."""
147 value_fn: Callable[[LifeSpanEvent], int | float]
150 LIFESPAN_ENTITY_DESCRIPTIONS =
tuple(
153 value_fn=
lambda e: e.percent,
154 key=f
"lifespan_{component.name.lower()}",
155 translation_key=f
"lifespan_{component.name.lower()}",
156 native_unit_of_measurement=PERCENTAGE,
157 entity_category=EntityCategory.DIAGNOSTIC,
159 for component
in SUPPORTED_LIFESPANS
163 @dataclass(kw_only=True, frozen=True)
165 """Ecovacs lifespan sensor entity description."""
173 key=f
"lifespan_{component}",
174 translation_key=f
"lifespan_{component}",
175 native_unit_of_measurement=PERCENTAGE,
176 entity_category=EntityCategory.DIAGNOSTIC,
178 for component
in LEGACY_SUPPORTED_LIFESPANS
184 config_entry: EcovacsConfigEntry,
185 async_add_entities: AddEntitiesCallback,
187 """Add entities for passed config_entry in HA."""
188 controller = config_entry.runtime_data
191 controller, EcovacsSensor, ENTITY_DESCRIPTIONS
195 for device
in controller.devices
196 for description
in LIFESPAN_ENTITY_DESCRIPTIONS
197 if description.component
in device.capabilities.life_span.types
201 for device
in controller.devices
202 if (capability := device.capabilities.error)
207 async
def _add_legacy_entities() -> None:
209 for device
in controller.legacy_devices:
210 for description
in LEGACY_LIFESPAN_SENSORS:
212 description.component
in device.components
213 and not controller.legacy_entity_is_added(
214 device, description.component
217 controller.add_legacy_entity(device, description.component)
223 def _fire_ecovacs_legacy_lifespan_event(_: Any) ->
None:
224 hass.create_task(_add_legacy_entities())
226 for device
in controller.legacy_devices:
227 config_entry.async_on_unload(
228 device.lifespanEvents.subscribe(
229 _fire_ecovacs_legacy_lifespan_event
235 EcovacsDescriptionEntity[CapabilityEvent],
238 """Ecovacs sensor."""
240 entity_description: EcovacsSensorEntityDescription
243 """Set up the event listeners now that hass is ready."""
246 async
def on_event(event: Event) ->
None:
258 EcovacsDescriptionEntity[CapabilityLifeSpan],
261 """Lifespan sensor."""
263 entity_description: EcovacsLifespanSensorEntityDescription
266 """Set up the event listeners now that hass is ready."""
269 async
def on_event(event: LifeSpanEvent) ->
None:
278 EcovacsEntity[CapabilityEvent[ErrorEvent]],
283 _always_available =
True
284 _unrecorded_attributes = frozenset({CONF_DESCRIPTION})
287 translation_key=
"error",
288 entity_registry_enabled_default=
False,
289 entity_category=EntityCategory.DIAGNOSTIC,
293 """Set up the event listeners now that hass is ready."""
306 """Legacy Lifespan sensor."""
308 entity_description: EcovacsLegacyLifespanSensorEntityDescription
313 description: EcovacsLegacyLifespanSensorEntityDescription,
315 """Initialize the entity."""
320 if (value := device.components.get(description.component))
is not None:
321 value =
int(value * 100)
325 """Set up the event listeners now that hass is ready."""
327 def on_event(_: Any) ->
None:
331 value =
int(value * 100)
335 self._event_listeners.append(self.
devicedevice.lifespanEvents.subscribe(on_event))
None _subscribe(self, type[EventT] event_type, Callable[[EventT], Coroutine[Any, Any, None]] callback)
None on_event(ErrorEvent event)
None async_added_to_hass(self)
_attr_extra_state_attributes
None __init__(self, VacBot device, EcovacsLegacyLifespanSensorEntityDescription description)
None async_added_to_hass(self)
None async_added_to_hass(self)
None async_added_to_hass(self)
None async_write_ha_state(self)
None schedule_update_ha_state(self, bool force_refresh=False)
None async_setup_entry(HomeAssistant hass, EcovacsConfigEntry config_entry, AddEntitiesCallback async_add_entities)
list[EcovacsEntity] get_supported_entitites(EcovacsController controller, type[EcovacsDescriptionEntity] entity_class, tuple[EcovacsCapabilityEntityDescription,...] descriptions)