Restore low battery state on ISY994 Insteon heartbeat nodes (#85209)

fixes undefined
This commit is contained in:
shbatm 2023-01-05 18:30:08 -06:00 committed by GitHub
parent bdd87bcc87
commit c4d03088c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,9 +20,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ON
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
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_track_point_in_utc_time
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .const import ( from .const import (
@ -390,7 +392,7 @@ class ISYInsteonBinarySensorEntity(ISYBinarySensorEntity):
return self._computed_state return self._computed_state
class ISYBinarySensorHeartbeat(ISYNodeEntity, BinarySensorEntity): class ISYBinarySensorHeartbeat(ISYNodeEntity, BinarySensorEntity, RestoreEntity):
"""Representation of the battery state of an ISY sensor.""" """Representation of the battery state of an ISY sensor."""
def __init__( def __init__(
@ -406,8 +408,9 @@ class ISYBinarySensorHeartbeat(ISYNodeEntity, BinarySensorEntity):
Computed state is set to UNKNOWN unless the ISY provided a valid Computed state is set to UNKNOWN unless the ISY provided a valid
state. See notes above regarding ISY Sensor status on ISY restart. state. See notes above regarding ISY Sensor status on ISY restart.
If a valid state is provided (either on or off), the computed state in If a valid state is provided (either on or off), the computed state in
HA is set to OFF (Normal). If the heartbeat is not received in 25 hours HA is restored to the previous value or defaulted to OFF (Normal).
then the computed state is set to ON (Low Battery). If the heartbeat is not received in 25 hours then the computed state is
set to ON (Low Battery).
""" """
super().__init__(node) super().__init__(node)
self._parent_device = parent_device self._parent_device = parent_device
@ -425,6 +428,11 @@ class ISYBinarySensorHeartbeat(ISYNodeEntity, BinarySensorEntity):
# Start the timer on bootup, so we can change from UNKNOWN to OFF # Start the timer on bootup, so we can change from UNKNOWN to OFF
self._restart_timer() self._restart_timer()
if (last_state := await self.async_get_last_state()) is not None:
# Only restore the state if it was previously ON (Low Battery)
if last_state.state == STATE_ON:
self._computed_state = True
def _heartbeat_node_control_handler(self, event: NodeProperty) -> None: def _heartbeat_node_control_handler(self, event: NodeProperty) -> None:
"""Update the heartbeat timestamp when any ON/OFF event is sent. """Update the heartbeat timestamp when any ON/OFF event is sent.