diff --git a/homeassistant/components/knx/binary_sensor.py b/homeassistant/components/knx/binary_sensor.py index 0faeb9f37b4..47462f272d4 100644 --- a/homeassistant/components/knx/binary_sensor.py +++ b/homeassistant/components/knx/binary_sensor.py @@ -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: diff --git a/homeassistant/components/knx/const.py b/homeassistant/components/knx/const.py index dfe357ef33c..78b3f5ec7f9 100644 --- a/homeassistant/components/knx/const.py +++ b/homeassistant/components/knx/const.py @@ -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): diff --git a/homeassistant/components/knx/sensor.py b/homeassistant/components/knx/sensor.py index f14cf7e5b29..f75f483b9fb 100644 --- a/homeassistant/components/knx/sensor.py +++ b/homeassistant/components/knx/sensor.py @@ -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: """