Do not reset current selection on reconfig or MQTT select (#85099)

* Do not reset current selection on reconfig

* Add a test
This commit is contained in:
Jan Bouwhuis 2023-01-04 10:29:53 +01:00 committed by GitHub
parent b29c96639b
commit 9f24897814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 2 deletions

View File

@ -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]

View File

@ -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

View File

@ -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"]