Skip to content

Commit

Permalink
Upgrade HAKit and use it for REST API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zacwest committed Aug 1, 2021
1 parent 70c6248 commit 2f1d6a0
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 218 deletions.
6 changes: 0 additions & 6 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,6 @@
B67CE89D22200F220034C1D0 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6F02BF11CB470570029ABE7 /* Config.swift */; };
B67CE89E22200F220034C1D0 /* DiscoveryInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B626AAEE1D8F44DC00A0D225 /* DiscoveryInfo.swift */; };
B67CE8A622200F220034C1D0 /* HAAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = B657A9191CA647C500121384 /* HAAPI.swift */; };
B67CE8A722200F220034C1D0 /* HAAPI+RequestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C884692120EB8A00CCB501 /* HAAPI+RequestHelpers.swift */; };
B67CE8A822200F220034C1D0 /* MJPEGStreamer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05A4D31216DD206009FD1EB /* MJPEGStreamer.swift */; };
B67CE8A922200F220034C1D0 /* SettingsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C884792122A65800CCB501 /* SettingsStore.swift */; };
B67CE8AA22200F220034C1D0 /* ConnectionInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D014EEA82128E192008EA6F5 /* ConnectionInfo.swift */; };
Expand Down Expand Up @@ -770,7 +769,6 @@
D0C88464211F33CE00CCB501 /* TokenManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C88463211F33CE00CCB501 /* TokenManager.swift */; };
D0C8847A2122A65800CCB501 /* SettingsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C884792122A65800CCB501 /* SettingsStore.swift */; };
D0DD2CEE213BCA8900C3D9F7 /* URL+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0DD2CED213BCA8900C3D9F7 /* URL+Extensions.swift */; };
D0EEF2C9214D89A700D1D360 /* HAAPI+RequestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C884692120EB8A00CCB501 /* HAAPI+RequestHelpers.swift */; };
D0EEF2CE214D8AE200D1D360 /* RealmZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6EE36A120CF593E001494E3 /* RealmZone.swift */; };
D0EEF2FF214D8D4C00D1D360 /* CLError+DebugDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C2C17C20D1EC1300BD810B /* CLError+DebugDescription.swift */; };
D0EEF301214D8EAB00D1D360 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0EEF300214D8EAB00D1D360 /* CoreLocation.framework */; };
Expand Down Expand Up @@ -1802,7 +1800,6 @@
D0C8845F211ED11900CCB501 /* SafariServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SafariServices.framework; path = System/Library/Frameworks/SafariServices.framework; sourceTree = SDKROOT; };
D0C88461211ED16300CCB501 /* OnboardingAuthenticationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingAuthenticationController.swift; sourceTree = "<group>"; };
D0C88463211F33CE00CCB501 /* TokenManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenManager.swift; sourceTree = "<group>"; };
D0C884692120EB8A00CCB501 /* HAAPI+RequestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HAAPI+RequestHelpers.swift"; sourceTree = "<group>"; };
D0C884792122A65800CCB501 /* SettingsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsStore.swift; sourceTree = "<group>"; };
D0DD2CED213BCA8900C3D9F7 /* URL+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Extensions.swift"; sourceTree = "<group>"; };
D0EEF300214D8EAB00D1D360 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -3197,7 +3194,6 @@
D0B25BCF2132179900678C2C /* Models */,
B6F02BEC1CB46E810029ABE7 /* Responses */,
B657A9191CA647C500121384 /* HAAPI.swift */,
D0C884692120EB8A00CCB501 /* HAAPI+RequestHelpers.swift */,
D05A4D31216DD206009FD1EB /* MJPEGStreamer.swift */,
11195F6C267EFC15003DF674 /* HACancellable+App.swift */,
);
Expand Down Expand Up @@ -5287,7 +5283,6 @@
B67CE89522200F220034C1D0 /* LocationHistory.swift in Sources */,
491E990025D543560077BBE3 /* LogbookEntry.swift in Sources */,
11521BD8254003B7009C5C72 /* SentryLogDestination.swift in Sources */,
B67CE8A722200F220034C1D0 /* HAAPI+RequestHelpers.swift in Sources */,
11C4628924B109C100031902 /* WebhookResponseLocation.swift in Sources */,
11C4628C24B1230E00031902 /* WebhookResponseServiceCall.swift in Sources */,
B67CE8AA22200F220034C1D0 /* ConnectionInfo.swift in Sources */,
Expand Down Expand Up @@ -5405,7 +5400,6 @@
D03D893B20E0B2E300D4F28D /* Constants.swift in Sources */,
119DE933263325C20099F7D8 /* IconDrawable+Settings.swift in Sources */,
D03D893520E0AEF100D4F28D /* Realm+Initialization.swift in Sources */,
D0EEF2C9214D89A700D1D360 /* HAAPI+RequestHelpers.swift in Sources */,
11195F72267F01E4003DF674 /* HACancellable+App.swift in Sources */,
113E73102518457C004006D8 /* LocalizedManager.swift in Sources */,
111D295624F30E2400C8A7D1 /* Updater.swift in Sources */,
Expand Down
14 changes: 7 additions & 7 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ PODS:
- EMTLoadingIndicator (4.0.0)
- Eureka (5.3.3)
- Firebase (8.1.0)
- HAKit (0.2.2):
- HAKit/Core (= 0.2.2)
- HAKit/Core (0.2.2):
- HAKit (0.3):
- HAKit/Core (= 0.3)
- HAKit/Core (0.3):
- Starscream (~> 4.0.4)
- HAKit/Mocks (0.2.2):
- HAKit/Mocks (0.3):
- HAKit/Core
- HAKit/PromiseKit (0.2.2):
- HAKit/PromiseKit (0.3):
- HAKit/Core
- PromiseKit (~> 6.13)
- KeychainAccess (4.2.2)
Expand Down Expand Up @@ -184,7 +184,7 @@ CHECKOUT OPTIONS:
:commit: ff5b4ccf9bb424cb454e76ee91e32165d2d9c12c
:git: https://github.com/hirokimu/EMTLoadingIndicator
HAKit:
:commit: 19e6267dff11426da2c090d5e7943953606ac334
:commit: 550871babc7c7d527a9e038497bfac8e61198351
:git: https://github.com/home-assistant/HAKit.git
NotificationTestCases:
:commit: bb5906878530973b74eed54f1751f580afa87260
Expand All @@ -211,7 +211,7 @@ SPEC CHECKSUMS:
EMTLoadingIndicator: 0d3256b0de3e6ba5ab17048acc7aa93af34e48d3
Eureka: e6d720290c56bb23333ad1a0e020b2a98a0e9a43
Firebase: bd494ca54f975ea92931aaa187a4e6650d7f121c
HAKit: 2fefcc8028f78611408a70f5bb37c01e776bfcae
HAKit: 5b5ed6dcd685f372fd76f0ef88f000924377794e
KeychainAccess: c0c4f7f38f6fc7bbe58f5702e25f7bd2f65abf51
Lokalise: 252c397887dfd9983331abffa90ab60cdb004e19
lottie-ios: c058aeafa76daa4cf64d773554bccc8385d0150e
Expand Down
16 changes: 16 additions & 0 deletions Sources/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Alamofire
import CallbackURLKit
import Communicator
import FirebaseMessaging
import HAKit
import KeychainAccess
#if canImport(Lokalise) && !targetEnvironment(macCatalyst)
import Lokalise
Expand Down Expand Up @@ -140,6 +141,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
checkForUpdate()
checkForAlerts()

