66from dataclasses import dataclass
77from typing import Any , Generic
88
9- from deebot_client .capabilities import CapabilityEvent , CapabilityLifeSpan
9+ from deebot_client .capabilities import CapabilityEvent , CapabilityLifeSpan , DeviceType
10+ from deebot_client .device import Device
1011from deebot_client .events import (
1112 BatteryEvent ,
1213 ErrorEvent ,
3435 UnitOfArea ,
3536 UnitOfTime ,
3637)
37- from homeassistant .core import HomeAssistant
38+ from homeassistant .core import HomeAssistant , callback
3839from homeassistant .helpers .entity_platform import AddConfigEntryEntitiesCallback
3940from homeassistant .helpers .typing import StateType
4041
@@ -59,6 +60,15 @@ class EcovacsSensorEntityDescription(
5960 """Ecovacs sensor entity description."""
6061
6162 value_fn : Callable [[EventT ], StateType ]
63+ native_unit_of_measurement_fn : Callable [[DeviceType ], str | None ] | None = None
64+
65+
66+ @callback
67+ def get_area_native_unit_of_measurement (device_type : DeviceType ) -> str | None :
68+ """Get the area native unit of measurement based on device type."""
69+ if device_type is DeviceType .MOWER :
70+ return UnitOfArea .SQUARE_CENTIMETERS
71+ return UnitOfArea .SQUARE_METERS
6272
6373
6474ENTITY_DESCRIPTIONS : tuple [EcovacsSensorEntityDescription , ...] = (
@@ -68,7 +78,7 @@ class EcovacsSensorEntityDescription(
6878 capability_fn = lambda caps : caps .stats .clean ,
6979 value_fn = lambda e : e .area ,
7080 translation_key = "stats_area" ,
71- native_unit_of_measurement = UnitOfArea . SQUARE_METERS ,
81+ native_unit_of_measurement_fn = get_area_native_unit_of_measurement ,
7282 ),
7383 EcovacsSensorEntityDescription [StatsEvent ](
7484 key = "stats_time" ,
@@ -85,7 +95,7 @@ class EcovacsSensorEntityDescription(
8595 value_fn = lambda e : e .area ,
8696 key = "total_stats_area" ,
8797 translation_key = "total_stats_area" ,
88- native_unit_of_measurement = UnitOfArea . SQUARE_METERS ,
98+ native_unit_of_measurement_fn = get_area_native_unit_of_measurement ,
8999 state_class = SensorStateClass .TOTAL_INCREASING ,
90100 ),
91101 EcovacsSensorEntityDescription [TotalStatsEvent ](
@@ -249,6 +259,27 @@ class EcovacsSensor(
249259
250260 entity_description : EcovacsSensorEntityDescription
251261
262+ def __init__ (
263+ self ,
264+ device : Device ,
265+ capability : CapabilityEvent ,
266+ entity_description : EcovacsSensorEntityDescription ,
267+ ** kwargs : Any ,
268+ ) -> None :
269+ """Initialize entity."""
270+ super ().__init__ (device , capability , entity_description , ** kwargs )
271+ if (
272+ entity_description .native_unit_of_measurement_fn
273+ and (
274+ native_unit_of_measurement
275+ := entity_description .native_unit_of_measurement_fn (
276+ device .capabilities .device_type
277+ )
278+ )
279+ is not None
280+ ):
281+ self ._attr_native_unit_of_measurement = native_unit_of_measurement
282+
252283 async def async_added_to_hass (self ) -> None :
253284 """Set up the event listeners now that hass is ready."""
254285 await super ().async_added_to_hass ()
0 commit comments