From f7eb01be69e254ec9175ef3a4afe5aa2882c2a13 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 19 Oct 2021 21:34:55 +0200 Subject: [PATCH] Added home assistant mqtt interface --- requirements.txt | 5 ++++- src/homeassistantcounter.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/homeassistantcounter.py diff --git a/requirements.txt b/requirements.txt index b623586..bc4d6c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,5 @@ smbus2 -vl53l1x \ No newline at end of file +vl53l1x + +# For Home Assistant MQTT Sensor +paho-mqtt diff --git a/src/homeassistantcounter.py b/src/homeassistantcounter.py new file mode 100644 index 0000000..1b69531 --- /dev/null +++ b/src/homeassistantcounter.py @@ -0,0 +1,31 @@ +from peoplecounter import PeopleCounter +from sensor.vl53l1xsensor import VL53L1XSensor +import paho.mqtt.client as mqtt + + +HA_URL = "" +HA_PORT = 1883 +HA_TOPIC = "" + + +# Setup connection to HA +mqttClient = mqtt.Client() +mqttClient.connect(HA_URL, HA_PORT) +mqttClient.loop_start() # Keep conneciton alive + + +def countChange(change: int) -> None: + """Called when people count change is detected. + Sends update to the initialized HA instance. + + Args: + change (int): Number of people leaving (<0) or entering (>0) a room. + """ + # Send update to HA + mqttClient.publish(HA_TOPIC, change) + + +# Setup people count sensor +counter = PeopleCounter(VL53L1XSensor()) +counter.hookCounting(countChange) +counter.run()