HomeKit Integration for Smart Home Control via iOS
HomeKit is Apple's smart home ecosystem: lamps, thermostats, locks, sensors. An application gains access to accessories through HomeKit.framework, manages them through HMAccessory and HMCharacteristic. Before writing the first line of code, understand: HomeKit works only with certified equipment, and the testing procedure with real accessories in Apple HomeKit simulator has limitations.
Core HomeKit Concepts
HMHome — a home. Can be multiple (primary residence, vacation home). HMRoom — a room inside the home. HMAccessory — physical device (lamp, lock). HMService — accessory function (lamp has lighting service + information service). HMCharacteristic — specific service attribute: brightness, color temperature, lock state.
Hierarchy: Home → Room → Accessory → Service → Characteristic.
Setup and Access
import HomeKit
class HomeKitManager: NSObject, HMHomeManagerDelegate {
let homeManager = HMHomeManager()
override init() {
super.init()
homeManager.delegate = self
}
func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
guard let home = manager.primaryHome else {
// no configured home — offer to create or add accessories
return
}
listAccessories(in: home)
}
func listAccessories(in home: HMHome) {
for accessory in home.accessories {
print("Accessory: \(accessory.name)")
for service in accessory.services {
print(" Service: \(service.serviceType)")
for characteristic in service.characteristics {
print(" Characteristic: \(characteristic.characteristicType)")
}
}
}
}
}
Info.plist is mandatory: NSHomeKitUsageDescription. Without it — crash on first HMHomeManager access.
Entitlement: com.apple.developer.homekit — requested via Apple Developer Portal. Without it, the application cannot interact with HomeKit even on simulator with virtual accessories.
Reading and Writing Characteristics
func setLightBrightness(_ accessory: HMAccessory, brightness: Int) {
guard let lightService = accessory.services.first(where: {
$0.serviceType == HMServiceTypeLightbulb
}),
let brightnessChar = lightService.characteristics.first(where: {
$0.characteristicType == HMCharacteristicTypeBrightness
}) else { return }
brightnessChar.writeValue(brightness) { error in
if let error = error {
// HMError.communicationFailure — accessory unavailable
// HMError.operationNotSupported — characteristic is read-only
print("Write failed: \(error)")
}
}
}
func readCurrentTemperature(_ accessory: HMAccessory) {
guard let thermostat = accessory.services.first(where: {
$0.serviceType == HMServiceTypeThermostat
}),
let tempChar = thermostat.characteristics.first(where: {
$0.characteristicType == HMCharacteristicTypeCurrentTemperature
}) else { return }
tempChar.readValue { error in
if error == nil {
let temp = tempChar.value as? Double
print("Temperature: \(temp ?? 0)°C")
}
}
}
Event Subscriptions
func subscribeToLockState(_ lockChar: HMCharacteristic) {
lockChar.enableNotification(true) { error in
guard error == nil else { return }
// now HMAccessoryDelegate receives notifications
}
}
// HMAccessoryDelegate:
func accessory(_ accessory: HMAccessory,
service: HMService,
didUpdateValueFor characteristic: HMCharacteristic) {
if characteristic.characteristicType == HMCharacteristicTypeCurrentLockMechanismState {
let isLocked = characteristic.value as? Int == 1
updateLockUI(isLocked: isLocked)
}
}
Automations and Triggers
HMTrigger — automation by condition. Two types:
-
HMTimerTrigger— by time -
HMEventTrigger— by event (door opening, temperature change)
let fireDate = Date().addingTimeInterval(3600)
let timer = HMTimerTrigger(name: "Evening lights", fireDate: fireDate,
timeZone: .current, recurrence: nil, recurrenceCalendar: nil)
home.addTrigger(timer) { error in
guard error == nil else { return }
// add action set to trigger
}
Accessory Simulator for Development
Apple HomeKit Accessory Simulator (part of Xcode Additional Tools) creates virtual accessories on Mac. Application in iOS simulator sees them via Wi-Fi. This is the only way to develop without real hardware.
Limitation: some scenarios (Bluetooth accessories, Thread devices) don't work fully with simulator — requires real iPhone and certified accessory.
Matter vs HomeKit
Matter (new smart home standard) works via HomeKit as one of the transports on iOS. HMAccessory with Matter profile appears in HomeKit automatically when added via HMHome.addAndSetupAccessories. No separate handling required — HomeKit abstracts the protocol.
Implementation timeframe: 1–2 weeks — accessory management, characteristic read/write, basic automations. Cost calculated individually.







