diff --git a/homeassistant/components/homekit/config_flow.py b/homeassistant/components/homekit/config_flow.py index ac932b1c31b..03dffc23bed 100644 --- a/homeassistant/components/homekit/config_flow.py +++ b/homeassistant/components/homekit/config_flow.py @@ -456,13 +456,16 @@ class OptionsFlowHandler(config_entries.OptionsFlow): domains=self.hk_options[CONF_DOMAINS], ) data_schema = {} - # Strip out entities that no longer exist to prevent error in the UI - valid_entities = [ - entity_id for entity_id in entities if entity_id in all_supported_entities - ] if self.hk_options[CONF_HOMEKIT_MODE] == HOMEKIT_MODE_ACCESSORY: # In accessory mode we can only have one - default_value = valid_entities[0] if valid_entities else None + default_value = next( + iter( + entity_id + for entity_id in entities + if entity_id in all_supported_entities + ), + None, + ) entity_schema = vol.In entities_schema_required = vol.Required else: @@ -476,12 +479,16 @@ class OptionsFlowHandler(config_entries.OptionsFlow): vol.Required(CONF_INCLUDE_EXCLUDE_MODE, default=include_exclude_mode) ] = vol.In(INCLUDE_EXCLUDE_MODES) entity_schema = cv.multi_select - default_value = valid_entities + # Strip out entities that no longer exist to prevent error in the UI + default_value = [ + entity_id + for entity_id in entities + if entity_id in all_supported_entities + ] data_schema[ entities_schema_required(CONF_ENTITIES, default=default_value) ] = entity_schema(all_supported_entities) - return self.async_show_form( step_id="include_exclude", data_schema=vol.Schema(data_schema) ) diff --git a/tests/components/homekit/test_config_flow.py b/tests/components/homekit/test_config_flow.py index ffd223d1d2a..4180dfa8458 100644 --- a/tests/components/homekit/test_config_flow.py +++ b/tests/components/homekit/test_config_flow.py @@ -335,6 +335,8 @@ async def test_options_flow_exclude_mode_basic(hass, mock_get_source_ip): config_entry.add_to_hass(hass) hass.states.async_set("climate.old", "off") + hass.states.async_set("climate.front_gate", "off") + await hass.async_block_till_done() result = await hass.config_entries.options.async_init( @@ -351,6 +353,8 @@ async def test_options_flow_exclude_mode_basic(hass, mock_get_source_ip): assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "include_exclude" + entities = result["data_schema"]({})["entities"] + assert entities == ["climate.front_gate"] # Inject garbage to ensure the options data # is being deep copied and we cannot mutate it in flight