Add reset cutting blade usage time to Husqvarna Automower (#149628)

This commit is contained in:
Thomas55555
2025-08-04 22:28:34 +02:00
committed by GitHub
parent 4d53450cbf
commit 99d580e371
5 changed files with 114 additions and 30 deletions

View File

@@ -21,6 +21,20 @@ _LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 1
async def async_reset_cutting_blade_usage_time(
session: AutomowerSession,
mower_id: str,
) -> None:
"""Reset cutting blade usage time."""
await session.commands.reset_cutting_blade_usage_time(mower_id)
def reset_cutting_blade_usage_time_availability(data: MowerAttributes) -> bool:
"""Return True if blade usage time is greater than 0."""
value = data.statistics.cutting_blade_usage_time
return value is not None and value > 0
@dataclass(frozen=True, kw_only=True)
class AutomowerButtonEntityDescription(ButtonEntityDescription):
"""Describes Automower button entities."""
@@ -28,6 +42,7 @@ class AutomowerButtonEntityDescription(ButtonEntityDescription):
available_fn: Callable[[MowerAttributes], bool] = lambda _: True
exists_fn: Callable[[MowerAttributes], bool] = lambda _: True
press_fn: Callable[[AutomowerSession, str], Awaitable[Any]]
poll_after_sending: bool = False
MOWER_BUTTON_TYPES: tuple[AutomowerButtonEntityDescription, ...] = (
@@ -43,6 +58,14 @@ MOWER_BUTTON_TYPES: tuple[AutomowerButtonEntityDescription, ...] = (
translation_key="sync_clock",
press_fn=lambda session, mower_id: session.commands.set_datetime(mower_id),
),
AutomowerButtonEntityDescription(
key="reset_cutting_blade_usage_time",
translation_key="reset_cutting_blade_usage_time",
available_fn=reset_cutting_blade_usage_time_availability,
exists_fn=lambda data: data.statistics.cutting_blade_usage_time is not None,
press_fn=async_reset_cutting_blade_usage_time,
poll_after_sending=True,
),
)
@@ -93,3 +116,5 @@ class AutomowerButtonEntity(AutomowerControlEntity, ButtonEntity):
async def async_press(self) -> None:
"""Send a command to the mower."""
await self.entity_description.press_fn(self.coordinator.api, self.mower_id)
if self.entity_description.poll_after_sending:
await self.coordinator.async_request_refresh()

View File

@@ -8,6 +8,9 @@
"button": {
"sync_clock": {
"default": "mdi:clock-check-outline"
},
"reset_cutting_blade_usage_time": {
"default": "mdi:saw-blade"
}
},
"number": {

View File

@@ -53,6 +53,9 @@
},
"sync_clock": {
"name": "Sync clock"
},
"reset_cutting_blade_usage_time": {
"name": "Reset cutting blade usage time"
}
},
"number": {

View File

@@ -47,6 +47,54 @@
'state': 'unavailable',
})
# ---
# name: test_button_snapshot[button.test_mower_1_reset_cutting_blade_usage_time-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': None,
'entity_id': 'button.test_mower_1_reset_cutting_blade_usage_time',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Reset cutting blade usage time',
'platform': 'husqvarna_automower',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'reset_cutting_blade_usage_time',
'unique_id': 'c7233734-b219-4287-a173-08e3643f89f0_reset_cutting_blade_usage_time',
'unit_of_measurement': None,
})
# ---
# name: test_button_snapshot[button.test_mower_1_reset_cutting_blade_usage_time-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Test Mower 1 Reset cutting blade usage time',
}),
'context': <ANY>,
'entity_id': 'button.test_mower_1_reset_cutting_blade_usage_time',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button_snapshot[button.test_mower_1_sync_clock-entry]
EntityRegistryEntrySnapshot({
'aliases': set({

View File

@@ -28,7 +28,7 @@ from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_plat
@pytest.mark.freeze_time(datetime.datetime(2023, 6, 5, tzinfo=datetime.UTC))
async def test_button_states_and_commands(
async def test_button_error_confirm(
hass: HomeAssistant,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
@@ -58,42 +58,43 @@ async def test_button_states_and_commands(
state = hass.states.get(entity_id)
assert state.state == STATE_UNKNOWN
await hass.services.async_call(
domain="button",
service=SERVICE_PRESS,
target={ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_automower_client.commands.error_confirm.assert_called_once_with(TEST_MOWER_ID)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == "2023-06-05T00:16:00+00:00"
mock_automower_client.commands.error_confirm.side_effect = ApiError("Test error")
with pytest.raises(
HomeAssistantError,
match="Failed to send command: Test error",
):
await hass.services.async_call(
domain="button",
service=SERVICE_PRESS,
target={ATTR_ENTITY_ID: entity_id},
blocking=True,
)
@pytest.mark.parametrize(
("entity_id", "name", "expected_command"),
[
(
"button.test_mower_1_confirm_error",
"Test Mower 1 Confirm error",
"error_confirm",
),
(
"button.test_mower_1_sync_clock",
"Test Mower 1 Sync clock",
"set_datetime",
),
(
"button.test_mower_1_reset_cutting_blade_usage_time",
"Test Mower 1 Reset cutting blade usage time",
"reset_cutting_blade_usage_time",
),
],
)
@pytest.mark.freeze_time(datetime.datetime(2024, 2, 29, 11, tzinfo=datetime.UTC))
async def test_sync_clock(
async def test_button_commands(
hass: HomeAssistant,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
values: dict[str, MowerAttributes],
entity_id: str,
name: str,
expected_command: str,
) -> None:
"""Test sync clock button command."""
entity_id = "button.test_mower_1_sync_clock"
"""Test Automower button commands."""
values[TEST_MOWER_ID].mower.is_error_confirmable = True
await setup_integration(hass, mock_config_entry)
state = hass.states.get(entity_id)
assert state.name == "Test Mower 1 Sync clock"
assert state.name == name
mock_automower_client.get_status.return_value = values
@@ -103,11 +104,15 @@ async def test_sync_clock(
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_automower_client.commands.set_datetime.assert_called_once_with(TEST_MOWER_ID)
command_mock = getattr(mock_automower_client.commands, expected_command)
command_mock.assert_called_once_with(TEST_MOWER_ID)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == "2024-02-29T11:00:00+00:00"
mock_automower_client.commands.set_datetime.side_effect = ApiError("Test error")
command_mock.reset_mock()
command_mock.side_effect = ApiError("Test error")
with pytest.raises(
HomeAssistantError,
match="Failed to send command: Test error",