1

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...

ori
  • 11
  • 2
  • if `myAccessory` is `nil` it means `home` is `nil` otherwise if there were no accessories your subscript `home?.accessories[0]` would crash instead of returning `nil`. In other words `manager.primaryHome` is `nil` – Leo Dabus Jan 18 '22 at 15:57
  • So.. I wonder why manager.primaryHome is nil. I add many print(xxx) at func CheckHue to examine my code, I found print(homeManager) is "". Though, I think this means my system recognizes and connect to my Home app, every variables shows nil.... I'm so sorry I don't understand what is going on my system... – ori Jan 19 '22 at 09:24
  • Were you able to solve this? Having a similar issue. – fluffykitten Apr 18 '22 at 05:14

0 Answers0