Browse Source

Communication adaptions

master
Dirk Alders 1 year ago
parent
commit
944824d502
1 changed files with 8 additions and 13 deletions
  1. 8
    13
      powerplug.py

+ 8
- 13
powerplug.py View File

@@ -73,7 +73,7 @@ class sispmctl(object):
73 73
             self.send_state(i)
74 74
 
75 75
     def send_state(self, output):
76
-        topic = config.MQTT_TOPIC + "/status/" + str(output)
76
+        topic = config.MQTT_TOPIC + "/output/" + str(output)
77 77
         logger.debug("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
78 78
         try:
79 79
             self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
@@ -85,23 +85,18 @@ class mqtt_powerplug(mqtt.mqtt_client):
85 85
     def __init__(self):
86 86
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
87 87
         for target in ["1", "2", "3", "4", "all"]:
88
-            self.add_callback(config.MQTT_TOPIC + "/set/" + target, self.__set__)
89
-            self.add_callback(config.MQTT_TOPIC + "/toggle/" + target, self.__toggle__)
88
+            self.add_callback(config.MQTT_TOPIC + "/output/%s/set" % target, self.__set__)
90 89
         #
91 90
         self.__sc__ = sispmctl(self)
92 91
 
93 92
     def __set__(self, client, userdata, message):
94
-        output = message.topic.split("/")[-1]
95
-        if message.topic.find("set") >= 0:
96
-            state = message.payload == b"true"
97
-            logger.info("Received request to set output channel %s to state %s", output, str(state))
98
-            self.__sc__.set_out_state(output, state)
99
-
100
-    def __toggle__(self, client, userdata, message):
101
-        output = message.topic.split("/")[-1]
102
-        logger.info("Received request to toggle output channel %s", output)
103
-        if message.payload != b"false":
93
+        output = message.topic.split("/")[-2]
94
+        if message.payload == b"true" or message.payload == b"false":
95
+            self.__sc__.set_out_state(output, message.payload == b"true")
96
+        elif message.payload.lower() == b'toggle':
104 97
             self.__sc__.toggle_out_state(output)
98
+        else:
99
+            logger.warning("Ignoring set command due to payload: %s", message.payload)
105 100
 
106 101
     def __del__(self):
107 102
         self.__client__.loop_stop()                                              # stop the loop

Loading…
Cancel
Save