Restore the previous state of a KNX binary sensor (#57891)

This commit is contained in:
Marvin Wichmann 2021-10-22 20:52:41 +02:00 committed by GitHub
parent b413f7434f
commit 5193e3115d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 3 deletions

View File

@ -7,9 +7,16 @@ from xknx import XKNX
from xknx.devices import BinarySensor as XknxBinarySensor
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_NAME,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt
@ -36,7 +43,7 @@ async def async_setup_platform(
)
class KNXBinarySensor(KnxEntity, BinarySensorEntity):
class KNXBinarySensor(KnxEntity, BinarySensorEntity, RestoreEntity):
"""Representation of a KNX binary sensor."""
_device: XknxBinarySensor
@ -61,6 +68,14 @@ class KNXBinarySensor(KnxEntity, BinarySensorEntity):
self._attr_force_update = self._device.ignore_internal_state
self._attr_unique_id = str(self._device.remote_value.group_address_state)
async def async_added_to_hass(self) -> None:
"""Restore last state."""
await super().async_added_to_hass()
if (
last_state := await self.async_get_last_state()
) and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
await self._device.remote_value.update_value(last_state.state == STATE_ON)
@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""

View File

@ -1,10 +1,11 @@
"""Test KNX binary sensor."""
from datetime import timedelta
from unittest.mock import patch
from homeassistant.components.knx.const import CONF_STATE_ADDRESS, CONF_SYNC_STATE
from homeassistant.components.knx.schema import BinarySensorSchema
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, State
from homeassistant.util import dt
from .conftest import KNXTestKit
@ -213,3 +214,69 @@ async def test_binary_sensor_reset(hass: HomeAssistant, knx: KNXTestKit):
# state reset after after timeout
state = hass.states.get("binary_sensor.test")
assert state.state is STATE_OFF
async def test_binary_sensor_restore_and_respond(hass, knx):
"""Test restoring KNX binary sensor state and respond to read."""
_ADDRESS = "2/2/2"
fake_state = State("binary_sensor.test", STATE_ON)
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=fake_state,
):
await knx.setup_integration(
{
BinarySensorSchema.PLATFORM_NAME: [
{
CONF_NAME: "test",
CONF_STATE_ADDRESS: _ADDRESS,
CONF_SYNC_STATE: False,
},
]
}
)
# restored state - doesn't send telegram
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_ON
await knx.assert_telegram_count(0)
await knx.receive_write(_ADDRESS, False)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state is STATE_OFF
async def test_binary_sensor_restore_invert(hass, knx):
"""Test restoring KNX binary sensor state with invert."""
_ADDRESS = "2/2/2"
fake_state = State("binary_sensor.test", STATE_ON)
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=fake_state,
):
await knx.setup_integration(
{
BinarySensorSchema.PLATFORM_NAME: [
{
CONF_NAME: "test",
CONF_STATE_ADDRESS: _ADDRESS,
BinarySensorSchema.CONF_INVERT: True,
CONF_SYNC_STATE: False,
},
]
}
)
# restored state - doesn't send telegram
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_ON
await knx.assert_telegram_count(0)
# inverted is on, make sure the state is off after it
await knx.receive_write(_ADDRESS, True)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state is STATE_OFF