2019-04-03 17:40:03 +02:00
|
|
|
"""Group platform for notify component."""
|
2022-09-14 11:36:28 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-01-18 07:08:03 +01:00
|
|
|
import asyncio
|
2022-09-14 11:36:28 +02:00
|
|
|
from collections.abc import Coroutine, Mapping
|
2016-11-03 22:56:55 -07:00
|
|
|
from copy import deepcopy
|
2022-09-14 11:36:28 +02:00
|
|
|
from typing import Any
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2016-08-16 22:14:04 -07:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-27 20:36:13 -07:00
|
|
|
from homeassistant.components.notify import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_MESSAGE,
|
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2019-12-05 13:44:11 +01:00
|
|
|
from homeassistant.const import ATTR_SERVICE
|
2022-09-14 11:36:28 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-05 13:44:11 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-09-14 11:36:28 +02:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-09-29 20:07:49 +03:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_SERVICES = "services"
|
2016-08-16 22:14:04 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
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}],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2016-08-16 22:14:04 -07:00
|
|
|
|
|
|
|
|
2022-09-14 11:36:28 +02:00
|
|
|
def update(input_dict: dict[str, Any], update_source: dict[str, Any]) -> dict[str, Any]:
|
2017-01-18 07:08:03 +01:00
|
|
|
"""Deep update a dictionary.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2016-08-16 22:14:04 -07:00
|
|
|
for key, val in update_source.items():
|
2018-07-25 12:35:22 +03:00
|
|
|
if isinstance(val, Mapping):
|
2022-09-14 11:36:28 +02:00
|
|
|
recurse = update(input_dict.get(key, {}), val) # type: ignore[arg-type]
|
2016-08-16 22:14:04 -07:00
|
|
|
input_dict[key] = recurse
|
|
|
|
else:
|
|
|
|
input_dict[key] = update_source[key]
|
|
|
|
return input_dict
|
|
|
|
|
|
|
|
|
2022-09-14 11:36:28 +02:00
|
|
|
async def async_get_service(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> GroupNotifyPlatform:
|
2016-08-16 22:14:04 -07:00
|
|
|
"""Get the Group notification service."""
|
2022-09-14 11:36:28 +02:00
|
|
|
return GroupNotifyPlatform(hass, config[CONF_SERVICES])
|
2016-08-16 22:14:04 -07:00
|
|
|
|
|
|
|
|
|
|
|
class GroupNotifyPlatform(BaseNotificationService):
|
2017-01-15 03:53:14 +01:00
|
|
|
"""Implement the notification service for the group notify platform."""
|
2016-08-16 22:14:04 -07:00
|
|
|
|
2022-09-14 11:36:28 +02:00
|
|
|
def __init__(self, hass: HomeAssistant, entities: list[dict[str, Any]]) -> None:
|
2016-08-16 22:14:04 -07:00
|
|
|
"""Initialize the service."""
|
|
|
|
self.hass = hass
|
|
|
|
self.entities = entities
|
|
|
|
|
2022-09-14 11:36:28 +02:00
|
|
|
async def async_send_message(self, message: str = "", **kwargs: Any) -> None:
|
2016-08-16 22:14:04 -07:00
|
|
|
"""Send message to all entities in the group."""
|
2022-09-14 11:36:28 +02:00
|
|
|
payload: dict[str, Any] = {ATTR_MESSAGE: message}
|
2016-08-16 22:14:04 -07:00
|
|
|
payload.update({key: val for key, val in kwargs.items() if val})
|
|
|
|
|
2022-09-14 11:36:28 +02:00
|
|
|
tasks: list[Coroutine[Any, Any, bool | None]] = []
|
2016-08-16 22:14:04 -07:00
|
|
|
for entity in self.entities:
|
2016-11-03 22:56:55 -07:00
|
|
|
sending_payload = deepcopy(payload.copy())
|
2022-09-14 11:36:28 +02:00
|
|
|
if (data := entity.get(ATTR_DATA)) is not None:
|
|
|
|
update(sending_payload, data)
|
2019-07-31 12:25:30 -07:00
|
|
|
tasks.append(
|
|
|
|
self.hass.services.async_call(
|
2022-09-14 11:36:28 +02:00
|
|
|
DOMAIN, entity[ATTR_SERVICE], sending_payload
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2017-01-18 07:08:03 +01:00
|
|
|
|
|
|
|
if tasks:
|
2019-05-22 21:09:59 -07:00
|
|
|
await asyncio.wait(tasks)
|