Hi,
I use homekit/homebridge for all my home automation, including exposing
the cameras via the homebridge-camera-ffmpeg plugin. I've been meaning
to, for ages now, figure out how I can integrate camect events into
homebridge (I like to have all of my notifications via a single
mechanism, and of course I can tie it into home automation). I have
toyed with either using the camera's motion detection or motioneye's
motion detection... but both are pretty basic, and we all have camect
for the awesome noise filtering that it does, so I wanted to use camect.
Well this turned out to be pretty trivial. Firstly, the
homebridge-camera-ffmpeg plugin supports getting motion events via MQTT
(
https://sunoo.github.io/homebridge-camera-ffmpeg/automation/mqtt.html).
I already run an MQTT server for various other home automation devices,
so that seemed like the easiest path to go, although it should be noted
that you could also use a HTTP based approach.
So, I needed a way of getting events from camect into MQTT... a little
bit of python hacking later and we have the following eye-sore attached
below. It isn't particularly pretty, but 42 lines of code later, I get
events via homekit!
(Yes it needs a little work, for example it treats any event as motion, which may not be accurate, and the whole sleep() thing is horrid).
One really nice thing about this is I have multiple cameras in some
areas, and homekit is smart enough to only send a single notification
about motion in one area, regardless of how many cameras are pointed at it.
Thanks to the camect guys for providing camect-py, and hopefully this
helps someone out that is thinking of doing something similar.
Cheers
Nick
import camect
import time
import paho.mqtt.client as mqtt
camect_host="your_ip_here:443"
camect_passwd="xxxxxx"
camect_user="admin"
mqtt_host="your_ip_here"
mqtt_port=1883
mqtt_user=None
mqtt_passwd=None
mqtt_topic="homebridge"
def post_event_to_mqtt(client, evt):
client.publish(mqtt_topic+"/motion", evt["cam_name"])
print(evt)
# The callback for when the client receives a CONNACK response from the
server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
# Connect to mqtt
client = mqtt.Client()
client.on_connect = on_connect
client.loop_start()
client.connect(mqtt_host, mqtt_port, 60)
home = camect.Home(camect_host, camect_user, camect_passwd)
home.add_event_listener(lambda evt: post_event_to_mqtt(client,evt))
progress=1
while True:
time.sleep(10)
print('\r{}% done...'.format(progress), end='', flush=True)
progress+=1