mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 17:57:55 +00:00

* Add chime play service * fix supported_feature * finalize * add tests * Adjust to device service * fix issue * Add tests * actions -> services * fix styling * Use conftest fixture for test_chime * Update tests/components/reolink/test_services.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * use ATTR_RINGTONE and rename chime_play to play_chime * Add translatable exceptions * fix styling * Remove option to use entity_id * fixes * Fix translations * fix * fix translation key * remove translation key * use callback for async_setup_services * fix styling * Add test_play_chime_service_unloaded --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
117 lines
4.0 KiB
Python
117 lines
4.0 KiB
Python
"""Test the Reolink services."""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
from reolink_aio.api import Chime
|
|
from reolink_aio.exceptions import InvalidParameterError, ReolinkError
|
|
|
|
from homeassistant.components.reolink.const import DOMAIN as REOLINK_DOMAIN
|
|
from homeassistant.components.reolink.services import ATTR_RINGTONE
|
|
from homeassistant.config_entries import ConfigEntryState
|
|
from homeassistant.const import ATTR_DEVICE_ID, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
|
from homeassistant.helpers import entity_registry as er
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_play_chime_service_entity(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
reolink_connect: MagicMock,
|
|
test_chime: Chime,
|
|
entity_registry: er.EntityRegistry,
|
|
) -> None:
|
|
"""Test chime play service."""
|
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SELECT]):
|
|
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.SELECT}.test_chime_visitor_ringtone"
|
|
entity = entity_registry.async_get(entity_id)
|
|
assert entity is not None
|
|
device_id = entity.device_id
|
|
|
|
# Test chime play service with device
|
|
test_chime.play = AsyncMock()
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|
|
test_chime.play.assert_called_once()
|
|
|
|
# Test errors
|
|
with pytest.raises(ServiceValidationError):
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: ["invalid_id"], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|
|
|
|
test_chime.play = AsyncMock(side_effect=ReolinkError("Test error"))
|
|
with pytest.raises(HomeAssistantError):
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|
|
|
|
test_chime.play = AsyncMock(side_effect=InvalidParameterError("Test error"))
|
|
with pytest.raises(ServiceValidationError):
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|
|
|
|
reolink_connect.chime.return_value = None
|
|
with pytest.raises(ServiceValidationError):
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|
|
|
|
|
|
async def test_play_chime_service_unloaded(
|
|
hass: HomeAssistant,
|
|
config_entry: MockConfigEntry,
|
|
reolink_connect: MagicMock,
|
|
test_chime: Chime,
|
|
entity_registry: er.EntityRegistry,
|
|
) -> None:
|
|
"""Test chime play service when config entry is unloaded."""
|
|
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SELECT]):
|
|
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.SELECT}.test_chime_visitor_ringtone"
|
|
entity = entity_registry.async_get(entity_id)
|
|
assert entity is not None
|
|
device_id = entity.device_id
|
|
|
|
# Unload the config entry
|
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
|
|
|
# Test chime play service
|
|
with pytest.raises(ServiceValidationError):
|
|
await hass.services.async_call(
|
|
REOLINK_DOMAIN,
|
|
"play_chime",
|
|
{ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"},
|
|
blocking=True,
|
|
)
|