Add KNX source address to Sensor and BinarySensor (#48857)

* Add source address to Sensor and BinarySensor

* Fix typing

* Review: Always use UTC time in state attributes

* Review: Add missing UTC conversion in sensor
This commit is contained in:
Marvin Wichmann 2021-04-10 18:12:43 +02:00 committed by GitHub
parent 676af205e4
commit 21744790d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View File

@ -9,8 +9,9 @@ from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt
from .const import ATTR_COUNTER, DOMAIN
from .const import ATTR_COUNTER, ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN
from .knx_entity import KnxEntity
@ -51,9 +52,16 @@ class KNXBinarySensor(KnxEntity, BinarySensorEntity):
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return device specific state attributes."""
attr: dict[str, Any] = {}
if self._device.counter is not None:
return {ATTR_COUNTER: self._device.counter}
return None
attr[ATTR_COUNTER] = self._device.counter
if self._device.last_telegram is not None:
attr[ATTR_SOURCE] = str(self._device.last_telegram.source_address)
attr[ATTR_LAST_KNX_UPDATE] = str(
dt.as_utc(self._device.last_telegram.timestamp)
)
return attr
@property
def force_update(self) -> bool:

View File

@ -26,6 +26,8 @@ CONF_SYNC_STATE = "sync_state"
CONF_RESET_AFTER = "reset_after"
ATTR_COUNTER = "counter"
ATTR_SOURCE = "source"
ATTR_LAST_KNX_UPDATE = "last_knx_update"
class ColorTempModes(Enum):

View File

@ -1,7 +1,7 @@
"""Support for KNX/IP sensors."""
from __future__ import annotations
from typing import Callable, Iterable
from typing import Any, Callable, Iterable
from xknx.devices import Sensor as XknxSensor
@ -9,8 +9,9 @@ from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from homeassistant.util import dt
from .const import DOMAIN
from .const import ATTR_LAST_KNX_UPDATE, ATTR_SOURCE, DOMAIN
from .knx_entity import KnxEntity
@ -54,6 +55,18 @@ class KNXSensor(KnxEntity, SensorEntity):
return device_class
return None
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return device specific state attributes."""
attr: dict[str, Any] = {}
if self._device.last_telegram is not None:
attr[ATTR_SOURCE] = str(self._device.last_telegram.source_address)
attr[ATTR_LAST_KNX_UPDATE] = str(
dt.as_utc(self._device.last_telegram.timestamp)
)
return attr
@property
def force_update(self) -> bool:
"""