From de4099c478ce031d69b378143500ad26c5b0458a Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Mon, 26 Oct 2026 22:18:53 +0100 Subject: [PATCH] Fix MQTT discovery updates without device and invalid device discovery components (#188780) --- homeassistant/components/mqtt/discovery.py | 4 +- homeassistant/components/mqtt/entity.py | 8 +- tests/components/mqtt/test_child_device.py | 186 ++++++++++++++++++++- 3 files changed, 191 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/mqtt/discovery.py b/homeassistant/components/mqtt/discovery.py index eb88ba5940ba..8417255f4b4f 100644 --- a/homeassistant/components/mqtt/discovery.py +++ b/homeassistant/components/mqtt/discovery.py @@ -258,7 +258,9 @@ def _replace_component_abbreviations(payload: dict[str, Any]) -> None: if not isinstance(components := payload.get(CONF_COMPONENTS), dict): return for comp_conf in components.values(): - _replace_all_abbreviations(comp_conf, component_only=True) + # The schema rejects a component that is not a mapping + if isinstance(comp_conf, dict): + _replace_all_abbreviations(comp_conf, component_only=True) @callback diff --git a/homeassistant/components/mqtt/entity.py b/homeassistant/components/mqtt/entity.py index e171eadb43d3..90b351bf4cc2 100644 --- a/homeassistant/components/mqtt/entity.py +++ b/homeassistant/components/mqtt/entity.py @@ -1704,14 +1704,14 @@ class MqttEntityDeviceInfo(Entity): device_specifications = self._device_specifications_from_config(config) ensure_via_device_exists(self.hass, device_specifications, self._config_entry) device = self.async_register_device(device_specifications) + device_id = device.id if device is not None else None # The entity platform only links the device when the entity is added if ( - device is None - or self.registry_entry is None - or (old_device_id := self.registry_entry.device_id) == device.id + self.registry_entry is None + or (old_device_id := self.registry_entry.device_id) == device_id ): return - er.async_get(self.hass).async_update_entity(self.entity_id, device_id=device.id) + er.async_get(self.hass).async_update_entity(self.entity_id, device_id=device_id) await async_cleanup_device_registry( self.hass, old_device_id, diff --git a/tests/components/mqtt/test_child_device.py b/tests/components/mqtt/test_child_device.py index 662c61af34f1..786c41ba575c 100644 --- a/tests/components/mqtt/test_child_device.py +++ b/tests/components/mqtt/test_child_device.py @@ -15,7 +15,7 @@ from homeassistant.components.mqtt.schemas import ( MQTT_DEVICE_INFO_SCHEMA, MQTT_ENTITY_DEVICE_INFO_SCHEMA, ) -from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.const import SERVICE_RELOAD, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.entity_component import DATA_INSTANCES @@ -439,6 +439,24 @@ async def test_device_discovery_with_child_devices_only( "value must be one of", id="child_tag", ), + pytest.param( + { + "dev": {"ids": ["parent"]}, + "o": {"name": "Foo2Mqtt"}, + "cmps": {"sw": None}, + }, + "expected a mapping", + id="component_not_a_mapping", + ), + pytest.param( + { + "dev": {"ids": ["parent"]}, + "o": {"name": "Foo2Mqtt"}, + "cdevs": [{"ids": ["sub"], "cmps": {"sw": 1}}], + }, + "expected a mapping", + id="child_component_not_a_mapping", + ), ], ) async def test_invalid_device_discovery_child_devices( @@ -1078,7 +1096,8 @@ async def test_reload_after_failed_parent_update_keeps_devices( ) -> None: """Test a reload after the parent device of a child device could not change. - The entity is not added, and keeps its child device and parent device. + The entity is not added, and keeps its child device and parent device. After + removing the child device, it is registered again with its new parent device. """ await mqtt_mock_entry() entry = hass.config_entries.async_entries(DOMAIN)[0] @@ -1123,6 +1142,26 @@ async def test_reload_after_failed_parent_update_keeps_devices( assert _get_device(hass, device_registry, "parent") is not None assert _get_device(hass, device_registry, "new_parent") is None + device_registry.async_remove_device(child_device.id) + await hass.async_block_till_done() + with patch("homeassistant.components.mqtt.client.AsyncMQTTClient"): + assert await hass.config_entries.async_reload(entry.entry_id) + await hass.async_block_till_done() + async_fire_mqtt_message(hass, SWITCH_TOPIC, update, retain=True) + await hass.async_block_till_done() + new_parent = _get_device(hass, device_registry, "new_parent") + moved_child_device = _get_child_device(hass, device_registry, "sub") + assert new_parent is not None + assert moved_child_device is not None + # The child device keeps its device ID + assert moved_child_device.id == child_device.id + assert moved_child_device.parent_device_id == new_parent.id + entity_id = entity_registry.async_get_entity_id("switch", DOMAIN, "sw1") + assert entity_id is not None + entity_entry = entity_registry.async_get(entity_id) + assert entity_entry is not None + assert entity_entry.device_id == child_device.id + async def test_remove_child_device_after_failed_parent_update( hass: HomeAssistant, @@ -1444,6 +1483,149 @@ async def test_discovery_update_registration_error_keeps_config( ) +@pytest.mark.parametrize( + ("device", "identifier"), + [ + pytest.param({"identifiers": ["plain"]}, "plain", id="device"), + pytest.param( + {**PARENT_DEVICE, "child_device": CHILD_DEVICE}, "sub", id="child_device" + ), + ], +) +async def test_discovery_update_drops_device( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + device: dict[str, Any], + identifier: str, +) -> None: + """Test a discovery update without device unlinks the entity right away.""" + await mqtt_mock_entry() + await _discover_switch(hass, "sw1", device) + entity_id = entity_registry.async_get_entity_id("switch", DOMAIN, "sw1") + assert entity_id is not None + assert entity_registry.async_get(entity_id).device_id is not None + + update = json.loads(_switch_config("sw1", device)) + del update["device"] + async_fire_mqtt_message(hass, SWITCH_TOPIC, json.dumps(update)) + await hass.async_block_till_done() + assert entity_registry.async_get(entity_id).device_id is None + entry_id = _entry_id(hass) + assert ( + device_registry.async_get_device_by_identifier((DOMAIN, identifier), entry_id) + is None + ) + assert ( + device_registry.async_get_child_device_by_identifier( + (DOMAIN, identifier), entry_id + ) + is None + ) + assert _get_device(hass, device_registry, "parent") is None + + +async def test_device_discovery_parent_change_with_child_device( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test a device discovery update that changes the parent device. + + The components of the device move to the new device. The components of the + child device keep their current device, as a child device can not be moved to + another parent device. + """ + await mqtt_mock_entry() + payload: dict[str, Any] = { + "dev": {"ids": ["parent"], "name": "Parent"}, + "o": {"name": "Foo2Mqtt"}, + "cdevs": [ + { + "ids": ["sub"], + "name": "Sub", + "cmps": {"sw1": {**DISCOVERY_SWITCH, "uniq_id": "sw1"}}, + } + ], + "cmps": {"sw": {**DISCOVERY_SWITCH, "uniq_id": "sw"}}, + } + async_fire_mqtt_message(hass, DEVICE_TOPIC, json.dumps(payload)) + await hass.async_block_till_done() + parent = _get_device(hass, device_registry, "parent") + child_device = _get_child_device(hass, device_registry, "sub") + assert parent is not None + assert child_device is not None + + payload["dev"] = {"ids": ["new_parent"], "name": "New parent"} + async_fire_mqtt_message(hass, DEVICE_TOPIC, json.dumps(payload)) + await hass.async_block_till_done() + assert REGISTRATION_WARNING in caplog.text + new_parent = _get_device(hass, device_registry, "new_parent") + assert new_parent is not None + device_entity_id = entity_registry.async_get_entity_id("switch", DOMAIN, "sw") + child_entity_id = entity_registry.async_get_entity_id("switch", DOMAIN, "sw1") + assert device_entity_id is not None + assert child_entity_id is not None + assert entity_registry.async_get(device_entity_id).device_id == new_parent.id + assert entity_registry.async_get(child_entity_id).device_id == child_device.id + # The previous parent device is kept for its child device + assert device_registry.async_get(parent.id) is not None + + +@pytest.mark.parametrize( + "hass_config", + [ + { + DOMAIN: { + "switch": { + "name": "Switch", + "command_topic": "sw1/set", + "unique_id": "sw1", + "device": {**PARENT_DEVICE, "child_device": CHILD_DEVICE}, + } + } + } + ], +) +async def test_yaml_reload_moves_entity_to_other_child_device( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test a YAML reload moves an entity to another child device.""" + await mqtt_mock_entry() + child_device = _get_child_device(hass, device_registry, "sub") + assert child_device is not None + assert entity_registry.async_get(SWITCH_ENTITY_ID).device_id == child_device.id + + new_config = { + DOMAIN: { + "switch": { + "name": "Switch", + "command_topic": "sw1/set", + "unique_id": "sw1", + "device": { + **PARENT_DEVICE, + "child_device": {"identifiers": ["sub2"], "name": "Sub 2"}, + }, + } + } + } + with patch("homeassistant.config.load_yaml_config_file", return_value=new_config): + await hass.services.async_call(DOMAIN, SERVICE_RELOAD, {}, blocking=True) + await hass.async_block_till_done() + + new_child_device = _get_child_device(hass, device_registry, "sub2") + assert new_child_device is not None + assert entity_registry.async_get(SWITCH_ENTITY_ID).device_id == new_child_device.id + assert _get_child_device(hass, device_registry, "sub") is None + assert _get_device(hass, device_registry, "parent") is not None + + async def test_device_registration_error( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator,