1 """Group platform for notify component."""
3 from __future__
import annotations
6 from collections.abc
import Mapping
7 from copy
import deepcopy
10 import voluptuous
as vol
16 DOMAIN
as NOTIFY_DOMAIN,
17 PLATFORM_SCHEMA
as NOTIFY_PLATFORM_SCHEMA,
19 BaseNotificationService,
36 from .entity
import GroupEntity
38 CONF_SERVICES =
"services"
42 """Backward compatibility for notify service schemas."""
44 if not isinstance(value, dict):
48 if CONF_SERVICE
in value:
49 if CONF_ACTION
in value:
51 "Cannot specify both 'service' and 'action'. Please use 'action' only."
53 value[CONF_ACTION] = value.pop(CONF_SERVICE)
58 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
60 vol.Required(CONF_SERVICES): vol.All(
64 _backward_compat_schema,
66 vol.Required(CONF_ACTION): cv.slug,
67 vol.Optional(ATTR_DATA): dict,
77 input_data: dict[str, Any], default_data: Mapping[str, Any]
79 """Deep update a dictionary with default values."""
80 for key, val
in default_data.items():
81 if isinstance(val, Mapping):
82 input_data[key] =
add_defaults(input_data.get(key, {}), val)
83 elif key
not in input_data:
91 discovery_info: DiscoveryInfoType |
None =
None,
92 ) -> GroupNotifyPlatform:
93 """Get the Group notification service."""
98 """Implement the notification service for the group notify platform."""
100 def __init__(self, hass: HomeAssistant, entities: list[dict[str, Any]]) ->
None:
101 """Initialize the service."""
106 """Send message to all entities in the group."""
107 payload: dict[str, Any] = {ATTR_MESSAGE: message}
108 payload.update({key: val
for key, val
in kwargs.items()
if val})
110 tasks: list[asyncio.Task[Any]] = []
111 for entity
in self.
entitiesentities:
112 sending_payload = deepcopy(payload.copy())
113 if (default_data := entity.get(ATTR_DATA))
is not None:
117 self.
hasshass.services.async_call(
127 await asyncio.wait(tasks)
132 config_entry: ConfigEntry,
133 async_add_entities: AddEntitiesCallback,
135 """Initialize Notify Group config entry."""
136 registry = er.async_get(hass)
137 entities = er.async_validate_entity_ids(
138 registry, config_entry.options[CONF_ENTITIES]
142 [
NotifyGroup(config_entry.entry_id, config_entry.title, entities)]
148 hass: HomeAssistant, name: str, validated_config: dict[str, Any]
150 """Create a preview notify group."""
154 validated_config[CONF_ENTITIES],
159 """Representation of a NotifyGroup."""
161 _attr_available: bool =
False
165 unique_id: str |
None,
167 entity_ids: list[str],
169 """Initialize a NotifyGroup."""
176 """Send a message to all members of the group."""
177 await self.
hasshass.services.async_call(
179 SERVICE_SEND_MESSAGE,
181 ATTR_MESSAGE: message,
191 """Query all members and determine the notify group state."""
194 state.state != STATE_UNAVAILABLE
196 if (state := self.
hasshass.states.get(entity_id))
is not None
None async_send_message(self, str message, str|None title=None)
_attr_extra_state_attributes
None __init__(self, str|None unique_id, str name, list[str] entity_ids)
None async_update_group_state(self)
GroupNotifyPlatform async_get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
dict[str, Any] add_defaults(dict[str, Any] input_data, Mapping[str, Any] default_data)
NotifyGroup async_create_preview_notify(HomeAssistant hass, str name, dict[str, Any] validated_config)
Any _backward_compat_schema(Any|None value)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)