Skip to content

Commit 63ae3a0

Browse files
authored
Fetch panels instead of subscribe (home-assistant#3275)
<!-- 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 --> ## 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. -->
1 parent 199c4f5 commit 63ae3a0

File tree

10 files changed

+227
-117
lines changed

10 files changed

+227
-117
lines changed

HomeAssistant.xcodeproj/project.pbxproj

+20-4
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@
416416
11E1639B250B1B760076D612 /* OnboardingStateObservation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E16399250B1B760076D612 /* OnboardingStateObservation.swift */; };
417417
11E5CF8124BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */; };
418418
11E5CF8224BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */; };
419-
11E7C4B02702E03000667342 /* WidgetOpenPageIntent+Observation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */; };
420419
11E99A5027156854003C8A65 /* OnboardingTerminalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11E99A4F27156854003C8A65 /* OnboardingTerminalViewController.swift */; };
421420
11ED43962726599D00B5FD45 /* OnboardingAuthStepModels.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11ED43952726599D00B5FD45 /* OnboardingAuthStepModels.test.swift */; };
422421
11ED439827265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11ED439727265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift */; };
@@ -766,6 +765,8 @@
766765
42CE8FB62B46D14C00C707F9 /* FrontendStrings+Values.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */; };
767766
42CE8FB72B46D14C00C707F9 /* FrontendStrings+Values.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */; };
768767
42CE8FBB2B46DB6200C707F9 /* Assets.swift in Sources */ = {isa = PBXBuildFile; fileRef = B65B15042273188300635D5C /* Assets.swift */; };
768+
42D334272D105990008D8E78 /* AppPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D334262D105990008D8E78 /* AppPanel.swift */; };
769+
42D334282D105990008D8E78 /* AppPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D334262D105990008D8E78 /* AppPanel.swift */; };
769770
42D3E49C2C5BB88F00444BE6 /* WatchBatterySensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */; };
770771
42D3E49D2C5BB88F00444BE6 /* WatchBatterySensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */; };
771772
42D3E4A12C5BCD1100444BE6 /* WatchContext.test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42D3E4A02C5BCD1100444BE6 /* WatchContext.test.swift */; };
@@ -788,6 +789,8 @@
788789
42DD84132B14ACAB00936F16 /* Color+ColorAsset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84122B14ACAB00936F16 /* Color+ColorAsset.swift */; };
789790
42DD84162B14D7AC00936F16 /* WebViewExternalBusMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84142B14D68C00936F16 /* WebViewExternalBusMessage.swift */; };
790791
42DD84192B14D83B00936F16 /* WebViewExternalBusMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DD84182B14D83B00936F16 /* WebViewExternalBusMessageTests.swift */; };
792+
42DE75D32D1061A600FF379F /* PanelsUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DE75D12D105F3000FF379F /* PanelsUpdater.swift */; };
793+
42DE75D42D1061A600FF379F /* PanelsUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DE75D12D105F3000FF379F /* PanelsUpdater.swift */; };
791794
42DEDA9A2C5B926400E9D29D /* AppVersionSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */; };
792795
42DEDA9B2C5B926400E9D29D /* AppVersionSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */; };
793796
42DF6B2D2CCF8A2200D7EC14 /* PermissionRequestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42DF6B2C2CCF8A2200D7EC14 /* PermissionRequestView.swift */; };
@@ -1749,7 +1752,6 @@
17491752
11DE9F3925B614EB0081C0ED /* Application.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = Application.xib; sourceTree = "<group>"; };
17501753
11E16399250B1B760076D612 /* OnboardingStateObservation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingStateObservation.swift; sourceTree = "<group>"; };
17511754
11E5CF8024BBCE1B009AC30F /* ProcessInfo+BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProcessInfo+BackgroundTask.swift"; sourceTree = "<group>"; };
1752-
11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WidgetOpenPageIntent+Observation.swift"; sourceTree = "<group>"; };
17531755
11E99A4F27156854003C8A65 /* OnboardingTerminalViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTerminalViewController.swift; sourceTree = "<group>"; };
17541756
11ED43952726599D00B5FD45 /* OnboardingAuthStepModels.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAuthStepModels.test.swift; sourceTree = "<group>"; };
17551757
11ED439727265B9C00B5FD45 /* OnboardingAuthStepNotify.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAuthStepNotify.test.swift; sourceTree = "<group>"; };
@@ -2072,6 +2074,7 @@
20722074
42CE8FAC2B46C12C00C707F9 /* Domain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Domain.swift; sourceTree = "<group>"; };
20732075
42CE8FAE2B46C3D600C707F9 /* CoreStrings+Values.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreStrings+Values.swift"; sourceTree = "<group>"; };
20742076
42CE8FB42B46CAFD00C707F9 /* FrontendStrings+Values.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FrontendStrings+Values.swift"; sourceTree = "<group>"; };
2077+
42D334262D105990008D8E78 /* AppPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppPanel.swift; sourceTree = "<group>"; };
20752078
42D3E49B2C5BB88F00444BE6 /* WatchBatterySensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchBatterySensor.swift; sourceTree = "<group>"; };
20762079
42D3E4A02C5BCD1100444BE6 /* WatchContext.test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchContext.test.swift; sourceTree = "<group>"; };
20772080
42D3E4A72C5D00AD00444BE6 /* ScriptAppIntent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptAppIntent.swift; sourceTree = "<group>"; };
@@ -2094,6 +2097,7 @@
20942097
42DD84372B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/Frontend.strings; sourceTree = "<group>"; };
20952098
42DD84382B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/InfoPlist.strings; sourceTree = "<group>"; };
20962099
42DD84392B15DC3F00936F16 /* et */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = et; path = et.lproj/Localizable.strings; sourceTree = "<group>"; };
2100+
42DE75D12D105F3000FF379F /* PanelsUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PanelsUpdater.swift; sourceTree = "<group>"; };
20972101
42DEDA992C5B926400E9D29D /* AppVersionSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppVersionSensor.swift; sourceTree = "<group>"; };
20982102
42DF6B2C2CCF8A2200D7EC14 /* PermissionRequestView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PermissionRequestView.swift; sourceTree = "<group>"; };
20992103
42DF6B2E2CCF918D00D7EC14 /* BluetoothPermissionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BluetoothPermissionView.swift; sourceTree = "<group>"; };
@@ -3139,7 +3143,6 @@
31393143
children = (
31403144
11EE9B5624C68F5700404AF8 /* Action+Observation.swift */,
31413145
11A48D8024CA8ADB0021BDD9 /* NotificationCategory+Observation.swift */,
3142-
11E7C4AF2702E03000667342 /* WidgetOpenPageIntent+Observation.swift */,
31433146
);
31443147
path = Observation;
31453148
sourceTree = "<group>";
@@ -4162,6 +4165,15 @@
41624165
path = Domain;
41634166
sourceTree = "<group>";
41644167
};
4168+
42D334252D105981008D8E78 /* Panels */ = {
4169+
isa = PBXGroup;
4170+
children = (
4171+
42DE75D12D105F3000FF379F /* PanelsUpdater.swift */,
4172+
42D334262D105990008D8E78 /* AppPanel.swift */,
4173+
);
4174+
path = Panels;
4175+
sourceTree = "<group>";
4176+
};
41654177
42D3E49F2C5BCCF600444BE6 /* Watch */ = {
41664178
isa = PBXGroup;
41674179
children = (
@@ -4961,6 +4973,7 @@
49614973
426740A42B17348700C1DD73 /* Assets */,
49624974
42CE8FAB2B46C11E00C707F9 /* Domain */,
49634975
42CA28AC2B101D320093B31A /* DesignSystem */,
4976+
42D334252D105981008D8E78 /* Panels */,
49644977
11B38EE0275C545C00205C7B /* Intents */,
49654978
D014EEAA212928EC008EA6F5 /* API */,
49664979
D0FF79C920D7787F0034574D /* ClientEvents */,
@@ -6909,7 +6922,6 @@
69096922
11E99A5027156854003C8A65 /* OnboardingTerminalViewController.swift in Sources */,
69106923
422F951F2CFDF7C5003B7514 /* HAApplicationShortcutItem.swift in Sources */,
69116924
1101568424D770B2009424C9 /* NFCWriter.swift in Sources */,
6912-
11E7C4B02702E03000667342 /* WidgetOpenPageIntent+Observation.swift in Sources */,
69136925
1187DE4624D7E1BD00F0A6A6 /* SimulatorNFCManager.swift in Sources */,
69146926
1185DF96271FBB9800ED7D9A /* OnboardingAuthLogin.swift in Sources */,
69156927
425573E62B5838B600145217 /* MaterialDesignIcons+CarPlay.swift in Sources */,
@@ -7234,6 +7246,7 @@
72347246
11CFD78227364F450082D557 /* Identifier.swift in Sources */,
72357247
11AF4D17249C8083006C74C0 /* With.swift in Sources */,
72367248
42333ADB2D0B1771001E8408 /* EntityRegistryListForDisplay.swift in Sources */,
7249+
42DE75D42D1061A600FF379F /* PanelsUpdater.swift in Sources */,
72377250
11B38EF7275C54A300205C7B /* UpdateSensorsIntentHandler.swift in Sources */,
72387251
1141182B24AFA10900E6525C /* WebhookResponseHandler.swift in Sources */,
72397252
426266462C11B02C0081A818 /* InteractiveImmediateMessages.swift in Sources */,
@@ -7304,6 +7317,7 @@
73047317
11F2F27F258725D300F61F7C /* NotificationAttachmentErrorImage.swift in Sources */,
73057318
B67CE8A622200F220034C1D0 /* HAAPI.swift in Sources */,
73067319
1105CE1D272B9CB300F33BD8 /* ServerManager.swift in Sources */,
7320+
42D334272D105990008D8E78 /* AppPanel.swift in Sources */,
73077321
1141182724AF9A0500E6525C /* WebhookManager.swift in Sources */,
73087322
1104FC9225322C1800B8BE34 /* Dictionary+Additions.swift in Sources */,
73097323
118261F824F8D6B0000795C6 /* SensorProviderDependencies.swift in Sources */,
@@ -7394,6 +7408,7 @@
73947408
11C4629124B14E6B00031902 /* XCGLogger+UNNotification.swift in Sources */,
73957409
110ED56325A563D600489AF7 /* DisplaySensor.swift in Sources */,
73967410
424151FB2CD8EF2200D7A6F9 /* MagicItem+Migration.swift in Sources */,
7411+
42DE75D32D1061A600FF379F /* PanelsUpdater.swift in Sources */,
73977412
11AF4D16249C8083006C74C0 /* With.swift in Sources */,
73987413
11B38EEC275C54A200205C7B /* IntentHandlerFactory.swift in Sources */,
73997414
1182620124F9C3F7000795C6 /* HACoreBlahObject.swift in Sources */,
@@ -7442,6 +7457,7 @@
74427457
116C0C2F267EB90F00A992E4 /* UserDefaultsValueSync.swift in Sources */,
74437458
11A3BD2D26192210005237E6 /* LocalPushManager.swift in Sources */,
74447459
42FCD00C2B9B25D60057783F /* ThreadClientProtocol.swift in Sources */,
7460+
42D334282D105990008D8E78 /* AppPanel.swift in Sources */,
74457461
11C4628824B109C100031902 /* WebhookResponseLocation.swift in Sources */,
74467462
11AF4D19249C8253006C74C0 /* PedometerSensor.swift in Sources */,
74477463
B6872E632226841400C475D1 /* MobileAppRegistrationRequest.swift in Sources */,

Sources/App/AppDelegate.swift

-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
401401
_ = Realm.live()
402402
Action.setupObserver()
403403
NotificationCategory.setupObserver()
404-
WidgetOpenPageIntent.setupObserver()
405404
}
406405

407406
private func setupMenus() {

Sources/App/Scenes/WebViewSceneDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ final class WebViewSceneDelegate: NSObject, UIWindowSceneDelegate {
120120
UIApplication.shared.applicationState == .active
121121
})
122122
Current.periodicAppEntitiesUpdater().setup()
123-
Current.periodicAppEntitiesUpdater().updateAppEntities()
123+
Current.panelsUpdater().update()
124124
}
125125

