Expose notify platform targets as individual services (#2837)
* First pass on providing individual services for all possible targets that a notification platform supports. * Add a quite hacky first version of notification groups * Add a docstring for get_targets * Register group service under notifygroup/ and safely check for notifygroups in config * Remove notifygroups, because it belongs in its own PR * Make @balloob requested changes * get_targets()->targets * Add tests for notify targets exposed as individual services * If we dont have a platform name set in configuration, lets use the name of the platform instead of notify * Fix test docstring. * Dont use a dictionary for just one value * No need to double slugify * targets is now just a list of strings instead of a dict
This commit is contained in:
1 parent
37561765ff
commit
c1ce6855c5
4 files changed
+64
-3
No files matched your search
@@ -64,6 +64,7 @@ def send_message(hass, message, title=None, data=None):
|
||||
hass.services.call(DOMAIN, SERVICE_NOTIFY, info)
|
||||
|
||||
|
||||
# pylint: disable=too-many-locals
|
||||
def setup(hass, config):
|
||||
"""Setup the notify services."""
|
||||
success = False
|
||||
@@ -71,6 +72,8 @@ def setup(hass, config):
|
||||
descriptions = load_yaml_config_file(
|
||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||
|
||||
targets = {}
|
||||
|
||||
for platform, p_config in config_per_platform(config, DOMAIN):
|
||||
notify_implementation = bootstrap.prepare_setup_platform(
|
||||
hass, config, DOMAIN, platform)
|
||||
@@ -92,7 +95,10 @@ def setup(hass, config):
|
||||
|
||||
title = template.render(
|
||||
hass, call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT))
|
||||
target = call.data.get(ATTR_TARGET)
|
||||
if targets.get(call.service) is not None:
|
||||
target = targets[call.service]
|
||||
else:
|
||||
target = call.data.get(ATTR_TARGET)
|
||||
message = template.render(hass, message)
|
||||
data = call.data.get(ATTR_DATA)
|
||||
|
||||
@@ -100,8 +106,22 @@ def setup(hass, config):
|
||||
data=data)
|
||||
|
||||
service_call_handler = partial(notify_message, notify_service)
|
||||
service_name = slugify(p_config.get(CONF_NAME) or SERVICE_NOTIFY)
|
||||
hass.services.register(DOMAIN, service_name, service_call_handler,
|
||||
|
||||
if hasattr(notify_service, 'targets'):
|
||||
platform_name = (p_config.get(CONF_NAME) or platform)
|
||||
for target in notify_service.targets:
|
||||
target_name = slugify("{}_{}".format(platform_name, target))
|
||||
targets[target_name] = target
|
||||
hass.services.register(DOMAIN, target_name,
|
||||
service_call_handler,
|
||||
descriptions.get(SERVICE_NOTIFY),
|
||||
schema=NOTIFY_SERVICE_SCHEMA)
|
||||
|
||||
platform_name = (p_config.get(CONF_NAME) or SERVICE_NOTIFY)
|
||||
platform_name_slug = slugify(platform_name)
|
||||
|
||||
hass.services.register(DOMAIN, platform_name_slug,
|
||||
service_call_handler,
|
||||
descriptions.get(SERVICE_NOTIFY),
|
||||
schema=NOTIFY_SERVICE_SCHEMA)
|
||||
success = True
|
||||
|
||||
Reference in new issue
Block a user