Switch mqtt to use async_call_later where possible (#99486)

This commit is contained in:
J. Nick Koston 2023-09-02 11:55:11 -05:00 committed by GitHub
parent defd9e4001
commit 6e743a5bb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 18 deletions

View file

@ -29,7 +29,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
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.typing import ConfigType, DiscoveryInfoType
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(
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
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
return
self._expired = False
self._attr_is_on = last_state.state == STATE_ON
self._expiration_trigger = async_track_point_in_utc_time(
self.hass, self._value_is_expired, expiration_at
self._expiration_trigger = async_call_later(
self.hass, remain_seconds, self._value_is_expired
)
_LOGGER.debug(
(
@ -144,7 +146,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
" expiring %s"
),
self.entity_id,
expiration_at - time_now,
remain_seconds,
)
async def async_will_remove_from_hass(self) -> None:
@ -202,10 +204,8 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
self._expiration_trigger()
# Set new trigger
expiration_at = dt_util.utcnow() + timedelta(seconds=self._expire_after)
self._expiration_trigger = async_track_point_in_utc_time(
self.hass, self._value_is_expired, expiration_at
self._expiration_trigger = async_call_later(
self.hass, self._expire_after, self._value_is_expired
)
payload = self._value_template(msg.payload)

View file

@ -32,7 +32,7 @@ from homeassistant.const import (
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, State, callback
import homeassistant.helpers.config_validation as cv
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.util import dt as dt_util
@ -162,15 +162,17 @@ class MqttSensor(MqttEntity, RestoreSensor):
and not self._expiration_trigger
):
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
_LOGGER.debug("Skip state recovery after reload for %s", self.entity_id)
return
self._expired = False
self._attr_native_value = last_sensor_data.native_value
self._expiration_trigger = async_track_point_in_utc_time(
self.hass, self._value_is_expired, expiration_at
self._expiration_trigger = async_call_later(
self.hass, remain_seconds, self._value_is_expired
)
_LOGGER.debug(
(
@ -178,7 +180,7 @@ class MqttSensor(MqttEntity, RestoreSensor):
" expiring %s"
),
self.entity_id,
expiration_at - time_now,
remain_seconds,
)
async def async_will_remove_from_hass(self) -> None:
@ -235,10 +237,8 @@ class MqttSensor(MqttEntity, RestoreSensor):
self._expiration_trigger()
# Set new trigger
expiration_at = dt_util.utcnow() + timedelta(seconds=self._expire_after)
self._expiration_trigger = async_track_point_in_utc_time(
self.hass, self._value_is_expired, expiration_at
self._expiration_trigger = async_call_later(
self.hass, self._expire_after, self._value_is_expired
)
payload = self._template(msg.payload, PayloadSentinel.DEFAULT)