-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Periodic update app entities instead of subscribing to state updates (#…
…3231) <!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> This PR will change the approach of how the app keeps it's data updated by not subscribing to all state changes and only periodic updating it's app entities instead. This will avoid several issues where users experience their app to freeze due to 9k entities updating every second and notifying the app. ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
- Loading branch information
Showing
11 changed files
with
106 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Sources/Shared/Environment/PeriodicAppEntitiesModelUpdater.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import HAKit | ||
|
||
public protocol PeriodicAppEntitiesModelUpdaterProtocol { | ||
func setup() | ||
func stop() | ||
func updateAppEntities() | ||
} | ||
|
||
final class PeriodicAppEntitiesModelUpdater: PeriodicAppEntitiesModelUpdaterProtocol { | ||
static var shared = PeriodicAppEntitiesModelUpdater() | ||
|
||
private var requestTokens: [HACancellable?] = [] | ||
private var timer: Timer? | ||
|
||
func setup() { | ||
startUpdateTimer() | ||
} | ||
|
||
func stop() { | ||
cancelOnGoingRequests() | ||
timer?.invalidate() | ||
} | ||
|
||
func updateAppEntities() { | ||
cancelOnGoingRequests() | ||
Current.servers.all.forEach { server in | ||
guard server.info.connection.activeURL() != nil else { return } | ||
let requestToken = Current.api(for: server)?.connection.send( | ||
HATypedRequest<[HAEntity]>.fetchStates(), | ||
completion: { result in | ||
switch result { | ||
case let .success(entities): | ||
Current.appEntitiesModel().updateModel(Set(entities), server: server) | ||
case let .failure(error): | ||
Current.Log.error("Failed to fetch states: \(error)") | ||
} | ||
} | ||
) | ||
requestTokens.append(requestToken) | ||
} | ||
} | ||
|
||
private func cancelOnGoingRequests() { | ||
requestTokens.forEach { $0?.cancel() } | ||
requestTokens = [] | ||
} | ||
|
||
// Start timer that updates app entities every 5 minutes | ||
private func startUpdateTimer() { | ||
timer?.invalidate() | ||
timer = Timer.scheduledTimer(withTimeInterval: 5 * 60, repeats: true) { [weak self] _ in | ||
self?.updateAppEntities() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters