From de0f6b781e275516176f35d6569f42a16055ade8 Mon Sep 17 00:00:00 2001 From: tedstriker Date: Sun, 11 Jun 2017 22:54:10 +0200 Subject: [PATCH] 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 --- .../components/persistent_notification.py | 31 +++++++++++++++++++ homeassistant/components/services.yaml | 8 +++++ .../test_persistent_notification.py | 13 ++++++++ 3 files changed, 52 insertions(+) diff --git a/homeassistant/components/persistent_notification.py b/homeassistant/components/persistent_notification.py index 5e36c471562..212b2e7e7da 100644 --- a/homeassistant/components/persistent_notification.py +++ b/homeassistant/components/persistent_notification.py @@ -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 diff --git a/homeassistant/components/services.yaml b/homeassistant/components/services.yaml index 0807eb617ee..8c211a190c2 100644 --- a/homeassistant/components/services.yaml +++ b/homeassistant/components/services.yaml @@ -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: diff --git a/tests/components/test_persistent_notification.py b/tests/components/test_persistent_notification.py index 55c78676858..75caae0015c 100644 --- a/tests/components/test_persistent_notification.py +++ b/tests/components/test_persistent_notification.py @@ -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