2019-04-03 15:40:03 +00:00
|
|
|
"""Group platform for notify component."""
|
2017-01-18 06:08:03 +00:00
|
|
|
import asyncio
|
2018-07-25 09:35:22 +00:00
|
|
|
from collections.abc import Mapping
|
2016-11-04 05:56:55 +00:00
|
|
|
from copy import deepcopy
|
2016-08-17 05:14:04 +00:00
|
|
|
import logging
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-08-17 05:14:04 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-09-05 11:27:10 +00:00
|
|
|
from homeassistant.const import ATTR_SERVICE
|
2016-08-17 05:14:04 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-03-21 05:56:46 +00:00
|
|
|
ATTR_DATA, ATTR_MESSAGE, DOMAIN, PLATFORM_SCHEMA, BaseNotificationService)
|
|
|
|
|
2016-08-17 05:14:04 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
CONF_SERVICES = 'services'
|
2016-08-17 05:14:04 +00:00
|
|
|
|
2016-09-05 11:27:10 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2016-08-17 05:14:04 +00:00
|
|
|
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):
|
2017-01-18 06:08:03 +00:00
|
|
|
"""Deep update a dictionary.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2016-08-17 05:14:04 +00:00
|
|
|
for key, val in update_source.items():
|
2018-07-25 09:35:22 +00:00
|
|
|
if isinstance(val, Mapping):
|
2016-08-17 05:14:04 +00:00
|
|
|
recurse = update(input_dict.get(key, {}), val)
|
|
|
|
input_dict[key] = recurse
|
|
|
|
else:
|
|
|
|
input_dict[key] = update_source[key]
|
|
|
|
return input_dict
|
|
|
|
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_get_service(hass, config, discovery_info=None):
|
2016-08-17 05:14:04 +00:00
|
|
|
"""Get the Group notification service."""
|
|
|
|
return GroupNotifyPlatform(hass, config.get(CONF_SERVICES))
|
|
|
|
|
|
|
|
|
|
|
|
class GroupNotifyPlatform(BaseNotificationService):
|
2017-01-15 02:53:14 +00:00
|
|
|
"""Implement the notification service for the group notify platform."""
|
2016-08-17 05:14:04 +00:00
|
|
|
|
|
|
|
def __init__(self, hass, entities):
|
|
|
|
"""Initialize the service."""
|
|
|
|
self.hass = hass
|
|
|
|
self.entities = entities
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_send_message(self, message="", **kwargs):
|
2016-08-17 05:14:04 +00:00
|
|
|
"""Send message to all entities in the group."""
|
|
|
|
payload = {ATTR_MESSAGE: message}
|
|
|
|
payload.update({key: val for key, val in kwargs.items() if val})
|
|
|
|
|
2017-01-18 06:08:03 +00:00
|
|
|
tasks = []
|
2016-08-17 05:14:04 +00:00
|
|
|
for entity in self.entities:
|
2016-11-04 05:56:55 +00:00
|
|
|
sending_payload = deepcopy(payload.copy())
|
2016-08-17 05:14:04 +00:00
|
|
|
if entity.get(ATTR_DATA) is not None:
|
|
|
|
update(sending_payload, entity.get(ATTR_DATA))
|
2017-01-18 06:08:03 +00:00
|
|
|
tasks.append(self.hass.services.async_call(
|
|
|
|
DOMAIN, entity.get(ATTR_SERVICE), sending_payload))
|
|
|
|
|
|
|
|
if tasks:
|
2019-05-23 04:09:59 +00:00
|
|
|
await asyncio.wait(tasks)
|