From f90a7404290c83fd25115d315140f86008ec6342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Thu, 19 Jun 2025 07:03:48 +0100 Subject: [PATCH] Use non-autospec mock for Reolink's binary_sensor, camera and diag tests (#147095) --- tests/components/reolink/conftest.py | 5 ++- .../components/reolink/test_binary_sensor.py | 34 +++++++++---------- tests/components/reolink/test_camera.py | 14 ++++---- tests/components/reolink/test_diagnostics.py | 4 +-- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/tests/components/reolink/conftest.py b/tests/components/reolink/conftest.py index 69eeee99fab..0ca5612f8fd 100644 --- a/tests/components/reolink/conftest.py +++ b/tests/components/reolink/conftest.py @@ -73,6 +73,10 @@ def _init_host_mock(host_mock: MagicMock) -> None: host_mock.reboot = AsyncMock() host_mock.set_ptz_command = AsyncMock() host_mock.get_motion_state_all_ch = AsyncMock(return_value=False) + host_mock.get_stream_source = AsyncMock() + host_mock.get_snapshot = AsyncMock() + host_mock.get_encoding = AsyncMock(return_value="h264") + host_mock.ONVIF_event_callback = AsyncMock() host_mock.is_nvr = True host_mock.is_hub = False host_mock.mac_address = TEST_MAC @@ -105,7 +109,6 @@ def _init_host_mock(host_mock: MagicMock) -> None: host_mock.camera_uid.return_value = TEST_UID_CAM host_mock.camera_online.return_value = True host_mock.channel_for_uid.return_value = 0 - host_mock.get_encoding.return_value = "h264" host_mock.firmware_update_available.return_value = False host_mock.session_active = True host_mock.timeout = 60 diff --git a/tests/components/reolink/test_binary_sensor.py b/tests/components/reolink/test_binary_sensor.py index 99c9efba002..e6275a2108e 100644 --- a/tests/components/reolink/test_binary_sensor.py +++ b/tests/components/reolink/test_binary_sensor.py @@ -21,11 +21,11 @@ async def test_motion_sensor( hass_client_no_auth: ClientSessionGenerator, freezer: FrozenDateTimeFactory, config_entry: MockConfigEntry, - reolink_connect: MagicMock, + reolink_host: MagicMock, ) -> None: """Test binary sensor entity with motion sensor.""" - reolink_connect.model = TEST_DUO_MODEL - reolink_connect.motion_detected.return_value = True + reolink_host.model = TEST_DUO_MODEL + reolink_host.motion_detected.return_value = True with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -34,7 +34,7 @@ async def test_motion_sensor( entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_motion_lens_0" assert hass.states.get(entity_id).state == STATE_ON - reolink_connect.motion_detected.return_value = False + reolink_host.motion_detected.return_value = False freezer.tick(DEVICE_UPDATE_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -42,8 +42,8 @@ async def test_motion_sensor( assert hass.states.get(entity_id).state == STATE_OFF # test ONVIF webhook callback - reolink_connect.motion_detected.return_value = True - reolink_connect.ONVIF_event_callback.return_value = [0] + reolink_host.motion_detected.return_value = True + reolink_host.ONVIF_event_callback.return_value = [0] webhook_id = config_entry.runtime_data.host.webhook_id client = await hass_client_no_auth() await client.post(f"/api/webhook/{webhook_id}", data="test_data") @@ -56,11 +56,11 @@ async def test_smart_ai_sensor( hass_client_no_auth: ClientSessionGenerator, freezer: FrozenDateTimeFactory, config_entry: MockConfigEntry, - reolink_connect: MagicMock, + reolink_host: MagicMock, ) -> None: """Test smart ai binary sensor entity.""" - reolink_connect.model = TEST_HOST_MODEL - reolink_connect.baichuan.smart_ai_state.return_value = True + reolink_host.model = TEST_HOST_MODEL + reolink_host.baichuan.smart_ai_state.return_value = True with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -69,7 +69,7 @@ async def test_smart_ai_sensor( entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_crossline_zone1_person" assert hass.states.get(entity_id).state == STATE_ON - reolink_connect.baichuan.smart_ai_state.return_value = False + reolink_host.baichuan.smart_ai_state.return_value = False freezer.tick(DEVICE_UPDATE_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -80,7 +80,7 @@ async def test_smart_ai_sensor( async def test_tcp_callback( hass: HomeAssistant, config_entry: MockConfigEntry, - reolink_connect: MagicMock, + reolink_host: MagicMock, ) -> None: """Test tcp callback using motion sensor.""" @@ -95,11 +95,11 @@ async def test_tcp_callback( callback_mock = callback_mock_class() - reolink_connect.model = TEST_HOST_MODEL - reolink_connect.baichuan.events_active = True - reolink_connect.baichuan.subscribe_events.reset_mock(side_effect=True) - reolink_connect.baichuan.register_callback = callback_mock.register_callback - reolink_connect.motion_detected.return_value = True + reolink_host.model = TEST_HOST_MODEL + reolink_host.baichuan.events_active = True + reolink_host.baichuan.subscribe_events.reset_mock(side_effect=True) + reolink_host.baichuan.register_callback = callback_mock.register_callback + reolink_host.motion_detected.return_value = True with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]): assert await hass.config_entries.async_setup(config_entry.entry_id) @@ -110,7 +110,7 @@ async def test_tcp_callback( assert hass.states.get(entity_id).state == STATE_ON # simulate a TCP push callback - reolink_connect.motion_detected.return_value = False + reolink_host.motion_detected.return_value = False assert callback_mock.callback_func is not None callback_mock.callback_func() diff --git a/tests/components/reolink/test_camera.py b/tests/components/reolink/test_camera.py index 4f18f769e02..4ab43de225f 100644 --- a/tests/components/reolink/test_camera.py +++ b/tests/components/reolink/test_camera.py @@ -25,7 +25,7 @@ async def test_camera( hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator, config_entry: MockConfigEntry, - reolink_connect: MagicMock, + reolink_host: MagicMock, ) -> None: """Test camera entity with fluent.""" with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): @@ -37,28 +37,26 @@ async def test_camera( assert hass.states.get(entity_id).state == CameraState.IDLE # check getting a image from the camera - reolink_connect.get_snapshot.return_value = b"image" + reolink_host.get_snapshot.return_value = b"image" assert (await async_get_image(hass, entity_id)).content == b"image" - reolink_connect.get_snapshot.side_effect = ReolinkError("Test error") + reolink_host.get_snapshot.side_effect = ReolinkError("Test error") with pytest.raises(HomeAssistantError): await async_get_image(hass, entity_id) # check getting the stream source assert await async_get_stream_source(hass, entity_id) is not None - reolink_connect.get_snapshot.reset_mock(side_effect=True) - @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_camera_no_stream_source( hass: HomeAssistant, config_entry: MockConfigEntry, - reolink_connect: MagicMock, + reolink_host: MagicMock, ) -> None: """Test camera entity with no stream source.""" - reolink_connect.model = TEST_DUO_MODEL - reolink_connect.get_stream_source.return_value = None + reolink_host.model = TEST_DUO_MODEL + reolink_host.get_stream_source.return_value = None with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): assert await hass.config_entries.async_setup(config_entry.entry_id) diff --git a/tests/components/reolink/test_diagnostics.py b/tests/components/reolink/test_diagnostics.py index d45163d3cf0..b347bae9ec0 100644 --- a/tests/components/reolink/test_diagnostics.py +++ b/tests/components/reolink/test_diagnostics.py @@ -15,8 +15,8 @@ from tests.typing import ClientSessionGenerator async def test_entry_diagnostics( hass: HomeAssistant, hass_client: ClientSessionGenerator, - reolink_connect: MagicMock, - test_chime: Chime, + reolink_host: MagicMock, + reolink_chime: Chime, config_entry: MockConfigEntry, snapshot: SnapshotAssertion, ) -> None: