Added timeout on request to work-around SSE disconnected #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As described in some submitted bugs the bridge connection sometimes gets lost.
I personally have this issue 'sometimes', but certainly every time my router auto-restarts (1x a week).
Bugs references I found :
I found out that the HueMagic engine has its own watchdog to check whether connection is up or not and re-connect if necessary.
However, it is not 100% reliable, and this is caused by the way the Hue bridge works with SSE (Server Side Events).
But the watchdog does 'only' check whether the Hue bridge is up, not if it is still registered as a subscriber...
There are 2 limitations to this way of working :
I was able to work-around the second issue : there was no time-out on calls made, which means even if the bridge was unreachable for a few minutes, it did not trigger a re-connection.
by adding a time-out of 3s to requests send, it solved the issue. The time-out could be set as a param in config but since I never have any request with a >1s delay, it should be OK for all.
I also -personally- did not encounter any disconnection at all... but it remains not 100% SSE-disconnection-proof ...
Note: Normally, most SSE providers have a 'keep-alive' concept which sends regularly an event to subscriber so that it is able to know it is still up...
... an act accordingly by trying to re-subscribe when the 'keep alive' message stops coming in.
The Hue Bridge does not offer any 'keep alive' mechanism :-(
It seems other tools would also like it... I even found this work-around which uses geofence to ensure responses should always be regularly sent as a keep-alive:
https://github.com/home-assistant-libs/aiohue/blob/58b24e2ad0dd212f34a780689d0d90aa050a80dd/aiohue/v2/controllers/events.py#L284
(which is quite complex and which I -personally- would not be able to integrate into current HueMagic engine)
@mauricedominic : could be interesting to merge the the main active fork too I guess