Cleanup entity and device registry on MQTT discovery removal (#32693)
* Cleanup entity and device registry on MQTT discovery removal. * Review comments
This commit is contained in:
1 parent
5f5cb8bea8
commit
8bc542776b
17 files changed
+466
-17
No files matched your search
@@ -233,8 +233,8 @@ async def test_update_remove_triggers(hass, device_reg, entity_reg, mqtt_mock):
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert_lists_same(triggers, [])
|
||||
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set())
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_if_fires_on_mqtt_message(hass, device_reg, calls, mqtt_mock):
|
||||
@@ -833,8 +833,8 @@ async def test_entity_device_info_update(hass, mqtt_mock):
|
||||
assert device.name == "Milk"
|
||||
|
||||
|
||||
async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test discovered device is cleaned up when removed from registry."""
|
||||
async def test_cleanup_trigger(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test trigger discovery topic is cleaned when device is removed from registry."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_start(hass, "homeassistant", {}, config_entry)
|
||||
@@ -863,10 +863,212 @@ async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set())
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is None
|
||||
|
||||
# Verify retained discovery topic has been cleared
|
||||
mqtt_mock.async_publish.assert_called_once_with(
|
||||
"homeassistant/device_automation/bla/config", "", 0, True
|
||||
)
|
||||
|
||||
|
||||
async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test removal from device registry when trigger is removed."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_start(hass, "homeassistant", {}, config_entry)
|
||||
|
||||
config = {
|
||||
"automation_type": "trigger",
|
||||
"topic": "test-topic",
|
||||
"type": "foo",
|
||||
"subtype": "bar",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
}
|
||||
|
||||
data = json.dumps(config)
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is created
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert triggers[0]["type"] == "foo"
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_cleanup_device_several_triggers(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test removal from device registry when the last trigger is removed."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_start(hass, "homeassistant", {}, config_entry)
|
||||
|
||||
config1 = {
|
||||
"automation_type": "trigger",
|
||||
"topic": "test-topic",
|
||||
"type": "foo",
|
||||
"subtype": "bar",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
}
|
||||
|
||||
config2 = {
|
||||
"automation_type": "trigger",
|
||||
"topic": "test-topic",
|
||||
"type": "foo2",
|
||||
"subtype": "bar",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
}
|
||||
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is created
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 2
|
||||
assert triggers[0]["type"] == "foo"
|
||||
assert triggers[1]["type"] == "foo2"
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is not cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 1
|
||||
assert triggers[0]["type"] == "foo2"
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_cleanup_device_with_entity1(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test removal from device registry for device with entity.
|
||||
|
||||
Trigger removed first, then entity.
|
||||
"""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_start(hass, "homeassistant", {}, config_entry)
|
||||
|
||||
config1 = {
|
||||
"automation_type": "trigger",
|
||||
"topic": "test-topic",
|
||||
"type": "foo",
|
||||
"subtype": "bar",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
}
|
||||
|
||||
config2 = {
|
||||
"name": "test_binary_sensor",
|
||||
"state_topic": "test-topic",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
"unique_id": "veryunique",
|
||||
}
|
||||
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla2/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is created
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 3 # 2 binary_sensor triggers + device trigger
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is not cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 2 # 2 binary_sensor triggers
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla2/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is None
|
||||
|
||||
|
||||
async def test_cleanup_device_with_entity2(hass, device_reg, entity_reg, mqtt_mock):
|
||||
"""Test removal from device registry for device with entity.
|
||||
|
||||
Entity removed first, then trigger.
|
||||
"""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN)
|
||||
config_entry.add_to_hass(hass)
|
||||
await async_start(hass, "homeassistant", {}, config_entry)
|
||||
|
||||
config1 = {
|
||||
"automation_type": "trigger",
|
||||
"topic": "test-topic",
|
||||
"type": "foo",
|
||||
"subtype": "bar",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
}
|
||||
|
||||
config2 = {
|
||||
"name": "test_binary_sensor",
|
||||
"state_topic": "test-topic",
|
||||
"device": {"identifiers": ["helloworld"]},
|
||||
"unique_id": "veryunique",
|
||||
}
|
||||
|
||||
data1 = json.dumps(config1)
|
||||
data2 = json.dumps(config2)
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla2/config", data2)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is created
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 3 # 2 binary_sensor triggers + device trigger
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/binary_sensor/bla2/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is not cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is not None
|
||||
|
||||
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
|
||||
assert len(triggers) == 1 # device trigger
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", "")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify device registry entry is cleared
|
||||
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}, set())
|
||||
assert device_entry is None
|
||||
Reference in new issue
Block a user