brutella / hc

hc is a lightweight framework to develop HomeKit accessories in Go.
Apache License 2.0
1.74k stars 189 forks source link

example describing how to modify a characteristic #160

Closed jmhodges closed 3 years ago

jmhodges commented 4 years ago

It'd be nice to have an example laying out exactly how to change the value of a characteristic on a device. That is, being able to turn on a lamp or tv or some such.

The projects and examples given all seem to either read data from homekit devices or expose non-homekit devices using homekit, but none seem to write back to those devices using HAP.

brutella commented 4 years ago

There are 2 parts in HomeKit – server (accessories) and clients (Apple Home app).

The flow goes like this. The user opens the Apple Home app, which triggers the app to connect to all available (and paired) HomeKit accessories and read the current data (like brightness). Clients can only connect to accessories if they know their private encryption key. Also the accessory has to know the private encryption key of the client. Those encryption keys are exchange only once during the pairing process. If you don't know the encryption key, you can connect to an accessory. On iOS those encryption keys are stored securely in the keychain. Only the HomeKit framework has access this key – iOS HomeKit apps have to use the HomeKit framework to communicate with accessory. Those apps themselves cannot read the encryption key.

jmhodges commented 4 years ago

Well, I'm asking for a mechanism in hc to pretend to be an iOS client in order to change characteristics. There's nothing magical of about the encryption keys stored on an iOS device; hc could be creating its own, no?

I have a HomeKit-enabled TV with an attached non-HomeKit sound system. I would like to be able to tell Siri "turn off my TV" and use hc to turn the TV off, but also tell Siri to "turn up the volume on my TV" and use hc to turn the volume up on the sound system.

Basically, I'm looking to use hc to proxy to the correct device depending on the operation needed while using the same name.

brutella commented 4 years ago

Why not create a HomeKit bridge for your sound system?

jmhodges commented 4 years ago

Because I don’t want to have to remember its name separately or tell my partner (or whoever visits) to do the same. The sound system is truly part of the TV for those folks.

brutella commented 4 years ago

Why not name the imaginary HomeKit bridge for your sound system the same as your TV? Maybe this will work.

There's nothing magical of about the encryption keys stored on an iOS device; hc could be creating its own, no?

No, an accessory only has one private and one public key. If you don't know the public key, you can't communicate with it.

jmhodges commented 4 years ago

Hunh, then what am I missing about https://github.com/forty2/hap-client which seems to provide this functionality?

brutella commented 4 years ago

There is a client implementation under _example/client.go which currently only pairs with a HomeKit accessory. Maybe this helps.

jmhodges commented 4 years ago

Thanks!

jmhodges commented 4 years ago

Oh, I guess I was looking for an example of actually sending characteristic changing messages?

Maybe I've missed something, but I don't see that in that example.

brutella commented 4 years ago

The example provides the initial pairing functionality. Sending messages to an accessory is not implemented anywhere yet.