From e567f8f3d5d9cdfb485c06794473c0608a76d656 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 26 Jun 2024 09:14:33 +0200 Subject: [PATCH] Fix issue where an MQTT device is removed linked to two config entries (#120430) * Fix issue where an MQTT device is removed linked to two config entries * Update homeassistant/components/mqtt/discovery.py Co-authored-by: J. Nick Koston * Update homeassistant/components/mqtt/debug_info.py Co-authored-by: J. Nick Koston --------- Co-authored-by: J. Nick Koston --- homeassistant/components/mqtt/debug_info.py | 2 +- homeassistant/components/mqtt/discovery.py | 2 +- homeassistant/components/mqtt/tag.py | 5 +++-- tests/components/mqtt/test_discovery.py | 4 +++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/mqtt/debug_info.py b/homeassistant/components/mqtt/debug_info.py index a8fd318b1e9..2985e6d7707 100644 --- a/homeassistant/components/mqtt/debug_info.py +++ b/homeassistant/components/mqtt/debug_info.py @@ -138,7 +138,7 @@ def remove_trigger_discovery_data( hass: HomeAssistant, discovery_hash: tuple[str, str] ) -> None: """Remove discovery data.""" - del hass.data[DATA_MQTT].debug_info_triggers[discovery_hash] + hass.data[DATA_MQTT].debug_info_triggers.pop(discovery_hash, None) def _info_for_entity(hass: HomeAssistant, entity_id: str) -> dict[str, Any]: diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index 0d93af26a57..cf2941a3665 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -73,7 +73,7 @@ class MQTTDiscoveryPayload(dict[str, Any]): def clear_discovery_hash(hass: HomeAssistant, discovery_hash: tuple[str, str]) -> None: """Clear entry from already discovered list.""" - hass.data[DATA_MQTT].discovery_already_discovered.remove(discovery_hash) + hass.data[DATA_MQTT].discovery_already_discovered.discard(discovery_hash) def set_discovery_hash(hass: HomeAssistant, discovery_hash: tuple[str, str]) -> None: diff --git a/homeassistant/components/mqtt/tag.py b/homeassistant/components/mqtt/tag.py index 22263a07499..fbb0ea813c2 100644 --- a/homeassistant/components/mqtt/tag.py +++ b/homeassistant/components/mqtt/tag.py @@ -180,5 +180,6 @@ class MQTTTagScanner(MqttDiscoveryDeviceUpdateMixin): self._sub_state = subscription.async_unsubscribe_topics( self.hass, self._sub_state ) - if self.device_id: - del self.hass.data[DATA_MQTT].tags[self.device_id][discovery_id] + tags = self.hass.data[DATA_MQTT].tags + if self.device_id in tags and discovery_id in tags[self.device_id]: + del tags[self.device_id][discovery_id] diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index fbf878a040a..23dea310199 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -809,7 +809,7 @@ async def test_duplicate_removal( assert "Component has already been discovered: binary_sensor bla" not in caplog.text -async def test_cleanup_device( +async def test_cleanup_device_manual( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, device_registry: dr.DeviceRegistry, @@ -1012,6 +1012,7 @@ async def test_cleanup_device_multiple_config_entries( async def test_cleanup_device_multiple_config_entries_mqtt( hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, mqtt_mock_entry: MqttMockHAClientGenerator, @@ -1093,6 +1094,7 @@ async def test_cleanup_device_multiple_config_entries_mqtt( # Verify retained discovery topics have not been cleared again mqtt_mock.async_publish.assert_not_called() + assert "KeyError:" not in caplog.text async def test_discovery_expansion(