-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
/
Copy pathtest_lawn_mower.py
273 lines (254 loc) · 8.38 KB
/
test_lawn_mower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""Tests for lawn_mower module."""
from datetime import timedelta
from unittest.mock import AsyncMock
from aioautomower.exceptions import ApiException
from aioautomower.utils import mower_list_to_dictionary_dataclass
from freezegun.api import FrozenDateTimeFactory
import pytest
from voluptuous.error import MultipleInvalid
from homeassistant.components.husqvarna_automower.const import DOMAIN
from homeassistant.components.husqvarna_automower.coordinator import SCAN_INTERVAL
from homeassistant.components.lawn_mower import LawnMowerActivity
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from . import setup_integration
from .const import TEST_MOWER_ID
from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_value_fixture,
)
async def test_lawn_mower_states(
hass: HomeAssistant,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test lawn_mower state."""
values = mower_list_to_dictionary_dataclass(
load_json_value_fixture("mower.json", DOMAIN)
)
await setup_integration(hass, mock_config_entry)
state = hass.states.get("lawn_mower.test_mower_1")
assert state is not None
assert state.state == LawnMowerActivity.DOCKED
for activity, state, expected_state in (
("UNKNOWN", "PAUSED", LawnMowerActivity.PAUSED),
("MOWING", "NOT_APPLICABLE", LawnMowerActivity.MOWING),
("NOT_APPLICABLE", "ERROR", LawnMowerActivity.ERROR),
):
values[TEST_MOWER_ID].mower.activity = activity
values[TEST_MOWER_ID].mower.state = state
mock_automower_client.get_status.return_value = values
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("lawn_mower.test_mower_1")
assert state.state == expected_state
@pytest.mark.parametrize(
("aioautomower_command", "service"),
[
("resume_schedule", "start_mowing"),
("pause_mowing", "pause"),
("park_until_next_schedule", "dock"),
],
)
async def test_lawn_mower_commands(
hass: HomeAssistant,
aioautomower_command: str,
service: str,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test lawn_mower commands."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
domain="lawn_mower",
service=service,
service_data={"entity_id": "lawn_mower.test_mower_1"},
blocking=True,
)
mocked_method = getattr(mock_automower_client.commands, aioautomower_command)
mocked_method.assert_called_once_with(TEST_MOWER_ID)
getattr(
mock_automower_client.commands, aioautomower_command
).side_effect = ApiException("Test error")
with pytest.raises(
HomeAssistantError,
match="Failed to send command: Test error",
):
await hass.services.async_call(
domain="lawn_mower",
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
blocking=True,
)
@pytest.mark.parametrize(
("aioautomower_command", "extra_data", "service", "service_data"),
[
(
"start_for",
timedelta(hours=3),
"override_schedule",
{
"duration": {"days": 0, "hours": 3, "minutes": 0},
"override_mode": "mow",
},
),
(
"park_for",
timedelta(days=1, hours=12, minutes=30),
"override_schedule",
{
"duration": {"days": 1, "hours": 12, "minutes": 30},
"override_mode": "park",
},
),
],
)
async def test_lawn_mower_service_commands(
hass: HomeAssistant,
aioautomower_command: str,
extra_data: timedelta,
service: str,
service_data: dict[str, int] | None,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test lawn_mower commands."""
await setup_integration(hass, mock_config_entry)
mocked_method = AsyncMock()
setattr(mock_automower_client.commands, aioautomower_command, mocked_method)
await hass.services.async_call(
domain=DOMAIN,
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
service_data=service_data,
blocking=True,
)
mocked_method.assert_called_once_with(TEST_MOWER_ID, extra_data)
getattr(
mock_automower_client.commands, aioautomower_command
).side_effect = ApiException("Test error")
with pytest.raises(
HomeAssistantError,
match="Failed to send command: Test error",
):
await hass.services.async_call(
domain=DOMAIN,
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
service_data=service_data,
blocking=True,
)
@pytest.mark.parametrize(
("aioautomower_command", "extra_data1", "extra_data2", "service", "service_data"),
[
(
"start_in_workarea",
123456,
timedelta(days=40),
"override_schedule_work_area",
{
"work_area_id": 123456,
"duration": {"days": 40},
},
),
],
)
async def test_lawn_mower_override_work_area_command(
hass: HomeAssistant,
aioautomower_command: str,
extra_data1: int,
extra_data2: timedelta,
service: str,
service_data: dict[str, int] | None,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test lawn_mower work area override commands."""
await setup_integration(hass, mock_config_entry)
mocked_method = AsyncMock()
setattr(mock_automower_client.commands, aioautomower_command, mocked_method)
await hass.services.async_call(
domain=DOMAIN,
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
service_data=service_data,
blocking=True,
)
mocked_method.assert_called_once_with(TEST_MOWER_ID, extra_data1, extra_data2)
getattr(
mock_automower_client.commands, aioautomower_command
).side_effect = ApiException("Test error")
with pytest.raises(
HomeAssistantError,
match="Failed to send command: Test error",
):
await hass.services.async_call(
domain=DOMAIN,
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
service_data=service_data,
blocking=True,
)
@pytest.mark.parametrize(
("service", "service_data", "mower_support_wa", "exception"),
[
(
"override_schedule",
{
"duration": {"days": 1, "hours": 12, "minutes": 30},
"override_mode": "fly_to_moon",
},
False,
MultipleInvalid,
),
(
"override_schedule_work_area",
{
"work_area_id": 123456,
"duration": {"days": 40},
},
False,
ServiceValidationError,
),
(
"override_schedule_work_area",
{
"work_area_id": 12345,
"duration": {"days": 40},
},
True,
ServiceValidationError,
),
],
)
async def test_lawn_mower_wrong_service_commands(
hass: HomeAssistant,
service: str,
service_data: dict[str, int] | None,
mower_support_wa: bool,
exception,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test lawn_mower commands."""
await setup_integration(hass, mock_config_entry)
values = mower_list_to_dictionary_dataclass(
load_json_value_fixture("mower.json", DOMAIN)
)
values[TEST_MOWER_ID].capabilities.work_areas = mower_support_wa
mock_automower_client.get_status.return_value = values
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
with pytest.raises(exception):
await hass.services.async_call(
domain=DOMAIN,
service=service,
target={"entity_id": "lawn_mower.test_mower_1"},
service_data=service_data,
blocking=True,
)