From cd945a42e6ea2d13f02b3a18be9828a247f39f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Sun, 10 May 2026 22:17:52 +0200 Subject: [PATCH] fix: Do not forget segments from state when a new config arrives (#170265) Co-authored-by: Jan Bouwhuis Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/mqtt/vacuum.py | 4 ++ tests/components/mqtt/test_vacuum.py | 50 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/homeassistant/components/mqtt/vacuum.py b/homeassistant/components/mqtt/vacuum.py index 3ab42edcbb8..bce59a8277c 100644 --- a/homeassistant/components/mqtt/vacuum.py +++ b/homeassistant/components/mqtt/vacuum.py @@ -249,6 +249,10 @@ class MqttStateVacuum(MqttEntity, StateVacuumEntity): supported_feature_strings: list[str] = config[CONF_SUPPORTED_FEATURES] self._attr_supported_features = _strings_to_services( supported_feature_strings, STRING_TO_SERVICE + ) | ( + self.supported_features & VacuumEntityFeature.CLEAN_AREA + if CONF_CLEAN_SEGMENTS_COMMAND_TOPIC in config + else 0 ) self._clean_segments_command_topic = config.get( CONF_CLEAN_SEGMENTS_COMMAND_TOPIC diff --git a/tests/components/mqtt/test_vacuum.py b/tests/components/mqtt/test_vacuum.py index 52ac9ad64c2..55bdf130773 100644 --- a/tests/components/mqtt/test_vacuum.py +++ b/tests/components/mqtt/test_vacuum.py @@ -592,6 +592,56 @@ async def test_removing_clean_segments_command_topic_resets_feature( ) +async def test_clean_area_feature_preserved_on_config_update( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, +) -> None: + """Test the clean area feature is preserved when config is updated with the same topic. + + When a new config arrives that still has `clean_segments_command_topic` and + segments were previously received from state, the CLEAN_AREA feature should + be preserved without needing another state message. + """ + await mqtt_mock_entry() + + config = CONFIG_CLEAN_SEGMENTS[mqtt.DOMAIN][vacuum.DOMAIN] + async_fire_mqtt_message( + hass, + "homeassistant/vacuum/bla/config", + json.dumps(config), + ) + await hass.async_block_till_done() + message = """{ + "battery_level": 54, + "state": "idle", + "segments":{ + "1":"Livingroom", + "2":"Kitchen" + } + }""" + async_fire_mqtt_message(hass, "vacuum/state", message) + await hass.async_block_till_done() + state = hass.states.get("vacuum.test") + assert ( + state.attributes.get(ATTR_SUPPORTED_FEATURES) + & vacuum.VacuumEntityFeature.CLEAN_AREA + ) + + updated_config = config.copy() + updated_config["name"] = "renamed" + async_fire_mqtt_message( + hass, + "homeassistant/vacuum/bla/config", + json.dumps(updated_config), + ) + await hass.async_block_till_done() + state = hass.states.get("vacuum.test") + assert ( + state.attributes.get(ATTR_SUPPORTED_FEATURES) + & vacuum.VacuumEntityFeature.CLEAN_AREA + ) + + @pytest.mark.parametrize("hass_config", [CONFIG_ALL_SERVICES]) async def test_status( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator