The document discusses the HomeKit framework which allows controlling home accessories like thermostats, lights, and sensors from iOS devices and Siri. It covers setting up accessories by discovering them on the local network, adding them to rooms in the HomeKit home database, and naming their services. It also explains how to control accessories by reading and writing characteristic values, setting up notifications for changes, and creating automations with triggers and scenes. Security features like end-to-end encryption using keys stored locally are highlighted.
4. Finalité
Concerne les accessoires de contrôle de la maison : Thermostats, Prises,
Interrupteurs, Ampoules, Fenêtres, volets, sensors, etc.
Coordonner et contrôler des accessoires de différents fabricants, avec :
➔Apps tierces iOS, watchOS, tvOS : Framework Homekit
➔Siri
➔Apple Home : automations
5. Technologies impliquées
Base de données partagée : iCloud (structure de la maison, nommage)
Clés cryptographiques partagées à travers iCloud Keychain
HomeKit Accessory Protocol : entre un contrôleur (iPhone, Apple TV) et un
accessoire (BLE, IP)
Framework : Découverte, contrôle, monitoring accessoires
6. Spécificités
Pas de compte utilisateur à créer
Pas de serveur tiers
Securité end to end:
➔Clés stockées seulement dans iOS et l’accessoire
➔Un seul master
Accès distant avec Apple TV et iPad
12. Créer/choisir une pièce
//func home(home: HMHome, assignAccessory accessory: HMAccessory, toRoom room: HMRoom) {
home.assignAccessory(accessory, toRoom: room) { error in
if let error = error {
//displayError(error)
} else {
//update view
}
//}
13. Nommer les services
//func updateName(name: String, forAccessory accessory: HMAccessory) {
accessory.updateName(name) { error in
if let error = error {
//displayError(error)
} else {
//update view
}
//}
14. Observe Home Database updates
public protocol HMHomeManagerDelegate : NSObjectProtocol {
optional public func homeManagerDidUpdateHomes(_ manager: HMHomeManager)
optional public func homeManagerDidUpdatePrimaryHome(_ manager: HMHomeManager)
optional public func homeManager(_ manager: HMHomeManager, didAddHome: HMHome)
optional public func homeManager(_ manager: HMHomeManager, didRemoveHome: HMHome)
}
15. Observe Home Database updates
public protocol HMHomeDelegate : NSObjectProtocol {
optional public func homeDidUpdateName(_ home: HMHome)
optional public func home(_ home: HMHome, didAddAccessory: HMAccessory)
optional public func home(_ home: HMHome, didRemoveAccessory: HMAccessory)
optional public func home(_ home: HMHome, didUpdateRoom: HMRoom, forAccessory: HMAccessory)
optional public func home(_ home: HMHome, didAddRoom: HMRoom)
optional public func home(_ home: HMHome, didRemoveRoom: HMRoom)
// (... a lot more)
}
16. Observe Home Database updates
public protocol HMHomeDelegate : NSObjectProtocol {
optional public func homeDidUpdateName(_ home: HMHome)
optional public func home(_ home: HMHome, didAddAccessory: HMAccessory)
optional public func home(_ home: HMHome, didRemoveAccessory: HMAccessory)
optional public func home(_ home: HMHome, didUpdateRoom: HMRoom, forAccessory: HMAccessory)
optional public func home(_ home: HMHome, didAddRoom: HMRoom)
optional public func home(_ home: HMHome, didRemoveRoom: HMRoom)
// (... a lot more)
}
// Notifications are only received
// if the database is modified by
// another device/app !
17. Contrôle de l’accessoire
Actions possibles:
Lire une caractéristique
Ecrire une caractéristique
Ecouter les notifications
Créer des scènes (écritures groupées de caractéristiques)
Triggers (Réagir aux changements de valeur des caractéristiques)
18. Contrôle de l’accessoire
import HomeKit
var lightBulb: HMAccessory?
// Get the PowerState characteristic through the LightBulb service
let lightBulbOn = lightBulb?.services.first(where: { (service) -> Bool in
service.serviceType == HMServiceTypeLightbulb
})?.characteristics.first(where: { (characteristic) -> Bool in
characteristic.characteristicType == HMCharacteristicTypePowerState
})
19. Lecture d’une caractéristique
// func readValue(completionHandler completion: @escaping (Error?) -> Void)
// var value: Any? { get }
lightBulbOn?.readValue(completionHandler: { (error) in
if let error = error {
// Handle the error
} else if let isOn = lightBulbOn?.value as? Bool {
print("Light bulb is (isOn ? "ON" : "OFF")")
}
})
20. Ecriture d’une caractéristique
// func writeValue(_ value: Any?, completionHandler completion: @escaping (Error?) -> Void)
lightBulbOn?.writeValue(false, completionHandler: { (error) in
if let error = error {
// Handle the error
} else {
print("Light bulb is OFF")
}
})
21. Notifications
// func enableNotification(_ enable: Bool, completionHandler completion: @escaping (Error?) -> Void)
lightBulbOn?.enableNotification(true, completionHandler: { (error) in
if let error = error {
// Handle error
} else {
print("Notifications are enabled")
}
})
class AccDelegate : NSObject, HMAccessoryDelegate {
public func accessory(_ accessory: HMAccessory, service: HMService, didUpdateValueForCharacteristic: HMCharacteristic) {
if let isOn = characteristic.value as? Bool {
print("Light bulb is now (isOn ? "ON" : "OFF")")
}
}
}
let delegate = AccDelegate()
lightBulb?.delegate = delegate;