2024-04-13 08:14:58 +00:00
|
|
|
"""The tests for notify entity platform."""
|
2024-03-08 18:16:21 +00:00
|
|
|
|
2024-04-11 10:04:08 +00:00
|
|
|
import copy
|
2024-04-13 08:14:58 +00:00
|
|
|
from unittest.mock import MagicMock
|
2022-03-28 06:53:30 +00:00
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
import pytest
|
2024-04-12 08:40:17 +00:00
|
|
|
import voluptuous as vol
|
2022-03-28 06:53:30 +00:00
|
|
|
|
2021-01-18 20:42:20 +00:00
|
|
|
from homeassistant.components import notify
|
2024-04-11 10:04:08 +00:00
|
|
|
from homeassistant.components.notify import (
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SEND_MESSAGE,
|
|
|
|
NotifyEntity,
|
|
|
|
NotifyEntityDescription,
|
2024-05-03 09:17:28 +00:00
|
|
|
NotifyEntityFeature,
|
2024-04-11 10:04:08 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-04-13 08:14:58 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
|
2024-04-11 10:04:08 +00:00
|
|
|
from homeassistant.core import HomeAssistant, State
|
2021-01-18 20:42:20 +00:00
|
|
|
|
2024-04-11 10:04:08 +00:00
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
MockEntity,
|
|
|
|
MockModule,
|
|
|
|
mock_integration,
|
|
|
|
mock_platform,
|
|
|
|
mock_restore_cache,
|
|
|
|
setup_test_component_platform,
|
|
|
|
)
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
TEST_KWARGS = {notify.ATTR_MESSAGE: "Test message"}
|
|
|
|
TEST_KWARGS_TITLE = {notify.ATTR_MESSAGE: "Test message", notify.ATTR_TITLE: "My title"}
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockNotifyEntity(MockEntity, NotifyEntity):
|
|
|
|
"""Mock Email notitier entity to use in tests."""
|
|
|
|
|
|
|
|
send_message_mock_calls = MagicMock()
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
async def async_send_message(self, message: str, title: str | None = None) -> None:
|
2024-04-11 10:04:08 +00:00
|
|
|
"""Send a notification message."""
|
2024-05-03 09:17:28 +00:00
|
|
|
self.send_message_mock_calls(message, title=title)
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MockNotifyEntityNonAsync(MockEntity, NotifyEntity):
|
|
|
|
"""Mock Email notitier entity to use in tests."""
|
|
|
|
|
|
|
|
send_message_mock_calls = MagicMock()
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
def send_message(self, message: str, title: str | None = None) -> None:
|
2024-04-11 10:04:08 +00:00
|
|
|
"""Send a notification message."""
|
2024-05-03 09:17:28 +00:00
|
|
|
self.send_message_mock_calls(message, title=title)
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def help_async_setup_entry_init(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Set up test config entry."""
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN])
|
2024-04-11 10:04:08 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def help_async_unload_entry(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Unload test config emntry."""
|
|
|
|
return await hass.config_entries.async_unload_platforms(
|
|
|
|
config_entry, [Platform.NOTIFY]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"entity",
|
|
|
|
[
|
|
|
|
MockNotifyEntityNonAsync(name="test", entity_id="notify.test"),
|
|
|
|
MockNotifyEntity(name="test", entity_id="notify.test"),
|
|
|
|
],
|
|
|
|
ids=["non_async", "async"],
|
|
|
|
)
|
|
|
|
async def test_send_message_service(
|
|
|
|
hass: HomeAssistant, config_flow_fixture: None, entity: NotifyEntity
|
|
|
|
) -> None:
|
|
|
|
"""Test send_message service."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain="test")
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=help_async_setup_entry_init,
|
|
|
|
async_unload_entry=help_async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity], from_config_entry=True)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
|
|
|
|
state = hass.states.get("notify.test")
|
|
|
|
assert state.state is STATE_UNKNOWN
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SEND_MESSAGE,
|
|
|
|
copy.deepcopy(TEST_KWARGS) | {"entity_id": "notify.test"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entity.send_message_mock_calls.assert_called_once()
|
2024-04-12 08:40:17 +00:00
|
|
|
entity.send_message_mock_calls.reset_mock()
|
|
|
|
|
|
|
|
# Test schema: `None` message fails
|
|
|
|
with pytest.raises(vol.Invalid) as exc:
|
|
|
|
await hass.services.async_call(
|
|
|
|
notify.DOMAIN,
|
|
|
|
notify.SERVICE_SEND_MESSAGE,
|
|
|
|
{"entity_id": "notify.test", notify.ATTR_MESSAGE: None},
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
str(exc.value) == "string value is None for dictionary value @ data['message']"
|
|
|
|
)
|
|
|
|
entity.send_message_mock_calls.assert_not_called()
|
|
|
|
|
|
|
|
# Test schema: No message fails
|
|
|
|
with pytest.raises(vol.Invalid) as exc:
|
|
|
|
await hass.services.async_call(
|
|
|
|
notify.DOMAIN, notify.SERVICE_SEND_MESSAGE, {"entity_id": "notify.test"}
|
|
|
|
)
|
|
|
|
assert str(exc.value) == "required key not provided @ data['message']"
|
|
|
|
entity.send_message_mock_calls.assert_not_called()
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
# Test unloading the entry succeeds
|
|
|
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
|
|
|
|
|
|
|
|
2024-05-03 09:17:28 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"entity",
|
|
|
|
[
|
|
|
|
MockNotifyEntityNonAsync(
|
|
|
|
name="test",
|
|
|
|
entity_id="notify.test",
|
|
|
|
supported_features=NotifyEntityFeature.TITLE,
|
|
|
|
),
|
|
|
|
MockNotifyEntity(
|
|
|
|
name="test",
|
|
|
|
entity_id="notify.test",
|
|
|
|
supported_features=NotifyEntityFeature.TITLE,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
ids=["non_async", "async"],
|
|
|
|
)
|
|
|
|
async def test_send_message_service_with_title(
|
|
|
|
hass: HomeAssistant, config_flow_fixture: None, entity: NotifyEntity
|
|
|
|
) -> None:
|
|
|
|
"""Test send_message service."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain="test")
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=help_async_setup_entry_init,
|
|
|
|
async_unload_entry=help_async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity], from_config_entry=True)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
|
|
|
|
state = hass.states.get("notify.test")
|
|
|
|
assert state.state is STATE_UNKNOWN
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SEND_MESSAGE,
|
|
|
|
copy.deepcopy(TEST_KWARGS_TITLE) | {"entity_id": "notify.test"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
entity.send_message_mock_calls.assert_called_once_with(
|
|
|
|
TEST_KWARGS_TITLE[notify.ATTR_MESSAGE],
|
|
|
|
title=TEST_KWARGS_TITLE[notify.ATTR_TITLE],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-11 10:04:08 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("state", "init_state"),
|
|
|
|
[
|
|
|
|
("2021-01-01T23:59:59+00:00", "2021-01-01T23:59:59+00:00"),
|
|
|
|
(STATE_UNAVAILABLE, STATE_UNKNOWN),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_restore_state(
|
|
|
|
hass: HomeAssistant, config_flow_fixture: None, state: str, init_state: str
|
|
|
|
) -> None:
|
|
|
|
"""Test we restore state integration."""
|
|
|
|
mock_restore_cache(hass, (State("notify.test", state),))
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=help_async_setup_entry_init,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
entity = MockNotifyEntity(name="test", entity_id="notify.test")
|
|
|
|
setup_test_component_platform(hass, DOMAIN, [entity], from_config_entry=True)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain="test")
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
|
|
|
|
state = hass.states.get("notify.test")
|
|
|
|
assert state is not None
|
|
|
|
assert state.state is init_state
|
|
|
|
|
|
|
|
|
|
|
|
async def test_name(hass: HomeAssistant, config_flow_fixture: None) -> None:
|
|
|
|
"""Test notify name."""
|
|
|
|
|
|
|
|
mock_platform(hass, "test.config_flow")
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=help_async_setup_entry_init,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Unnamed notify entity -> no name
|
|
|
|
entity1 = NotifyEntity()
|
|
|
|
entity1.entity_id = "notify.test1"
|
|
|
|
|
|
|
|
# Unnamed notify entity and has_entity_name True -> unnamed
|
|
|
|
entity2 = NotifyEntity()
|
|
|
|
entity2.entity_id = "notify.test3"
|
|
|
|
entity2._attr_has_entity_name = True
|
|
|
|
|
|
|
|
# Named notify entity and has_entity_name True -> named
|
|
|
|
entity3 = NotifyEntity()
|
|
|
|
entity3.entity_id = "notify.test4"
|
|
|
|
entity3.entity_description = NotifyEntityDescription("test", has_entity_name=True)
|
|
|
|
|
|
|
|
setup_test_component_platform(
|
|
|
|
hass, DOMAIN, [entity1, entity2, entity3], from_config_entry=True
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain="test")
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity1.entity_id)
|
|
|
|
assert state
|
2024-05-03 09:17:28 +00:00
|
|
|
assert state.attributes == {"supported_features": NotifyEntityFeature(0)}
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity2.entity_id)
|
|
|
|
assert state
|
2024-05-03 09:17:28 +00:00
|
|
|
assert state.attributes == {"supported_features": NotifyEntityFeature(0)}
|
2024-04-11 10:04:08 +00:00
|
|
|
|
|
|
|
state = hass.states.get(entity3.entity_id)
|
|
|
|
assert state
|
2024-05-03 09:17:28 +00:00
|
|
|
assert state.attributes == {"supported_features": NotifyEntityFeature(0)}
|