Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2021 System Services

WWDC21 · 18 min · System Services

Add support for Matter in your smart home app

The enhanced and new APIs in HomeKit enable smart home developers to integrate with the new Matter protocol in the most convenient way. Tour the Matter protocol, and discover how to set up and manage Matter accessories on our platforms and within your smart home apps.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 7 snippets

Add a Matter accessory to your HomeKit app swift · at 4:58 ↗
home.addAndSetupAccessories() { error in
    if let error = error {
         print("Error occurred in accessory setup \(error)”)
    } else {
         print("Successfully added accessory to HomeKit")
    }
}
Invocation example swift · at 9:12 ↗
let homes = proprietaryHomeStorage.homes.map { home in
    HMCHIPServiceHome(uuid: home.uuid, name: home.name)
}

let topology = HMCHIPServiceTopology(homes: homes)
let setupManager = HMAccessorySetupManager()

do {
    try await setupManager.addAndSetUpAccessories(for: topology)
    print("Successfully added accessory to my app”)
} catch {
    print("Error occurred in accessory setup \(error)")
}
Extension communication swift · at 10:15 ↗
class RequestHandler: HMCHIPServiceRequestHandler, CHIPDevicePairingDelegate {

   // . . .

   override func pairAccessory(in: HMCHIPServiceHome, onboardingPayload: String) async throws -> Void {
        // iOS is instructing the extension to pair the accessory via CHIP.framework
  }

   // . . .
}
Extension communication swift · at 10:39 ↗
class RequestHandler: HMCHIPServiceRequestHandler, CHIPDevicePairingDelegate {

   // . . .

   override func rooms(in: HMCHIPServiceHome) async throws -> [HMCHIPServiceRoom] {
        // iOS is querying for a room list that corresponds to the given home
    }

   // . . .
}
Extension communication swift · at 11:03 ↗
class RequestHandler: HMCHIPServiceRequestHandler, CHIPDevicePairingDelegate {

   // . . .

   override func configureAccessory(named accessoryName: String, room accessoryRoom: HMCHIPServiceRoom) async throws -> Void {
        // iOS is instructing the extension to apply configuration via CHIP.framework.
    }

   // . . .
}
Extension communication swift · at 11:27 ↗
class RequestHandler: HMCHIPServiceRequestHandler, CHIPDevicePairingDelegate {

    override func rooms(in: HMCHIPServiceHome) async throws -> [HMCHIPServiceRoom] {
        // iOS is querying for rooms that match the given home.  These rooms will be shown in system UI and the selection will be vended back to your extension's `configureAccessory` function
   }

    override func pairAccessory(in: HMCHIPServiceHome, onboardingPayload: String) async throws -> Void {
        // iOS is instructing the extension to pair the accessory via CHIP.framework
   }
    
    override func configureAccessory(named accessoryName: String, room accessoryRoom: HMCHIPServiceRoom) async throws -> Void {
       // iOS is instructing the extension to apply configuration via CHIP.framework.
    }
}
Status and Control swift · at 14:27 ↗
let controller = CHIPDeviceController.shared()
do {
    let device = try controller.getPairedDevice(accessoryDeviceID)
    let onOffCluster = CHIPOnOff(device: device,
                               endpoint: lightEndpoint,
                                  queue: DispatchQueue.main)
    onOffCluster?.toggle({ (error, values) in
        // Error handling code here
    })
    onOffCluster?.readAttributeOnOff(responseHandler: { error, response in
        if let state = response?[VALUE_KEY] as? NSInteger {
           updateLightState(state: state)
        }
    })
} catch {
    print("Error occurred in accessory control \(error)")
}