mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 07:37:34 +00:00
Use non-autospec mock for Reolink's binary_sensor, camera and diag tests (#147095)
This commit is contained in:
parent
3dba7e5bd2
commit
f90a740429
@ -73,6 +73,10 @@ def _init_host_mock(host_mock: MagicMock) -> None:
|
|||||||
host_mock.reboot = AsyncMock()
|
host_mock.reboot = AsyncMock()
|
||||||
host_mock.set_ptz_command = AsyncMock()
|
host_mock.set_ptz_command = AsyncMock()
|
||||||
host_mock.get_motion_state_all_ch = AsyncMock(return_value=False)
|
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_nvr = True
|
||||||
host_mock.is_hub = False
|
host_mock.is_hub = False
|
||||||
host_mock.mac_address = TEST_MAC
|
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_uid.return_value = TEST_UID_CAM
|
||||||
host_mock.camera_online.return_value = True
|
host_mock.camera_online.return_value = True
|
||||||
host_mock.channel_for_uid.return_value = 0
|
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.firmware_update_available.return_value = False
|
||||||
host_mock.session_active = True
|
host_mock.session_active = True
|
||||||
host_mock.timeout = 60
|
host_mock.timeout = 60
|
||||||
|
@ -21,11 +21,11 @@ async def test_motion_sensor(
|
|||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test binary sensor entity with motion sensor."""
|
"""Test binary sensor entity with motion sensor."""
|
||||||
reolink_connect.model = TEST_DUO_MODEL
|
reolink_host.model = TEST_DUO_MODEL
|
||||||
reolink_connect.motion_detected.return_value = True
|
reolink_host.motion_detected.return_value = True
|
||||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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"
|
entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_motion_lens_0"
|
||||||
assert hass.states.get(entity_id).state == STATE_ON
|
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)
|
freezer.tick(DEVICE_UPDATE_INTERVAL)
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -42,8 +42,8 @@ async def test_motion_sensor(
|
|||||||
assert hass.states.get(entity_id).state == STATE_OFF
|
assert hass.states.get(entity_id).state == STATE_OFF
|
||||||
|
|
||||||
# test ONVIF webhook callback
|
# test ONVIF webhook callback
|
||||||
reolink_connect.motion_detected.return_value = True
|
reolink_host.motion_detected.return_value = True
|
||||||
reolink_connect.ONVIF_event_callback.return_value = [0]
|
reolink_host.ONVIF_event_callback.return_value = [0]
|
||||||
webhook_id = config_entry.runtime_data.host.webhook_id
|
webhook_id = config_entry.runtime_data.host.webhook_id
|
||||||
client = await hass_client_no_auth()
|
client = await hass_client_no_auth()
|
||||||
await client.post(f"/api/webhook/{webhook_id}", data="test_data")
|
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,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test smart ai binary sensor entity."""
|
"""Test smart ai binary sensor entity."""
|
||||||
reolink_connect.model = TEST_HOST_MODEL
|
reolink_host.model = TEST_HOST_MODEL
|
||||||
reolink_connect.baichuan.smart_ai_state.return_value = True
|
reolink_host.baichuan.smart_ai_state.return_value = True
|
||||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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"
|
entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_crossline_zone1_person"
|
||||||
assert hass.states.get(entity_id).state == STATE_ON
|
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)
|
freezer.tick(DEVICE_UPDATE_INTERVAL)
|
||||||
async_fire_time_changed(hass)
|
async_fire_time_changed(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
@ -80,7 +80,7 @@ async def test_smart_ai_sensor(
|
|||||||
async def test_tcp_callback(
|
async def test_tcp_callback(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test tcp callback using motion sensor."""
|
"""Test tcp callback using motion sensor."""
|
||||||
|
|
||||||
@ -95,11 +95,11 @@ async def test_tcp_callback(
|
|||||||
|
|
||||||
callback_mock = callback_mock_class()
|
callback_mock = callback_mock_class()
|
||||||
|
|
||||||
reolink_connect.model = TEST_HOST_MODEL
|
reolink_host.model = TEST_HOST_MODEL
|
||||||
reolink_connect.baichuan.events_active = True
|
reolink_host.baichuan.events_active = True
|
||||||
reolink_connect.baichuan.subscribe_events.reset_mock(side_effect=True)
|
reolink_host.baichuan.subscribe_events.reset_mock(side_effect=True)
|
||||||
reolink_connect.baichuan.register_callback = callback_mock.register_callback
|
reolink_host.baichuan.register_callback = callback_mock.register_callback
|
||||||
reolink_connect.motion_detected.return_value = True
|
reolink_host.motion_detected.return_value = True
|
||||||
|
|
||||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
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
|
assert hass.states.get(entity_id).state == STATE_ON
|
||||||
|
|
||||||
# simulate a TCP push callback
|
# 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
|
assert callback_mock.callback_func is not None
|
||||||
callback_mock.callback_func()
|
callback_mock.callback_func()
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ async def test_camera(
|
|||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test camera entity with fluent."""
|
"""Test camera entity with fluent."""
|
||||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
|
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
|
assert hass.states.get(entity_id).state == CameraState.IDLE
|
||||||
|
|
||||||
# check getting a image from the camera
|
# 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"
|
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):
|
with pytest.raises(HomeAssistantError):
|
||||||
await async_get_image(hass, entity_id)
|
await async_get_image(hass, entity_id)
|
||||||
|
|
||||||
# check getting the stream source
|
# check getting the stream source
|
||||||
assert await async_get_stream_source(hass, entity_id) is not None
|
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")
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
async def test_camera_no_stream_source(
|
async def test_camera_no_stream_source(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test camera entity with no stream source."""
|
"""Test camera entity with no stream source."""
|
||||||
reolink_connect.model = TEST_DUO_MODEL
|
reolink_host.model = TEST_DUO_MODEL
|
||||||
reolink_connect.get_stream_source.return_value = None
|
reolink_host.get_stream_source.return_value = None
|
||||||
|
|
||||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]):
|
||||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
@ -15,8 +15,8 @@ from tests.typing import ClientSessionGenerator
|
|||||||
async def test_entry_diagnostics(
|
async def test_entry_diagnostics(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
hass_client: ClientSessionGenerator,
|
hass_client: ClientSessionGenerator,
|
||||||
reolink_connect: MagicMock,
|
reolink_host: MagicMock,
|
||||||
test_chime: Chime,
|
reolink_chime: Chime,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user