Simplest way to light up using Homekit with watchOS

202 views Asked by At

I'm japanese student and I'm newbie of programing swift.

Now, I'm trying to light up the Hue lightbulb using Homekit.

As a test, I tried to check whether the system detects the home and accessories. Though, results are only zero and nil.

To solve this, I checked many websites and Apple document. But I can't make it. I am stucking this situation for a whole week.

I've already enabled homekit capability and NSHomeKitUsageDescription and I've connected 2 Hue lightbulb to my Home app. I can control them using Home app.

Would someone kindly help me?

import UIKit
import HomeKit
import Foundation

class  HomeHome: NSObject, HMHomeManagerDelegate {

   let homeManager = HMHomeManager()
   var home: HMHome?
   var myAccessory: HMAccessory?
   
   func viewDidLoad() {
           viewDidLoad()
           homeManager.delegate = self
   }

   func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
       home = manager.primaryHome
       print(#function)
       myAccessory = home?.accessories[0]  //first accessory  -> nil
       print(myAccessory)   -> nil
       print(homeManager.homes.count)  //counting homes   -> 0
   }
   
   func checkHue(){
       homeManagerDidUpdateHomes(homeManager)
       print(home?.accessories[0])  
       print("test")
   }
}

PS. I add

var authorizationStatus: HMHomeManagerAuthorizationStatus!
if homeManager.authorizationStatus.contains(.authorized) 
{print("The app is authorized to access home data.")}

to check whether my systems can access my home and it shows "The app is authorized to access home data.". So I think my system access Home app at least...

0

There are 0 answers