Current.apiConnection.send(.init(
type: .rest(.post, "template"),
data: ["template": "{{ now() }}"]
)) { result in
print("result: \(result)")
}

firstly {
Current.api.then { $0.GetLogbook() }
}.done { entries in
print("got \(entries)")
}.catch { error in
print("got \(error)")
}

return true
}

Expand Down
135 changes: 0 additions & 135 deletions Sources/Shared/API/HAAPI+RequestHelpers.swift

This file was deleted.

42 changes: 25 additions & 17 deletions Sources/Shared/API/HAAPI.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Alamofire
import CoreLocation
import Foundation
import HAKit
import Intents
import KeychainAccess
import ObjectMapper
Expand Down Expand Up @@ -284,7 +285,16 @@ public class HomeAssistantAPI {
if useWebhook {
promise = Current.webhooks.sendEphemeral(request: .init(type: "get_config", data: [:]))
} else {
promise = request(path: "config", callingFunctionName: "\(#function)")
promise = firstly {
Current.apiConnection.send(.init(type: .rest(.get, "config"))).promise
}.compactMap { (data: HAData) -> ConfigResponse? in
if case let .dictionary(dictionary) = data,
let result = Mapper<ConfigResponse>().map(JSONObject: dictionary) {
return result
} else {
return nil
}
}
}

return promise.then { config -> Promise<ConfigResponse> in
Expand Down Expand Up @@ -318,7 +328,8 @@ public class HomeAssistantAPI {
}

public func GetLogbook() -> Promise<[LogbookEntry]> {
request(path: "logbook", callingFunctionName: "\(#function)")
let request = HATypedRequest<[LogbookEntry]>(request: .init(type: .rest(.get, "logbook")))
return Current.apiConnection.send(request).promise
}

public func CallService(
Expand Down Expand Up @@ -362,23 +373,21 @@ public class HomeAssistantAPI {
}

public func Register() -> Promise<MobileAppRegistrationResponse> {
request(
path: "mobile_app/registrations",
callingFunctionName: "\(#function)",
method: .post,
parameters: buildMobileAppRegistration(),
encoding: JSONEncoding.default
)
.then { (resp: MobileAppRegistrationResponse) -> Promise<MobileAppRegistrationResponse> in
firstly { () -> Promise<MobileAppRegistrationResponse> in
let request = HATypedRequest<MobileAppRegistrationResponse>(request: .init(
type: .rest(.post, "mobile_app/registrations"),
data: buildMobileAppRegistration()
))

return Current.apiConnection.send(request).promise
}.get { resp in
Current.Log.verbose("Registration response \(resp)")

let connectionInfo = try self.connectionInfo()
connectionInfo.setAddress(resp.RemoteUIURL, .remoteUI)
connectionInfo.cloudhookURL = resp.CloudhookURL
connectionInfo.webhookID = resp.WebhookID
connectionInfo.webhookSecret = resp.WebhookSecret

return Promise.value(resp)
}
}

Expand All @@ -392,13 +401,12 @@ public class HomeAssistantAPI {
public func GetMobileAppConfig() -> Promise<MobileAppConfig> {
firstly { () -> Promise<MobileAppConfig> in
if let version = Current.serverVersion(), version < .actionSyncing {
let old: Promise<MobileAppConfigPush> = requestImmutable(
path: "ios/push",
callingFunctionName: "\(#function)"
)
let request = HATypedRequest<MobileAppConfigPush>(request: .init(type: .rest(.get, "ios/push")))
let old = Current.apiConnection.send(request).promise
return old.map { MobileAppConfig(push: $0) }
} else {
return requestImmutable(path: "ios/config", callingFunctionName: "\(#function)")
let request = HATypedRequest<MobileAppConfig>(request: .init(type: .rest(.get, "ios/config")))
return Current.apiConnection.send(request).promise
}
}.recover { error -> Promise<MobileAppConfig> in
if case AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 404)) = error {
Expand Down
22 changes: 11 additions & 11 deletions Sources/Shared/API/Responses/LogbookEntry.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Foundation
import ObjectMapper
import HAKit

public struct LogbookEntry: ImmutableMappable {
public let entityId: String
public struct LogbookEntry: HADataDecodable {
public let entityId: String?
public let when: Date
public let domain: String?
public let message: String?
public let state: String?
public let name: String?
public let iconName: String?

public init(map: Map) throws {
self.entityId = try map.value("entity_id")
self.when = try map.value("when", using: HomeAssistantTimestampTransform())
self.domain = try? map.value("domain")
self.message = try? map.value("message")
self.state = try? map.value("state")
self.name = try? map.value("name")
self.iconName = try? map.value("icon")
public init(data: HAData) throws {
self.when = try data.decode("when")
self.entityId = data.decode("entity_id", fallback: nil)
self.domain = data.decode("domain", fallback: nil)
self.message = data.decode("message", fallback: nil)
self.state = data.decode("state", fallback: nil)
self.name = data.decode("name", fallback: nil)
self.iconName = data.decode("icon", fallback: nil)
}
}

0 comments on commit 2f1d6a0

Please sign in to comment.