Add 100% coverage of Reolink switch platform (#124482)

* Add 100% switch test coverage

* use DOMAIN instead of const.DOMAIN

* Split tests and use parametrize

* Revert "Split tests and use parametrize"

This reverts commit 50d2184ce6.

* fixes
This commit is contained in:
starkillerOG 2024-08-30 14:39:12 +02:00 committed by GitHub
parent 6589216ed3
commit a5bacf5652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 215 additions and 14 deletions

View file

@ -33,6 +33,7 @@ TEST_UID = "ABC1234567D89EFG"
TEST_UID_CAM = "DEF7654321D89GHT"
TEST_PORT = 1234
TEST_NVR_NAME = "test_reolink_name"
TEST_CAM_NAME = "test_reolink_cam"
TEST_NVR_NAME2 = "test2_reolink_name"
TEST_USE_HTTPS = True
TEST_HOST_MODEL = "RLN8-410"

View file

@ -1,15 +1,31 @@
"""Test the Reolink switch platform."""
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
from homeassistant.components.reolink import const
from homeassistant.const import Platform
from freezegun.api import FrozenDateTimeFactory
import pytest
from reolink_aio.api import Chime
from reolink_aio.exceptions import ReolinkError
from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL
from homeassistant.components.reolink.const import DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er, issue_registry as ir
from .conftest import TEST_UID
from .conftest import TEST_CAM_NAME, TEST_NVR_NAME, TEST_UID
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed
async def test_cleanup_hdr_switch_(
@ -27,23 +43,21 @@ async def test_cleanup_hdr_switch_(
entity_registry.async_get_or_create(
domain=domain,
platform=const.DOMAIN,
platform=DOMAIN,
unique_id=original_id,
config_entry=config_entry,
suggested_object_id=original_id,
disabled_by=er.RegistryEntryDisabler.USER,
)
assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id)
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id)
# setup CH 0 and host entities/device
with patch("homeassistant.components.reolink.PLATFORMS", [domain]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert (
entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) is None
)
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None
async def test_hdr_switch_deprecated_repair_issue(
@ -62,20 +76,206 @@ async def test_hdr_switch_deprecated_repair_issue(
entity_registry.async_get_or_create(
domain=domain,
platform=const.DOMAIN,
platform=DOMAIN,
unique_id=original_id,
config_entry=config_entry,
suggested_object_id=original_id,
disabled_by=None,
)
assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id)
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id)
# setup CH 0 and host entities/device
with patch("homeassistant.components.reolink.PLATFORMS", [domain]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id)
assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id)
assert (const.DOMAIN, "hdr_switch_deprecated") in issue_registry.issues
assert (DOMAIN, "hdr_switch_deprecated") in issue_registry.issues
async def test_switch(
hass: HomeAssistant,
config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock,
) -> None:
"""Test switch entity."""
reolink_connect.camera_name.return_value = TEST_CAM_NAME
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
entity_id = f"{Platform.SWITCH}.{TEST_CAM_NAME}_record"
assert hass.states.get(entity_id).state == STATE_ON
reolink_connect.recording_enabled.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_OFF
# test switch turn on
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
reolink_connect.set_recording.assert_called_with(0, True)
reolink_connect.set_recording.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
# test switch turn off
reolink_connect.set_recording.side_effect = None
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
reolink_connect.set_recording.assert_called_with(0, False)
reolink_connect.set_recording.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
async def test_host_switch(
hass: HomeAssistant,
config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock,
) -> None:
"""Test host switch entity."""
reolink_connect.camera_name.return_value = TEST_CAM_NAME
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
entity_id = f"{Platform.SWITCH}.{TEST_NVR_NAME}_record"
assert hass.states.get(entity_id).state == STATE_ON
reolink_connect.recording_enabled.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_OFF
# test switch turn on
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
reolink_connect.set_recording.assert_called_with(None, True)
reolink_connect.set_recording.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
# test switch turn off
reolink_connect.set_recording.side_effect = None
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
reolink_connect.set_recording.assert_called_with(None, False)
reolink_connect.set_recording.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
async def test_chime_switch(
hass: HomeAssistant,
config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
reolink_connect: MagicMock,
test_chime: Chime,
) -> None:
"""Test host switch entity."""
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
entity_id = f"{Platform.SWITCH}.test_chime_led"
assert hass.states.get(entity_id).state == STATE_ON
test_chime.led_state = 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_OFF
# test switch turn on
test_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)
test_chime.set_option.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
# test switch turn off
test_chime.set_option.side_effect = None
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)
test_chime.set_option.side_effect = ReolinkError("Test error")
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)