From 159411df8bfe6d688e304ee8c63638a4f84f6768 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 18 Aug 2015 22:28:40 -0400 Subject: [PATCH 1/3] Updated notifier component. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This update to the notifier component allows multiple notifiers to be configured. In order to support this, an optional property “name” has been added to all the notifier’s configurations. The notifier can now be called with the service “notify.NAME”. If the name is not provided, the service will be mapped to “notify.notify”. Because of this, this update should be fully backwards compatible. --- homeassistant/components/notify/__init__.py | 61 +++++++++++---------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index ecd15aeb8e2..eaac1a4d082 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -4,12 +4,13 @@ homeassistant.components.notify Provides functionality to notify people. """ +from functools import partial import logging from homeassistant.loader import get_component -from homeassistant.helpers import validate_config +from homeassistant.helpers import config_per_platform -from homeassistant.const import CONF_PLATFORM +from homeassistant.const import CONF_PLATFORM, CONF_NAME DOMAIN = "notify" DEPENDENCIES = [] @@ -33,42 +34,46 @@ def send_message(hass, message): def setup(hass, config): """ Sets up notify services. """ + success = False - if not validate_config(config, {DOMAIN: [CONF_PLATFORM]}, _LOGGER): - return False + for platform, p_config in config_per_platform(config, DOMAIN, _LOGGER): + # create platform + platform = p_config[CONF_PLATFORM] + notify_implementation = get_component( + 'notify.{}'.format(platform)) - platform = config[DOMAIN].get(CONF_PLATFORM) + if notify_implementation is None: + _LOGGER.error("Unknown notification service specified.") + continue - notify_implementation = get_component( - 'notify.{}'.format(platform)) + # create platform service + notify_service = notify_implementation.get_service( + hass, {DOMAIN: p_config}) - if notify_implementation is None: - _LOGGER.error("Unknown notification service specified.") + if notify_service is None: + _LOGGER.error("Failed to initialize notification service %s", + platform) + continue - return False + # create service handler + def notify_message(notify_service, call): + """ Handle sending notification message service calls. """ + message = call.data.get(ATTR_MESSAGE) - notify_service = notify_implementation.get_service(hass, config) + if message is None: + return - if notify_service is None: - _LOGGER.error("Failed to initialize notification service %s", - platform) + title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) - return False + notify_service.send_message(message, title=title) - def notify_message(call): - """ Handle sending notification message service calls. """ - message = call.data.get(ATTR_MESSAGE) + # register service + service_call_handler = partial(notify_message, notify_service) + service_notify = p_config.get(CONF_NAME, SERVICE_NOTIFY) + hass.services.register(DOMAIN, service_notify, service_call_handler) + success = True - if message is None: - return - - title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) - - notify_service.send_message(message, title=title) - - hass.services.register(DOMAIN, SERVICE_NOTIFY, notify_message) - - return True + return success # pylint: disable=too-few-public-methods From b8b5ac0653577e8df3db67ab7796c443938e2e5f Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 18 Aug 2015 22:42:01 -0400 Subject: [PATCH 2/3] Removed unnecessary line from notify component. --- homeassistant/components/notify/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index eaac1a4d082..fedd0b3412c 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -37,8 +37,7 @@ def setup(hass, config): success = False for platform, p_config in config_per_platform(config, DOMAIN, _LOGGER): - # create platform - platform = p_config[CONF_PLATFORM] + # get platform notify_implementation = get_component( 'notify.{}'.format(platform)) From a23ab44a6dd9fd0f2bd6325cac2aabae47e2f992 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Tue, 18 Aug 2015 22:49:27 -0400 Subject: [PATCH 3/3] Removed unnecessary import in notify component. --- homeassistant/components/notify/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index fedd0b3412c..ee53159d5e6 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -10,7 +10,7 @@ import logging from homeassistant.loader import get_component from homeassistant.helpers import config_per_platform -from homeassistant.const import CONF_PLATFORM, CONF_NAME +from homeassistant.const import CONF_NAME DOMAIN = "notify" DEPENDENCIES = []