Skip to content

Subscribe only to domains that the app has usage for #3154

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

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pod 'Alamofire', '~> 5.8'
pod 'Communicator', git: 'https://github.com/zacwest/Communicator.git', branch: 'observation-memory-direct'
pod 'KeychainAccess'
pod 'ObjectMapper', git: 'https://github.com/tristanhimmelman/ObjectMapper.git', branch: 'master'
pod 'PromiseKit'
pod 'PromiseKit', '~> 8.1.1'
pod 'Improv-iOS', '~> 0.0.6'
pod 'SFSafeSymbols', '~> 5.3'

Expand All @@ -37,9 +37,9 @@ pod 'XCGLogger'

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

def test_pods
pod 'OHHTTPStubs/Swift'
Expand Down
44 changes: 22 additions & 22 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ PODS:
- GRDB.swift (6.29.3):
- GRDB.swift/standard (= 6.29.3)
- GRDB.swift/standard (6.29.3)
- HAKit (0.4.2):
- HAKit/Core (= 0.4.2)
- HAKit/Core (0.4.2):
- HAKit (0.4.3):
- HAKit/Core (= 0.4.3)
- HAKit/Core (0.4.3):
- Starscream (~> 4.0.4)
- HAKit/Mocks (0.4.2):
- HAKit/Mocks (0.4.3):
- HAKit/Core
- HAKit/PromiseKit (0.4.2):
- HAKit/PromiseKit (0.4.3):
- HAKit/Core
- PromiseKit (~> 6.13)
- PromiseKit (~> 8.1.1)
- Improv-iOS (0.0.6)
- KeychainAccess (4.2.2)
- MBProgressHUD (1.2.0)
Expand All @@ -88,14 +88,14 @@ PODS:
- OHHTTPStubs/OHPathHelpers (9.1.0)
- OHHTTPStubs/Swift (9.1.0):
- OHHTTPStubs/Default
- PromiseKit (6.18.1):
- PromiseKit/CorePromise (= 6.18.1)
- PromiseKit/Foundation (= 6.18.1)
- PromiseKit/UIKit (= 6.18.1)
- PromiseKit/CorePromise (6.18.1)
- PromiseKit/Foundation (6.18.1):
- PromiseKit (8.1.2):
- PromiseKit/CorePromise (= 8.1.2)
- PromiseKit/Foundation (= 8.1.2)
- PromiseKit/UIKit (= 8.1.2)
- PromiseKit/CorePromise (8.1.2)
- PromiseKit/Foundation (8.1.2):
- PromiseKit/CorePromise
- PromiseKit/UIKit (6.18.1):
- PromiseKit/UIKit (8.1.2):
- PromiseKit/CorePromise
- PromisesObjC (2.1.1)
- ReachabilitySwift (5.0.0)
Expand Down Expand Up @@ -133,15 +133,15 @@ DEPENDENCIES:
- Eureka (from `https://github.com/xmartlabs/Eureka`, branch `master`)
- FirebaseMessaging
- GRDB.swift (from `https://github.com/groue/GRDB.swift.git`, tag `v6.29.3`)
- HAKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.2`)
- HAKit/Mocks (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.2`)
- HAKit/PromiseKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.2`)
- HAKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
- HAKit/Mocks (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
- HAKit/PromiseKit (from `https://github.com/home-assistant/HAKit.git`, tag `0.4.3`)
- Improv-iOS (~> 0.0.6)
- KeychainAccess
- MBProgressHUD (~> 1.2.0)
- ObjectMapper (from `https://github.com/tristanhimmelman/ObjectMapper.git`, branch `master`)
- OHHTTPStubs/Swift
- PromiseKit
- PromiseKit (~> 8.1.1)
- ReachabilitySwift
- RealmSwift
- SFSafeSymbols (~> 5.3)
Expand Down Expand Up @@ -209,7 +209,7 @@ EXTERNAL SOURCES:
:tag: v6.29.3
HAKit:
:git: https://github.com/home-assistant/HAKit.git
:tag: 0.4.2
:tag: 0.4.3
ObjectMapper:
:branch: master
:git: https://github.com/tristanhimmelman/ObjectMapper.git
Expand Down Expand Up @@ -244,7 +244,7 @@ CHECKOUT OPTIONS:
:tag: v6.29.3
HAKit:
:git: https://github.com/home-assistant/HAKit.git
:tag: 0.4.2
:tag: 0.4.3
ObjectMapper:
:commit: a593b4d647a970b3d184d046f8f52b945083ccf9
:git: https://github.com/tristanhimmelman/ObjectMapper.git
Expand Down Expand Up @@ -273,15 +273,15 @@ SPEC CHECKSUMS:
GoogleDataTransport: ea169759df570f4e37bdee1623ec32a7e64e67c4
GoogleUtilities: c2bdc4cf2ce786c4d2e6b3bcfd599a25ca78f06f
GRDB.swift: 764235dee4a4fbe3ddcb4f85c61752e8d8da9ad0
HAKit: 92e9bd30717270df15025184b608b7902a773286
HAKit: 28aad2aa3691fc0a51b039e5f960036edba6baf6
Improv-iOS: 8973990c1b1f3e3aed7fc600c8efce95359cadd0
KeychainAccess: c0c4f7f38f6fc7bbe58f5702e25f7bd2f65abf51
MBProgressHUD: 3ee5efcc380f6a79a7cc9b363dd669c5e1ae7406
nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431
ObjcExceptionBridging: c30e00eb3700467e695faeea30e26e18bd445001
ObjectMapper: 5a6c9f063ef67e7fc0d7eb8958919e3121396a96
OHHTTPStubs: 90eac6d8f2c18317baeca36698523dc67c513831
PromiseKit: 49d70c53d5d20e346beaea4b276b5dd2ab446bb4
PromiseKit: e057b5b3c0ce9a0145674d633930924234104ee3
PromisesObjC: ab77feca74fa2823e7af4249b8326368e61014cb
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
Realm: e6b04f15a814cb22aae621e7e15f2ccb27ac8f95
Expand All @@ -299,6 +299,6 @@ SPEC CHECKSUMS:
XCGLogger: 1943831ef907df55108b0b18657953f868de973b
ZIPFoundation: d170fa8e270b2a32bef9dcdcabff5b8f1a5deced