126126
func windowScene(

Sources/App/Settings/Observation/WidgetOpenPageIntent+Observation.swift

-61
This file was deleted.

Sources/Extensions/Widgets/OpenPage/PageAppEntity.swift

+21-29
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
3030
panels.filter({ panel in
3131
identifiers.contains(id(for: panel, server: server))
3232
}).compactMap { panel in
33-
PageAppEntity(id: id(for: panel, server: server), panel: panel, serverId: server.identifier.rawValue)
33+
PageAppEntity(
34+
id: id(for: panel, server: server),
35+
panel: toHAPanel(appPanel: panel),
36+
serverId: server.identifier.rawValue
37+
)
3438
}
3539
}
3640
}
@@ -42,7 +46,7 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
4246
}).map({ panel in
4347
PageAppEntity(
4448
id: id(for: panel, server: server),
45-
panel: panel,
49+
panel: toHAPanel(appPanel: panel),
4650
serverId: server.identifier.rawValue
4751
)
4852
}))
@@ -54,42 +58,30 @@ struct PageAppEntityQuery: EntityQuery, EntityStringQuery {
5458
.init(.init(stringLiteral: server.info.name), items: panels.map({ panel in
5559
PageAppEntity(
5660
id: id(for: panel, server: server),
57-
panel: panel,
61+
panel: toHAPanel(appPanel: panel),
5862
serverId: server.identifier.rawValue
5963
)
6064
}))
6165
}))
6266
}
6367

