1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -20,12 +20,11 @@ class TestPersistentNotification:
def test_create(self):
"""Test creating notification without title or notification id."""
notifications = self.hass.data[pn.DOMAIN]['notifications']
notifications = self.hass.data[pn.DOMAIN]["notifications"]
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
assert len(notifications) == 0
pn.create(self.hass, 'Hello World {{ 1 + 1 }}',
title='{{ 1 + 1 }} beers')
pn.create(self.hass, "Hello World {{ 1 + 1 }}", title="{{ 1 + 1 }} beers")
self.hass.block_till_done()
entity_ids = self.hass.states.entity_ids(pn.DOMAIN)
@@ -34,55 +33,55 @@ class TestPersistentNotification:
state = self.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'
assert state.attributes.get("message") == "Hello World 2"
assert state.attributes.get("title") == "2 beers"
notification = notifications.get(entity_ids[0])
assert notification['status'] == pn.STATUS_UNREAD
assert notification['message'] == 'Hello World 2'
assert notification['title'] == '2 beers'
assert notification['created_at'] is not None
assert notification["status"] == pn.STATUS_UNREAD
assert notification["message"] == "Hello World 2"
assert notification["title"] == "2 beers"
assert notification["created_at"] is not None
notifications.clear()
def test_create_notification_id(self):
"""Ensure overwrites existing notification with same id."""
notifications = self.hass.data[pn.DOMAIN]['notifications']
notifications = self.hass.data[pn.DOMAIN]["notifications"]
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
assert len(notifications) == 0
pn.create(self.hass, 'test', notification_id='Beer 2')
pn.create(self.hass, "test", notification_id="Beer 2")
self.hass.block_till_done()
assert len(self.hass.states.entity_ids()) == 1
assert len(notifications) == 1
entity_id = 'persistent_notification.beer_2'
entity_id = "persistent_notification.beer_2"
state = self.hass.states.get(entity_id)
assert state.attributes.get('message') == 'test'
assert state.attributes.get("message") == "test"
notification = notifications.get(entity_id)
assert notification['message'] == 'test'
assert notification['title'] is None
assert notification["message"] == "test"
assert notification["title"] is None
pn.create(self.hass, 'test 2', notification_id='Beer 2')
pn.create(self.hass, "test 2", notification_id="Beer 2")
self.hass.block_till_done()
# We should have overwritten old one
assert len(self.hass.states.entity_ids()) == 1
state = self.hass.states.get(entity_id)
assert state.attributes.get('message') == 'test 2'
assert state.attributes.get("message") == "test 2"
notification = notifications.get(entity_id)
assert notification['message'] == 'test 2'
assert notification["message"] == "test 2"
notifications.clear()
def test_create_template_error(self):
"""Ensure we output templates if contain error."""
notifications = self.hass.data[pn.DOMAIN]['notifications']
notifications = self.hass.data[pn.DOMAIN]["notifications"]
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
assert len(notifications) == 0
pn.create(self.hass, '{{ message + 1 }}', '{{ title + 1 }}')
pn.create(self.hass, "{{ message + 1 }}", "{{ title + 1 }}")
self.hass.block_till_done()
entity_ids = self.hass.states.entity_ids(pn.DOMAIN)
@@ -90,26 +89,26 @@ class TestPersistentNotification:
assert len(notifications) == 1
state = self.hass.states.get(entity_ids[0])
assert state.attributes.get('message') == '{{ message + 1 }}'
assert state.attributes.get('title') == '{{ title + 1 }}'
assert state.attributes.get("message") == "{{ message + 1 }}"
assert state.attributes.get("title") == "{{ title + 1 }}"
notification = notifications.get(entity_ids[0])
assert notification['message'] == '{{ message + 1 }}'
assert notification['title'] == '{{ title + 1 }}'
assert notification["message"] == "{{ message + 1 }}"
assert notification["title"] == "{{ title + 1 }}"
notifications.clear()
def test_dismiss_notification(self):
"""Ensure removal of specific notification."""
notifications = self.hass.data[pn.DOMAIN]['notifications']
notifications = self.hass.data[pn.DOMAIN]["notifications"]
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
assert len(notifications) == 0
pn.create(self.hass, 'test', notification_id='Beer 2')
pn.create(self.hass, "test", notification_id="Beer 2")
self.hass.block_till_done()
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 1
assert len(notifications) == 1
pn.dismiss(self.hass, notification_id='Beer 2')
pn.dismiss(self.hass, notification_id="Beer 2")
self.hass.block_till_done()
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
@@ -118,25 +117,25 @@ class TestPersistentNotification:
def test_mark_read(self):
"""Ensure notification is marked as Read."""
notifications = self.hass.data[pn.DOMAIN]['notifications']
notifications = self.hass.data[pn.DOMAIN]["notifications"]
assert len(notifications) == 0
pn.create(self.hass, 'test', notification_id='Beer 2')
pn.create(self.hass, "test", notification_id="Beer 2")
self.hass.block_till_done()
entity_id = 'persistent_notification.beer_2'
entity_id = "persistent_notification.beer_2"
assert len(notifications) == 1
notification = notifications.get(entity_id)
assert notification['status'] == pn.STATUS_UNREAD
assert notification["status"] == pn.STATUS_UNREAD
self.hass.services.call(pn.DOMAIN, pn.SERVICE_MARK_READ, {
'notification_id': 'Beer 2'
})
self.hass.services.call(
pn.DOMAIN, pn.SERVICE_MARK_READ, {"notification_id": "Beer 2"}
)
self.hass.block_till_done()
assert len(notifications) == 1
notification = notifications.get(entity_id)
assert notification['status'] == pn.STATUS_READ
assert notification["status"] == pn.STATUS_READ
notifications.clear()
@@ -146,56 +145,45 @@ async def test_ws_get_notifications(hass, hass_ws_client):
client = await hass_ws_client(hass)
await client.send_json({
'id': 5,
'type': 'persistent_notification/get'
})
await client.send_json({"id": 5, "type": "persistent_notification/get"})
msg = await client.receive_json()
assert msg['id'] == 5
assert msg['type'] == TYPE_RESULT
assert msg['success']
notifications = msg['result']
assert msg["id"] == 5
assert msg["type"] == TYPE_RESULT
assert msg["success"]
notifications = msg["result"]
assert len(notifications) == 0
# Create
hass.components.persistent_notification.async_create(
'test', notification_id='Beer 2')
await client.send_json({
'id': 6,
'type': 'persistent_notification/get'
})
"test", notification_id="Beer 2"
)
await client.send_json({"id": 6, "type": "persistent_notification/get"})
msg = await client.receive_json()
assert msg['id'] == 6
assert msg['type'] == TYPE_RESULT
assert msg['success']
notifications = msg['result']
assert msg["id"] == 6
assert msg["type"] == TYPE_RESULT
assert msg["success"]
notifications = msg["result"]
assert len(notifications) == 1
notification = 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
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, pn.SERVICE_MARK_READ, {
'notification_id': 'Beer 2'
})
await client.send_json({
'id': 7,
'type': 'persistent_notification/get'
})
await hass.services.async_call(
pn.DOMAIN, pn.SERVICE_MARK_READ, {"notification_id": "Beer 2"}
)
await client.send_json({"id": 7, "type": "persistent_notification/get"})
msg = await client.receive_json()
notifications = msg['result']
notifications = msg["result"]
assert len(notifications) == 1
assert notifications[0]['status'] == pn.STATUS_READ
assert notifications[0]["status"] == pn.STATUS_READ
# Dismiss
hass.components.persistent_notification.async_dismiss('Beer 2')
await client.send_json({
'id': 8,
'type': 'persistent_notification/get'
})
hass.components.persistent_notification.async_dismiss("Beer 2")
await client.send_json({"id": 8, "type": "persistent_notification/get"})
msg = await client.receive_json()
notifications = msg['result']
notifications = msg["result"]
assert len(notifications) == 0