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

Replace "getStates" by more efficient entities subscription (compressed states) #49

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

bgoncal
Copy link
Member

@bgoncal bgoncal commented Feb 21, 2024

This depends on #48

Copy link

codecov bot commented Feb 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.87%. Comparing base (9cde64f) to head (ab2f0f7).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #49      +/-   ##
==========================================
- Coverage   99.93%   99.87%   -0.06%     
==========================================
  Files          46       50       +4     
  Lines        1499     1593      +94     
==========================================
+ Hits         1498     1591      +93     
- Misses          1        2       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zacwest zacwest changed the base branch from main to subscribe-without-populate February 22, 2024 05:11
/// Logic from: https://github.com/home-assistant/home-assistant-js-websocket/blob/master/lib/entities.ts
static func processUpdates(info: HACacheTransformInfo<HACompressedStatesUpdates, HACachedStates?>)
-> HACachedStates {
var states = info.current ?? .init(entities: [])
Copy link
Member

Choose a reason for hiding this comment

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

What happens if we're starting a new subscription and we get back the 'initial' value set. Do we handle the case where: old value contains entity X, new subscription's first result doesn't include X - does X continue to show? I think it does from how this code path works.

Copy link
Member

Choose a reason for hiding this comment

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

This may be a reason to 'populate' (but always set the value to nil) the cache for this particular state, which might require changes to the other PR, too. Or perhaps a flag to let it know, in this case, that it's coming from stale data. Hmm.

Copy link
Member Author

@bgoncal bgoncal Feb 28, 2024

Choose a reason for hiding this comment

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

If X is not included in the second update it's going to be removed by the "processUpdate" right? during the "subtractions" check on line 41

In the end this should be almost instant, all these changes are just to accommodate the fact that the new entities subscription sends a message in between the call and sending the entities state

Copy link
Member

Choose a reason for hiding this comment

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

If the old value is from an hour ago, the initial subscription isn't going to include it as a deletion because it doesn't know you want to delete it.

do {
states[entityId] = try updates.asEntity(entityId: entityId)
} catch {
HAGlobal.log(.error, "Failed adding new entity: \(error)")
Copy link
Member

Choose a reason for hiding this comment

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

You should still invoke this case in tests, if you can.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did a little workaround temporaraly implementing logs and then resetting it, let me know what you think

}
}

public struct CompressedEntityDiff: HADataDecodable {
Copy link
Member

Choose a reason for hiding this comment

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

Do many of these need to be public?

Also: prefix

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes because HACompressedStatesUpdates needs to be public so HATypedSubscription.subscribeEntities can also be public

@home-assistant home-assistant bot marked this pull request as draft February 22, 2024 05:15
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@bgoncal bgoncal force-pushed the subscribe-without-populate branch from 2fd57b6 to f682702 Compare February 22, 2024 10:08
Base automatically changed from subscribe-without-populate to main February 28, 2024 09:43
@bgoncal bgoncal force-pushed the new-entities-subscription-2 branch 9 times, most recently from 6f15a35 to 046e64e Compare February 28, 2024 13:38
@bgoncal bgoncal force-pushed the new-entities-subscription-2 branch from 046e64e to 335db2d Compare February 28, 2024 13:58
@bgoncal bgoncal marked this pull request as ready for review February 28, 2024 14:23
@home-assistant home-assistant bot requested a review from zacwest February 28, 2024 14:23
@bgoncal bgoncal force-pushed the new-entities-subscription-2 branch from ebcc786 to 4a57ed1 Compare February 28, 2024 16:37
@bgoncal bgoncal force-pushed the new-entities-subscription-2 branch from 4a57ed1 to 26c5f37 Compare February 29, 2024 09:28
Copy link
Member

@zacwest zacwest left a comment

Choose a reason for hiding this comment

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

Very ergonomic!

@bgoncal bgoncal merged commit 5070f2e into main Mar 6, 2024
4 of 5 checks passed
@bgoncal bgoncal deleted the new-entities-subscription-2 branch March 6, 2024 10:00
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.

2 participants