65 rivejä
2.0 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from devices.base import base
class powerplug(base):
PROPERTIES = [
"output/1",
"output/2",
"output/3",
"output/4",
]
def __init__(self, mqtt_client, topic):
super().__init__(mqtt_client, topic)
#
for i in range(0, 4):
self[self.PROPERTIES[i]] = 'false'
self.mqtt_client.add_callback(self.topic + '/output/%d/set' % (i + 1), self.__rx_set__)
#
cmd_base = self.topic.replace('/', '.') + '.'
self.user_cmds = {
cmd_base + 'toggle1': self.__ui_toggle_output_0__,
cmd_base + 'toggle2': self.__ui_toggle_output_1__,
cmd_base + 'toggle3': self.__ui_toggle_output_2__,
cmd_base + 'toggle4': self.__ui_toggle_output_3__,
}
def __rx_set__(self, client, userdata, message):
data = message.payload.decode('utf-8')
key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
self.logger.info("Received set data for %s: %s", key, repr(data))
self.__set__(key, data)
def __set__(self, key, data):
base.__set__(self, key, data)
#
self.send_device_status(key)
if key.startswith("output/"):
if data == "true":
self.power_on(key)
def send_device_status(self, key):
data = self[key]
self.logger.info("Sending status for %s: %s", key, repr(data))
self.mqtt_client.send(self.topic + '/' + key, data)
def __ui_toggle_output__(self, num):
key = self.PROPERTIES[num]
self.__set__(key, 'false' if self.get(key).lower() == 'true' else 'true')
def __ui_toggle_output_0__(self, *args):
self.__ui_toggle_output__(0)
def __ui_toggle_output_1__(self, *args):
self.__ui_toggle_output__(1)
def __ui_toggle_output_2__(self, *args):
self.__ui_toggle_output__(2)
def __ui_toggle_output_3__(self, *args):
self.__ui_toggle_output__(3)