Browse Source

Toggle added

master
Dirk Alders 2 years ago
parent
commit
33a68f3c0e
1 changed files with 21 additions and 4 deletions
  1. 21
    4
      powerplug.py

+ 21
- 4
powerplug.py View File

51
                 logger.info('sispmctl channel %s changed to %s', output, state)
51
                 logger.info('sispmctl channel %s changed to %s', output, state)
52
                 self.publish_states()
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
     def get_out_states(self):
61
     def get_out_states(self):
55
         try:
62
         try:
56
             out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
63
             out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
79
         "set/2",
86
         "set/2",
80
         "set/3",
87
         "set/3",
81
         "set/4",
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
     def __init__(self):
97
     def __init__(self):
97
     def __receive__(self, client, userdata, message):
109
     def __receive__(self, client, userdata, message):
98
         if message.topic in self.__topics__:
110
         if message.topic in self.__topics__:
99
             output = message.topic.split("/")[-1]
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
         else:
120
         else:
104
             logger.warning("Ignoring unknown mqtt topic %s", message.topic)
121
             logger.warning("Ignoring unknown mqtt topic %s", message.topic)
105
 
122
 

Loading…
Cancel
Save