104 lines
2.9 KiB
Python
104 lines
2.9 KiB
Python
"""Tests for the Alexa Devices notify platform."""
|
|
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
import pytest
|
|
from syrupy.assertion import SnapshotAssertion
|
|
|
|
from homeassistant.components.alexa_devices.coordinator import SCAN_INTERVAL
|
|
from homeassistant.components.notify import (
|
|
ATTR_MESSAGE,
|
|
DOMAIN as NOTIFY_DOMAIN,
|
|
SERVICE_SEND_MESSAGE,
|
|
)
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import entity_registry as er
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
from . import setup_integration
|
|
from .const import TEST_SERIAL_NUMBER
|
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
|
|
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
async def test_all_entities(
|
|
hass: HomeAssistant,
|
|
snapshot: SnapshotAssertion,
|
|
mock_amazon_devices_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
entity_registry: er.EntityRegistry,
|
|
) -> None:
|
|
"""Test all entities."""
|
|
with patch("homeassistant.components.alexa_devices.PLATFORMS", [Platform.NOTIFY]):
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"mode",
|
|
["speak", "announce"],
|
|
)
|
|
async def test_notify_send_message(
|
|
hass: HomeAssistant,
|
|
freezer: FrozenDateTimeFactory,
|
|
mock_amazon_devices_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
mode: str,
|
|
) -> None:
|
|
"""Test notify send message."""
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
entity_id = f"notify.echo_test_{mode}"
|
|
|
|
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
|
assert now
|
|
|
|
freezer.move_to(now)
|
|
await hass.services.async_call(
|
|
NOTIFY_DOMAIN,
|
|
SERVICE_SEND_MESSAGE,
|
|
{
|
|
ATTR_ENTITY_ID: entity_id,
|
|
ATTR_MESSAGE: "Test Message",
|
|
},
|
|
blocking=True,
|
|
)
|
|
|
|
assert (state := hass.states.get(entity_id))
|
|
assert state.state == now.isoformat()
|
|
|
|
|
|
async def test_offline_device(
|
|
hass: HomeAssistant,
|
|
freezer: FrozenDateTimeFactory,
|
|
mock_amazon_devices_client: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test offline device handling."""
|
|
|
|
entity_id = "notify.echo_test_announce"
|
|
|
|
mock_amazon_devices_client.get_devices_data.return_value[
|
|
TEST_SERIAL_NUMBER
|
|
].online = False
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
assert (state := hass.states.get(entity_id))
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
mock_amazon_devices_client.get_devices_data.return_value[
|
|
TEST_SERIAL_NUMBER
|
|
].online = True
|
|
|
|
freezer.tick(SCAN_INTERVAL)
|
|
async_fire_time_changed(hass)
|
|
await hass.async_block_till_done()
|
|
|
|
assert (state := hass.states.get(entity_id))
|
|
assert state.state != STATE_UNAVAILABLE
|