diff --git a/homeassistant/components/husqvarna_automower/binary_sensor.py b/homeassistant/components/husqvarna_automower/binary_sensor.py index 9b80f6d57ac4..6922fb85693e 100644 --- a/homeassistant/components/husqvarna_automower/binary_sensor.py +++ b/homeassistant/components/husqvarna_automower/binary_sensor.py @@ -5,14 +5,14 @@ from dataclasses import dataclass import logging from typing import override -from aioautomower.model import MowerActivities, MowerAttributes +from aioautomower.model import MowerActivities, MowerAttributes, MowerStates from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from . import AutomowerConfigEntry @@ -24,17 +24,30 @@ _LOGGER = logging.getLogger(__name__) PARALLEL_UPDATES = 0 +@callback +def _get_charging_value(data: MowerAttributes) -> bool | None: + """Return the charging state of the mower. + + The mower only returns the charging state during normal operation. + If the mower is in a restricted state, the charging state is set to unknown. + """ + + if data.mower.state == MowerStates.RESTRICTED: + return None + return data.mower.activity == MowerActivities.CHARGING + + @dataclass(frozen=True, kw_only=True) class AutomowerBinarySensorEntityDescription(BinarySensorEntityDescription): """Describes Automower binary sensor entity.""" - value_fn: Callable[[MowerAttributes], bool] + value_fn: Callable[[MowerAttributes], bool | None] MOWER_BINARY_SENSOR_TYPES: tuple[AutomowerBinarySensorEntityDescription, ...] = ( AutomowerBinarySensorEntityDescription( key="battery_charging", - value_fn=lambda data: data.mower.activity == MowerActivities.CHARGING, + value_fn=_get_charging_value, device_class=BinarySensorDeviceClass.BATTERY_CHARGING, ), AutomowerBinarySensorEntityDescription( @@ -82,6 +95,6 @@ class AutomowerBinarySensorEntity(AutomowerBaseEntity, BinarySensorEntity): @property @override - def is_on(self) -> bool: + def is_on(self) -> bool | None: """Return the state of the binary sensor.""" return self.entity_description.value_fn(self.mower_attributes) diff --git a/tests/components/husqvarna_automower/fixtures/mower.json b/tests/components/husqvarna_automower/fixtures/mower.json index 73fc6dbbc7da..205723161cdc 100644 --- a/tests/components/husqvarna_automower/fixtures/mower.json +++ b/tests/components/husqvarna_automower/fixtures/mower.json @@ -247,7 +247,7 @@ "mode": "MAIN_AREA", "activity": "PARKED_IN_CS", "inactiveReason": "NONE", - "state": "RESTRICTED", + "state": "IN_OPERATION", "errorCode": 0, "errorCodeTimestamp": 0 }, @@ -271,7 +271,7 @@ "override": { "action": "NOT_ACTIVE" }, - "restrictedReason": "WEEK_SCHEDULE" + "restrictedReason": "NONE" }, "metadata": { "connected": true, diff --git a/tests/components/husqvarna_automower/snapshots/test_binary_sensor.ambr b/tests/components/husqvarna_automower/snapshots/test_binary_sensor.ambr index efe6ee78c96e..70529f1f3d8e 100644 --- a/tests/components/husqvarna_automower/snapshots/test_binary_sensor.ambr +++ b/tests/components/husqvarna_automower/snapshots/test_binary_sensor.ambr @@ -47,7 +47,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'off', + 'state': 'unknown', }) # --- # name: test_binary_sensor_snapshot[binary_sensor.garden_test_mower_1_leaving_dock-entry] diff --git a/tests/components/husqvarna_automower/snapshots/test_sensor.ambr b/tests/components/husqvarna_automower/snapshots/test_sensor.ambr index 0f82b9db2982..fe9e2d9b5606 100644 --- a/tests/components/husqvarna_automower/snapshots/test_sensor.ambr +++ b/tests/components/husqvarna_automower/snapshots/test_sensor.ambr @@ -2363,6 +2363,6 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'week_schedule', + 'state': 'none', }) # ---