Fix Notify Group payload data mis-merge (#90253)

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Erik J. Olson 2023-03-28 07:56:10 -05:00 committed by GitHub
parent 8e7013b079
commit d228df6d81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 16 deletions

View file

@ -32,18 +32,16 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
)
def update(input_dict: dict[str, Any], update_source: dict[str, Any]) -> dict[str, Any]:
"""Deep update a dictionary.
Async friendly.
"""
for key, val in update_source.items():
def add_defaults(
input_data: dict[str, Any], default_data: dict[str, Any]
) -> dict[str, Any]:
"""Deep update a dictionary with default values."""
for key, val in default_data.items():
if isinstance(val, Mapping):
recurse = update(input_dict.get(key, {}), val) # type: ignore[arg-type]
input_dict[key] = recurse
else:
input_dict[key] = update_source[key]
return input_dict
input_data[key] = add_defaults(input_data.get(key, {}), val) # type: ignore[arg-type]
elif key not in input_data:
input_data[key] = val
return input_data
async def async_get_service(
@ -71,8 +69,8 @@ class GroupNotifyPlatform(BaseNotificationService):
tasks: list[asyncio.Task[bool | None]] = []
for entity in self.entities:
sending_payload = deepcopy(payload.copy())
if (data := entity.get(ATTR_DATA)) is not None:
update(sending_payload, data)
if (default_data := entity.get(ATTR_DATA)) is not None:
add_defaults(sending_payload, default_data)
tasks.append(
asyncio.create_task(
self.hass.services.async_call(

View file

@ -54,14 +54,14 @@ async def test_send_message_with_data(hass: HomeAssistant) -> None:
"service": "demo2",
"data": {
"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(
"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] == {
"target": ["unnamed device"],
"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"},
}