In the following quick and dirty test app, I am trying to receive light level updates from a Hue motion detector accessory using accessoryDidUpdateServices(). However the delegate function is never called. I'm running it on an iPhone with iOS 12.1.4 installed.
Can anyone explain what I'm doing wrong, and what I need to do to properly handle HMService updates?
import HomeKit
public class ViewController: UIViewController, HMHomeManagerDelegate, HMAccessoryDelegate {
var manager: HMHomeManager!
public override func viewDidLoad() {
super.viewDidLoad()
manager = HMHomeManager()
manager.delegate = self
}
public func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
let service = manager.primaryHome!.servicesWithTypes([HMServiceTypeLightSensor])!.first!
service.accessory!.delegate = self
let characteristic = service.characteristics.filter({ $0.characteristicType == HMCharacteristicTypeCurrentLightLevel }).first!
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
print("Light level: \(characteristic.value!)")
}
}
public func accessoryDidUpdateServices(_ accessory: HMAccessory) {
print("Service updated: \(accessory.name)")
}
}
The problem was that I was using accessoryDidUpdateService when I should have used accessory:didUpdateValueFor. With the latter func in place, the code runs as expected.