|
3 | 3 | from datetime import timedelta |
4 | 4 | from unittest.mock import Mock |
5 | 5 |
|
| 6 | +from automower_ble.protocol import MowerActivity, MowerState |
6 | 7 | from bleak import BleakError |
7 | 8 | from freezegun.api import FrozenDateTimeFactory |
8 | 9 | import pytest |
9 | 10 |
|
| 11 | +from homeassistant.components.lawn_mower import LawnMowerActivity |
10 | 12 | from homeassistant.config_entries import ConfigEntryState |
11 | 13 | from homeassistant.const import STATE_UNAVAILABLE |
12 | 14 | from homeassistant.core import HomeAssistant |
@@ -124,3 +126,83 @@ async def test_bleak_error_data_update( |
124 | 126 | await hass.async_block_till_done() |
125 | 127 |
|
126 | 128 | 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