Skip to content

Commit f90a740

Browse files
authored
Use non-autospec mock for Reolink's binary_sensor, camera and diag tests (home-assistant#147095)
1 parent 3dba7e5 commit f90a740

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

tests/components/reolink/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def _init_host_mock(host_mock: MagicMock) -> None:
7373
host_mock.reboot = AsyncMock()
7474
host_mock.set_ptz_command = AsyncMock()
7575
host_mock.get_motion_state_all_ch = AsyncMock(return_value=False)
76+
host_mock.get_stream_source = AsyncMock()
77+
host_mock.get_snapshot = AsyncMock()
78+
host_mock.get_encoding = AsyncMock(return_value="h264")
79+
host_mock.ONVIF_event_callback = AsyncMock()
7680
host_mock.is_nvr = True
7781
host_mock.is_hub = False
7882
host_mock.mac_address = TEST_MAC
@@ -105,7 +109,6 @@ def _init_host_mock(host_mock: MagicMock) -> None:
105109
host_mock.camera_uid.return_value = TEST_UID_CAM
106110
host_mock.camera_online.return_value = True
107111
host_mock.channel_for_uid.return_value = 0
108-
host_mock.get_encoding.return_value = "h264"
109112
host_mock.firmware_update_available.return_value = False
110113
host_mock.session_active = True
111114
host_mock.timeout = 60

