mirror of
https://github.com/elisspace/core.git
synced 2026-09-29 13:59:54 +00:00
Complete persistent notifications migration (#92828)
* Complete migration of persistent notifications Persistent notifications are no longer stored in the state machine and no longer fire events * Complete migration of persistent notifications Persistent notifications are no longer stored in the state machine and no longer fire events * fixes * fixes * fixes * ws test * update tests * update tests * fix more tests * fix more tests * more fixes * fix * fix person * fix person * keep whitelist * use singleton
This commit is contained in:
@@ -6,7 +6,6 @@ from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_capture_events
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
|
||||
@@ -18,22 +17,14 @@ async def setup_integration(hass):
|
||||
|
||||
async def test_create(hass: HomeAssistant) -> None:
|
||||
"""Test creating notification without title or notification id."""
|
||||
notifications = hass.data[pn.DOMAIN]
|
||||
notifications = pn._async_get_or_create_notifications(hass)
|
||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
||||
assert len(notifications) == 0
|
||||
|
||||
pn.async_create(hass, "Hello World 2", title="2 beers")
|
||||
|
||||
entity_ids = hass.states.async_entity_ids(pn.DOMAIN)
|
||||
assert len(entity_ids) == 1
|
||||
assert len(notifications) == 1
|
||||
|
||||
state = hass.states.get(entity_ids[0])
|
||||
assert state.state == pn.STATE
|
||||
assert state.attributes.get("message") == "Hello World 2"
|
||||
assert state.attributes.get("title") == "2 beers"
|
||||
|
||||
notification = notifications.get(entity_ids[0])
|
||||
notification = notifications[list(notifications)[0]]
|
||||
assert notification["status"] == pn.STATUS_UNREAD
|
||||
assert notification["message"] == "Hello World 2"
|
||||
assert notification["title"] == "2 beers"
|
||||
@@ -42,54 +33,42 @@ async def test_create(hass: HomeAssistant) -> None:
|
||||
|
||||
async def test_create_notification_id(hass: HomeAssistant) -> None:
|
||||
"""Ensure overwrites existing notification with same id."""
|
||||
notifications = hass.data[pn.DOMAIN]
|
||||
notifications = pn._async_get_or_create_notifications(hass)
|
||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
||||
assert len(notifications) == 0
|
||||
|
||||
pn.async_create(hass, "test", notification_id="Beer 2")
|
||||
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
assert len(notifications) == 1
|
||||
notification = notifications[list(notifications)[0]]
|
||||
|
||||
entity_id = "persistent_notification.beer_2"
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.attributes.get("message") == "test"
|
||||
|
||||
notification = notifications.get(entity_id)
|
||||
assert notification["message"] == "test"
|
||||
assert notification["title"] is None
|
||||
|
||||
pn.async_create(hass, "test 2", notification_id="Beer 2")
|
||||
|
||||
# We should have overwritten old one
|
||||
assert len(hass.states.async_entity_ids()) == 1
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.attributes.get("message") == "test 2"
|
||||
notification = notifications[list(notifications)[0]]
|
||||
|
||||
notification = notifications.get(entity_id)
|
||||
assert notification["message"] == "test 2"
|
||||
|
||||
|
||||
async def test_dismiss_notification(hass: HomeAssistant) -> None:
|
||||
"""Ensure removal of specific notification."""
|
||||
notifications = hass.data[pn.DOMAIN]
|
||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
||||
notifications = pn._async_get_or_create_notifications(hass)
|
||||
assert len(notifications) == 0
|
||||
|
||||
pn.async_create(hass, "test", notification_id="Beer 2")
|
||||
|
||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 1
|
||||
assert len(notifications) == 1
|
||||
pn.async_dismiss(hass, notification_id="Beer 2")
|
||||
|
||||
assert len(hass.states.async_entity_ids(pn.DOMAIN)) == 0
|
||||
assert len(notifications) == 0
|
||||
|
||||
|
||||
async def test_mark_read(hass: HomeAssistant) -> None:
|
||||
"""Ensure notification is marked as Read."""
|
||||
events = async_capture_events(hass, pn.EVENT_PERSISTENT_NOTIFICATIONS_UPDATED)
|
||||
notifications = hass.data[pn.DOMAIN]
|
||||
notifications = pn._async_get_or_create_notifications(hass)
|
||||
assert len(notifications) == 0
|
||||
|
||||
await hass.services.async_call(
|
||||
@@ -99,20 +78,17 @@ async def test_mark_read(hass: HomeAssistant) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
entity_id = "persistent_notification.beer_2"
|
||||
assert len(notifications) == 1
|
||||
notification = notifications.get(entity_id)
|
||||
notification = notifications[list(notifications)[0]]
|
||||
assert notification["status"] == pn.STATUS_UNREAD
|
||||
assert len(events) == 1
|
||||
|
||||
await hass.services.async_call(
|
||||
pn.DOMAIN, "mark_read", {"notification_id": "Beer 2"}, blocking=True
|
||||
)
|
||||
|
||||
assert len(notifications) == 1
|
||||
notification = notifications.get(entity_id)
|
||||
notification = notifications[list(notifications)[0]]
|
||||
assert notification["status"] == pn.STATUS_READ
|
||||
assert len(events) == 2
|
||||
|
||||
await hass.services.async_call(
|
||||
pn.DOMAIN,
|
||||
@@ -121,7 +97,6 @@ async def test_mark_read(hass: HomeAssistant) -> None:
|
||||
blocking=True,
|
||||
)
|
||||
assert len(notifications) == 0
|
||||
assert len(events) == 3
|
||||
|
||||
|
||||
async def test_ws_get_notifications(
|
||||
@@ -172,3 +147,68 @@ async def test_ws_get_notifications(
|
||||
msg = await client.receive_json()
|
||||
notifications = msg["result"]
|
||||
assert len(notifications) == 0
|
||||
|
||||
|
||||
async def test_ws_get_subscribe(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test websocket subscribe endpoint for retrieving persistent notifications."""
|
||||
await async_setup_component(hass, pn.DOMAIN, {})
|
||||
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
await client.send_json({"id": 5, "type": "persistent_notification/subscribe"})
|
||||
msg = await client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == TYPE_RESULT
|
||||
assert msg["success"]
|
||||
|
||||
msg = await client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == "event"
|
||||
assert msg["event"]
|
||||
event = msg["event"]
|
||||
assert event["type"] == "current"
|
||||
assert event["notifications"] == {}
|
||||
|
||||
# Create
|
||||
pn.async_create(hass, "test", notification_id="Beer 2")
|
||||
|
||||
msg = await client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == "event"
|
||||
assert msg["event"]
|
||||
event = msg["event"]
|
||||
assert event["type"] == "added"
|
||||
notifications = event["notifications"]
|
||||
assert len(notifications) == 1
|
||||
notification = notifications[list(notifications)[0]]
|
||||
assert notification["notification_id"] == "Beer 2"
|
||||
assert notification["message"] == "test"
|
||||
assert notification["title"] is None
|
||||
assert notification["status"] == pn.STATUS_UNREAD
|
||||
assert notification["created_at"] is not None
|
||||
|
||||
# Mark Read
|
||||
await hass.services.async_call(
|
||||
pn.DOMAIN, "mark_read", {"notification_id": "Beer 2"}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == "event"
|
||||
assert msg["event"]
|
||||
event = msg["event"]
|
||||
assert event["type"] == "updated"
|
||||
notifications = event["notifications"]
|
||||
assert len(notifications) == 1
|
||||
notification = notifications[list(notifications)[0]]
|
||||
assert notification["status"] == pn.STATUS_READ
|
||||
|
||||
# Dismiss
|
||||
pn.async_dismiss(hass, "Beer 2")
|
||||
msg = await client.receive_json()
|
||||
assert msg["id"] == 5
|
||||
assert msg["type"] == "event"
|
||||
assert msg["event"]
|
||||
event = msg["event"]
|
||||
assert event["type"] == "removed"
|
||||
|
||||
Reference in New Issue
Block a user