Implemented home assistant mqtt binder

This commit is contained in:
Maximilian Giller 2021-10-19 22:10:54 +02:00
parent f7eb01be69
commit 8650c01582
2 changed files with 13 additions and 2 deletions

View file

@ -3,3 +3,4 @@ vl53l1x
# For Home Assistant MQTT Sensor
paho-mqtt
homeassistant-mqtt-binding

View file

@ -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