Bouke / HAP

Swift implementation of the Homekit Accessory Protocol
https://boukehaarsma.nl/HAP/
MIT License
364 stars 50 forks source link

Help using Accessory.Television #108

Closed pixlwave closed 4 years ago

pixlwave commented 4 years ago

Hi

Happy HAP user here currently using virtual switches for automation. However I'm now venturing out to using the Television accessory and I'm unsure how to use it. I've made a subclass like so:

class MyTV: Accessory.Television {
    override func characteristic<T>(_ characteristic: GenericCharacteristic<T>, ofService service: Service, didChangeValue newValue: T?) where T : CharacteristicValueType {
        print(characteristic)
    }
}

and instantiated it like so:

let tvInfo = Service.Info(name: "TV", serialNumber: "1234")
let tv = MyTV(info: tvInfo, inputs: [("HDMI 1", .hdmi), ("HDMI 2", .hdmi), ("HDMI 3", .hdmi), ("HDMI 4", .hdmi)])

I can see the print statements, but I'm not really sure how to interpret the characteristic as a power change or input change. Sorry if I'm being a bit slow, any help would be much appreciated 😄

pixlwave commented 4 years ago

Ok, I figured it out in the end. For reference in case anyone who finds this, the input source characteristic is an activeIdentifier so distinguishing between that and the power is done like this:

class MyTV: Accessory.Television {
    override func characteristic<T>(_ characteristic: GenericCharacteristic<T>, ofService service: Service, didChangeValue newValue: T?) where T : CharacteristicValueType {
        if characteristic.type == .active, let state = newValue as? Enums.Active {
            print("**** Power: \(state) ****")
        } else if characteristic.type == .activeIdentifier, let inputNumber = newValue as? UInt32 {
            print("**** Input: \(inputNumber) ****")
        }
    }
}