Skip to content

Keep the session when the bridge refuses a sensor subscription - #182

Merged
michaelarnauts merged 3 commits into
masterfrom
rpdo-not-allowed-keeps-session
Nov 23, 2026
Merged

michaelarnauts merged 3 commits into
masterfrom
rpdo-not-allowed-keeps-session

Conversation

@michaelarnauts

@michaelarnauts michaelarnauts commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Some bridges answer a second CnRpdoRequest for a sensor that is already subscribed on the session with NOT_ALLOWED. The session stays valid: other requests and the sensor values keep working (tested in michaelarnauts/home-assistant-comfoconnect#265). Since #155, _send treated every NOT_ALLOWED as a lost session and closed the connection. The sensors that were still subscribing then failed, register_sensor dropped them, and most Home Assistant entities stayed unavailable.

Changes in ComfoConnect:

  • _send: a NOT_ALLOWED on a CnRpdoRequest only fails that request, the connection stays open. Every other request, and OTHER_SESSION, still closes it. A session that is really gone (the case of Reconnect when the bridge drops our session without closing it #155) refuses every request, so the heartbeat or the next other request still notices it.

  • register_sensor:

    • A sensor that is already registered isn't subscribed again. Its latest value goes to the sensor callback right away, so a second caller gets a value without waiting for a change. A sensor that is still held emits its value when the hold ends, like before.
    • A second registration while the first subscription still waits for the bridge (Home Assistant registers sensor 56 from two entities at the same time) waits for it. When that subscription failed, the second registration subscribes itself, so it isn't left with a sensor that was dropped.
    • On NOT_ALLOWED the sensor is kept, with a debug log, and no exception is raised. Either the session already has the sensor (a duplicate, or a resumed session that kept the subscriptions of the previous one, as in "The bridge dropped our session" during setup, most entities fail to load (works on 0.3.0) #181), or the session is gone and the next session subscribes to it again. Other errors still drop the sensor and raise, like before.
  • _start_session: a NOT_ALLOWED when subscribing to the sensors again after a reconnect is logged at debug instead of as a warning. A resumed session refuses every sensor it still has, which gave a warning per sensor on every reconnect.

docs/PROTOCOL.md now describes the duplicate subscription, when a LAN C resumes a session, and the exception for CnRpdoRequestType.

Not covered: on a resumed session, the bridge pushes the current values before we register the sensors, and those are ignored. Such a sensor gets its first value on its next change.

Fixes #181.

Testing

  • New tests with the fake bridge, which fail without the change:
    • test_refused_subscription_keeps_session: a subscription refused with NOT_ALLOWED keeps the session and the sensor, and its values still reach the callback.
    • test_lost_session_refused_subscription: on a lost session, the refused subscription keeps the sensor and the connection, then the heartbeat reconnects and the new session subscribes to it.
    • test_register_sensor_twice: one subscription, and the second registration gets the latest value at once.
    • test_register_sensor_twice_without_value: a second registration during the hold doesn't restart the hold or emit anything, and the value comes once.
    • test_refused_resubscription_after_reconnect: a refused subscription after a reconnect keeps the sensor and logs no warning.
    • test_register_same_sensor_concurrently, ..._first_fails and test_cancelled_registration_while_waiting: a second registration during the first subscription waits for it and sends nothing, subscribes itself when the first one failed, and can be cancelled without affecting the first one.
  • New tests that already passed before: OTHER_SESSION on a subscription still starts a new session, and NOT_ALLOWED on an unsubscription keeps the session.
  • The tests of Reconnect when the bridge drops our session without closing it #155 still pass. pytest (408 passed), pylint 10/10, black --check aiocomfoconnect/*.py.
  • On a real LAN C (R1.5.1), which accepts a duplicate subscription:
    • Registering sensor 56 twice sent one subscription, and the second registration emitted the value at once.
    • We let another client take over the session with the same uuid and leave. The bridge then refused a subscription with NOT_ALLOWED: the sensor was kept and the connection stayed open. The heartbeat got NOT_ALLOWED too and reconnected, and the new session subscribed to both sensors and received their values.

Some bridges answer a second CnRpdoRequest for a sensor that is already
subscribed on the session with NOT_ALLOWED, and keep the session. Since
#155, any NOT_ALLOWED closed the connection, so the sensors that were
still subscribing failed and most entities became unavailable.

A NOT_ALLOWED on a CnRpdoRequest now only fails that request. A session
that is really gone is still detected by the heartbeat and the next other
request, which get NOT_ALLOWED too. register_sensor keeps a refused
sensor, and doesn't subscribe again to a sensor that is already
registered: it emits its latest value instead.
A second register_sensor for a sensor whose subscription was still
waiting for the bridge returned at once. When that subscription then
failed, the sensor was dropped and the second caller never got values.
It now waits for the first subscription, and subscribes itself when that
failed.

Also document the duplicate subscription and the resumed session
behaviour, and test OTHER_SESSION on a subscription and NOT_ALLOWED on
an unsubscription.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"The bridge dropped our session" during setup, most entities fail to load (works on 0.3.0)

1 participant