Support for commands handling

This commit is contained in:
zewelor
2017-11-26 21:57:12 +01:00
parent 09d1332b92
commit 6a1c7b0181
8 changed files with 160 additions and 70 deletions
+76 -19
View File
@@ -1,41 +1,98 @@
from builtins import staticmethod
from mqtt import MqttMessage
from workers.base import BaseWorker
REQUIREMENTS = ['python-eq3bt']
monitoredAttrs = ["low_battery", "valve_state", "target_temperature"]
class ThermostatWorker:
def __init__(self, devices):
self._devices = devices
STATE_BOOST = 'boost'
STATE_AWAY = 'away'
STATE_MANUAL = 'manual'
def status_update(self):
from eq3bt import Thermostat, Mode
class ThermostatWorker(BaseWorker):
class ModesMapper():
from eq3bt import Mode
haModesMapping = {
MAPPED_MODES = {
Mode.Closed: 'off',
Mode.Open: 'on',
Mode.Auto: 'auto',
Mode.Manual: 'manual',
Mode.Away: 'away',
Mode.Boost: 'boost',
Mode.Manual: STATE_MANUAL,
Mode.Away: STATE_AWAY,
Mode.Boost: STATE_BOOST,
}
ret = []
REVERSED_MODES = {v: k for k, v in MAPPED_MODES.items()}
@classmethod
def get_mapping(cls, mode):
return cls.MAPPED_MODES[mode]
@classmethod
def get_reverse_mapping(cls, mode):
return cls.REVERSED_MODES[mode]
@staticmethod
def away_mode_on_off(mode):
if mode == STATE_AWAY:
return 'ON'
else:
return 'OFF'
@staticmethod
def on_off_to_mode(on_off):
if on_off == 'ON':
return STATE_AWAY
else:
return 'auto'
def _setup(self):
from eq3bt import Thermostat
for name, mac in self.devices.items():
thermostat = Thermostat(mac)
thermostat.update()
self.devices[name] = Thermostat(mac)
for attr in monitoredAttrs:
ret.append(MqttMessage(topic=self.format_topic(name, attr), payload=getattr(thermostat, attr)))
ret.append(MqttMessage(topic=self.format_topic(name, 'mode'), payload=haModesMapping[thermostat.mode]))
ret.append(MqttMessage(topic=self.format_topic(name, 'away'), payload='ON' if thermostat.mode == Mode.Away else 'OFF'))
def status_update(self):
ret = []
for name, thermostat in self.devices.items():
ret += self.update_device_state(name, thermostat)
return ret
@property
def devices(self):
return self._devices
def on_command(self, topic, value):
_, device_name, method = topic.split('/')
thermostat = self.devices[device_name]
if method == STATE_AWAY:
method = "mode"
value = ModesMapper.on_off_to_mode(value)
# It needs to be on separate if because first if can change method
if method == "mode":
value = ModesMapper.get_reverse_mapping(value)
elif method == "target_temperature":
value = float(value)
setattr(thermostat, method, value)
return self.update_device_state(device_name, thermostat)
def update_device_state(self, name, thermostat):
thermostat.update()
ret = []
for attr in monitoredAttrs:
ret.append(MqttMessage(topic=self.format_topic(name, attr), payload=getattr(thermostat, attr)))
ret.append(MqttMessage(topic=self.format_topic(name, 'mode'), payload=self.ModesMapper.get_mapping(thermostat.mode)))
ret.append(MqttMessage(topic=self.format_topic(name, 'away'), payload=self.ModesMapper.away_mode_on_off(thermostat.mode)))
return ret
def device_for(self, mac):
return eq3bt.Thermostat(mac)
def format_topic(self, device, attr):
return '/'.join([device, attr])