Fix race when handling rapid succession of MQTT discovery messages (#68785)
Co-authored-by: jbouwh <jan@jbsoft.nl>
This commit is contained in:
parent
3d378449e8
commit
7e8d52e5a3
7 changed files with 103 additions and 14 deletions
|
@ -118,16 +118,15 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||
|
||||
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
async def mqtt_async_added_to_hass(self) -> None:
|
||||
"""Restore state for entities with expire_after set."""
|
||||
await super().async_added_to_hass()
|
||||
if (
|
||||
(expire_after := self._config.get(CONF_EXPIRE_AFTER)) is not None
|
||||
and expire_after > 0
|
||||
and (last_state := await self.async_get_last_state()) is not None
|
||||
and last_state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE]
|
||||
# We might have set up a trigger already after subscribing from
|
||||
# super().async_added_to_hass(), then we should not restore state
|
||||
# MqttEntity.async_added_to_hass(), then we should not restore state
|
||||
and not self._expiration_trigger
|
||||
):
|
||||
expiration_at = last_state.last_changed + timedelta(seconds=expire_after)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue