swifthomekit

Homekit's HMCharacteristicTypeBrightness does not get current values


I try to get current brightness values using following code.

It collects old values.

If I change brightness of a light in native Home app ( the same light stored in lightData[0] ) and come back to my app and call the following function it shows the right value.

I could not find a way to show current light brightness values.

readValue supposes to get values from accessories directly not from buffer in iPhone.

func getThem() {
        
        lightData[0].characteristic.readValue(completionHandler: {(error) in

            if error != nil {

                print(error!.localizedDescription)

            } else  {

                let type =  self.lightData[0].characteristic.value

                guard let value = (type as? NSNumber)?.intValue

                else {

                    return
                }

                print("values = \(value)")
             
            }       
            
        })
    } 

PS: (I showed only lightData[0] here to make code more readable, app collects and store all lights data in lightData struct array at func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {} when app started)


Solution

  • I "discovered" that if power state of lightbulb is 0 you can not read or write brightness level.

    To get over this issue I read power state and if it was 0 I changed it to 1 first then change brightness level.

    When reading brightness level just to show on screen if power state was 0 I just made it 0 in my indication system.

    It is a shame that I could not read this in documentation.