From 7521754516cb4e7baafc4912affad65b8864d93e Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Mon, 15 Feb 2021 23:54:17 +0100 Subject: [PATCH] return `None` for `BinarySensor.counter` when context timeout is not used (#591) * return `None` for `BinarySensor.counter` when context timeout is not used * Update changelog.md --- changelog.md | 4 ++++ .../custom_components/xknx/binary_sensor.py | 4 +++- xknx/devices/binary_sensor.py | 10 ++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 277367050..7911553b7 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## Unreleased changes +### Devices + +- BinarySensor: return `None` for `BinarySensor.counter` when context timeout is not used (and don't calculate it) + ### Internals - RemoteValue is Generic now accepting DPTArray or DPTBinary diff --git a/home-assistant-plugin/custom_components/xknx/binary_sensor.py b/home-assistant-plugin/custom_components/xknx/binary_sensor.py index 35feb09dc..f7ec3e80f 100644 --- a/home-assistant-plugin/custom_components/xknx/binary_sensor.py +++ b/home-assistant-plugin/custom_components/xknx/binary_sensor.py @@ -40,7 +40,9 @@ def is_on(self): @property def device_state_attributes(self) -> Optional[Dict[str, Any]]: """Return device specific state attributes.""" - return {ATTR_COUNTER: self._device.counter} + if self._device.counter is not None: + return {ATTR_COUNTER: self._device.counter} + return None @property def force_update(self) -> bool: diff --git a/xknx/devices/binary_sensor.py b/xknx/devices/binary_sensor.py index f162774cf..8185964bd 100644 --- a/xknx/devices/binary_sensor.py +++ b/xknx/devices/binary_sensor.py @@ -122,9 +122,9 @@ async def _set_internal_state(self, state: bool) -> None: """Set the internal state of the device. If state was changed after_update hooks and connected Actions are executed.""" if state != self.state or self.ignore_internal_state: self.state = state - self.bump_and_get_counter(state) if self.ignore_internal_state and self._context_timeout: + self.bump_and_get_counter(state) if self._context_task: self._context_task.cancel() self._context_task = asyncio.create_task( @@ -152,9 +152,11 @@ async def _trigger_callbacks(self) -> None: await action.execute() @property - def counter(self) -> int: + def counter(self) -> Optional[int]: """Return current counter for sensor.""" - return self._count_set_on if self.state else self._count_set_off + if self._context_timeout: + return self._count_set_on if self.state else self._count_set_off + return None def bump_and_get_counter(self, state: bool) -> int: """Bump counter and return the number of times a state was set to the same value within CONTEXT_TIMEOUT.""" @@ -169,7 +171,7 @@ def within_same_context() -> bool: self._last_set = new_set_time return time_diff < cast(float, self._context_timeout) - if self._context_timeout and within_same_context(): + if within_same_context(): if state: self._count_set_on = self._count_set_on + 1 return self._count_set_on