Initial commit
This commit is contained in:
81
controller.py
Normal file
81
controller.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from mqtt import MQTTConnection
|
||||
from device import Device
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
class Controller:
|
||||
def __init__(self,
|
||||
broker: str,
|
||||
mac: str | None = None,
|
||||
updateFrequency: int = 5,
|
||||
port: int = 1883,
|
||||
topic: str = "thingy52",
|
||||
clientId: str = "thingy52",
|
||||
username: str | None = None,
|
||||
password: str | None = None,
|
||||
caCertPath: str | None = None,
|
||||
certPath: str | None = None,
|
||||
keyPath: str | None = None,
|
||||
mqttMinReconnectDelay: int = 1,
|
||||
mqttMaxReconnectDelay: int = 60,
|
||||
mqttReconnectAttempts: int = 20,
|
||||
mqttReconnectDelayMultiplier: int = 2,
|
||||
sensors: list[str] = [
|
||||
"battery",
|
||||
"temperature",
|
||||
"pressure",
|
||||
"humidity",
|
||||
"eco2",
|
||||
"tvoc",
|
||||
"button",
|
||||
"taps",
|
||||
"tapDirection",
|
||||
"orient",
|
||||
"stepCount",
|
||||
"colour"
|
||||
],
|
||||
temperatureOffset: int = 0
|
||||
):
|
||||
self.__mqtt = MQTTConnection(
|
||||
broker,
|
||||
port,
|
||||
topic,
|
||||
clientId,
|
||||
username,
|
||||
password,
|
||||
caCertPath,
|
||||
certPath,
|
||||
keyPath,
|
||||
mqttMinReconnectDelay,
|
||||
mqttMaxReconnectDelay,
|
||||
mqttReconnectAttempts,
|
||||
mqttReconnectDelayMultiplier,
|
||||
self.__stop
|
||||
)
|
||||
self.__thingy = Device(
|
||||
mac,
|
||||
updateFrequency,
|
||||
mqttMinReconnectDelay,
|
||||
mqttMaxReconnectDelay,
|
||||
mqttReconnectAttempts,
|
||||
mqttReconnectDelayMultiplier,
|
||||
self.__stop,
|
||||
sensors,
|
||||
temperatureOffset
|
||||
)
|
||||
|
||||
try:
|
||||
self.__mqtt.publish("available", "online")
|
||||
self.__mqtt.subscribe("homeassistant/available", lambda msg : self.__mqtt.publish("available", "online") if (msg == "online") else None, False)
|
||||
|
||||
for sensor in sensors:
|
||||
self.__thingy.subscribe(sensor, lambda val, sensor=sensor : self.__mqtt.publish(sensor, val))
|
||||
|
||||
self.__thingy.start()
|
||||
finally:
|
||||
self.__stop()
|
||||
|
||||
def __stop(self):
|
||||
self.__thingy.disconnect()
|
||||
self.__mqtt.disconnect()
|
||||
Reference in New Issue
Block a user