Skip to content

Commit 97afec1

Browse files
authored
Record last_reported for KNX BinarySensor entitiy states (home-assistant#154392)
1 parent 0bfdd70 commit 97afec1

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

homeassistant/components/knx/binary_sensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __init__(self, knx_module: KNXModule, config: ConfigType) -> None:
125125
ignore_internal_state=config[CONF_IGNORE_INTERNAL_STATE],
126126
context_timeout=config.get(CONF_CONTEXT_TIMEOUT),
127127
reset_after=config.get(CONF_RESET_AFTER),
128+
always_callback=True,
128129
),
129130
)
130131
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
@@ -159,5 +160,6 @@ def __init__(
159160
),
160161
context_timeout=knx_conf.get(CONF_CONTEXT_TIMEOUT),
161162
reset_after=knx_conf.get(CONF_RESET_AFTER),
163+
always_callback=True,
162164
)
163165
self._attr_force_update = self._device.ignore_internal_state

tests/components/knx/test_binary_sensor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@ async def test_binary_sensor(hass: HomeAssistant, knx: KNXTestKit) -> None:
109109
await knx.assert_telegram_count(0)
110110

111111

112+
async def test_last_reported(
113+
hass: HomeAssistant,
114+
knx: KNXTestKit,
115+
freezer: FrozenDateTimeFactory,
116+
) -> None:
117+
"""Test KNX binary sensor properly sets last_reported."""
118+
119+
await knx.setup_integration(
120+
{
121+
BinarySensorSchema.PLATFORM: [
122+
{
123+
CONF_NAME: "test",
124+
CONF_STATE_ADDRESS: "1/1/1",
125+
CONF_SYNC_STATE: False,
126+
},
127+
]
128+
}
129+
)
130+
events = async_capture_events(hass, "state_changed")
131+
132+
# receive initial telegram
133+
await knx.receive_write("1/1/1", True)
134+
first_reported = hass.states.get("binary_sensor.test").last_reported
135+
assert len(events) == 1
136+
137+
# receive second telegram with identical payload
138+
freezer.tick(1)
139+
async_fire_time_changed(hass)
140+
await knx.receive_write("1/1/1", True)
141+
142+
assert first_reported != hass.states.get("binary_sensor.test").last_reported
143+
assert len(events) == 1, events # last_reported shall not fire state_changed
144+
145+
112146
async def test_binary_sensor_ignore_internal_state(
113147
hass: HomeAssistant, knx: KNXTestKit
114148
) -> None:

tests/components/knx/test_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async def test_last_reported(
4848
knx: KNXTestKit,
4949
freezer: FrozenDateTimeFactory,
5050
) -> None:
51-
"""Test KNX sensor with last_reported."""
51+
"""Test KNX sensor properly sets last_reported."""
5252

5353
await knx.setup_integration(
5454
{

0 commit comments

Comments
 (0)