From 8650c01582607201e6f27cbedfbb15f64187ae3c Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 19 Oct 2021 22:10:54 +0200 Subject: [PATCH] Implemented home assistant mqtt binder --- requirements.txt | 1 + src/homeassistantcounter.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index bc4d6c6..a852959 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ vl53l1x # For Home Assistant MQTT Sensor paho-mqtt +homeassistant-mqtt-binding diff --git a/src/homeassistantcounter.py b/src/homeassistantcounter.py index 1b69531..0dfae97 100644 --- a/src/homeassistantcounter.py +++ b/src/homeassistantcounter.py @@ -1,11 +1,14 @@ from peoplecounter import PeopleCounter from sensor.vl53l1xsensor import VL53L1XSensor import paho.mqtt.client as mqtt +from HaMqtt.MQTTSensor import MQTTSensor +import logging HA_URL = "" HA_PORT = 1883 -HA_TOPIC = "" +HA_SENSOR_NAME = "" +HA_SENSOR_ID = "" # Setup connection to HA @@ -13,6 +16,10 @@ mqttClient = mqtt.Client() mqttClient.connect(HA_URL, HA_PORT) mqttClient.loop_start() # Keep conneciton alive +# Setup mqtt binding +sensor = MQTTSensor(HA_SENSOR_NAME, HA_SENSOR_ID, mqttClient) +logging.debug(f'Connected to topic {sensor.state_topic}') + def countChange(change: int) -> None: """Called when people count change is detected. @@ -22,7 +29,10 @@ def countChange(change: int) -> None: change (int): Number of people leaving (<0) or entering (>0) a room. """ # Send update to HA - mqttClient.publish(HA_TOPIC, change) + global sensor + sensor.publish_state(change) + + logging.debug(f'People count changed by {change}') # Setup people count sensor