Skip to content

Commit 5a13362

Browse files
committed
Fix for accessory configuration on restart. Add didChangeAccessoryList to DeviceDelegate.
1 parent 60d7875 commit 5a13362

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Sources/HAP/Server/Configuration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension Device {
4242
// Accessories must increment the config number after a firmware update.
4343
// This must have a range of 1-4294967295 and wrap to 1 when it overflows.
4444
// This value must persist across reboots, power cycles, etc.
45-
internal var number: UInt32 = 0
45+
internal var number: UInt32 = 1
4646

4747
// HAP Specification 2.6.1: Instance IDs
4848
//

Sources/HAP/Server/Device.swift

+16-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public class Device {
150150
// The first accessory must be aid 1
151151
accessories[0].aid = 1
152152

153-
addAccessories(accessories)
153+
addToAccessoryList(accessories)
154154
}
155155

156156
private func persistConfig() {
@@ -185,7 +185,7 @@ public class Device {
185185
/// It is an error to try and add accessories with duplicate serial numbers.
186186
/// It is an error to try and add accessories to a non-bridge device.
187187
/// It is an error to try and increase the number of accessories above 99.
188-
public func addAccessories(_ newAccessories: [Accessory]) {
188+
private func addToAccessoryList(_ newAccessories: [Accessory]) {
189189
let totalNumberOfAccessories = accessories.count + newAccessories.count
190190
precondition(
191191
(isBridge && totalNumberOfAccessories <= 100) ||
@@ -228,6 +228,18 @@ public class Device {
228228
configuration.aidForAccessorySerialNumber[serialNumber] = accessory.aid
229229
}
230230
}
231+
}
232+
233+
/// Add an array of accessories to this bridge device, and notify changes
234+
///
235+
/// It is an error to try and add accessories with duplicate serial numbers.
236+
/// It is an error to try and add accessories to a non-bridge device.
237+
/// It is an error to try and increase the number of accessories above 99.
238+
public func addAccessories(_ newAccessories: [Accessory]) {
239+
240+
addToAccessoryList(newAccessories)
241+
242+
delegate?.didChangeAccessoryList()
231243

232244
// Write configuration data to persist updated aid's and notify listeners
233245
updatedConfiguration()
@@ -266,6 +278,8 @@ public class Device {
266278
let serialNumber = accessory.serialNumber
267279
configuration.aidForAccessorySerialNumber.removeValue(forKey: serialNumber)
268280
}
281+
delegate?.didChangeAccessoryList()
282+
269283
// write configuration data to persist updated aid's
270284
updatedConfiguration()
271285
}

Sources/HAP/Server/DeviceDelegate.swift

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public protocol DeviceDelegate: class {
5151
///
5252
func didChangePairingState(from: PairingState, to: PairingState)
5353

54+
/// Tells the delegate that one or more Accessories were added or removed.
55+
///
56+
func didChangeAccessoryList()
57+
5458
/// Tells the delegate that the value of a characteristic has changed.
5559
///
5660
/// - Parameters:
@@ -83,6 +87,8 @@ public extension DeviceDelegate {
8387

8488
func didChangePairingState(from: PairingState, to: PairingState) { }
8589

90+
func didChangeAccessoryList() { }
91+
8692
func characteristic<T>(
8793
_ characteristic: GenericCharacteristic<T>,
8894
ofService: Service,

0 commit comments

Comments
 (0)