|
@@ -32,6 +32,7 @@
|
32
|
32
|
|
33
|
33
|
from devices.base import base
|
34
|
34
|
import json
|
|
35
|
+import task
|
35
|
36
|
|
36
|
37
|
|
37
|
38
|
class shelly_sw1(base):
|
|
@@ -49,6 +50,8 @@ class shelly_sw1(base):
|
49
|
50
|
super().__init__(mqtt_client, topic)
|
50
|
51
|
self["state"] = "off"
|
51
|
52
|
#
|
|
53
|
+ self.__auto_off__ = None
|
|
54
|
+ #
|
52
|
55
|
self.mqtt_client.add_callback(self.topic + '/relay/0/command', self.__rx_set__)
|
53
|
56
|
|
54
|
57
|
def __rx_set__(self, client, userdata, message):
|
|
@@ -57,10 +60,24 @@ class shelly_sw1(base):
|
57
|
60
|
self.logger.info("Received set data for %s: %s", key, repr(data))
|
58
|
61
|
self.__set__(key, data)
|
59
|
62
|
self.send_device_status(key)
|
60
|
|
- if key == "relay/0" and data.lower() == "on":
|
61
|
|
- self.power_on()
|
|
63
|
+ if key == "relay/0":
|
|
64
|
+ if data.lower() == "on":
|
|
65
|
+ self.power_on()
|
|
66
|
+ if self.__auto_off__ is not None:
|
|
67
|
+ self.__auto_off__.run()
|
|
68
|
+ else:
|
|
69
|
+ if self.__auto_off__ is not None:
|
|
70
|
+ self.__auto_off__.stop()
|
62
|
71
|
|
63
|
72
|
def send_device_status(self, key):
|
64
|
73
|
data = self[key]
|
65
|
74
|
self.logger.info("Sending status for %s: %s", key, repr(data))
|
66
|
75
|
self.mqtt_client.send(self.topic + '/' + key, data)
|
|
76
|
+
|
|
77
|
+ def auto_off(self, sec):
|
|
78
|
+ self.__auto_off__ = task.delayed(sec, self.__auto_off_func__)
|
|
79
|
+
|
|
80
|
+ def __auto_off_func__(self):
|
|
81
|
+ if self.get(self.PROPERTIES[0]).lower() != 'off':
|
|
82
|
+ self.__set__(self.PROPERTIES[0], "off")
|
|
83
|
+ self.send_device_status(self.PROPERTIES[0])
|