tests/components/reolink/test_binary_sensor.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ async def test_motion_sensor(
2121
hass_client_no_auth: ClientSessionGenerator,
2222
freezer: FrozenDateTimeFactory,
2323
config_entry: MockConfigEntry,
24-
reolink_connect: MagicMock,
24+
reolink_host: MagicMock,
2525
) -> None:
2626
"""Test binary sensor entity with motion sensor."""
27-
reolink_connect.model = TEST_DUO_MODEL
28-
reolink_connect.motion_detected.return_value = True
27+
reolink_host.model = TEST_DUO_MODEL
28+
reolink_host.motion_detected.return_value = True
2929
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
3030
assert await hass.config_entries.async_setup(config_entry.entry_id)
3131
await hass.async_block_till_done()
@@ -34,16 +34,16 @@ async def test_motion_sensor(
3434
entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_motion_lens_0"
3535
assert hass.states.get(entity_id).state == STATE_ON
3636

37-
reolink_connect.motion_detected.return_value = False
37+
reolink_host.motion_detected.return_value = False
3838
freezer.tick(DEVICE_UPDATE_INTERVAL)
3939
async_fire_time_changed(hass)
4040
await hass.async_block_till_done()
4141

4242
assert hass.states.get(entity_id).state == STATE_OFF
4343

4444
# test ONVIF webhook callback
45-
reolink_connect.motion_detected.return_value = True
46-
reolink_connect.ONVIF_event_callback.return_value = [0]
45+
reolink_host.motion_detected.return_value = True
46+
reolink_host.ONVIF_event_callback.return_value = [0]
4747
webhook_id = config_entry.runtime_data.host.webhook_id
4848
client = await hass_client_no_auth()
4949
await client.post(f"/api/webhook/{webhook_id}", data="test_data")
@@ -56,11 +56,11 @@ async def test_smart_ai_sensor(
5656
hass_client_no_auth: ClientSessionGenerator,
5757
freezer: FrozenDateTimeFactory,
5858
config_entry: MockConfigEntry,
59-
reolink_connect: MagicMock,
59+
reolink_host: MagicMock,
6060
) -> None:
6161
"""Test smart ai binary sensor entity."""
62-
reolink_connect.model = TEST_HOST_MODEL
63-
reolink_connect.baichuan.smart_ai_state.return_value = True
62+
reolink_host.model = TEST_HOST_MODEL
63+
reolink_host.baichuan.smart_ai_state.return_value = True
6464
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
6565
assert await hass.config_entries.async_setup(config_entry.entry_id)
6666
await hass.async_block_till_done()
@@ -69,7 +69,7 @@ async def test_smart_ai_sensor(
6969
entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_crossline_zone1_person"
7070
assert hass.states.get(entity_id).state == STATE_ON
7171

72-
reolink_connect.baichuan.smart_ai_state.return_value = False
72+
reolink_host.baichuan.smart_ai_state.return_value = False
7373
freezer.tick(DEVICE_UPDATE_INTERVAL)
7474
async_fire_time_changed(hass)
7575
await hass.async_block_till_done()
@@ -80,7 +80,7 @@ async def test_smart_ai_sensor(
8080
async def test_tcp_callback(
8181
hass: HomeAssistant,
8282
config_entry: MockConfigEntry,
83-
reolink_connect: MagicMock,
83+
reolink_host: MagicMock,
8484
) -> None:
8585
"""Test tcp callback using motion sensor."""
8686

@@ -95,11 +95,11 @@ def register_callback(
9595

9696
callback_mock = callback_mock_class()
9797

98-
reolink_connect.model = TEST_HOST_MODEL
99-
reolink_connect.baichuan.events_active = True
100-
reolink_connect.baichuan.subscribe_events.reset_mock(side_effect=True)
101-
reolink_connect.baichuan.register_callback = callback_mock.register_callback
102-
reolink_connect.motion_detected.return_value = True
98+
reolink_host.model = TEST_HOST_MODEL
99+
reolink_host.baichuan.events_active = True
100+
reolink_host.baichuan.subscribe_events.reset_mock(side_effect=True)
101+
reolink_host.baichuan.register_callback = callback_mock.register_callback
102+
reolink_host.motion_detected.return_value = True
103103

104104
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
105105
assert await hass.config_entries.async_setup(config_entry.entry_id)
@@ -110,7 +110,7 @@ def register_callback(
110110
assert hass.states.get(entity_id).state == STATE_ON
111111

112112
# simulate a TCP push callback
113-
reolink_connect.motion_detected.return_value = False
113+
reolink_host.motion_detected.return_value = False
114114
assert callback_mock.callback_func is not None
115115
callback_mock.callback_func()
116116

tests/components/reolink/test_camera.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_camera(
2525
hass: HomeAssistant,
2626
hass_client_no_auth: ClientSessionGenerator,
2727
config_entry: MockConfigEntry,
28-
reolink_connect: MagicMock,
28+
reolink_host: MagicMock,
2929
) -> None:
3030
"""Test camera entity with fluent."""
3131
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
@@ -37,28 +37,26 @@ async def test_camera(
3737
assert hass.states.get(entity_id).state == CameraState.IDLE
3838

3939
# check getting a image from the camera
40-
reolink_connect.get_snapshot.return_value = b"image"
40+
reolink_host.get_snapshot.return_value = b"image"
4141
assert (await async_get_image(hass, entity_id)).content == b"image"
4242

43-
reolink_connect.get_snapshot.side_effect = ReolinkError("Test error")
43+
reolink_host.get_snapshot.side_effect = ReolinkError("Test error")
4444
with pytest.raises(HomeAssistantError):
4545
await async_get_image(hass, entity_id)
4646

4747
# check getting the stream source
4848
assert await async_get_stream_source(hass, entity_id) is not None
4949

50-
reolink_connect.get_snapshot.reset_mock(side_effect=True)
51-
5250

5351
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
5452
async def test_camera_no_stream_source(
5553
hass: HomeAssistant,
5654
config_entry: MockConfigEntry,
57-
reolink_connect: MagicMock,
55+
reolink_host: MagicMock,
5856
) -> None:
5957
"""Test camera entity with no stream source."""
60-
reolink_connect.model = TEST_DUO_MODEL
61-
reolink_connect.get_stream_source.return_value = None
58+
reolink_host.model = TEST_DUO_MODEL
59+
reolink_host.get_stream_source.return_value = None
6260

6361
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
6462
assert await hass.config_entries.async_setup(config_entry.entry_id)

tests/components/reolink/test_diagnostics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
async def test_entry_diagnostics(
1616
hass: HomeAssistant,
1717
hass_client: ClientSessionGenerator,
18-
reolink_connect: MagicMock,
19-
test_chime: Chime,
18+
reolink_host: MagicMock,
19+
reolink_chime: Chime,
2020
config_entry: MockConfigEntry,
2121
snapshot: SnapshotAssertion,
2222
) -> None:

0 commit comments

Comments
 (0)