2016-06-25 23:40:33 +00:00
|
|
|
"""The tests for the persistent notification component."""
|
2018-10-01 09:21:00 +00:00
|
|
|
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
2018-09-11 09:39:30 +00:00
|
|
|
from homeassistant.setup import setup_component, async_setup_component
|
2016-06-25 23:40:33 +00:00
|
|
|
import homeassistant.components.persistent_notification as pn
|
|
|
|
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
|
|
|
|
class TestPersistentNotification:
|
|
|
|
"""Test persistent notification component."""
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-06-25 23:40:33 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-09-25 21:15:21 +00:00
|
|
|
setup_component(self.hass, pn.DOMAIN, {})
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
def teardown_method(self, method):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
"""Test creating notification without title or notification id."""
|
2018-09-11 09:39:30 +00:00
|
|
|
notifications = self.hass.data[pn.DOMAIN]['notifications']
|
2016-06-25 23:40:33 +00:00
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 0
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
pn.create(self.hass, 'Hello World {{ 1 + 1 }}',
|
|
|
|
title='{{ 1 + 1 }} beers')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
entity_ids = self.hass.states.entity_ids(pn.DOMAIN)
|
|
|
|
assert len(entity_ids) == 1
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 1
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get(entity_ids[0])
|
2017-10-21 19:59:05 +00:00
|
|
|
assert state.state == pn.STATE
|
|
|
|
assert state.attributes.get('message') == 'Hello World 2'
|
2016-06-25 23:40:33 +00:00
|
|
|
assert state.attributes.get('title') == '2 beers'
|
|
|
|
|
2018-09-11 09:39:30 +00:00
|
|
|
notification = notifications.get(entity_ids[0])
|
|
|
|
assert notification['status'] == pn.STATUS_UNREAD
|
|
|
|
assert notification['message'] == 'Hello World 2'
|
|
|
|
assert notification['title'] == '2 beers'
|
2018-10-04 08:24:14 +00:00
|
|
|
assert notification['created_at'] is not None
|
2018-09-11 09:39:30 +00:00
|
|
|
notifications.clear()
|
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
def test_create_notification_id(self):
|
|
|
|
"""Ensure overwrites existing notification with same id."""
|
2018-09-11 09:39:30 +00:00
|
|
|
notifications = self.hass.data[pn.DOMAIN]['notifications']
|
2016-06-25 23:40:33 +00:00
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 0
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
pn.create(self.hass, 'test', notification_id='Beer 2')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
assert len(self.hass.states.entity_ids()) == 1
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 1
|
|
|
|
|
|
|
|
entity_id = 'persistent_notification.beer_2'
|
|
|
|
state = self.hass.states.get(entity_id)
|
2017-10-21 19:59:05 +00:00
|
|
|
assert state.attributes.get('message') == 'test'
|
2016-06-25 23:40:33 +00:00
|
|
|
|
2018-09-11 09:39:30 +00:00
|
|
|
notification = notifications.get(entity_id)
|
|
|
|
assert notification['message'] == 'test'
|
|
|
|
assert notification['title'] is None
|
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
pn.create(self.hass, 'test 2', notification_id='Beer 2')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
# We should have overwritten old one
|
|
|
|
assert len(self.hass.states.entity_ids()) == 1
|
2018-09-11 09:39:30 +00:00
|
|
|
state = self.hass.states.get(entity_id)
|
2017-10-21 19:59:05 +00:00
|
|
|
assert state.attributes.get('message') == 'test 2'
|
2016-06-25 23:40:33 +00:00
|
|
|
|
2018-09-11 09:39:30 +00:00
|
|
|
notification = notifications.get(entity_id)
|
|
|
|
assert notification['message'] == 'test 2'
|
|
|
|
notifications.clear()
|
|
|
|
|
2016-06-25 23:40:33 +00:00
|
|
|
def test_create_template_error(self):
|
|
|
|
"""Ensure we output templates if contain error."""
|
2018-09-11 09:39:30 +00:00
|
|
|
notifications = self.hass.data[pn.DOMAIN]['notifications']
|
2016-06-25 23:40:33 +00:00
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 0
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
pn.create(self.hass, '{{ message + 1 }}', '{{ title + 1 }}')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
entity_ids = self.hass.states.entity_ids(pn.DOMAIN)
|
|
|
|
assert len(entity_ids) == 1
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 1
|
2016-06-25 23:40:33 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get(entity_ids[0])
|
2017-10-21 19:59:05 +00:00
|
|
|
assert state.attributes.get('message') == '{{ message + 1 }}'
|
2016-06-25 23:40:33 +00:00
|
|
|
assert state.attributes.get('title') == '{{ title + 1 }}'
|
2017-06-11 20:54:10 +00:00
|
|
|
|
2018-09-11 09:39:30 +00:00
|
|
|
notification = notifications.get(entity_ids[0])
|
|
|
|
assert notification['message'] == '{{ message + 1 }}'
|
|
|
|
assert notification['title'] == '{{ title + 1 }}'
|
|
|
|
notifications.clear()
|
|
|
|
|
2017-06-11 20:54:10 +00:00
|
|
|
def test_dismiss_notification(self):
|
|
|
|
"""Ensure removal of specific notification."""
|
2018-09-11 09:39:30 +00:00
|
|
|
notifications = self.hass.data[pn.DOMAIN]['notifications']
|
2017-06-11 20:54:10 +00:00
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 0
|
2017-06-11 20:54:10 +00:00
|
|
|
|
|
|
|
pn.create(self.hass, 'test', notification_id='Beer 2')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 1
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 1
|
2017-06-11 20:54:10 +00:00
|
|
|
pn.dismiss(self.hass, notification_id='Beer 2')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
2018-09-11 09:39:30 +00:00
|
|
|
assert len(notifications) == 0
|
|
|
|
notifications.clear()
|
|
|
|
|
|
|
|
def test_mark_read(self):
|
|
|
|
"""Ensure notification is marked as Read."""
|
|
|
|
notifications = self.hass.data[pn.DOMAIN]['notifications']
|
|
|
|
assert len(notifications) == 0
|
|
|
|
|
|
|
|
pn.create(self.hass, 'test', notification_id='Beer 2')
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
|
|
|
entity_id = 'persistent_notification.beer_2'
|
|
|
|
assert len(notifications) == 1
|
|
|
|
notification = notifications.get(entity_id)
|
|
|
|
assert notification['status'] == pn.STATUS_UNREAD
|
|
|
|
|
|
|
|
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
|
|
|
|
notifications.clear()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_ws_get_notifications(hass, hass_ws_client):
|
|
|
|
"""Test websocket 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/get'
|
|
|
|
})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
assert msg['id'] == 5
|
2018-10-01 09:21:00 +00:00
|
|
|
assert msg['type'] == TYPE_RESULT
|
2018-09-11 09:39:30 +00:00
|
|
|
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'
|
|
|
|
})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
assert msg['id'] == 6
|
2018-10-01 09:21:00 +00:00
|
|
|
assert msg['type'] == TYPE_RESULT
|
2018-09-11 09:39:30 +00:00
|
|
|
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
|
2018-10-04 08:24:14 +00:00
|
|
|
assert notification['created_at'] is not None
|
2018-09-11 09:39:30 +00:00
|
|
|
|
|
|
|
# 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'
|
|
|
|
})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
notifications = msg['result']
|
|
|
|
assert len(notifications) == 1
|
|
|
|
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'
|
|
|
|
})
|
|
|
|
msg = await client.receive_json()
|
|
|
|
notifications = msg['result']
|
|
|
|
assert len(notifications) == 0
|