commit e384a19eff817f9b7d1bcbba7633a146577de0a9 Author: zewelor Date: Wed Nov 22 17:28:01 2017 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5392b09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.yaml +__pycache__/ diff --git a/config.py b/config.py new file mode 100644 index 0000000..ae6b1c3 --- /dev/null +++ b/config.py @@ -0,0 +1,4 @@ +import yaml + +with open("config.yaml", "r") as f: + settings = yaml.load(f) diff --git a/config.yaml.example b/config.yaml.example new file mode 100644 index 0000000..f1277c5 --- /dev/null +++ b/config.yaml.example @@ -0,0 +1,10 @@ +mqtt: + host: 192.168.1.1 + username: user + password: password +workers: + thermostat: + args: + devices: + bedroom: 00:11:22:33:44:55 + topic_prefix: thermostats \ No newline at end of file diff --git a/gateway.py b/gateway.py new file mode 100644 index 0000000..ade964c --- /dev/null +++ b/gateway.py @@ -0,0 +1,10 @@ +from config import settings +from mqtt import MqttClient +from workers.thermostat import ThermostatWorker + +print(settings) + +mqtt = MqttClient() + +args = settings['workers']['thermostat']['args'] +mqtt.publish(ThermostatWorker(**args).status_update()) \ No newline at end of file diff --git a/mqtt.py b/mqtt.py new file mode 100644 index 0000000..c7e8bb4 --- /dev/null +++ b/mqtt.py @@ -0,0 +1,23 @@ +from config import settings +import paho.mqtt.publish as publish + +class MqttClient: + def __init__(self): + self._connected = False + self._config = settings['mqtt'] + + def publish(self, messages): + print(messages) + publish.multiple(messages, hostname=self.hostname, auth = {'username':self.username, 'password':self.password}) + + @property + def hostname(self): + return self._config['host'] + + @property + def username(self): + return self._config['username'] + + @property + def password(self): + return self._config['password'] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a560e6d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +paho-mqtt +python-eq3bt \ No newline at end of file diff --git a/workers/thermostat.py b/workers/thermostat.py new file mode 100644 index 0000000..88540be --- /dev/null +++ b/workers/thermostat.py @@ -0,0 +1,41 @@ +from eq3bt import Thermostat, Mode + +readableModes = { + 0: 'off', + 1: 'on', + 2: 'auto', + 3: 'manual', + 4: 'away', + 5: 'boost', +} + +monitoredAttrs = ["low_battery", "valve_state", "target_temperature"] + +class ThermostatWorker: + def __init__(self, devices=None, topic_prefix=None): + self._devices = devices + self._topic_prefix = topic_prefix + + def status_update(self): + ret = [] + for name, mac in self.devices.items(): + thermostat = Thermostat(mac) + thermostat.update() + + for attr in monitoredAttrs: + ret.append({ + 'topic': self.format_topic(name, attr), + 'payload': getattr(thermostat,attr), + }) + return ret + + @property + def devices(self): + return self._devices + + @property + def topic_prefix(self): + return self._topic_prefix + + def format_topic(self, device, attr): + return '/'.join([self.topic_prefix, device, attr])