Skip to content

Commit 62be31f

Browse files
emontnemeryCopilot
andauthored
Add test of automower_ble activity mapping (#150983)
Co-authored-by: Copilot <[email protected]>
1 parent ca75581 commit 62be31f

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/components/husqvarna_automower_ble/test_lawn_mower.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from datetime import timedelta
44
from unittest.mock import Mock
55

6+
from automower_ble.protocol import MowerActivity, MowerState
67
from bleak import BleakError
78
from freezegun.api import FrozenDateTimeFactory
89
import pytest
910

11+
from homeassistant.components.lawn_mower import LawnMowerActivity
1012
from homeassistant.config_entries import ConfigEntryState
1113
from homeassistant.const import STATE_UNAVAILABLE
1214
from homeassistant.core import HomeAssistant
@@ -124,3 +126,83 @@ async def test_bleak_error_data_update(
124126
await hass.async_block_till_done()
125127

126128
assert hass.states.get("lawn_mower.husqvarna_automower").state == STATE_UNAVAILABLE
129+
130+
131+
OPERATIONAL_STATES = [
132+
MowerState.IN_OPERATION,
133+
MowerState.PENDING_START,
134+
MowerState.RESTRICTED,
135+
]
136+
137+
138+
@pytest.mark.parametrize(
139+
("mower_states", "mower_activities", "expected_state"),
140+
[
141+
# MowerState ERROR, FATAL_ERROR, OFF, STOPPED, WAIT_FOR_SAFETYPIN -> Mapped to
142+
# LawnMowerActivity.ERROR
143+
(
144+
[
145+
MowerState.ERROR,
146+
MowerState.FATAL_ERROR,
147+
MowerState.OFF,
148+
MowerState.STOPPED,
149+
MowerState.WAIT_FOR_SAFETYPIN,
150+
],
151+
list(MowerActivity),
152+
LawnMowerActivity.ERROR,
153+
),
154+
# MowerState PAUSED -> Mapped to LawnMowerActivity.PAUSED
155+
([MowerState.PAUSED], list(MowerActivity), LawnMowerActivity.PAUSED),
156+
# Operational states are mapped according to the activity
157+
(
158+
OPERATIONAL_STATES,
159+
[MowerActivity.CHARGING, MowerActivity.NONE, MowerActivity.PARKED],
160+
LawnMowerActivity.DOCKED,
161+
),
162+
(
163+
OPERATIONAL_STATES,
164+
[MowerActivity.GOING_OUT, MowerActivity.MOWING],
165+
LawnMowerActivity.MOWING,
166+
),
167+
(
168+
OPERATIONAL_STATES,
169+
[MowerActivity.GOING_HOME],
170+
LawnMowerActivity.RETURNING,
171+
),
172+
(
173+
OPERATIONAL_STATES,
174+
[MowerActivity.STOPPED_IN_GARDEN],
175+
LawnMowerActivity.ERROR,
176+
),
177+
],
178+
)
179+
async def test_mower_activity_mapping(
180+
hass: HomeAssistant,
181+
mock_automower_client: Mock,
182+
mock_config_entry: MockConfigEntry,
183+
freezer: FrozenDateTimeFactory,
184+
mower_states: list[MowerState],
185+
mower_activities: list[MowerActivity],
186+
expected_state: str,
187+
) -> None:
188+
"""Test mower state and activity mapping to LawnMowerActivity states."""
189+
190+
mock_config_entry.add_to_hass(hass)
191+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
192+
await hass.async_block_till_done()
193+
194+
assert mock_config_entry.state is ConfigEntryState.LOADED
195+
196+
for mower_state in mower_states:
197+
for mower_activity in mower_activities:
198+
mock_automower_client.mower_state.return_value = mower_state
199+
mock_automower_client.mower_activity.return_value = mower_activity
200+
201+
freezer.tick(timedelta(seconds=60))
202+
async_fire_time_changed(hass)
203+
await hass.async_block_till_done()
204+
205+
assert (
206+
hass.states.get("lawn_mower.husqvarna_automower").state
207+
== expected_state
208+
)

0 commit comments

Comments
 (0)