Fix MQTT debug info for removed triggers (#50859)

This commit is contained in:
Erik Montnemery 2021-05-20 09:27:38 +02:00 committed by GitHub
parent 623baa7964
commit d4d335fb9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 6 deletions

View file

@ -159,7 +159,7 @@ async def info_for_device(hass, device_id):
)
for trigger in mqtt_debug_info["triggers"].values():
if trigger["device_id"] != device_id:
if trigger["device_id"] != device_id or trigger["discovery_data"] is None:
continue
discovery_data = {

View file

@ -1164,7 +1164,7 @@ async def test_trigger_debug_info(hass, mqtt_mock):
"""
registry = dr.async_get(hass)
config = {
config1 = {
"platform": "mqtt",
"automation_type": "trigger",
"topic": "test-topic",
@ -1178,8 +1178,20 @@ async def test_trigger_debug_info(hass, mqtt_mock):
"sw_version": "0.1-beta",
},
}
data = json.dumps(config)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data)
config2 = {
"platform": "mqtt",
"automation_type": "trigger",
"topic": "test-topic2",
"type": "foo",
"subtype": "bar",
"device": {
"connections": [[dr.CONNECTION_NETWORK_MAC, "02:5b:26:a8:dc:12"]],
},
}
data = json.dumps(config1)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data)
data = json.dumps(config2)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data)
await hass.async_block_till_done()
device = registry.async_get_device(
@ -1187,11 +1199,33 @@ async def test_trigger_debug_info(hass, mqtt_mock):
)
assert device is not None
debug_info_data = await debug_info.info_for_device(hass, device.id)
assert len(debug_info_data["entities"]) == 0
assert len(debug_info_data["triggers"]) == 2
topic_map = {
"homeassistant/device_automation/bla1/config": config1,
"homeassistant/device_automation/bla2/config": config2,
}
assert (
topic_map[debug_info_data["triggers"][0]["discovery_data"]["topic"]]
!= topic_map[debug_info_data["triggers"][1]["discovery_data"]["topic"]]
)
assert (
debug_info_data["triggers"][0]["discovery_data"]["payload"]
== topic_map[debug_info_data["triggers"][0]["discovery_data"]["topic"]]
)
assert (
debug_info_data["triggers"][1]["discovery_data"]["payload"]
== topic_map[debug_info_data["triggers"][1]["discovery_data"]["topic"]]
)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
await hass.async_block_till_done()
debug_info_data = await debug_info.info_for_device(hass, device.id)
assert len(debug_info_data["entities"]) == 0
assert len(debug_info_data["triggers"]) == 1
assert (
debug_info_data["triggers"][0]["discovery_data"]["topic"]
== "homeassistant/device_automation/bla/config"
== "homeassistant/device_automation/bla2/config"
)
assert debug_info_data["triggers"][0]["discovery_data"]["payload"] == config
assert debug_info_data["triggers"][0]["discovery_data"]["payload"] == config2