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 removed
This commit is contained in:
parent
314bce1073
commit
de0f6b781e
3 changed files with 52 additions and 0 deletions
|
@ -26,6 +26,7 @@ DOMAIN = 'persistent_notification'
|
||||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||||
|
|
||||||
SERVICE_CREATE = 'create'
|
SERVICE_CREATE = 'create'
|
||||||
|
SERVICE_DISMISS = 'dismiss'
|
||||||
|
|
||||||
SCHEMA_SERVICE_CREATE = vol.Schema({
|
SCHEMA_SERVICE_CREATE = vol.Schema({
|
||||||
vol.Required(ATTR_MESSAGE): cv.template,
|
vol.Required(ATTR_MESSAGE): cv.template,
|
||||||
|
@ -33,6 +34,10 @@ SCHEMA_SERVICE_CREATE = vol.Schema({
|
||||||
vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
|
vol.Optional(ATTR_NOTIFICATION_ID): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
SCHEMA_SERVICE_DISMISS = vol.Schema({
|
||||||
|
vol.Required(ATTR_NOTIFICATION_ID): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_OBJECT_ID = 'notification'
|
DEFAULT_OBJECT_ID = 'notification'
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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)
|
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
|
@callback
|
||||||
def async_create(hass, message, title=None, notification_id=None):
|
def async_create(hass, message, title=None, notification_id=None):
|
||||||
"""Generate a notification."""
|
"""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))
|
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
|
@asyncio.coroutine
|
||||||
def async_setup(hass, config):
|
def async_setup(hass, config):
|
||||||
"""Set up the persistent notification component."""
|
"""Set up the persistent notification component."""
|
||||||
|
@ -92,12 +110,25 @@ def async_setup(hass, config):
|
||||||
|
|
||||||
hass.states.async_set(entity_id, message, attr)
|
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(
|
descriptions = yield from hass.async_add_job(
|
||||||
load_yaml_config_file, os.path.join(
|
load_yaml_config_file, os.path.join(
|
||||||
os.path.dirname(__file__), 'services.yaml')
|
os.path.dirname(__file__), 'services.yaml')
|
||||||
)
|
)
|
||||||
|
|
||||||
hass.services.async_register(DOMAIN, SERVICE_CREATE, create_service,
|
hass.services.async_register(DOMAIN, SERVICE_CREATE, create_service,
|
||||||
descriptions[DOMAIN][SERVICE_CREATE],
|
descriptions[DOMAIN][SERVICE_CREATE],
|
||||||
SCHEMA_SERVICE_CREATE)
|
SCHEMA_SERVICE_CREATE)
|
||||||
|
|
||||||
|
hass.services.async_register(DOMAIN, SERVICE_DISMISS, dismiss_service,
|
||||||
|
descriptions[DOMAIN][SERVICE_DISMISS],
|
||||||
|
SCHEMA_SERVICE_DISMISS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -73,6 +73,14 @@ persistent_notification:
|
||||||
description: Target ID of the notification, will replace a notification with the same Id. [Optional]
|
description: Target ID of the notification, will replace a notification with the same Id. [Optional]
|
||||||
example: 1234
|
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:
|
homematic:
|
||||||
virtualkey:
|
virtualkey:
|
||||||
description: Press a virtual key from CCU/Homegear or simulate keypress
|
description: Press a virtual key from CCU/Homegear or simulate keypress
|
||||||
|
|
|
@ -64,3 +64,16 @@ class TestPersistentNotification:
|
||||||
state = self.hass.states.get(entity_ids[0])
|
state = self.hass.states.get(entity_ids[0])
|
||||||
assert state.state == '{{ message + 1 }}'
|
assert state.state == '{{ message + 1 }}'
|
||||||
assert state.attributes.get('title') == '{{ title + 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…
Add table
Add a link
Reference in a new issue