Skip to content

Commit fe4ff4f

Browse files
authored
Use non-autospec mock for Reolink switch tests (home-assistant#147441)
1 parent cefc882 commit fe4ff4f

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

tests/components/reolink/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def _init_host_mock(host_mock: MagicMock) -> None:
7777
host_mock.get_stream_source = AsyncMock()
7878
host_mock.get_snapshot = AsyncMock()
7979
host_mock.get_encoding = AsyncMock(return_value="h264")
80+
host_mock.pull_point_request = AsyncMock()
81+
host_mock.set_audio = AsyncMock()
82+
host_mock.set_email = AsyncMock()
8083
host_mock.ONVIF_event_callback = AsyncMock()
8184
host_mock.is_nvr = True
8285
host_mock.is_hub = False

tests/components/reolink/test_switch.py

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ async def test_switch(
3333
hass: HomeAssistant,
3434
config_entry: MockConfigEntry,
3535
freezer: FrozenDateTimeFactory,
36-
reolink_connect: MagicMock,
36+
reolink_host: MagicMock,
3737
) -> None:
3838
"""Test switch entity."""
39-
reolink_connect.camera_name.return_value = TEST_CAM_NAME
40-
reolink_connect.audio_record.return_value = True
39+
reolink_host.camera_name.return_value = TEST_CAM_NAME
40+
reolink_host.audio_record.return_value = True
4141

4242
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
4343
assert await hass.config_entries.async_setup(config_entry.entry_id)
@@ -47,7 +47,7 @@ async def test_switch(
4747
entity_id = f"{Platform.SWITCH}.{TEST_CAM_NAME}_record_audio"
4848
assert hass.states.get(entity_id).state == STATE_ON
4949

50-
reolink_connect.audio_record.return_value = False
50+
reolink_host.audio_record.return_value = False
5151
freezer.tick(DEVICE_UPDATE_INTERVAL)
5252
async_fire_time_changed(hass)
5353
await hass.async_block_till_done()
@@ -61,9 +61,9 @@ async def test_switch(
6161
{ATTR_ENTITY_ID: entity_id},
6262
blocking=True,
6363
)
64-
reolink_connect.set_audio.assert_called_with(0, True)
64+
reolink_host.set_audio.assert_called_with(0, True)
6565

66-
reolink_connect.set_audio.side_effect = ReolinkError("Test error")
66+
reolink_host.set_audio.side_effect = ReolinkError("Test error")
6767
with pytest.raises(HomeAssistantError):
6868
await hass.services.async_call(
6969
SWITCH_DOMAIN,
@@ -73,16 +73,16 @@ async def test_switch(
7373
)
7474

7575
# test switch turn off
76-
reolink_connect.set_audio.reset_mock(side_effect=True)
76+
reolink_host.set_audio.reset_mock(side_effect=True)
7777
await hass.services.async_call(
7878
SWITCH_DOMAIN,
7979
SERVICE_TURN_OFF,
8080
{ATTR_ENTITY_ID: entity_id},
8181
blocking=True,
8282
)
83-
reolink_connect.set_audio.assert_called_with(0, False)
83+
reolink_host.set_audio.assert_called_with(0, False)
8484

85-
reolink_connect.set_audio.side_effect = ReolinkError("Test error")
85+
reolink_host.set_audio.side_effect = ReolinkError("Test error")
8686
with pytest.raises(HomeAssistantError):
8787
await hass.services.async_call(
8888
SWITCH_DOMAIN,
@@ -91,29 +91,27 @@ async def test_switch(
9191
blocking=True,
9292
)
9393

94-
reolink_connect.set_audio.reset_mock(side_effect=True)
94+
reolink_host.set_audio.reset_mock(side_effect=True)
9595

96-
reolink_connect.camera_online.return_value = False
96+
reolink_host.camera_online.return_value = False
9797
freezer.tick(DEVICE_UPDATE_INTERVAL)
9898
async_fire_time_changed(hass)
9999
await hass.async_block_till_done()
100100

101101
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
102102

103-
reolink_connect.camera_online.return_value = True
104-
105103

106104
async def test_host_switch(
107105
hass: HomeAssistant,
108106
config_entry: MockConfigEntry,
109107
freezer: FrozenDateTimeFactory,
110-
reolink_connect: MagicMock,
108+
reolink_host: MagicMock,
111109
) -> None:
112110
"""Test host switch entity."""
113-
reolink_connect.camera_name.return_value = TEST_CAM_NAME
114-
reolink_connect.email_enabled.return_value = True
115-
reolink_connect.is_hub = False
116-
reolink_connect.supported.return_value = True
111+
reolink_host.camera_name.return_value = TEST_CAM_NAME
112+
reolink_host.email_enabled.return_value = True
113+
reolink_host.is_hub = False
114+
reolink_host.supported.return_value = True
117115

118116
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
119117
assert await hass.config_entries.async_setup(config_entry.entry_id)
@@ -123,7 +121,7 @@ async def test_host_switch(
123121
entity_id = f"{Platform.SWITCH}.{TEST_NVR_NAME}_email_on_event"
124122
assert hass.states.get(entity_id).state == STATE_ON
125123

126-
reolink_connect.email_enabled.return_value = False
124+
reolink_host.email_enabled.return_value = False
127125
freezer.tick(DEVICE_UPDATE_INTERVAL)
128126
async_fire_time_changed(hass)
129127
await hass.async_block_till_done()
@@ -137,9 +135,9 @@ async def test_host_switch(
137135
{ATTR_ENTITY_ID: entity_id},
138136
blocking=True,
139137
)
140-
reolink_connect.set_email.assert_called_with(None, True)
138+
reolink_host.set_email.assert_called_with(None, True)
141139

142-
reolink_connect.set_email.side_effect = ReolinkError("Test error")
140+
reolink_host.set_email.side_effect = ReolinkError("Test error")
143141
with pytest.raises(HomeAssistantError):
144142
await hass.services.async_call(
145143
SWITCH_DOMAIN,
@@ -149,16 +147,16 @@ async def test_host_switch(
149147
)
150148

151149
# test switch turn off
152-
reolink_connect.set_email.reset_mock(side_effect=True)
150+
reolink_host.set_email.reset_mock(side_effect=True)
153151
await hass.services.async_call(
154152
SWITCH_DOMAIN,
155153
SERVICE_TURN_OFF,
156154
{ATTR_ENTITY_ID: entity_id},
157155
blocking=True,
158156
)
159-
reolink_connect.set_email.assert_called_with(None, False)
157+
reolink_host.set_email.assert_called_with(None, False)
160158

161-
reolink_connect.set_email.side_effect = ReolinkError("Test error")
159+
reolink_host.set_email.side_effect = ReolinkError("Test error")
162160
with pytest.raises(HomeAssistantError):
163161
await hass.services.async_call(
164162
SWITCH_DOMAIN,
@@ -167,15 +165,13 @@ async def test_host_switch(
167165
blocking=True,
168166
)
169167

170-
reolink_connect.set_email.reset_mock(side_effect=True)
171-
172168

173169
async def test_chime_switch(
174170
hass: HomeAssistant,
175171
config_entry: MockConfigEntry,
176172
freezer: FrozenDateTimeFactory,
177-
reolink_connect: MagicMock,
178-
test_chime: Chime,
173+
reolink_host: MagicMock,
174+
reolink_chime: Chime,
179175
) -> None:
180176
"""Test host switch entity."""
181177
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
@@ -186,24 +182,24 @@ async def test_chime_switch(
186182
entity_id = f"{Platform.SWITCH}.test_chime_led"
187183
assert hass.states.get(entity_id).state == STATE_ON
188184

189-
test_chime.led_state = False
185+
reolink_chime.led_state = False
190186
freezer.tick(DEVICE_UPDATE_INTERVAL)
191187
async_fire_time_changed(hass)
192188
await hass.async_block_till_done()
193189

194190
assert hass.states.get(entity_id).state == STATE_OFF
195191

196192
# test switch turn on
197-
test_chime.set_option = AsyncMock()
193+
reolink_chime.set_option = AsyncMock()
198194
await hass.services.async_call(
199195
SWITCH_DOMAIN,
200196
SERVICE_TURN_ON,
201197
{ATTR_ENTITY_ID: entity_id},
202198
blocking=True,
203199
)
204-
test_chime.set_option.assert_called_with(led=True)
200+
reolink_chime.set_option.assert_called_with(led=True)
205201

206-
test_chime.set_option.side_effect = ReolinkError("Test error")
202+
reolink_chime.set_option.side_effect = ReolinkError("Test error")
207203
with pytest.raises(HomeAssistantError):
208204
await hass.services.async_call(
209205
SWITCH_DOMAIN,
@@ -213,16 +209,16 @@ async def test_chime_switch(
213209
)
214210

215211
# test switch turn off
216-
test_chime.set_option.reset_mock(side_effect=True)
212+
reolink_chime.set_option.reset_mock(side_effect=True)
217213
await hass.services.async_call(
218214
SWITCH_DOMAIN,
219215
SERVICE_TURN_OFF,
220216
{ATTR_ENTITY_ID: entity_id},
221217
blocking=True,
222218
)
223-
test_chime.set_option.assert_called_with(led=False)
219+
reolink_chime.set_option.assert_called_with(led=False)
224220

225-
test_chime.set_option.side_effect = ReolinkError("Test error")
221+
reolink_chime.set_option.side_effect = ReolinkError("Test error")
226222
with pytest.raises(HomeAssistantError):
227223
await hass.services.async_call(
228224
SWITCH_DOMAIN,
@@ -231,8 +227,6 @@ async def test_chime_switch(
231227
blocking=True,
232228
)
233229

234-
test_chime.set_option.reset_mock(side_effect=True)
235-
236230

237231
@pytest.mark.parametrize(
238232
(
@@ -265,7 +259,7 @@ async def test_chime_switch(
265259
async def test_cleanup_hub_switches(
266260
hass: HomeAssistant,
267261
config_entry: MockConfigEntry,
268-
reolink_connect: MagicMock,
262+
reolink_host: MagicMock,
269263
entity_registry: er.EntityRegistry,
270264
original_id: str,
271265
capability: str,
@@ -279,9 +273,9 @@ def mock_supported(ch, cap):
279273

280274
domain = Platform.SWITCH
281275

282-
reolink_connect.channels = [0]
283-
reolink_connect.is_hub = True
284-
reolink_connect.supported = mock_supported
276+
reolink_host.channels = [0]
277+
reolink_host.is_hub = True
278+
reolink_host.supported = mock_supported
285279

286280
entity_registry.async_get_or_create(
287281
domain=domain,
@@ -301,9 +295,6 @@ def mock_supported(ch, cap):
301295

302296
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None
303297

304-
reolink_connect.is_hub = False
305-
reolink_connect.supported.return_value = True
306-
307298

308299
@pytest.mark.parametrize(
309300
(
@@ -336,7 +327,7 @@ def mock_supported(ch, cap):
336327
async def test_hub_switches_repair_issue(
337328
hass: HomeAssistant,
338329
config_entry: MockConfigEntry,
339-
reolink_connect: MagicMock,
330+
reolink_host: MagicMock,
340331
entity_registry: er.EntityRegistry,
341332
issue_registry: ir.IssueRegistry,
342333
original_id: str,
@@ -351,9 +342,9 @@ def mock_supported(ch, cap):
351342

352343
domain = Platform.SWITCH
353344

354-
reolink_connect.channels = [0]
355-
reolink_connect.is_hub = True
356-
reolink_connect.supported = mock_supported
345+
reolink_host.channels = [0]
346+
reolink_host.is_hub = True
347+
reolink_host.supported = mock_supported
357348

358349
entity_registry.async_get_or_create(
359350
domain=domain,
@@ -373,6 +364,3 @@ def mock_supported(ch, cap):
373364

374365
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id)
375366
assert (DOMAIN, "hub_switch_deprecated") in issue_registry.issues
376-
377-
reolink_connect.is_hub = False
378-
reolink_connect.supported.return_value = True

0 commit comments

Comments
 (0)