Pārlūkot izejas kodu

Toggle added

master
Dirk Alders 2 gadus atpakaļ
vecāks
revīzija
33a68f3c0e
1 mainītis faili ar 21 papildinājumiem un 4 dzēšanām
  1. 21
    4
      powerplug.py

+ 21
- 4
powerplug.py Parādīt failu

@@ -51,6 +51,13 @@ class sispmctl(object):
51 51
                 logger.info('sispmctl channel %s changed to %s', output, state)
52 52
                 self.publish_states()
53 53
 
54
+    def toggle_out_state(self, output):
55
+        if output == "all":
56
+            for i in range(1,5):
57
+                self.toggle_out_state(str(i))
58
+        else:
59
+            self.set_out_state(output, not self.__state__[int(output) - 1])
60
+
54 61
     def get_out_states(self):
55 62
         try:
56 63
             out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
@@ -79,7 +86,12 @@ class mqtt_powerplug(object):
79 86
         "set/2",
80 87
         "set/3",
81 88
         "set/4",
82
-        "set/all"
89
+        "set/all",
90
+        "toggle/1",
91
+        "toggle/2",
92
+        "toggle/3",
93
+        "toggle/4",
94
+        "toggle/all",
83 95
     ]
84 96
 
85 97
     def __init__(self):
@@ -97,9 +109,14 @@ class mqtt_powerplug(object):
97 109
     def __receive__(self, client, userdata, message):
98 110
         if message.topic in self.__topics__:
99 111
             output = message.topic.split("/")[-1]
100
-            state = message.payload == b"true"
101
-            logger.info("Received request to set output channel %s to state %s", output, str(state))
102
-            self.__sc__.set_out_state(output, state)
112
+            if message.topic.find("set") >= 0:
113
+                state = message.payload == b"true"
114
+                logger.info("Received request to set output channel %s to state %s", output, str(state))
115
+                self.__sc__.set_out_state(output, state)
116
+            elif message.topic.find("toggle") >= 0:
117
+                logger.info("Received request to toggle output channel %s", output)
118
+                if message.payload != b"false":
119
+                    self.__sc__.toggle_out_state(output)
103 120
         else:
104 121
             logger.warning("Ignoring unknown mqtt topic %s", message.topic)
105 122
 

Notiek ielāde…
Atcelt
Saglabāt