mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Correct MQTT binary_sensor and sensor state restoring (#66690)
This commit is contained in:
parent
413e317c65
commit
ebe7c95e4f
@ -125,6 +125,9 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
and expire_after > 0
|
and expire_after > 0
|
||||||
and (last_state := await self.async_get_last_state()) is not None
|
and (last_state := await self.async_get_last_state()) is not None
|
||||||
and last_state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE]
|
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
|
||||||
|
and not self._expiration_trigger
|
||||||
):
|
):
|
||||||
expiration_at = last_state.last_changed + timedelta(seconds=expire_after)
|
expiration_at = last_state.last_changed + timedelta(seconds=expire_after)
|
||||||
if expiration_at < (time_now := dt_util.utcnow()):
|
if expiration_at < (time_now := dt_util.utcnow()):
|
||||||
@ -134,10 +137,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
self._expired = False
|
self._expired = False
|
||||||
self._state = last_state.state == STATE_ON
|
self._state = last_state.state == STATE_ON
|
||||||
|
|
||||||
if self._expiration_trigger:
|
|
||||||
# We might have set up a trigger already after subscribing from
|
|
||||||
# super().async_added_to_hass()
|
|
||||||
self._expiration_trigger()
|
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
self._expiration_trigger = async_track_point_in_utc_time(
|
||||||
self.hass, self._value_is_expired, expiration_at
|
self.hass, self._value_is_expired, expiration_at
|
||||||
)
|
)
|
||||||
|
@ -171,6 +171,9 @@ class MqttSensor(MqttEntity, SensorEntity, RestoreEntity):
|
|||||||
and expire_after > 0
|
and expire_after > 0
|
||||||
and (last_state := await self.async_get_last_state()) is not None
|
and (last_state := await self.async_get_last_state()) is not None
|
||||||
and last_state.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE]
|
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
|
||||||
|
and not self._expiration_trigger
|
||||||
):
|
):
|
||||||
expiration_at = last_state.last_changed + timedelta(seconds=expire_after)
|
expiration_at = last_state.last_changed + timedelta(seconds=expire_after)
|
||||||
if expiration_at < (time_now := dt_util.utcnow()):
|
if expiration_at < (time_now := dt_util.utcnow()):
|
||||||
@ -180,10 +183,6 @@ class MqttSensor(MqttEntity, SensorEntity, RestoreEntity):
|
|||||||
self._expired = False
|
self._expired = False
|
||||||
self._state = last_state.state
|
self._state = last_state.state
|
||||||
|
|
||||||
if self._expiration_trigger:
|
|
||||||
# We might have set up a trigger already after subscribing from
|
|
||||||
# super().async_added_to_hass()
|
|
||||||
self._expiration_trigger()
|
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
self._expiration_trigger = async_track_point_in_utc_time(
|
||||||
self.hass, self._value_is_expired, expiration_at
|
self.hass, self._value_is_expired, expiration_at
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user