dismiss service for persistent notifications (#7996)
* dismiss service for persistent notifications Unnecessary notifications can now be removed automatically. Added a dismiss service to remove persistent notifications via script and/or automation. * removed unnecessary loop loop removedpull/8004/head
parent
314bce1073
commit
de0f6b781e
|
@ -26,6 +26,7 @@ DOMAIN = 'persistent_notification'
|
|||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
|
||||
SERVICE_CREATE = 'create'
|
||||
SERVICE_DISMISS = 'dismiss'
|
||||
|
||||
SCHEMA_SERVICE_CREATE = vol.Schema({
|
||||
vol.Required(ATTR_MESSAGE): cv.template,
|
||||
|
@ -33,6 +34,10 @@ SCHEMA_SERVICE_CREATE = vol.Schema({
|
|||
vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
|
||||
})
|
||||
|
||||
SCHEMA_SERVICE_DISMISS = vol.Schema({
|
||||
vol.Required(ATTR_NOTIFICATION_ID): cv.string,
|
||||
})
|
||||
|
||||
|
||||
DEFAULT_OBJECT_ID = 'notification'
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -43,6 +48,11 @@ def create(hass, message, title=None, notification_id=None):
|
|||
hass.add_job(async_create, hass, message, title, notification_id)
|
||||
|
||||
|
||||
def dismiss(hass, notification_id):
|
||||
"""Remove a notification."""
|
||||
hass.add_job(async_dismiss, hass, notification_id)
|
||||
|
||||
|
||||
@callback
|
||||
def async_create(hass, message, title=None, notification_id=None):
|
||||
"""Generate a notification."""
|
||||
|
@ -57,6 +67,14 @@ def async_create(hass, message, title=None, notification_id=None):
|
|||
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_CREATE, data))
|
||||
|
||||
|
||||
@callback
|
||||
def async_dismiss(hass, notification_id):
|
||||
"""Remove a notification."""
|
||||
data = {ATTR_NOTIFICATION_ID: notification_id}
|
||||
|
||||
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_DISMISS, data))
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
"""Set up the persistent notification component."""
|
||||
|
@ -92,12 +110,25 @@ def async_setup(hass, config):
|
|||
|
||||
hass.states.async_set(entity_id, message, attr)
|
||||
|
||||
@callback
|
||||
def dismiss_service(call):
|
||||
"""Handle the dismiss notification service call."""
|
||||
notification_id = call.data.get(ATTR_NOTIFICATION_ID)
|
||||
entity_id = ENTITY_ID_FORMAT.format(slugify(notification_id))
|
||||
|
||||
hass.states.async_remove(entity_id)
|
||||
|
||||
descriptions = yield from hass.async_add_job(
|
||||
load_yaml_config_file, os.path.join(
|
||||
os.path.dirname(__file__), 'services.yaml')
|
||||
)
|
||||
|
||||
hass.services.async_register(DOMAIN, SERVICE_CREATE, create_service,
|
||||
descriptions[DOMAIN][SERVICE_CREATE],
|
||||
SCHEMA_SERVICE_CREATE)
|
||||
|
||||
hass.services.async_register(DOMAIN, SERVICE_DISMISS, dismiss_service,
|
||||
descriptions[DOMAIN][SERVICE_DISMISS],
|
||||
SCHEMA_SERVICE_DISMISS)
|
||||
|
||||
return True
|
||||
|
|
|
@ -72,6 +72,14 @@ persistent_notification:
|
|||
notification_id:
|
||||
description: Target ID of the notification, will replace a notification with the same Id. [Optional]
|
||||
example: 1234
|
||||
|
||||
dismiss:
|
||||
description: Remove a notification from the frontend
|
||||
|
||||
fields:
|
||||
notification_id:
|
||||
description: Target ID of the notification, which should be removed. [Required]
|
||||
example: 1234
|
||||
|
||||
homematic:
|
||||
virtualkey:
|
||||
|
|
|
@ -64,3 +64,16 @@ class TestPersistentNotification:
|
|||
state = self.hass.states.get(entity_ids[0])
|
||||
assert state.state == '{{ message + 1 }}'
|
||||
assert state.attributes.get('title') == '{{ title + 1 }}'
|
||||
|
||||
def test_dismiss_notification(self):
|
||||
"""Ensure removal of specific notification."""
|
||||
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
||||
|
||||
pn.create(self.hass, 'test', notification_id='Beer 2')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 1
|
||||
pn.dismiss(self.hass, notification_id='Beer 2')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(self.hass.states.entity_ids(pn.DOMAIN)) == 0
|
||||
|
|
Loading…
Reference in New Issue