2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the notify demo platform."""
|
2024-03-08 18:16:21 +00:00
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
from collections.abc import Generator
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2018-11-30 20:28:35 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-04-01 09:11:59 +00:00
|
|
|
from homeassistant.components import notify
|
2024-04-13 12:27:07 +00:00
|
|
|
from homeassistant.components.demo import DOMAIN
|
2019-03-28 03:36:13 +00:00
|
|
|
import homeassistant.components.demo.notify as demo
|
2024-04-13 12:27:07 +00:00
|
|
|
from homeassistant.const import Platform
|
|
|
|
from homeassistant.core import Event, HomeAssistant, callback
|
2020-10-19 21:29:44 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-09-26 16:02:05 +00:00
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
from tests.common import MockConfigEntry, async_capture_events
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def notify_only() -> Generator[None, None]:
|
|
|
|
"""Enable only the notify platform."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
|
|
|
|
[Platform.NOTIFY],
|
|
|
|
):
|
|
|
|
yield
|
2016-02-14 23:08:23 +00:00
|
|
|
|
2015-12-10 07:46:50 +00:00
|
|
|
|
2023-06-14 14:50:35 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2024-04-13 12:27:07 +00:00
|
|
|
async def setup_notify(hass: HomeAssistant, notify_only: None) -> None:
|
|
|
|
"""Initialize setup demo Notify entity."""
|
|
|
|
entry = MockConfigEntry(domain=DOMAIN)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("notify.notifier")
|
|
|
|
assert state is not None
|
2023-06-14 14:50:35 +00:00
|
|
|
|
|
|
|
|
2020-10-19 21:29:44 +00:00
|
|
|
@pytest.fixture
|
2024-04-13 12:27:07 +00:00
|
|
|
def events(hass: HomeAssistant) -> list[Event]:
|
2020-10-19 21:29:44 +00:00
|
|
|
"""Fixture that catches notify events."""
|
2021-02-26 21:28:52 +00:00
|
|
|
return async_capture_events(hass, demo.EVENT_NOTIFY)
|
2017-01-15 02:53:14 +00:00
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2020-10-19 21:29:44 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def calls():
|
|
|
|
"""Fixture to calls."""
|
|
|
|
return []
|
|
|
|
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2020-10-19 21:29:44 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def record_calls(calls):
|
|
|
|
"""Fixture to record calls."""
|
2016-08-17 05:05:41 +00:00
|
|
|
|
2020-10-19 21:29:44 +00:00
|
|
|
@callback
|
|
|
|
def record_calls(*args):
|
|
|
|
"""Record calls."""
|
|
|
|
calls.append(args)
|
|
|
|
|
|
|
|
return record_calls
|
|
|
|
|
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
async def test_sending_message(hass: HomeAssistant, events: list[Event]) -> None:
|
|
|
|
"""Test sending a message."""
|
2020-10-19 21:29:44 +00:00
|
|
|
data = {
|
2024-04-13 12:27:07 +00:00
|
|
|
"entity_id": "notify.notifier",
|
|
|
|
notify.ATTR_MESSAGE: "Test message",
|
2020-10-19 21:29:44 +00:00
|
|
|
}
|
2024-04-13 12:27:07 +00:00
|
|
|
await hass.services.async_call(notify.DOMAIN, notify.SERVICE_SEND_MESSAGE, data)
|
2020-10-19 21:29:44 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
last_event = events[-1]
|
2024-05-03 09:17:28 +00:00
|
|
|
assert last_event.data == {notify.ATTR_MESSAGE: "Test message"}
|
|
|
|
|
|
|
|
data[notify.ATTR_TITLE] = "My title"
|
|
|
|
# Test with Title
|
|
|
|
await hass.services.async_call(notify.DOMAIN, notify.SERVICE_SEND_MESSAGE, data)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
last_event = events[-1]
|
|
|
|
assert last_event.data == {
|
|
|
|
notify.ATTR_MESSAGE: "Test message",
|
|
|
|
notify.ATTR_TITLE: "My title",
|
|
|
|
}
|
2020-10-19 21:29:44 +00:00
|
|
|
|
|
|
|
|
2024-04-13 12:27:07 +00:00
|
|
|
async def test_calling_notify_from_script_loaded_from_yaml(
|
|
|
|
hass: HomeAssistant, events: list[Event]
|
2023-02-11 07:26:13 +00:00
|
|
|
) -> None:
|
2020-10-19 21:29:44 +00:00
|
|
|
"""Test if we can call a notify from a script."""
|
|
|
|
step = {
|
2024-04-13 12:27:07 +00:00
|
|
|
"service": "notify.send_message",
|
2020-10-19 21:29:44 +00:00
|
|
|
"data": {
|
2024-04-13 12:27:07 +00:00
|
|
|
"entity_id": "notify.notifier",
|
2020-10-19 21:29:44 +00:00
|
|
|
},
|
|
|
|
"data_template": {"message": "Test 123 {{ 2 + 2 }}\n"},
|
|
|
|
}
|
|
|
|
await async_setup_component(
|
|
|
|
hass, "script", {"script": {"test": {"sequence": step}}}
|
|
|
|
)
|
|
|
|
await hass.services.async_call("script", "test")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(events) == 1
|
|
|
|
assert {
|
|
|
|
"message": "Test 123 4",
|
|
|
|
} == events[0].data
|