Add 100% coverage of Reolink button platform (#124380)
* Add 100% button coverage * review comments * fix * Use SERVICE_PRESS constant * Use DOMAIN instead of const.DOMAIN * styling * User entity_registry_enabled_by_default fixture * fixes * Split out ptz_move test * use SERVICE_PTZ_MOVE constant
This commit is contained in:
parent
fb5afff9d5
commit
4b111008df
7 changed files with 164 additions and 57 deletions
112
tests/components/reolink/test_button.py
Normal file
112
tests/components/reolink/test_button.py
Normal file
|
@ -0,0 +1,112 @@
|
|||
"""Test the Reolink button platform."""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from reolink_aio.exceptions import ReolinkError
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.components.reolink.button import ATTR_SPEED, SERVICE_PTZ_MOVE
|
||||
from homeassistant.components.reolink.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from .conftest import TEST_NVR_NAME
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_button(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
) -> None:
|
||||
"""Test button entity with ptz up."""
|
||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]):
|
||||
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.BUTTON}.{TEST_NVR_NAME}_ptz_up"
|
||||
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
reolink_connect.set_ptz_command.assert_called_once()
|
||||
|
||||
reolink_connect.set_ptz_command.side_effect = ReolinkError("Test error")
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_ptz_move_service(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
) -> None:
|
||||
"""Test ptz_move entity service using PTZ button entity."""
|
||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]):
|
||||
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.BUTTON}.{TEST_NVR_NAME}_ptz_up"
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PTZ_MOVE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_SPEED: 5},
|
||||
blocking=True,
|
||||
)
|
||||
reolink_connect.set_ptz_command.assert_called_with(0, command="Up", speed=5)
|
||||
|
||||
reolink_connect.set_ptz_command.side_effect = ReolinkError("Test error")
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_PTZ_MOVE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_SPEED: 5},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_host_button(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
reolink_connect: MagicMock,
|
||||
) -> None:
|
||||
"""Test host button entity with reboot."""
|
||||
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]):
|
||||
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.BUTTON}.{TEST_NVR_NAME}_restart"
|
||||
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
reolink_connect.reboot.assert_called_once()
|
||||
|
||||
reolink_connect.reboot.side_effect = ReolinkError("Test error")
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue