From 0bcee213338df162de00f0f269bffb3024602b6f Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Tue, 28 Jul 2020 01:45:41 +0200 Subject: [PATCH] Restore rfxtrx state to off when delay off is in effect (#38239) --- .../components/rfxtrx/binary_sensor.py | 15 ++++----- tests/components/rfxtrx/test_binary_sensor.py | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index b8976454d68..8f7229299ee 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -13,6 +13,7 @@ from homeassistant.const import ( CONF_COMMAND_ON, CONF_DEVICE_CLASS, CONF_DEVICES, + STATE_ON, ) from homeassistant.core import callback from homeassistant.helpers import event as evt @@ -28,12 +29,7 @@ from . import ( get_pt2262_cmd, get_rfx_object, ) -from .const import ( - ATTR_EVENT, - COMMAND_OFF_LIST, - COMMAND_ON_LIST, - DEVICE_PACKET_TYPE_LIGHTING4, -) +from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, DEVICE_PACKET_TYPE_LIGHTING4 _LOGGER = logging.getLogger(__name__) @@ -184,9 +180,10 @@ class RfxtrxBinarySensor(RfxtrxEntity, BinarySensorEntity): if self._event is None: old_state = await self.async_get_last_state() if old_state is not None: - event = old_state.attributes.get(ATTR_EVENT) - if event: - self._apply_event(get_rfx_object(event)) + self._state = old_state.state == STATE_ON + + if self._state and self._off_delay is not None: + self._state = False @property def force_update(self) -> bool: diff --git a/tests/components/rfxtrx/test_binary_sensor.py b/tests/components/rfxtrx/test_binary_sensor.py index 8fbc06f6ddb..56aa7126a5b 100644 --- a/tests/components/rfxtrx/test_binary_sensor.py +++ b/tests/components/rfxtrx/test_binary_sensor.py @@ -16,6 +16,8 @@ EVENT_MOTION_DETECTOR_NO_MOTION = "08200100a109000570" EVENT_LIGHT_DETECTOR_LIGHT = "08200100a109001570" EVENT_LIGHT_DETECTOR_DARK = "08200100a109001470" +EVENT_AC_118CDEA_2_ON = "0b1100100118cdea02010f70" + async def test_one(hass, rfxtrx): """Test with 1 sensor.""" @@ -137,6 +139,37 @@ async def test_discover(hass, rfxtrx_automatic): assert state.state == "on" +async def test_off_delay_restore(hass, rfxtrx): + """Make sure binary sensor restore as off, if off delay is active.""" + mock_restore_cache( + hass, + [ + State( + "binary_sensor.ac_118cdea_2", + "on", + attributes={ATTR_EVENT: EVENT_AC_118CDEA_2_ON}, + ) + ], + ) + + assert await async_setup_component( + hass, + "rfxtrx", + { + "rfxtrx": { + "device": "abcd", + "devices": {EVENT_AC_118CDEA_2_ON: {"off_delay": 5}}, + } + }, + ) + await hass.async_block_till_done() + await hass.async_start() + + state = hass.states.get("binary_sensor.ac_118cdea_2") + assert state + assert state.state == "off" + + async def test_off_delay(hass, rfxtrx, timestep): """Test with discovery.""" assert await async_setup_component(