64-
func id(for panel: HAPanel, server: Server) -> String {
68+
func id(for panel: AppPanel, server: Server) -> String {
6569
"\(server.identifier.rawValue)-\(panel.path)"
6670
}
6771

68-
private func panels() async throws -> [Server: [HAPanel]] {
69-
await withCheckedContinuation { continuation in
70-
var panelsPerServer: [Server: [HAPanel]] = [:]
71-
var finishedPipesCount = 0
72-
for server in Current.servers.all {
73-
(
74-
Current.diskCache
75-
.value(
76-
for: OpenPageIntentHandler
77-
.cacheKey(serverIdentifier: server.identifier.rawValue)
78-
) as Promise<HAPanels>
79-
).pipe { result in
80-
switch result {
81-
case let .fulfilled(panels):
82-
panelsPerServer[server] = panels.allPanels
83-
case let .rejected(error):
84-
Current.Log.error("Failed to retrieve HAPanels, error: \(error.localizedDescription)")
85-
}
86-
finishedPipesCount += 1
72+
// Since AppPanels came afterwards we need to keep the same
73+
// object as before to not break previously created widgets
74+
private func toHAPanel(appPanel: AppPanel) -> HAPanel {
75+
.init(
76+
icon: appPanel.icon,
77+
title: appPanel.title,
78+
path: appPanel.path,
79+
component: appPanel.component,
80+
showInSidebar: appPanel.showInSidebar
81+
)
82+
}
8783

88-
if finishedPipesCount == Current.servers.all.count {
89-
continuation.resume(returning: panelsPerServer)
90-
}
91-
}
92-
}
93-
}
84+
private func panels() async throws -> [Server: [AppPanel]] {
85+
try AppPanel.panelsPerServer()
9486
}
9587
}

Sources/Shared/Common/Extensions/GRDB+Initialization.swift

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum GRDBDatabaseTable: String {
88
case carPlayConfig
99
case clientEvent
1010
case appEntityRegistryListForDisplay
11+
case appPanel
1112
}
1213

1314
public enum DatabaseTables {
@@ -55,6 +56,16 @@ public enum DatabaseTables {
5556
case entityId
5657
case registry
5758
}
59+
60+
public enum AppPanel: String {
61+
case id
62+
case serverId
63+
case icon
64+
case title
65+
case path
66+
case component
67+
case showInSidebar
68+
}
5869
}
5970

6071
public extension DatabaseQueue {
@@ -94,6 +105,7 @@ public extension DatabaseQueue {
94105
var shouldCreateCarPlayConfig = false
95106
var shouldCreateClientEvent = false
96107
var shouldCreateAppEntityRegistryListForDisplay = false
108+
var shouldCreateAppPanel = false
97109

98110
do {
99111
try database.read { db in
@@ -104,6 +116,8 @@ public extension DatabaseQueue {
104116
shouldCreateClientEvent = try !db.tableExists(GRDBDatabaseTable.clientEvent.rawValue)
105117
shouldCreateAppEntityRegistryListForDisplay = try !db
106118
.tableExists(GRDBDatabaseTable.appEntityRegistryListForDisplay.rawValue)
119+
shouldCreateAppPanel = try !db
120+
.tableExists(GRDBDatabaseTable.appPanel.rawValue)
107121
}
108122
} catch {
109123
let errorMessage = "Failed to check if GRDB tables exist, error: \(error.localizedDescription)"
@@ -210,5 +224,26 @@ public extension DatabaseQueue {
210224
Current.Log.error(errorMessage)
211225
}
212226
}
227+
228+
// AppPanel
229+
if shouldCreateAppPanel {
230+
do {
231+
try database.write { db in
232+
try db.create(table: GRDBDatabaseTable.appPanel.rawValue) { t in
233+
t.primaryKey(DatabaseTables.AppPanel.id.rawValue, .text).notNull()
234+
t.column(DatabaseTables.AppPanel.serverId.rawValue, .text).notNull()
235+
t.column(DatabaseTables.AppPanel.icon.rawValue, .text)
236+
t.column(DatabaseTables.AppPanel.title.rawValue, .text).notNull()
237+
t.column(DatabaseTables.AppPanel.path.rawValue, .text).notNull()
238+
t.column(DatabaseTables.AppPanel.component.rawValue, .text).notNull()
239+
t.column(DatabaseTables.AppPanel.showInSidebar.rawValue, .boolean).notNull()
240+
}
241+
}
242+
} catch {
243+
let errorMessage =
244+
"Failed to create AppPanel GRDB table, error: \(error.localizedDescription)"
245+
Current.Log.error(errorMessage)
246+
}
247+
}
213248
}
214249
}

0 commit comments

Comments
 (0)