hass-core/homeassistant/components/notify/group.py
Martin Hjelmare 9db1aa7629 Add discovery notify support and mysensors notify (#5219)
* Add mysensors notify platform

* Make add_devices optional in platform callback function.
* Use new argument structure for all existing mysensors platforms.
* Add notify platform.
* Update mysensors gateway.

* Refactor notify setup

* Enable discovery of notify platforms.
* Update and add tests for notify component and some platforms.
* Continue setup of notify platforms if a platform fails setup.
* Remove notify tests that check platform config. These tests are not
  needed when config validation is used.
* Add config validation to APNS notify platform.
* Use discovery to set up mysensors notify platform.

* Add discovery_info to get_service and update tests

* Add discovery_info as keyword argument to the get_service function
  signature and update all notify platforms.
* Update existing notify tests to check config validation using test
  helper.
* Add removed tests back in that checked config in apns, command_line
  and file platforms, but use config validation test helper to verify
  config.
* Add a test for notify file to increase coverage.
* Fix some PEP issues.

* Fix comments and use more constants

* Move apns notify service under notify domain
2017-01-15 03:53:14 +01:00

64 lines
2.1 KiB
Python

"""
Group platform for notify component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.group/
"""
import collections
from copy import deepcopy
import logging
import voluptuous as vol
from homeassistant.const import ATTR_SERVICE
from homeassistant.components.notify import (DOMAIN, ATTR_MESSAGE, ATTR_DATA,
PLATFORM_SCHEMA,
BaseNotificationService)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
CONF_SERVICES = "services"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SERVICES): vol.All(cv.ensure_list, [{
vol.Required(ATTR_SERVICE): cv.slug,
vol.Optional(ATTR_DATA): dict,
}])
})
def update(input_dict, update_source):
"""Deep update a dictionary."""
for key, val in update_source.items():
if isinstance(val, collections.Mapping):
recurse = update(input_dict.get(key, {}), val)
input_dict[key] = recurse
else:
input_dict[key] = update_source[key]
return input_dict
def get_service(hass, config, discovery_info=None):
"""Get the Group notification service."""
return GroupNotifyPlatform(hass, config.get(CONF_SERVICES))
class GroupNotifyPlatform(BaseNotificationService):
"""Implement the notification service for the group notify platform."""
def __init__(self, hass, entities):
"""Initialize the service."""
self.hass = hass
self.entities = entities
def send_message(self, message="", **kwargs):
"""Send message to all entities in the group."""
payload = {ATTR_MESSAGE: message}
payload.update({key: val for key, val in kwargs.items() if val})
for entity in self.entities:
sending_payload = deepcopy(payload.copy())
if entity.get(ATTR_DATA) is not None:
update(sending_payload, entity.get(ATTR_DATA))
self.hass.services.call(DOMAIN, entity.get(ATTR_SERVICE),
sending_payload)