Skip to content

Commit 059ea08

Browse files
committed
HMHome promises
1 parent 1304224 commit 059ea08

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Sources/HMHome+Promise.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// HMHome+Promise.swift
3+
// Onboarding
4+
//
5+
// Created by Chris Chares on 6/29/18.
6+
// Copyright © 2018 Hunter Douglas. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import HomeKit
11+
import PromiseKit
12+
13+
extension HMHome {
14+
15+
public func updateName(_ name: String) -> Promise<Void> {
16+
return Promise { seal in
17+
self.updateName(name, completionHandler: seal.resolve)
18+
}
19+
}
20+
21+
22+
#if !os(tvOS) && swift(>=3.2)
23+
/// Add and setup a new HMAccessory. Displays it's own UI
24+
@available(iOS 11.3, OSX 10.13, watchOS 4.0, *)
25+
public func addAndSetupAccessories(with payload: HMAccessorySetupPayload) -> Promise<[HMAccessory]> {
26+
return Promise { seal in
27+
self.addAndSetupAccessories(with: payload, completionHandler: seal.resolve)
28+
}
29+
}
30+
#endif
31+
32+
#if !os(tvOS) && swift(>=3.2)
33+
/// Add and setup a new HMAccessory. Displays it's own UI
34+
@available(iOS 10.0, OSX 10.13, watchOS 4.0, *)
35+
public func addAndSetupAccessories() -> Promise<[HMAccessory]> {
36+
// We need to compare what we have before the action to after to know what is new
37+
let beforeAccessories = self.accessories
38+
let home = self
39+
40+
return Promise { seal in
41+
self.addAndSetupAccessories { error in
42+
if let error = error { seal.reject(error) }
43+
else {
44+
let newAccessories = home.accessories.filter { beforeAccessories.contains($0) == false }
45+
seal.fulfill(newAccessories)
46+
}
47+
}
48+
}
49+
}
50+
#endif
51+
52+
public func addAccessory(_ accessory: HMAccessory) -> Promise<Void> {
53+
return Promise { seal in
54+
self.addAccessory(accessory, completionHandler: seal.resolve)
55+
}
56+
}
57+
58+
public func assignAccessory(_ accessory: HMAccessory, to room: HMRoom) -> Promise<Void> {
59+
return Promise { seal in
60+
self.assignAccessory(accessory, to: room, completionHandler: seal.resolve)
61+
}
62+
}
63+
64+
public func removeAccessory(_ accessory: HMAccessory) -> Promise<Void> {
65+
return Promise { seal in
66+
self.removeAccessory(accessory, completionHandler: seal.resolve)
67+
}
68+
}
69+
70+
/**
71+
Rooms
72+
*/
73+
public func addRoom(withName name: String) -> Promise<HMRoom> {
74+
return Promise { seal in
75+
self.addRoom(withName: name, completionHandler: seal.resolve)
76+
}
77+
}
78+
79+
public func removeRoom(_ room: HMRoom) -> Promise<Void> {
80+
return Promise { seal in
81+
self.removeRoom(room, completionHandler: seal.resolve)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)