Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix websocket stays subscribed after widget edit due to multiple instances #2753

Merged

Conversation

jpelgrom
Copy link
Member

@jpelgrom jpelgrom commented Aug 3, 2022

Summary

When a widget is added or edited, the system will send a broadcast to a new instance of the widget class (this is easy to spot when debugging or adding additional logging). Because there is a websocket connection to keep widgets updated, we should use a singleton variable to only start a subscription in one of these instances + subscribe for screen off broadcasts in that instance. This will stop the connection from staying active after the screen is turned off due to multiple instances of the class listening and only one receiving screen off events.

When the screen is turned on, only one instance of the widget provider is needed to update all existing widgets and created by the initial broadcast receiver in the app class.

Fixes #2750

Screenshots

n/a

Link to pull request in Documentation repository

n/a

Any other notes

Discussion

…ances

 - When a widget is edited, the system will send a broadcast to _a new instance_ of the widget class. Because there is a websocket connection to keep widgets updated, the new instance of the class should also receive events to unsubscribe.
@jpelgrom jpelgrom marked this pull request as draft August 3, 2022 19:28
 - Only actually start listening for updates in one of the instances in order to prevent unnecessary duplicate widget updates
@jpelgrom
Copy link
Member Author

jpelgrom commented Aug 3, 2022

I've updated the PR to use the companion object to only subscribe once, to prevent widgets from being updated multiple times after editing as well.

(I believe you're not supposed to put coroutine contexts in the companion object? Otherwise we could put mainScope and entityUpdates in there.)

@jpelgrom jpelgrom marked this pull request as ready for review August 3, 2022 19:46
Copy link
Member

@dshokouhi dshokouhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the debug APK and it looks good to me.

@JBassett JBassett merged commit 7953f81 into home-assistant:master Aug 3, 2022
@jpelgrom jpelgrom deleted the fix-widget-websocket-active-2750 branch August 4, 2022 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template widget causes app to continue receiving events while screen is off after editing
4 participants