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

Improve camera tile add/remove event handling #3954

Merged

Conversation

jpelgrom
Copy link
Member

@jpelgrom jpelgrom commented Oct 19, 2023

Summary

Following reports by @dshokouhi in chats and on Discord of camera tiles resetting unexpectedly or not being removed from the app, I tested adding/removing camera tiles again.

For camera tiles resetting, this can be caused by onTileAddEvent or onTileRemoveEvent. This PR updates onTileAddEvent to not overwrite tile data if it already exists, hopefully fixing this (remove+add is annoying, the app would have to add soft delete).

For camera tiles sticking around, during testing I noticed that the system was often destroying the service almost immediately after the add/remove events were sent. As a result, code started in the service's coroutine was cancelled before completing, and a camera tile wasn't added to the database or not removed from the database. I could semi-reliably get it to destroy the service within several milliseconds of adding/removing.
Logcat example showing 10ms between event and destroy
To prevent this, making sure the database work is completed before completing the callback function seems to be the only reliable way (runBlocking). This PR changes it for the camera tiles, during testing it still finishes in <100ms and the app isn't visible at the time (system plays animation) so blocking isn't an issue.

Screenshots

n/a

Link to pull request in Documentation repository

n/a

Any other notes

If accepted I intend to update other tile services to do the same later (there haven't been any complaints yet, but they use other storage which may behave differently on cancellation).

 - If there is already a camera tile with the ID of the tile in `onTileAddEvent` stored in the database, don't overwrite it with a new blank tile as the user configuration might be lost.
 - Run add/remove event code blocking to prevent the system immediately destroying the service and as a result cancelling the work to save tile data in the database. Without the blocking code I could semi-reliably get it to destroy the service within several milliseconds of adding/removing, often preventing the database code from running.
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.

Thanks for the fix 🙏 ! Tough to actually test my issue since theres no direct path to reproduce and it can be days in between but I think the approach is correct and should solve some issues we've seen reported around this.

@JBassett JBassett merged commit a94c90e into home-assistant:master Oct 20, 2023
4 checks passed
@jpelgrom jpelgrom deleted the wear-camera-tile-keep-existing branch October 20, 2023 21:56
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.

None yet

3 participants