mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Switch mqtt to use async_call_later where possible (#99486)
This commit is contained in:
parent
defd9e4001
commit
6e743a5bb2
@ -29,7 +29,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.helpers.event as evt
|
import homeassistant.helpers.event as evt
|
||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
@ -128,15 +128,17 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
expiration_at: datetime = last_state.last_changed + timedelta(
|
expiration_at: datetime = last_state.last_changed + timedelta(
|
||||||
seconds=self._expire_after
|
seconds=self._expire_after
|
||||||
)
|
)
|
||||||
if expiration_at < (time_now := dt_util.utcnow()):
|
remain_seconds = (expiration_at - dt_util.utcnow()).total_seconds()
|
||||||
|
|
||||||
|
if remain_seconds <= 0:
|
||||||
# Skip reactivating the binary_sensor
|
# Skip reactivating the binary_sensor
|
||||||
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
||||||
return
|
return
|
||||||
self._expired = False
|
self._expired = False
|
||||||
self._attr_is_on = last_state.state == STATE_ON
|
self._attr_is_on = last_state.state == STATE_ON
|
||||||
|
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
self._expiration_trigger = async_call_later(
|
||||||
self.hass, self._value_is_expired, expiration_at
|
self.hass, remain_seconds, self._value_is_expired
|
||||||
)
|
)
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
(
|
(
|
||||||
@ -144,7 +146,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
" expiring %s"
|
" expiring %s"
|
||||||
),
|
),
|
||||||
self.entity_id,
|
self.entity_id,
|
||||||
expiration_at - time_now,
|
remain_seconds,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self) -> None:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
@ -202,10 +204,8 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
self._expiration_trigger()
|
self._expiration_trigger()
|
||||||
|
|
||||||
# Set new trigger
|
# Set new trigger
|
||||||
expiration_at = dt_util.utcnow() + timedelta(seconds=self._expire_after)
|
self._expiration_trigger = async_call_later(
|
||||||
|
self.hass, self._expire_after, self._value_is_expired
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
|
||||||
self.hass, self._value_is_expired, expiration_at
|
|
||||||
)
|
)
|
||||||
|
|
||||||
payload = self._value_template(msg.payload)
|
payload = self._value_template(msg.payload)
|
||||||
|
@ -32,7 +32,7 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
|
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
@ -162,15 +162,17 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
and not self._expiration_trigger
|
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()):
|
remain_seconds = (expiration_at - dt_util.utcnow()).total_seconds()
|
||||||
|
|
||||||
|
if remain_seconds <= 0:
|
||||||
# Skip reactivating the sensor
|
# Skip reactivating the sensor
|
||||||
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
|
||||||
return
|
return
|
||||||
self._expired = False
|
self._expired = False
|
||||||
self._attr_native_value = last_sensor_data.native_value
|
self._attr_native_value = last_sensor_data.native_value
|
||||||
|
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
self._expiration_trigger = async_call_later(
|
||||||
self.hass, self._value_is_expired, expiration_at
|
self.hass, remain_seconds, self._value_is_expired
|
||||||
)
|
)
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
(
|
(
|
||||||
@ -178,7 +180,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
" expiring %s"
|
" expiring %s"
|
||||||
),
|
),
|
||||||
self.entity_id,
|
self.entity_id,
|
||||||
expiration_at - time_now,
|
remain_seconds,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self) -> None:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
@ -235,10 +237,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
self._expiration_trigger()
|
self._expiration_trigger()
|
||||||
|
|
||||||
# Set new trigger
|
# Set new trigger
|
||||||
expiration_at = dt_util.utcnow() + timedelta(seconds=self._expire_after)
|
self._expiration_trigger = async_call_later(
|
||||||
|
self.hass, self._expire_after, self._value_is_expired
|
||||||
self._expiration_trigger = async_track_point_in_utc_time(
|
|
||||||
self.hass, self._value_is_expired, expiration_at
|
|
||||||
)
|
)
|
||||||
|
|
||||||
payload = self._template(msg.payload, PayloadSentinel.DEFAULT)
|
payload = self._template(msg.payload, PayloadSentinel.DEFAULT)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user