Skip to content

Commit

Permalink
Fix KNX device trigger passing info data (#95076)
Browse files Browse the repository at this point in the history
  • Loading branch information
farmio committed Jun 22, 2023
1 parent 66b2214 commit 733bca8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/knx/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ async def async_attach_trigger(
trigger_info: TriggerInfo,
) -> CALLBACK_TYPE:
"""Attach a trigger."""
trigger_data = trigger_info["trigger_data"]
dst_addresses: list[str] = config.get(EXTRA_FIELD_DESTINATION, [])
job = HassJob(action, f"KNX device trigger {trigger_info}")
knx: KNXModule = hass.data[DOMAIN]
Expand All @@ -95,7 +96,7 @@ def async_call_trigger_action(telegram: TelegramDict) -> None:
return
hass.async_run_hass_job(
job,
{"trigger": telegram},
{"trigger": {**trigger_data, **telegram}},
)

return knx.telegrams.async_listen_telegram(
Expand Down
20 changes: 15 additions & 5 deletions tests/components/knx/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ async def test_if_fires_on_telegram(
identifiers={(DOMAIN, f"_{knx.mock_config_entry.entry_id}_interface")}
)

# "id" field added to action to test if `trigger_data` passed correctly in `async_attach_trigger`
assert await async_setup_component(
hass,
automation.DOMAIN,
Expand All @@ -71,7 +72,8 @@ async def test_if_fires_on_telegram(
"action": {
"service": "test.automation",
"data_template": {
"catch_all": ("telegram - {{ trigger.destination }}")
"catch_all": ("telegram - {{ trigger.destination }}"),
"id": (" {{ trigger.id }}"),
},
},
},
Expand All @@ -82,11 +84,13 @@ async def test_if_fires_on_telegram(
"device_id": device_entry.id,
"type": "telegram",
"destination": ["1/2/3", "1/2/4"],
"id": "test-id",
},
"action": {
"service": "test.automation",
"data_template": {
"specific": ("telegram - {{ trigger.destination }}")
"specific": ("telegram - {{ trigger.destination }}"),
"id": (" {{ trigger.id }}"),
},
},
},
Expand All @@ -96,12 +100,18 @@ async def test_if_fires_on_telegram(

await knx.receive_write("0/0/1", (0x03, 0x2F))
assert len(calls) == 1
assert calls.pop().data["catch_all"] == "telegram - 0/0/1"
test_call = calls.pop()
assert test_call.data["catch_all"] == "telegram - 0/0/1"
assert test_call.data["id"] == 0

await knx.receive_write("1/2/4", (0x03, 0x2F))
assert len(calls) == 2
assert calls.pop().data["specific"] == "telegram - 1/2/4"
assert calls.pop().data["catch_all"] == "telegram - 1/2/4"
test_call = calls.pop()
assert test_call.data["specific"] == "telegram - 1/2/4"
assert test_call.data["id"] == "test-id"
test_call = calls.pop()
assert test_call.data["catch_all"] == "telegram - 1/2/4"
assert test_call.data["id"] == 0


async def test_remove_device_trigger(
Expand Down

0 comments on commit 733bca8

Please sign in to comment.