45 lines
956 B
Python
45 lines
956 B
Python
|
from threading import Timer
|
||
|
|
||
|
import paho.mqtt.client as mqtt
|
||
|
|
||
|
from .display import Display
|
||
|
from .sensors import Sensors
|
||
|
|
||
|
dt = 10
|
||
|
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||
|
hat_sensors = Sensors(mqttc, "oin", "Oin")
|
||
|
hat_display = Display(mqttc, "oin", "Oin")
|
||
|
|
||
|
|
||
|
@mqttc.connect_callback()
|
||
|
def on_connect(client, userdata, flags, reason_code, properties):
|
||
|
print(f"Connected with result code {reason_code}")
|
||
|
|
||
|
hat_sensors.publish_discovery()
|
||
|
hat_display.publish_discovery()
|
||
|
|
||
|
hat_sensors.publish_online()
|
||
|
hat_display.publish_online()
|
||
|
|
||
|
timer = Timer(0, send_data)
|
||
|
timer.start()
|
||
|
|
||
|
|
||
|
@mqttc.message_callback()
|
||
|
def on_message(*args, **kwargs):
|
||
|
hat_display.on_message(*args, **kwargs)
|
||
|
|
||
|
|
||
|
mqttc.username_pw_set(username="oin", password="n+Bi58l7LxbH5nEJ")
|
||
|
mqttc.connect("homeassistant.local", 1883, 60)
|
||
|
|
||
|
|
||
|
def send_data():
|
||
|
timer = Timer(dt, send_data)
|
||
|
timer.start()
|
||
|
|
||
|
hat_sensors.publish_state()
|
||
|
|
||
|
|
||
|
mqttc.loop_forever()
|