Hello Coders,
I'm new to Swift, iOS, HomeKit and Delegates. I'm trying to get a HomeKit Accessory to notify my function when a value changes. I'm testing with Brightness of a lamp for now. In other parts of my app I can read and write brightness.
I've created two different functions to receive the brightness change. I realize that I will only need one, once I learn more. I'm able to enable Notifications for this one device.
Any suggestions would be helpful, including general coding style!
Thanks Steve
import Foundation
import HomeKit
//let hkHomeManager = HMHomeManager()
let hkHomeManager = HomeKitManagerClass()
let hkAccessoryManager = HomeKitAccessoryClass()
let myCharTypes = readCharacteristicsCSV()
protocol HomeKitAccessoryDelegate {
func didUpdateValueFor (_ accessory: HMAccessory, service: HMService, didUpdateValueFor characteristic: HMCharacteristic)
func didReceiveBrightness(value: Int)
}
class HomeKitAccessoryClass: NSObject, HMAccessoryDelegate {
var accDelegate:HomeKitAccessoryDelegate?
func didUpdateValueFor (_ accessory: HMAccessory!, service: HMService!, didUpdateValueFor characteristic: HMCharacteristic) {
var jsmAcc: HMAccessory = HMAccessory()
var jsmSer: HMService = HMService()
var jsmChar: HMCharacteristic = HMCharacteristic()
if let accDelegate = self.accDelegate {
accDelegate.didUpdateValueFor(jsmAcc, service: jsmSer, didUpdateValueFor: jsmChar)
print ("Did Update Value For...")
print ("Did Update Value For...")
}
}
func brightnessUpdated () {
var brightnessReceived: Int = Int()
if let accDelegate = self.accDelegate {
accDelegate.didReceiveBrightness(value: brightnessReceived)
print ("Did Update Value For...")
print ("Did Update Value For...\(brightnessReceived)")
}
}
func hkNotifications(idIn: String) {
if appSettings.key.homeKitAllowed == false { return }
let timeStart = CFAbsoluteTimeGetCurrent()
if let index = getDeviceIndexById(idIn) {
if (globalDevices.deviceList[index].displayName).contains("Couch") == false { return } // Test ONLY LR Couch Light
print("hkNotificaitons Start \(globalDevices.deviceList[index].displayName)")
let oneDevice = globalDevices.deviceList[index].hkDevice // Device = Accessory from HomeKit
if oneDevice == nil { return } // no HomeKit Accessory Found
if oneDevice!.isReachable == false && oneDevice!.isBlocked == true { return } // Don't try to access this device}
for indexServices in 0..<oneDevice!.services.count {
let hkCharacteristics = oneDevice!.services[indexServices].characteristics
for indexCharacteristics in 0..<hkCharacteristics.count {
let hkChar = hkCharacteristics[indexCharacteristics]
if hkChar.isNotificationEnabled {
print("hkNotificaitons are ALREADY enabled for: \(globalDevices.deviceList[index].displayName)")
} else {
if hkChar.characteristicType == "00000008-0000-1000-8000-0026BB765291" { // Test ONLY Brightness for now
hkChar.enableNotification(true, completionHandler: { error in
if error != nil {
print("\(hkChar), Error in Notification Sets \(error!.localizedDescription)")
} else {
print("hkNotificaitons are set to TRUE for: \(globalDevices.deviceList[index].displayName)")
}
}
)
}
}
}
}
}
}
}