mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Fix Notify Group payload data mis-merge (#90253)
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
8e7013b079
commit
d228df6d81
@ -32,18 +32,16 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def update(input_dict: dict[str, Any], update_source: dict[str, Any]) -> dict[str, Any]:
|
def add_defaults(
|
||||||
"""Deep update a dictionary.
|
input_data: dict[str, Any], default_data: dict[str, Any]
|
||||||
|
) -> dict[str, Any]:
|
||||||
Async friendly.
|
"""Deep update a dictionary with default values."""
|
||||||
"""
|
for key, val in default_data.items():
|
||||||
for key, val in update_source.items():
|
|
||||||
if isinstance(val, Mapping):
|
if isinstance(val, Mapping):
|
||||||
recurse = update(input_dict.get(key, {}), val) # type: ignore[arg-type]
|
input_data[key] = add_defaults(input_data.get(key, {}), val) # type: ignore[arg-type]
|
||||||
input_dict[key] = recurse
|
elif key not in input_data:
|
||||||
else:
|
input_data[key] = val
|
||||||
input_dict[key] = update_source[key]
|
return input_data
|
||||||
return input_dict
|
|
||||||
|
|
||||||
|
|
||||||
async def async_get_service(
|
async def async_get_service(
|
||||||
@ -71,8 +69,8 @@ class GroupNotifyPlatform(BaseNotificationService):
|
|||||||
tasks: list[asyncio.Task[bool | None]] = []
|
tasks: list[asyncio.Task[bool | None]] = []
|
||||||
for entity in self.entities:
|
for entity in self.entities:
|
||||||
sending_payload = deepcopy(payload.copy())
|
sending_payload = deepcopy(payload.copy())
|
||||||
if (data := entity.get(ATTR_DATA)) is not None:
|
if (default_data := entity.get(ATTR_DATA)) is not None:
|
||||||
update(sending_payload, data)
|
add_defaults(sending_payload, default_data)
|
||||||
tasks.append(
|
tasks.append(
|
||||||
asyncio.create_task(
|
asyncio.create_task(
|
||||||
self.hass.services.async_call(
|
self.hass.services.async_call(
|
||||||
|
@ -54,14 +54,14 @@ async def test_send_message_with_data(hass: HomeAssistant) -> None:
|
|||||||
"service": "demo2",
|
"service": "demo2",
|
||||||
"data": {
|
"data": {
|
||||||
"target": "unnamed device",
|
"target": "unnamed device",
|
||||||
"data": {"test": "message"},
|
"data": {"test": "message", "default": "default"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
"""Test sending a message with to a notify group."""
|
"""Test sending a message to a notify group."""
|
||||||
await service.async_send_message(
|
await service.async_send_message(
|
||||||
"Hello", title="Test notification", data={"hello": "world"}
|
"Hello", title="Test notification", data={"hello": "world"}
|
||||||
)
|
)
|
||||||
@ -77,7 +77,28 @@ async def test_send_message_with_data(hass: HomeAssistant) -> None:
|
|||||||
assert service2.send_message.mock_calls[0][2] == {
|
assert service2.send_message.mock_calls[0][2] == {
|
||||||
"target": ["unnamed device"],
|
"target": ["unnamed device"],
|
||||||
"title": "Test notification",
|
"title": "Test notification",
|
||||||
"data": {"hello": "world", "test": "message"},
|
"data": {"hello": "world", "test": "message", "default": "default"},
|
||||||
|
}
|
||||||
|
|
||||||
|
"""Test sending a message which overrides service defaults to a notify group."""
|
||||||
|
await service.async_send_message(
|
||||||
|
"Hello",
|
||||||
|
title="Test notification",
|
||||||
|
data={"hello": "world", "default": "override"},
|
||||||
|
)
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert service1.send_message.mock_calls[1][1][0] == "Hello"
|
||||||
|
assert service1.send_message.mock_calls[1][2] == {
|
||||||
|
"title": "Test notification",
|
||||||
|
"data": {"hello": "world", "default": "override"},
|
||||||
|
}
|
||||||
|
assert service2.send_message.mock_calls[1][1][0] == "Hello"
|
||||||
|
assert service2.send_message.mock_calls[1][2] == {
|
||||||
|
"target": ["unnamed device"],
|
||||||
|
"title": "Test notification",
|
||||||
|
"data": {"hello": "world", "test": "message", "default": "override"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user