mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Use non-autospec mock for Reolink switch tests (#147441)
This commit is contained in:
parent
cefc8822b6
commit
fe4ff4f835
@ -77,6 +77,9 @@ def _init_host_mock(host_mock: MagicMock) -> None:
|
||||
host_mock.get_stream_source = AsyncMock()
|
||||
host_mock.get_snapshot = AsyncMock()
|
||||
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.is_nvr = True
|
||||
host_mock.is_hub = False
|
||||
|
@ -33,11 +33,11 @@ async def test_switch(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_host: MagicMock,
|
||||
) -> None:
|
||||
"""Test switch entity."""
|
||||
reolink_connect.camera_name.return_value = TEST_CAM_NAME
|
||||
reolink_connect.audio_record.return_value = True
|
||||
reolink_host.camera_name.return_value = TEST_CAM_NAME
|
||||
reolink_host.audio_record.return_value = True
|
||||
|
||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
|
||||
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"
|
||||
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)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
@ -61,9 +61,9 @@ async def test_switch(
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -73,16 +73,16 @@ async def test_switch(
|
||||
)
|
||||
|
||||
# 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(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -91,29 +91,27 @@ async def test_switch(
|
||||
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)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
|
||||
|
||||
reolink_connect.camera_online.return_value = True
|
||||
|
||||
|
||||
async def test_host_switch(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_host: MagicMock,
|
||||
) -> None:
|
||||
"""Test host switch entity."""
|
||||
reolink_connect.camera_name.return_value = TEST_CAM_NAME
|
||||
reolink_connect.email_enabled.return_value = True
|
||||
reolink_connect.is_hub = False
|
||||
reolink_connect.supported.return_value = True
|
||||
reolink_host.camera_name.return_value = TEST_CAM_NAME
|
||||
reolink_host.email_enabled.return_value = True
|
||||
reolink_host.is_hub = False
|
||||
reolink_host.supported.return_value = True
|
||||
|
||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
|
||||
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"
|
||||
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)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
@ -137,9 +135,9 @@ async def test_host_switch(
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -149,16 +147,16 @@ async def test_host_switch(
|
||||
)
|
||||
|
||||
# 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(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -167,15 +165,13 @@ async def test_host_switch(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
reolink_connect.set_email.reset_mock(side_effect=True)
|
||||
|
||||
|
||||
async def test_chime_switch(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
reolink_connect: MagicMock,
|
||||
test_chime: Chime,
|
||||
reolink_host: MagicMock,
|
||||
reolink_chime: Chime,
|
||||
) -> None:
|
||||
"""Test host switch entity."""
|
||||
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"
|
||||
assert hass.states.get(entity_id).state == STATE_ON
|
||||
|
||||
test_chime.led_state = False
|
||||
reolink_chime.led_state = False
|
||||
freezer.tick(DEVICE_UPDATE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
@ -194,16 +190,16 @@ async def test_chime_switch(
|
||||
assert hass.states.get(entity_id).state == STATE_OFF
|
||||
|
||||
# test switch turn on
|
||||
test_chime.set_option = AsyncMock()
|
||||
reolink_chime.set_option = AsyncMock()
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -213,16 +209,16 @@ async def test_chime_switch(
|
||||
)
|
||||
|
||||
# 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(
|
||||
SWITCH_DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
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):
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@ -231,8 +227,6 @@ async def test_chime_switch(
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
test_chime.set_option.reset_mock(side_effect=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
@ -265,7 +259,7 @@ async def test_chime_switch(
|
||||
async def test_cleanup_hub_switches(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_host: MagicMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
original_id: str,
|
||||
capability: str,
|
||||
@ -279,9 +273,9 @@ async def test_cleanup_hub_switches(
|
||||
|
||||
domain = Platform.SWITCH
|
||||
|
||||
reolink_connect.channels = [0]
|
||||
reolink_connect.is_hub = True
|
||||
reolink_connect.supported = mock_supported
|
||||
reolink_host.channels = [0]
|
||||
reolink_host.is_hub = True
|
||||
reolink_host.supported = mock_supported
|
||||
|
||||
entity_registry.async_get_or_create(
|
||||
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
|
||||
|
||||
reolink_connect.is_hub = False
|
||||
reolink_connect.supported.return_value = True
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
@ -336,7 +327,7 @@ async def test_cleanup_hub_switches(
|
||||
async def test_hub_switches_repair_issue(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
reolink_host: MagicMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
original_id: str,
|
||||
@ -351,9 +342,9 @@ async def test_hub_switches_repair_issue(
|
||||
|
||||
domain = Platform.SWITCH
|
||||
|
||||
reolink_connect.channels = [0]
|
||||
reolink_connect.is_hub = True
|
||||
reolink_connect.supported = mock_supported
|
||||
reolink_host.channels = [0]
|
||||
reolink_host.is_hub = True
|
||||
reolink_host.supported = mock_supported
|
||||
|
||||
entity_registry.async_get_or_create(
|
||||
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 (DOMAIN, "hub_switch_deprecated") in issue_registry.issues
|
||||
|
||||
reolink_connect.is_hub = False
|
||||
reolink_connect.supported.return_value = True
|
||||
|
Loading…
x
Reference in New Issue
Block a user