Skip to content

Commit

Permalink
return None for BinarySensor.counter when context timeout is not …
Browse files Browse the repository at this point in the history
…used (#591)

* return `None` for `BinarySensor.counter` when context timeout is not used

* Update changelog.md
  • Loading branch information
farmio committed Feb 15, 2021
1 parent 4560a0c commit 7521754
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Expand Up @@ -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
Expand Down
Expand Up @@ -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:
Expand Down
10 changes: 6 additions & 4 deletions xknx/devices/binary_sensor.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down

0 comments on commit 7521754

Please sign in to comment.