PODFILE CHECKSUM: 30b5c6369c33ce4a4fa8239d093141d7b7cba3f0
PODFILE CHECKSUM: 51870af5d4e6bda2dc7a4ef9bd4e81df4d169ac7

COCOAPODS: 1.15.2
93 changes: 0 additions & 93 deletions Sources/App/AppEntitiesObserver.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Sources/App/Scenes/CarPlaySceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class CarPlaySceneDelegate: UIResponder {
private func subscribeToEntitiesChanges() {
guard let server = Current.servers.server(forServerIdentifier: preferredServerId) ?? Current.servers.all.first else { return }
entitiesSubscriptionToken?.cancel()
entitiesSubscriptionToken = Current.api(for: server)?.connection.caches.states
entitiesSubscriptionToken = Current.api(for: server)?.connection.caches.states()
.subscribe { [weak self] _, states in
self?.allTemplates.forEach {
$0.entitiesStateChange(entities: states)
Expand Down
2 changes: 1 addition & 1 deletion Sources/App/Settings/Eureka/AccountRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ final class HomeAssistantAccountRow: Row<AccountCell>, RowType {
}
}

avatarSubscription = api.connection.caches.states.subscribe { [weak self] _, states in
avatarSubscription = api.connection.caches.states().subscribe { [weak self] _, states in
firstly { () -> Guarantee<Set<HAEntity>> in
Guarantee.value(states.all)
}.map { states throws -> HAEntity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final class CarPlayAreasViewModel {
// swiftlint:enable cyclomatic_complexity

private func listItemHandler(area: HAAreaResponse, entityIdsForAreaId: [String], server: Server) {
guard let entitiesCachedStates = Current.api(for: server)?.connection.caches.states.value else { return }
guard let entitiesCachedStates = Current.api(for: server)?.connection.caches.states().value else { return }
entitiesListTemplate = CarPlayEntitiesListTemplate.build(
title: area.name,
filterType: .areaId(entityIds: entityIdsForAreaId),
Expand Down
8 changes: 6 additions & 2 deletions Sources/CarPlay/Templates/HAEntity+CarPlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension HAEntity {
default:
break
}
case .none, .sensor:
case .none, .sensor, .binarySensor, .zone:
break
}
if let request {
Expand Down Expand Up @@ -99,7 +99,11 @@ extension HAEntity {
case .switch:
image = getSwitchIcon()
case .sensor:
image = MaterialDesignIcons.thermometerIcon
image = MaterialDesignIcons.eyeIcon
case .binarySensor:
image = MaterialDesignIcons.eyeIcon
case .zone:
image = MaterialDesignIcons.mapIcon
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ final class CarPlayQuickAccessTemplate: CarPlayTemplateProvider {
case .entity:
guard let server = Current.servers.server(forServerIdentifier: preferredServerId) ?? Current.servers.all
.first, let connection = Current.api(for: server)?.connection,
let item = connection.caches.states.value?.all.first(where: {
let item = connection.caches.states().value?.all.first(where: {
$0.entityId == magicItem.id
}) else { return .init(text: "", detailText: "") }

Expand Down
43 changes: 41 additions & 2 deletions Sources/Shared/API/Models/ModelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ public class ModelManager: ServerObserver {
private var subscribedSubscriptions = [SubscribeDefinition]()
private var cleanupDefinitions = [CleanupDefinition]()

private static var includedDomains: [Domain] = {
// Mac does not need all domains given it does not have all features as iOS (CarPlay, Watch)
#if targetEnvironment(macCatalyst)
[
.cover,
.light,
.scene,
.script,
.switch,
.sensor,
.binarySensor,
.zone,
]
#else
[
.button,
.cover,
.inputBoolean,
.inputButton,
.light,
.lock,
.scene,
.script,
.switch,
.sensor,
.binarySensor,
.zone,
]
#endif
}()

public var workQueue: DispatchQueue = .global(qos: .userInitiated)
static var isAppInForeground: () -> Bool = { false }

Expand Down Expand Up @@ -205,12 +236,20 @@ public class ModelManager: ServerObserver {
.init(subscribe: { connection, server, queue, manager in
// working around a swift compiler crash, xcode 12.4
let someManager = manager

var filter: [String: Any] = [:]
var lastEntities = Set<HAEntity>()
var lastUpdate: Date?

if server.info.version > .canSubscribeEntitiesChangesWithFilter {
filter = [
"include": [
"domains": ModelManager.includedDomains.map(\.rawValue),
],
]
}

return [
connection.caches.states.subscribe { [weak someManager] token, value in
connection.caches.states(filter).subscribe { [weak someManager] token, value in
queue.async {
guard let manager = someManager else {
token.cancel()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/API/WebSocket/HAPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public extension HATypedRequest {
}

private struct HAPanelsCacheKey: HACacheKey {
static func create(connection: HAConnection) -> HACache<HAPanels> {
static func create(connection: HAConnection, data: [String: Any]) -> HACache<HAPanels> {
HACache(
connection: connection,
populate: .init(
Expand Down
Loading
Loading