Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I read Homekit values for all accessories paired? #166

Open
sderuiter opened this issue Aug 27, 2019 · 4 comments
Open

Can I read Homekit values for all accessories paired? #166

sderuiter opened this issue Aug 27, 2019 · 4 comments

Comments

@sderuiter
Copy link

Hi,

Possibly a dumb question, but I can't get from the readme whether this implementation would fit my needs.

I have Philip Hue lights and sensors and Tradfri power plugs and a Raspberry Pi with a sensor, running Homebridge.

My goal was to build a Python script to read all values from all sensors (and light/power states), for storage into a db. My assumption was that homekit_python gives me that possibilty.
However, I just can't seem to grasp how this works?
I can do homekit.discover, which gives me my three 'bridges' (Hue, Tradfri, Raspberry Pi), but I can't seem to get the accessories from these bridges?

To confirm: is homekit_python able to get readings from Homekit accessories?

Thank you!

@jlusiardi
Copy link
Owner

Hey
You should be able to read the values after you paired the homekit python code as a Controller.
Joachim

@sderuiter
Copy link
Author

sderuiter commented Aug 27, 2019

Thank you Joachim, is there an example script available somewhere?
https://github.com/jlusiardi/homekit_python/blob/master/demoserver.py is for the server component, correct, not the Controller implementation?

@jlusiardi
Copy link
Owner

jlusiardi commented Aug 27, 2019

Hi @sderuiter,
you can achieve a regular polling of all paired accessories with the commands described in the readme:

  1. create empty controller data file with (python3 -m homekit.init_controller_storage -f ${PAIRINGDATAFILE}) or reuse an existing one
  2. pair all accessories with python3 -m homekit.pair -d ${DEVICEID} -p ${SETUPCODE} -f ${PAIRINGDATAFILE} -a ${ALIAS} or python3 -m homekit.pair -m ${MACADDRESS} -p ${SETUPCODE} -f ${PAIRINGDATAFILE} -a ${ALIAS}
  3. Iterate over all accessories and call python3 -m homekit.get_accessories -f ${PAIRINGDATAFILE} -a ${ALIAS}. This gives you all the characteristics to read for all accessories. Look for all characteristics with a pr. Obtain a list like
accessory characteristics
sensor_1 1.8 1.9
sensor_2 1.10 20.21
  1. For each polling run, iterate over the list and perform python3 -m homekit.get_characteristic -f ${PAIRINGDATAFILE} -a ${ALIAS} -c ${CHARACTERISTICS} (e.g. for sensor 1: python3 -m homekit.get_characteristic -f pairing_data.json -a sensor_1 -c 1.8 -c 1.9). This will give you the readings.

Downside: this might be slow since all the crypto for a session is redone every time. And the you will not be able to have a pairing with an iOS devices.

@Jc2k
Copy link
Collaborator

Jc2k commented Aug 27, 2019

We use homekit_python in home-assistant for the homekit_controller plugin and it indirectly does what you are looking for (there is a recorder component which logs entity states into a database). I don't know if it supports the device types you are interested in or does so in a format that is useful for you, but if it does it might save you some dev effort if it does.

As @jlusiardi said you can only pair with a single controller at once. And you can't read the values without pairing. If you do want to continue to use these devices with an iPhone etc at the same time then home-assistant has a homekit integration which can re-export them to your phone (this means home-assistant acts like a proxy between your phone and your bridges, and thus can record the data you want as well).

If you decide to go down the route of writing a custom script to poll the accessories note that homebridge support is currently problematic. When reusing an existing crypto session homebridge sends unsolicited events about once a minute to try and keep the connection alive. There is no mechanism to stop it short of hacking the homebridge code. The synchronous client cannot handle mixing request/reply semantics of polling and unsolicited event notifications on the same TCP connection. In the homebridge case this manifests as one poll returning a blank value (from the keep alive event) and the next poll returning the value of the previous poll. I started trying to fix this in #152, but ultimately for my use case i need a native asyncio client (we are working on this in #154). I think we reached the consensus that you shouldn't mix events and request/reply on the sync client, but we could make the situation slightly better and ignore event messages when handling a request/reply.

Right now if you did go down the home-assistant route then you would currently get database logging once a minute. Depending on how quickly we can get #154 stabilised this would then be enhanced with event support. In my testing this works great with Hue - Home Assistant reacts to me changing the light state in <1s - so your database would be updated almost immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants