Skip to content

Commit 34455f9

Browse files
edenhausfrenck
authored andcommitted
Fix Ecovacs mower area sensors (home-assistant#145071)
1 parent 8c4eec2 commit 34455f9

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

homeassistant/components/ecovacs/sensor.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from dataclasses import dataclass
77
from 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
1011
from deebot_client.events import (
1112
BatteryEvent,
1213
ErrorEvent,
@@ -34,7 +35,7 @@
3435
UnitOfArea,
3536
UnitOfTime,
3637
)
37-
from homeassistant.core import HomeAssistant
38+
from homeassistant.core import HomeAssistant, callback
3839
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
3940
from 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

6474
ENTITY_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()

tests/components/ecovacs/snapshots/test_sensor.ambr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@
181181
'supported_features': 0,
182182
'translation_key': 'stats_area',
183183
'unique_id': '8516fbb1-17f1-4194-0000000_stats_area',
184-
'unit_of_measurement': <UnitOfArea.SQUARE_METERS: 'm²'>,
184+
'unit_of_measurement': <UnitOfArea.SQUARE_CENTIMETERS: 'cm²'>,
185185
})
186186
# ---
187187
# name: test_sensors[5xu9h3][sensor.goat_g1_area_cleaned:state]
188188
StateSnapshot({
189189
'attributes': ReadOnlyDict({
190190
'friendly_name': 'Goat G1 Area cleaned',
191-
'unit_of_measurement': <UnitOfArea.SQUARE_METERS: 'm²'>,
191+
'unit_of_measurement': <UnitOfArea.SQUARE_CENTIMETERS: 'cm²'>,
192192
}),
193193
'context': <ANY>,
194194
'entity_id': 'sensor.goat_g1_area_cleaned',
@@ -523,15 +523,15 @@
523523
'supported_features': 0,
524524
'translation_key': 'total_stats_area',
525525
'unique_id': '8516fbb1-17f1-4194-0000000_total_stats_area',
526-
'unit_of_measurement': <UnitOfArea.SQUARE_METERS: 'm²'>,
526+
'unit_of_measurement': <UnitOfArea.SQUARE_CENTIMETERS: 'cm²'>,
527527
})
528528
# ---
529529
# name: test_sensors[5xu9h3][sensor.goat_g1_total_area_cleaned:state]
530530
StateSnapshot({
531531
'attributes': ReadOnlyDict({
532532
'friendly_name': 'Goat G1 Total area cleaned',
533533
'state_class': <SensorStateClass.TOTAL_INCREASING: 'total_increasing'>,
534-
'unit_of_measurement': <UnitOfArea.SQUARE_METERS: 'm²'>,
534+
'unit_of_measurement': <UnitOfArea.SQUARE_CENTIMETERS: 'cm²'>,
535535
}),
536536
'context': <ANY>,
537537
'entity_id': 'sensor.goat_g1_total_area_cleaned',

0 commit comments

Comments
 (0)