From 5193e3115d8a9dabef3b7b6cd423b8df5eaaca1b Mon Sep 17 00:00:00 2001 From: Marvin Wichmann Date: Fri, 22 Oct 2021 20:52:41 +0200 Subject: [PATCH] Restore the previous state of a KNX binary sensor (#57891) --- homeassistant/components/knx/binary_sensor.py | 19 ++++- tests/components/knx/test_binary_sensor.py | 69 ++++++++++++++++++- 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/knx/binary_sensor.py b/homeassistant/components/knx/binary_sensor.py index 9a9c9627670..7a3de7e1ec0 100644 --- a/homeassistant/components/knx/binary_sensor.py +++ b/homeassistant/components/knx/binary_sensor.py @@ -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.""" diff --git a/tests/components/knx/test_binary_sensor.py b/tests/components/knx/test_binary_sensor.py index 1cb9f14e61f..811c8ab1341 100644 --- a/tests/components/knx/test_binary_sensor.py +++ b/tests/components/knx/test_binary_sensor.py @@ -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