From 9f24897814c518278caaefc9d667f1ea608a4a66 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Wed, 4 Jan 2023 10:29:53 +0100 Subject: [PATCH] Do not reset current selection on reconfig or MQTT select (#85099) * Do not reset current selection on reconfig * Add a test --- homeassistant/components/mqtt/select.py | 2 +- tests/components/mqtt/test_common.py | 13 ++++++++++++ tests/components/mqtt/test_select.py | 27 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 65cf406522c..6d07a1a5fff 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -113,6 +113,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity): discovery_data: DiscoveryInfoType | None, ) -> None: """Initialize the MQTT select.""" + self._attr_current_option = None SelectEntity.__init__(self) MqttEntity.__init__(self, hass, config, config_entry, discovery_data) @@ -123,7 +124,6 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity): def _setup_from_config(self, config: ConfigType) -> None: """(Re)Setup the entity.""" - self._attr_current_option = None self._optimistic = config[CONF_OPTIMISTIC] self._attr_options = config[CONF_OPTIONS] diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index 82e94c1e65e..f0a22987aa3 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -18,6 +18,7 @@ from homeassistant.const import ( SERVICE_RELOAD, STATE_UNAVAILABLE, ) +from homeassistant.core import HomeAssistant from homeassistant.generated.mqtt import MQTT from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -1818,3 +1819,15 @@ async def help_test_unload_config_entry_with_platform( discovery_setup_entity = hass.states.get(f"{domain}.discovery_setup") assert discovery_setup_entity is None + + +async def help_test_discovery_setup( + hass: HomeAssistant, domain: str, discovery_data_payload: str, name: str +) -> None: + """Test setting up an MQTT entity using discovery.""" + async_fire_mqtt_message( + hass, f"homeassistant/{domain}/{name}/config", discovery_data_payload + ) + await hass.async_block_till_done() + state = hass.states.get(f"{domain}.{name}") + assert state.state is not None diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index e82bd20aa2b..ddfd0074694 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -29,6 +29,7 @@ from .test_common import ( help_test_default_availability_payload, help_test_discovery_broken, help_test_discovery_removal, + help_test_discovery_setup, help_test_discovery_update, help_test_discovery_update_attr, help_test_discovery_update_unchanged, @@ -455,7 +456,7 @@ async def test_discovery_update_select(hass, mqtt_mock_entry_no_yaml_config, cap "name": "Milk", "state_topic": "test-topic", "command_topic": "test-topic", - "options": ["milk", "beer"], + "options": ["milk"], } await help_test_discovery_update( @@ -701,3 +702,27 @@ async def test_unload_entry(hass, mqtt_mock_entry_with_yaml_config, tmp_path): await help_test_unload_config_entry_with_platform( hass, mqtt_mock_entry_with_yaml_config, tmp_path, domain, config ) + + +async def test_persistent_state_after_reconfig( + hass: ha.HomeAssistant, mqtt_mock_entry_no_yaml_config +) -> None: + """Test of the state is persistent after reconfiguring the select options.""" + await mqtt_mock_entry_no_yaml_config() + discovery_data = '{ "name": "Milk", "state_topic": "test-topic", "command_topic": "test-topic", "options": ["milk", "beer"]}' + await help_test_discovery_setup(hass, SELECT_DOMAIN, discovery_data, "milk") + + # assign an initial state + async_fire_mqtt_message(hass, "test-topic", "beer") + state = hass.states.get("select.milk") + assert state.state == "beer" + assert state.attributes["options"] == ["milk", "beer"] + + # remove "milk" option + discovery_data = '{ "name": "Milk", "state_topic": "test-topic", "command_topic": "test-topic", "options": ["beer"]}' + await help_test_discovery_setup(hass, SELECT_DOMAIN, discovery_data, "milk") + + # assert the state persistent + state = hass.states.get("select.milk") + assert state.state == "beer" + assert state.attributes["options"] == ["beer"]