Skip to content

Commit cd945a4

Browse files
HypferjbouwhCopilot
authored
fix: Do not forget segments from state when a new config arrives (home-assistant#170265)
Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent afc9726 commit cd945a4

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

homeassistant/components/mqtt/vacuum.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ def _strings_to_services(
249249
supported_feature_strings: list[str] = config[CONF_SUPPORTED_FEATURES]
250250
self._attr_supported_features = _strings_to_services(
251251
supported_feature_strings, STRING_TO_SERVICE
252+
) | (
253+
self.supported_features & VacuumEntityFeature.CLEAN_AREA
254+
if CONF_CLEAN_SEGMENTS_COMMAND_TOPIC in config
255+
else 0
252256
)
253257
self._clean_segments_command_topic = config.get(
254258
CONF_CLEAN_SEGMENTS_COMMAND_TOPIC

tests/components/mqtt/test_vacuum.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,56 @@ async def test_removing_clean_segments_command_topic_resets_feature(
592592
)
593593

594594

595+
async def test_clean_area_feature_preserved_on_config_update(
596+
hass: HomeAssistant,
597+
mqtt_mock_entry: MqttMockHAClientGenerator,
598+
) -> None:
599+
"""Test the clean area feature is preserved when config is updated with the same topic.
600+
601+
When a new config arrives that still has `clean_segments_command_topic` and
602+
segments were previously received from state, the CLEAN_AREA feature should
603+
be preserved without needing another state message.
604+
"""
605+
await mqtt_mock_entry()
606+
607+
config = CONFIG_CLEAN_SEGMENTS[mqtt.DOMAIN][vacuum.DOMAIN]
608+
async_fire_mqtt_message(
609+
hass,
610+
"homeassistant/vacuum/bla/config",
611+
json.dumps(config),
612+
)
613+
await hass.async_block_till_done()
614+
message = """{
615+
"battery_level": 54,
616+
"state": "idle",
617+
"segments":{
618+
"1":"Livingroom",
619+
"2":"Kitchen"
620+
}
621+
}"""
622+
async_fire_mqtt_message(hass, "vacuum/state", message)
623+
await hass.async_block_till_done()
624+
state = hass.states.get("vacuum.test")
625+
assert (
626+
state.attributes.get(ATTR_SUPPORTED_FEATURES)
627+
& vacuum.VacuumEntityFeature.CLEAN_AREA
628+
)
629+
630+
updated_config = config.copy()
631+
updated_config["name"] = "renamed"
632+
async_fire_mqtt_message(
633+
hass,
634+
"homeassistant/vacuum/bla/config",
635+
json.dumps(updated_config),
636+
)
637+
await hass.async_block_till_done()
638+
state = hass.states.get("vacuum.test")
639+
assert (
640+
state.attributes.get(ATTR_SUPPORTED_FEATURES)
641+
& vacuum.VacuumEntityFeature.CLEAN_AREA
642+
)
643+
644+
595645
@pytest.mark.parametrize("hass_config", [CONFIG_ALL_SERVICES])
596646
async def test_status(
597647
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator

0 commit comments

Comments
 (0)