Fix race and add test coverage for esphome select platform (#95019)

This commit is contained in:
J. Nick Koston 2023-06-22 01:19:47 +02:00 committed by GitHub
parent ef2669afe4
commit 65a5244d5a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 47 deletions

View file

@ -845,6 +845,7 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
self._on_static_info_update,
)
)
self._update_state_from_entry_data()
@callback
def _on_static_info_update(self, static_info: EntityInfo) -> None:
@ -868,11 +869,9 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
self._attr_icon = None
@callback
def _on_state_update(self) -> None:
"""Call when state changed.
def _update_state_from_entry_data(self) -> None:
"""Update state from entry data."""
Behavior can be changed in child classes
"""
state = self._entry_data.state
key = self._key
state_type = self._state_type
@ -880,6 +879,14 @@ class EsphomeEntity(Entity, Generic[_InfoT, _StateT]):
if has_state:
self._state = cast(_StateT, state[state_type][key])
self._has_state = has_state
@callback
def _on_state_update(self) -> None:
"""Call when state changed.
Behavior can be changed in child classes
"""
self._update_state_from_entry_data()
self.async_write_ha_state()
@callback

View file

@ -53,9 +53,8 @@ class EsphomeSelect(EsphomeEntity[SelectInfo, SelectState], SelectEntity):
@esphome_state_property
def current_option(self) -> str | None:
"""Return the state of the entity."""
if self._state.missing_state:
return None
return self._state.state
state = self._state
return None if state.missing_state else state.state
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""