Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions homeassistant/components/group/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
SERVICE_SEND_MESSAGE,
BaseNotificationService,
NotifyEntity,
NotifyEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
CONF_ACTION,
CONF_ENTITIES,
CONF_SERVICE,
Expand Down Expand Up @@ -173,14 +175,23 @@ def __init__(

async def async_send_message(self, message: str, title: str | None = None) -> None:
"""Send a message to all members of the group."""

data = {
ATTR_MESSAGE: message,
ATTR_ENTITY_ID: self._entity_ids,
}

# add title only if supported and provided
if (
title is not None
and self._attr_supported_features & NotifyEntityFeature.TITLE
):
data[ATTR_TITLE] = title

await self.hass.services.async_call(
NOTIFY_DOMAIN,
SERVICE_SEND_MESSAGE,
{
ATTR_MESSAGE: message,
ATTR_TITLE: title,
ATTR_ENTITY_ID: self._entity_ids,
},
data,
blocking=True,
context=self._context,
)
Expand All @@ -194,3 +205,15 @@ def async_update_group_state(self) -> None:
for entity_id in self._entity_ids
if (state := self.hass.states.get(entity_id)) is not None
)

# Support title if all members support it
self._attr_supported_features |= NotifyEntityFeature.TITLE
for entity_id in self._entity_ids:
state = self.hass.states.get(entity_id)
if (
state is None
or not state.attributes.get(ATTR_SUPPORTED_FEATURES, 0)
& NotifyEntityFeature.TITLE
):
self._attr_supported_features &= ~NotifyEntityFeature.TITLE
break
30 changes: 30 additions & 0 deletions tests/components/group/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DOMAIN as NOTIFY_DOMAIN,
SERVICE_SEND_MESSAGE,
NotifyEntity,
NotifyEntityFeature,
)
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import (
Expand Down Expand Up @@ -166,6 +167,34 @@ async def test_send_message_with_data(hass: HomeAssistant, tmp_path: Path) -> No
)
send_message_mock.reset_mock()

# Test sending a message to a notify group which does not have title.
await hass.services.async_call(
"notify",
"my_notification_group",
{"message": "Hello", "data": {"hello": "world"}},
blocking=True,
)
send_message_mock.assert_has_calls(
[
call(
"Hello",
{
"target": [1],
"data": {"hello": "world"},
},
),
call(
"Hello",
{
"target": [2],
"data": {"hello": "world", "test": "message", "default": "default"},
},
),
],
any_order=True,
)
send_message_mock.reset_mock()

# Test sending a message which overrides service defaults to a notify group
await hass.services.async_call(
"notify",
Expand Down Expand Up @@ -301,6 +330,7 @@ class MockNotifyEntity(MockEntity, NotifyEntity):
def __init__(self, **values: Any) -> None:
"""Initialize the mock entity."""
super().__init__(**values)
self._attr_supported_features = NotifyEntityFeature.TITLE
self.send_message_mock_calls = MagicMock()

async def async_send_message(self, message: str, title: str | None = None) -> None:
Expand Down
Loading