Skip to content

Commit 882d804

Browse files
authored
Improv CarPlay states subscription (home-assistant#3236)
<!-- 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 8e3ecdd commit 882d804

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

Podfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pod 'XCGLogger'
3737

3838
# Keep Starscream reference even though HAKit already install it, because it defines our fork with the necessary fix
3939
pod 'Starscream', git: 'https://github.com/bgoncal/starscream', branch: 'ha-URLSession-fix'
40-
pod 'HAKit', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.3'
41-
pod 'HAKit/PromiseKit', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.3'
42-
pod 'HAKit/Mocks', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.3'
40+
pod 'HAKit', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.4'
41+
pod 'HAKit/PromiseKit', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.4'
42+
pod 'HAKit/Mocks', git: 'https://github.com/home-assistant/HAKit.git', tag: '0.4.4'
4343

4444
def test_pods
4545
pod 'OHHTTPStubs/Swift'

Podfile.lock

+12-12
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ PODS:
5454
- GRDB.swift (6.29.3):
5555
- GRDB.swift/standard (= 6.29.3)
5656
- GRDB.swift/standard (6.29.3)
57-
- HAKit (0.4.3):
58-
- HAKit/Core (= 0.4.3)
59-
- HAKit/Core (0.4.3):
57+
- HAKit (0.4.4):
58+
- HAKit/Core (= 0.4.4)
59+
- HAKit/Core (0.4.4):
6060
- Starscream (~> 4.0.4)
61-
- HAKit/Mocks (0.4.3):
61+
- HAKit/Mocks (0.4.4):
6262
- HAKit/Core
63-
- HAKit/PromiseKit (0.4.3):
63+
- HAKit/PromiseKit (0.4.4):
6464
- HAKit/Core
6565
- PromiseKit (~> 8.1.1)
6666
- Improv-iOS (0.0.6)
@@ -133,9 +133,9 @@ DEPENDENCIES:
133133
- Eureka (from `https://github.com/xmartlabs/Eureka`, branch `master`)
134134
- FirebaseMessaging
135135
- GRDB.swift (from `https://github.com/groue/GRDB.swift.git`, tag `v6.29.3`)
136-
- HAKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
137-
- HAKit/Mocks (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
138-
- HAKit/PromiseKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
136+
- HAKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.4`)
137+
- HAKit/Mocks (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.4`)
138+
- HAKit/PromiseKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.4`)
139139
- Improv-iOS (~> 0.0.6)
140140
- KeychainAccess
141141
- MBProgressHUD (~> 1.2.0)
@@ -209,7 +209,7 @@ EXTERNAL SOURCES:
209209
:tag: v6.29.3
210210
HAKit:
211211
:git: https://github.com/home-assistant/HAKit.git
212-
:tag: 0.4.3
212+
:tag: 0.4.4
213213
ObjectMapper:
214214
:branch: master
215215
:git: https://github.com/tristanhimmelman/ObjectMapper.git
@@ -244,7 +244,7 @@ CHECKOUT OPTIONS:
244244
:tag: v6.29.3
245245
HAKit:
246246
:git: https://github.com/home-assistant/HAKit.git
247-
:tag: 0.4.3
247+
:tag: 0.4.4
248248
ObjectMapper:
249249
:commit: a593b4d647a970b3d184d046f8f52b945083ccf9
250250
:git: https://github.com/tristanhimmelman/ObjectMapper.git
@@ -273,7 +273,7 @@ SPEC CHECKSUMS:
273273
GoogleDataTransport: ea169759df570f4e37bdee1623ec32a7e64e67c4
274274
GoogleUtilities: c2bdc4cf2ce786c4d2e6b3bcfd599a25ca78f06f
275275
GRDB.swift: 764235dee4a4fbe3ddcb4f85c61752e8d8da9ad0
276-
HAKit: 28aad2aa3691fc0a51b039e5f960036edba6baf6
276+
HAKit: d5c0028afd8c5053537fcbc4b0ed2e6e27d3bc2b
277277
Improv-iOS: 8973990c1b1f3e3aed7fc600c8efce95359cadd0
278278
KeychainAccess: c0c4f7f38f6fc7bbe58f5702e25f7bd2f65abf51
279279
MBProgressHUD: 3ee5efcc380f6a79a7cc9b363dd669c5e1ae7406
@@ -299,6 +299,6 @@ SPEC CHECKSUMS:
299299
XCGLogger: 1943831ef907df55108b0b18657953f868de973b
300300
ZIPFoundation: d170fa8e270b2a32bef9dcdcabff5b8f1a5deced
301301

302-
PODFILE CHECKSUM: 51870af5d4e6bda2dc7a4ef9bd4e81df4d169ac7
302+
PODFILE CHECKSUM: 9bace27ccfcc32ab1a602221366738bacf658272
303303

304304
COCOAPODS: 1.15.2

Sources/App/Scenes/CarPlaySceneDelegate.swift

+21-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ class CarPlaySceneDelegate: UIResponder {
2020
private var serversListTemplate: (any CarPlayTemplateProvider)?
2121
private var quickAccessListTemplate: (any CarPlayTemplateProvider)?
2222
private var areasZonesListTemplate: (any CarPlayTemplateProvider)?
23+
private var includedDomains: [Domain] = [
24+
.light,
25+
.button,
26+
.cover,
27+
.inputBoolean,
28+
.inputButton,
29+
.lock,
30+
.scene,
31+
.script,
32+
.switch,
33+
]
2334

2435
private var allTemplates: [any CarPlayTemplateProvider] {
2536
[
@@ -101,7 +112,16 @@ class CarPlaySceneDelegate: UIResponder {
101112
private func subscribeToEntitiesChanges() {
102113
guard let server = Current.servers.server(forServerIdentifier: preferredServerId) ?? Current.servers.all.first else { return }
103114
entitiesSubscriptionToken?.cancel()
104-
entitiesSubscriptionToken = Current.api(for: server)?.connection.caches.states()
115+
116+
var filter: [String: Any] = [:]
117+
if server.info.version > .canSubscribeEntitiesChangesWithFilter {
118+
filter = [
119+
"include": [
120+
"domains": includedDomains.map(\.rawValue),
121+
],
122+
]
123+
}
124+
entitiesSubscriptionToken = Current.api(for: server)?.connection.caches.states(filter)
105125
.subscribe { [weak self] _, states in
106126
self?.allTemplates.forEach {
107127
$0.entitiesStateChange(entities: states)

Sources/CarPlay/Templates/Areas/CarPlayAreasViewModel.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ final class CarPlayAreasViewModel {
99
weak var templateProvider: CarPlayAreasZonesTemplate?
1010

1111
var entitiesListTemplate: CarPlayEntitiesListTemplate?
12+
private var entities: HACachedStates?
1213

1314
private var preferredServerId: String {
1415
prefs.string(forKey: CarPlayServersListTemplate.carPlayPreferredServerKey) ?? ""
@@ -41,6 +42,11 @@ final class CarPlayAreasViewModel {
4142
})
4243
}
4344

45+
func entitiesStateChange(entities: HACachedStates) {
46+
self.entities = entities
47+
entitiesListTemplate?.entitiesStateChange(entities: entities)
48+
}
49+
4450
private func fetchEntitiesForAreas(_ areas: [HAAreaResponse], server: Server) {
4551
guard let connection = Current.api(for: server)?.connection else {
4652
Current.Log.error("No API available to fetch entities for areas")
@@ -120,12 +126,12 @@ final class CarPlayAreasViewModel {
120126
// swiftlint:enable cyclomatic_complexity
121127

122128
private func listItemHandler(area: HAAreaResponse, entityIdsForAreaId: [String], server: Server) {
123-
guard let entitiesCachedStates = Current.api(for: server)?.connection.caches.states().value else { return }
129+
guard let entities else { return }
124130
entitiesListTemplate = CarPlayEntitiesListTemplate.build(
125131
title: area.name,
126132
filterType: .areaId(entityIds: entityIdsForAreaId),
127133
server: server,
128-
entitiesCachedStates: entitiesCachedStates
134+
entitiesCachedStates: entities
129135
)
130136
guard let entitiesListTemplate else { return }
131137
templateProvider?.presentEntitiesList(template: entitiesListTemplate)

Sources/CarPlay/Templates/Areas/CarPlayAreasZonesTemplate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class CarPlayAreasZonesTemplate: CarPlayTemplateProvider {
4040
}
4141

4242
func entitiesStateChange(entities: HACachedStates) {
43-
viewModel.entitiesListTemplate?.entitiesStateChange(entities: entities)
43+
viewModel.entitiesStateChange(entities: entities)
4444
}
4545

4646
func update() {

0 commit comments

Comments
 (0)