Use non-autospec mock for Reolink switch tests (#147441)

This commit is contained in:
Abílio Costa 2025-06-24 18:19:41 +01:00 committed by GitHub
parent cefc8822b6
commit fe4ff4f835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 51 deletions

View File

@ -77,6 +77,9 @@ def _init_host_mock(host_mock: MagicMock) -> None:
host_mock.get_stream_source = AsyncMock() host_mock.get_stream_source = AsyncMock()
host_mock.get_snapshot = AsyncMock() host_mock.get_snapshot = AsyncMock()
host_mock.get_encoding = AsyncMock(return_value="h264") host_mock.get_encoding = AsyncMock(return_value="h264")
host_mock.pull_point_request = AsyncMock()
host_mock.set_audio = AsyncMock()
host_mock.set_email = AsyncMock()
host_mock.ONVIF_event_callback = AsyncMock() 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

View File

@ -33,11 +33,11 @@ async def test_switch(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock, reolink_host: MagicMock,
) -> None: ) -> None:
"""Test switch entity.""" """Test switch entity."""
reolink_connect.camera_name.return_value = TEST_CAM_NAME reolink_host.camera_name.return_value = TEST_CAM_NAME
reolink_connect.audio_record.return_value = True reolink_host.audio_record.return_value = True
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -47,7 +47,7 @@ async def test_switch(
entity_id = f"{Platform.SWITCH}.{TEST_CAM_NAME}_record_audio" entity_id = f"{Platform.SWITCH}.{TEST_CAM_NAME}_record_audio"
assert hass.states.get(entity_id).state == STATE_ON assert hass.states.get(entity_id).state == STATE_ON
reolink_connect.audio_record.return_value = False reolink_host.audio_record.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()
@ -61,9 +61,9 @@ async def test_switch(
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
reolink_connect.set_audio.assert_called_with(0, True) reolink_host.set_audio.assert_called_with(0, True)
reolink_connect.set_audio.side_effect = ReolinkError("Test error") reolink_host.set_audio.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -73,16 +73,16 @@ async def test_switch(
) )
# test switch turn off # test switch turn off
reolink_connect.set_audio.reset_mock(side_effect=True) reolink_host.set_audio.reset_mock(side_effect=True)
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
reolink_connect.set_audio.assert_called_with(0, False) reolink_host.set_audio.assert_called_with(0, False)
reolink_connect.set_audio.side_effect = ReolinkError("Test error") reolink_host.set_audio.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -91,29 +91,27 @@ async def test_switch(
blocking=True, blocking=True,
) )
reolink_connect.set_audio.reset_mock(side_effect=True) reolink_host.set_audio.reset_mock(side_effect=True)
reolink_connect.camera_online.return_value = False reolink_host.camera_online.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()
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
reolink_connect.camera_online.return_value = True
async def test_host_switch( async def test_host_switch(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock, reolink_host: MagicMock,
) -> None: ) -> None:
"""Test host switch entity.""" """Test host switch entity."""
reolink_connect.camera_name.return_value = TEST_CAM_NAME reolink_host.camera_name.return_value = TEST_CAM_NAME
reolink_connect.email_enabled.return_value = True reolink_host.email_enabled.return_value = True
reolink_connect.is_hub = False reolink_host.is_hub = False
reolink_connect.supported.return_value = True reolink_host.supported.return_value = True
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
assert await hass.config_entries.async_setup(config_entry.entry_id) assert await hass.config_entries.async_setup(config_entry.entry_id)
@ -123,7 +121,7 @@ async def test_host_switch(
entity_id = f"{Platform.SWITCH}.{TEST_NVR_NAME}_email_on_event" entity_id = f"{Platform.SWITCH}.{TEST_NVR_NAME}_email_on_event"
assert hass.states.get(entity_id).state == STATE_ON assert hass.states.get(entity_id).state == STATE_ON
reolink_connect.email_enabled.return_value = False reolink_host.email_enabled.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()
@ -137,9 +135,9 @@ async def test_host_switch(
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
reolink_connect.set_email.assert_called_with(None, True) reolink_host.set_email.assert_called_with(None, True)
reolink_connect.set_email.side_effect = ReolinkError("Test error") reolink_host.set_email.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -149,16 +147,16 @@ async def test_host_switch(
) )
# test switch turn off # test switch turn off
reolink_connect.set_email.reset_mock(side_effect=True) reolink_host.set_email.reset_mock(side_effect=True)
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
reolink_connect.set_email.assert_called_with(None, False) reolink_host.set_email.assert_called_with(None, False)
reolink_connect.set_email.side_effect = ReolinkError("Test error") reolink_host.set_email.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -167,15 +165,13 @@ async def test_host_switch(
blocking=True, blocking=True,
) )
reolink_connect.set_email.reset_mock(side_effect=True)
async def test_chime_switch( async def test_chime_switch(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock, reolink_host: MagicMock,
test_chime: Chime, reolink_chime: Chime,
) -> None: ) -> None:
"""Test host switch entity.""" """Test host switch entity."""
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
@ -186,7 +182,7 @@ async def test_chime_switch(
entity_id = f"{Platform.SWITCH}.test_chime_led" entity_id = f"{Platform.SWITCH}.test_chime_led"
assert hass.states.get(entity_id).state == STATE_ON assert hass.states.get(entity_id).state == STATE_ON
test_chime.led_state = False reolink_chime.led_state = 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()
@ -194,16 +190,16 @@ async def test_chime_switch(
assert hass.states.get(entity_id).state == STATE_OFF assert hass.states.get(entity_id).state == STATE_OFF
# test switch turn on # test switch turn on
test_chime.set_option = AsyncMock() reolink_chime.set_option = AsyncMock()
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
SERVICE_TURN_ON, SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
test_chime.set_option.assert_called_with(led=True) reolink_chime.set_option.assert_called_with(led=True)
test_chime.set_option.side_effect = ReolinkError("Test error") reolink_chime.set_option.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -213,16 +209,16 @@ async def test_chime_switch(
) )
# test switch turn off # test switch turn off
test_chime.set_option.reset_mock(side_effect=True) reolink_chime.set_option.reset_mock(side_effect=True)
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
SERVICE_TURN_OFF, SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id}, {ATTR_ENTITY_ID: entity_id},
blocking=True, blocking=True,
) )
test_chime.set_option.assert_called_with(led=False) reolink_chime.set_option.assert_called_with(led=False)
test_chime.set_option.side_effect = ReolinkError("Test error") reolink_chime.set_option.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
await hass.services.async_call( await hass.services.async_call(
SWITCH_DOMAIN, SWITCH_DOMAIN,
@ -231,8 +227,6 @@ async def test_chime_switch(
blocking=True, blocking=True,
) )
test_chime.set_option.reset_mock(side_effect=True)
@pytest.mark.parametrize( @pytest.mark.parametrize(
( (
@ -265,7 +259,7 @@ async def test_chime_switch(
async def test_cleanup_hub_switches( async def test_cleanup_hub_switches(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_connect: MagicMock, reolink_host: MagicMock,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
original_id: str, original_id: str,
capability: str, capability: str,
@ -279,9 +273,9 @@ async def test_cleanup_hub_switches(
domain = Platform.SWITCH domain = Platform.SWITCH
reolink_connect.channels = [0] reolink_host.channels = [0]
reolink_connect.is_hub = True reolink_host.is_hub = True
reolink_connect.supported = mock_supported reolink_host.supported = mock_supported
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
domain=domain, domain=domain,
@ -301,9 +295,6 @@ async def test_cleanup_hub_switches(
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None
reolink_connect.is_hub = False
reolink_connect.supported.return_value = True
@pytest.mark.parametrize( @pytest.mark.parametrize(
( (
@ -336,7 +327,7 @@ async def test_cleanup_hub_switches(
async def test_hub_switches_repair_issue( async def test_hub_switches_repair_issue(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
reolink_connect: MagicMock, reolink_host: MagicMock,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
issue_registry: ir.IssueRegistry, issue_registry: ir.IssueRegistry,
original_id: str, original_id: str,
@ -351,9 +342,9 @@ async def test_hub_switches_repair_issue(
domain = Platform.SWITCH domain = Platform.SWITCH
reolink_connect.channels = [0] reolink_host.channels = [0]
reolink_connect.is_hub = True reolink_host.is_hub = True
reolink_connect.supported = mock_supported reolink_host.supported = mock_supported
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
domain=domain, domain=domain,
@ -373,6 +364,3 @@ async def test_hub_switches_repair_issue(
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id)
assert (DOMAIN, "hub_switch_deprecated") in issue_registry.issues assert (DOMAIN, "hub_switch_deprecated") in issue_registry.issues
reolink_connect.is_hub = False
reolink_connect.supported.return_value = True