How to register / listen to python events

openhabian 3.2.0 - Release Build

I do use homekit_python (GitHub - jlusiardi/homekit_python: A python implementation to work as both HomeKit controller and accessory.) to control some homekit devices within openHAB. Commands are sent by executeCommandLine(). Here an example:

executeCommandLine(Duration.ofSeconds(10), "python3", "-m", "homekit.put_characteristic", "-f", "/home/openhabian/velux", "-a", "velux", "-c", ident, targetPosition)

This all works perfectly fine. However, instead of polling for changes I would like to get notified of any changes by an event. homekit_python does provide an option to register for events but I have no clue how to get these events into openHAB to be able to trigger rules.

Out of homekit_python documentation (GitHub - jlusiardi/homekit_python: A python implementation to work as both HomeKit controller and accessory.):

get_events

This tool will register with an accessory and listen to the events send back from it.

Usage

python3 -m homekit.get_events -f ${PAIRINGDATAFILE} -a ${ALIAS} -c ${Characteristics} \ [--adapter ${ADAPTER}] [--log ${LOGLEVEL}]

The option -f specifies the file that contains the pairing data.

The option -a specifies the alias for the device.

The option -c specifies the characteristics to change. The format is <aid>.<cid>. This option can be repeated to listen to multiple characteristics with one call.

For example, you can listen to characteristics 1.8 (on characteristic), 1.22 (1 REALTIME_ENERGY) and 1.23 (2 CURRENT_HOUR_DATA) of the Koogeek P1EU Plug with:

python3 -m homekit.get_events -f koogeek.json -a koogeek -c 1.8 -c 1.22 -c 1.23

This results in

event for 1.8: True
event for 1.22: 6.0
event for 1.23: 0.01666
event for 1.22: 17.0
event for 1.23: 0.06388
event for 1.23: 0.11111
event for 1.22: 18.0
event for 1.23: 0.16111
event for 1.8: False

Executing this command in a terminal does show the events properly.
Is there an option to listen on these events and trigger any actions within openHAB?
Any help is highly appreciated.

Is there a reason you are not using the Homekit integration openHAB provides? HomeKit Add-on - System Integrations | openHAB

You’ll need to flip the flow around. Rather than having openHAB call the script periodically, your script will need to run on its own and push the events to openHAB. A quick scan of that script shows you’ll probably need to add that capability to the code yourself (I saw mention of plugins).

Thanks for the fast feedback.

My goal is to control devices with a homekit interface from openHAB. The homekit addon does provide options to control openHAB (and connected items) from homekit. To me this sounds like the opposite way. Also I do not have any equipment from Apple. However, maybe I missed something?

To be more precise: I would like to integrate my skylights (Velux) into openHAB. There is a central gateway (KIX 300) which is homekit compatible. Here is a discussion about integrating it in openHAB with a recommondation:

You are completely right - the flow needs to be flipped around. Do you have any further information about pushing events to openHAB from a script? If it is possible by small modifications of the python script I’d be happy to try.

You’ve two main options. Make the script talk MQTT and configure the MQTT binding, or have the script push Item updates through openHAB’s REST API. I’ve now idea how easily either would be to add to this script.