diff --git a/CHANGELOG.md b/CHANGELOG.md index af1bc80..87e36fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.3] - 2024-11-12 +- Changed: Allow passing data to entities state subcription, so you can filter what you want to receive + ## [0.4.2] - 2024-04-22 - Changed: Avoid JSON cache by JSONSerialization NSString diff --git a/HAKit.podspec b/HAKit.podspec index 9328749..143b0b5 100644 --- a/HAKit.podspec +++ b/HAKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'HAKit' - s.version = '0.4.2' + s.version = '0.4.3' s.summary = 'Communicate with a Home Assistant instance.' s.author = 'Home Assistant' diff --git a/README.md b/README.md index 22451a5..7d6b493 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ To add it to an Xcode project, you can do this by adding the URL to File > Swift Add the following line to your Podfile: ```ruby -pod "HAKit", "~> 0.4.2" +pod "HAKit", "~> 0.4.3" # We are working from a fork of Starscream due to a necessary fix, please specify in your podfile pod 'Starscream', git: 'https://github.com/bgoncal/starscream', branch: 'ha-URLSession-fix' # pod "HAKit/PromiseKit" # optional, for PromiseKit support diff --git a/Source/Caches/HACacheKeyStates.swift b/Source/Caches/HACacheKeyStates.swift index 693958b..fd3db9f 100644 --- a/Source/Caches/HACacheKeyStates.swift +++ b/Source/Caches/HACacheKeyStates.swift @@ -2,12 +2,12 @@ import Foundation /// Key for the cache internal struct HACacheKeyStates: HACacheKey { - static func create(connection: HAConnection) -> HACache { + static func create(connection: HAConnection, data: [String: Any]) -> HACache { .init( connection: connection, subscribe: .init( - subscription: .subscribeEntities(), + subscription: .subscribeEntities(data: data), transform: { info in .replace(processUpdates( info: info, diff --git a/Source/Caches/HACachedStates.swift b/Source/Caches/HACachedStates.swift index 4fedd4d..cb17388 100644 --- a/Source/Caches/HACachedStates.swift +++ b/Source/Caches/HACachedStates.swift @@ -1,6 +1,10 @@ public extension HACachesContainer { /// Cache of entity states, see `HACachedStates` for values. - var states: HACache { self[HACacheKeyStates.self] } + /// - Parameter data: The data passed to connection request + /// - Returns: The cache object with states + func states(_ data: [String: Any] = [:]) -> HACache { + self[HACacheKeyStates.self, data] + } } /// Cached version of all entity states diff --git a/Source/Caches/HACachedUser.swift b/Source/Caches/HACachedUser.swift index 4fd8bb0..1cbe2d8 100644 --- a/Source/Caches/HACachedUser.swift +++ b/Source/Caches/HACachedUser.swift @@ -5,7 +5,7 @@ public extension HACachesContainer { /// Key for the cache private struct HACacheKeyCurrentUser: HACacheKey { - static func create(connection: HAConnection) -> HACache { + static func create(connection: HAConnection, data: [String: Any]) -> HACache { .init( connection: connection, populate: .init(request: .currentUser(), transform: \.incoming), diff --git a/Source/Caches/HACachesContainer.swift b/Source/Caches/HACachesContainer.swift index 48bef30..19aff69 100644 --- a/Source/Caches/HACachesContainer.swift +++ b/Source/Caches/HACachesContainer.swift @@ -6,9 +6,14 @@ public protocol HACacheKey { /// /// This is called exactly once per connection per cache key. /// - /// - Parameter connection: The connection to create on + /// - Parameters: + /// - connection: The connection to create on + /// - data: The data passed to connection request /// - Returns: The cache you want to associate with the key - static func create(connection: HAConnection) -> HACache + static func create( + connection: HAConnection, + data: [String: Any] + ) -> HACache } /// Container for caches @@ -56,7 +61,7 @@ public class HACachesContainer { /// - SeeAlso: `HACachesContainer` class description for how to use keys to retrieve caches. /// - Subscript: The key to look up /// - Returns: Either the existing cache for the key, or a new one created on-the-fly if none was available - public subscript(_ key: KeyType.Type) -> HACache { + public subscript(_ key: KeyType.Type, data: [String: Any] = [:]) -> HACache { // ObjectIdentifier is globally unique per class _or_ meta type, and we're using meta type here let key = ObjectIdentifier(KeyType.self) @@ -64,7 +69,7 @@ public class HACachesContainer { return value } - let value = KeyType.create(connection: connection) + let value = KeyType.create(connection: connection, data: data) values[key] = value return value } diff --git a/Source/Convenience/States.swift b/Source/Convenience/States.swift index 401f6e9..8ce835b 100644 --- a/Source/Convenience/States.swift +++ b/Source/Convenience/States.swift @@ -11,9 +11,10 @@ public extension HATypedSubscription { } /// Listen for compressed state changes of all entities + /// - Parameter data: The data passed to connection request /// - Returns: A typed subscriptions that can be sent via `HAConnection` - static func subscribeEntities() -> HATypedSubscription { - .init(request: .init(type: .subscribeEntities, data: [:])) + static func subscribeEntities(data: [String: Any]) -> HATypedSubscription { + .init(request: .init(type: .subscribeEntities, data: data)) } } diff --git a/Tests/HACachedStates.test.swift b/Tests/HACachedStates.test.swift index dc53317..3176955 100644 --- a/Tests/HACachedStates.test.swift +++ b/Tests/HACachedStates.test.swift @@ -19,7 +19,7 @@ internal class HACachedStatesTests: XCTestCase { } func testPopulateAndSubscribeInfo() throws { - let cache = container.states + let cache = container.states() XCTAssertNil(cache.populateInfo) XCTAssertNil(cache.subscribeInfo) let subscribeOnlyInfo = try XCTUnwrap(cache.subscribeOnlyInfo) diff --git a/Tests/HACachesContainer.test.swift b/Tests/HACachesContainer.test.swift index 7976008..bb4a192 100644 --- a/Tests/HACachesContainer.test.swift +++ b/Tests/HACachesContainer.test.swift @@ -41,7 +41,7 @@ internal class HACachesContainerTests: XCTestCase { } private struct Key1: HACacheKey { - static func create(connection: HAConnection) -> HACache { + static func create(connection: HAConnection, data: [String: Any]) -> HACache { .init( connection: connection, populate: HACachePopulateInfo( @@ -61,7 +61,7 @@ private struct Value1: HADataDecodable { } private struct Key2: HACacheKey { - static func create(connection: HAConnection) -> HACache { + static func create(connection: HAConnection, data: [String: Any]) -> HACache { .init( connection: connection, populate: HACachePopulateInfo( diff --git a/Tests/States.test.swift b/Tests/States.test.swift index 10a93bb..958f75d 100644 --- a/Tests/States.test.swift +++ b/Tests/States.test.swift @@ -141,7 +141,7 @@ internal class StatesTests: XCTestCase { } func testSubscribeEntitiesRequest() throws { - let request = HATypedSubscription.subscribeEntities() + let request = HATypedSubscription.subscribeEntities(data: [:]) XCTAssertEqual(request.request.type, .subscribeEntities) } }