Timestamp relay changes with a read-only state listener
Polling only sees the relay's current state and last change, so two or more changes between polls (e.g. an on/off round trip) made the lag estimate unknown and paused learning for about half an hour. Subscribe to the relay's state changes (read-only) and integrate every recorded change between polls. Because HA delivers these events one loop iteration late, the current state stays authoritative and fills in a change not yet recorded. Unknown periods (non on/off states, missed transitions, event overflow) still restart the estimate with an explicit error bound. Without the listener, behavior is as before. HA skips unload callbacks after unexpected setup errors, so setup now removes the listener itself on any failure and registers it for unload only once setup succeeds. Tested on HA Core 2025.12.0 and 2026.2.3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
af0a564279
commit
955c679f5e
6 files changed
+185
-22
No files matched your search
@@ -148,6 +148,17 @@ class ModelTests(unittest.TestCase):
|
||||
lag, half_width = m.lag_from_time_in_state(command, 900)
|
||||
self.assertLessEqual(abs(m.advance_lag(start, command, 900) - lag), half_width + 1e-12)
|
||||
|
||||
def test_relay_segments(self):
|
||||
seg = m.relay_segments
|
||||
self.assertEqual(seg(False, 0, [], 300), [(False, 300)])
|
||||
self.assertEqual(seg(False, 0, [(60, 'on'), (200, 'off')], 300),
|
||||
[(False, 60), (True, 140), (False, 100)])
|
||||
self.assertEqual(seg(True, 0, [(300, 'off')], 300), [(True, 300), (False, 0)])
|
||||
for changes in ([(60, 'unavailable')], [(60, 'off')], [(200, 'on'), (60, 'off')],
|
||||
[(-1, 'on')], [(301, 'on')]):
|
||||
with self.subTest(changes=changes):
|
||||
self.assertIsNone(seg(False, 0, changes, 300))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user