Skip to content

Commit ec9f22e

Browse files
committed
HMCharacteristics promises
1 parent d0c26f5 commit ec9f22e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

PMKHomeKit.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
63199F171D5FF7B9007E8A0E /* CLLocationManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63199F151D5FF7B9007E8A0E /* CLLocationManagerTests.swift */; };
1818
6358AB7A1D5D4B6700B9B157 /* PMKCoreLocation.h in Headers */ = {isa = PBXBuildFile; fileRef = 6358AB791D5D4B6700B9B157 /* PMKCoreLocation.h */; settings = {ATTRIBUTES = (Public, ); }; };
1919
63C7FFF71D5C020D003BAE60 /* PMKHomeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKHomeKit.framework */; };
20+
838C29D32103FB9700CE5309 /* HMAcessoryBrowser+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29CE2103FB9600CE5309 /* HMAcessoryBrowser+Promise.swift */; };
21+
838C29D42103FB9700CE5309 /* HMHome+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29CF2103FB9700CE5309 /* HMHome+Promise.swift */; };
22+
838C29D52103FB9700CE5309 /* HMCharacteristic+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29D02103FB9700CE5309 /* HMCharacteristic+Promise.swift */; };
23+
838C29D62103FB9700CE5309 /* HMHomeManager+Promise.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29D12103FB9700CE5309 /* HMHomeManager+Promise.swift */; };
24+
838C29D72103FB9700CE5309 /* PromiseProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 838C29D22103FB9700CE5309 /* PromiseProxy.swift */; };
2025
/* End PBXBuildFile section */
2126

2227
/* Begin PBXContainerItemProxy section */
@@ -44,6 +49,11 @@
4449
63C7FFF21D5C020D003BAE60 /* PMKHKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PMKHKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4550
63CCF8121D5C0C4E00503216 /* Cartfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile; sourceTree = "<group>"; };
4651
63CCF8171D5C11B500503216 /* Carthage.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Carthage.xcconfig; sourceTree = "<group>"; };
52+
838C29CE2103FB9600CE5309 /* HMAcessoryBrowser+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMAcessoryBrowser+Promise.swift"; sourceTree = "<group>"; };
53+
838C29CF2103FB9700CE5309 /* HMHome+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMHome+Promise.swift"; sourceTree = "<group>"; };
54+
838C29D02103FB9700CE5309 /* HMCharacteristic+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMCharacteristic+Promise.swift"; sourceTree = "<group>"; };
55+
838C29D12103FB9700CE5309 /* HMHomeManager+Promise.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "HMHomeManager+Promise.swift"; sourceTree = "<group>"; };
56+
838C29D22103FB9700CE5309 /* PromiseProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PromiseProxy.swift; sourceTree = "<group>"; };
4757
/* End PBXFileReference section */
4858

4959
/* Begin PBXFrameworksBuildPhase section */
@@ -89,6 +99,11 @@
8999
63C7FFA91D5BEE09003BAE60 /* Sources */ = {
90100
isa = PBXGroup;
91101
children = (
102+
838C29D02103FB9700CE5309 /* HMCharacteristic+Promise.swift */,
103+
838C29CE2103FB9600CE5309 /* HMAcessoryBrowser+Promise.swift */,
104+
838C29CF2103FB9700CE5309 /* HMHome+Promise.swift */,
105+
838C29D12103FB9700CE5309 /* HMHomeManager+Promise.swift */,
106+
838C29D22103FB9700CE5309 /* PromiseProxy.swift */,
92107
6358AB791D5D4B6700B9B157 /* PMKCoreLocation.h */,
93108
63199F061D5FF25C007E8A0E /* CLGeocoder+AnyPromise.h */,
94109
63199F071D5FF25C007E8A0E /* CLGeocoder+AnyPromise.m */,
@@ -224,7 +239,12 @@
224239
buildActionMask = 2147483647;
225240
files = (
226241
63199F0E1D5FF25C007E8A0E /* CLGeocoder+Promise.swift in Sources */,
242+
838C29D62103FB9700CE5309 /* HMHomeManager+Promise.swift in Sources */,
243+
838C29D52103FB9700CE5309 /* HMCharacteristic+Promise.swift in Sources */,
244+
838C29D42103FB9700CE5309 /* HMHome+Promise.swift in Sources */,
245+
838C29D32103FB9700CE5309 /* HMAcessoryBrowser+Promise.swift in Sources */,
227246
63199F101D5FF25C007E8A0E /* CLLocationManager+AnyPromise.m in Sources */,
247+
838C29D72103FB9700CE5309 /* PromiseProxy.swift in Sources */,
228248
63199F0D1D5FF25C007E8A0E /* CLGeocoder+AnyPromise.m in Sources */,
229249
63199F111D5FF25C007E8A0E /* CLLocationManager+Promise.swift in Sources */,
230250
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// HMAccessory+Promise.swift
3+
// Onboarding
4+
//
5+
// Created by Chris Chares on 7/12/18.
6+
// Copyright © 2018 Hunter Douglas. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import HomeKit
11+
import PromiseKit
12+
13+
public enum AccessoryError: Error {
14+
case incorrectType
15+
case serviceMissing
16+
case characteristicMissing
17+
}
18+
19+
extension HMCharacteristic {
20+
/**
21+
A simple typesafe promise wrapper around readValue
22+
*/
23+
public func read<T>() -> Promise<T> {
24+
return Promise { seal in
25+
self.readValue { error in
26+
if let error = error { seal.reject(error) }
27+
else if let value = self.value as? T { seal.fulfill(value) }
28+
else { seal.reject(AccessoryError.incorrectType) }
29+
}
30+
}
31+
}
32+
/// Because type inference is great... until you can't compile (thanks Swift)
33+
public func readFloat() -> Promise<Float> { return read() }
34+
public func readDouble() -> Promise<Double> { return read() }
35+
public func readInt() -> Promise<Int> { return read() }
36+
public func readString() -> Promise<String> { return read() }
37+
38+
/**
39+
A simple promise wrapper around writeValue
40+
*/
41+
public func write(_ value: Any?) -> Promise<Void> {
42+
return Promise { seal in
43+
self.writeValue(value, completionHandler: seal.resolve)
44+
}
45+
}
46+
/// Explicit is good
47+
public func writeFloat(_ value: Float) -> Promise<Void> { return write(value) }
48+
public func writeDouble(_ value: Double) -> Promise<Void> { return write(value) }
49+
public func writeInt(_ value: Int) -> Promise<Void> { return write(value) }
50+
public func writeString(_ value: String) -> Promise<Void> { return write(value) }
51+
}

0 commit comments

Comments
 (0)