From 33a68f3c0edd7aa385d5fe35c92027245dad1e43 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Wed, 24 Aug 2022 13:16:30 +0100 Subject: [PATCH] Toggle added --- powerplug.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/powerplug.py b/powerplug.py index 241cacf..95161af 100644 --- a/powerplug.py +++ b/powerplug.py @@ -51,6 +51,13 @@ class sispmctl(object): logger.info('sispmctl channel %s changed to %s', output, state) self.publish_states() + def toggle_out_state(self, output): + if output == "all": + for i in range(1,5): + self.toggle_out_state(str(i)) + else: + self.set_out_state(output, not self.__state__[int(output) - 1]) + def get_out_states(self): try: out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"]) @@ -79,7 +86,12 @@ class mqtt_powerplug(object): "set/2", "set/3", "set/4", - "set/all" + "set/all", + "toggle/1", + "toggle/2", + "toggle/3", + "toggle/4", + "toggle/all", ] def __init__(self): @@ -97,9 +109,14 @@ class mqtt_powerplug(object): def __receive__(self, client, userdata, message): if message.topic in self.__topics__: output = message.topic.split("/")[-1] - state = message.payload == b"true" - logger.info("Received request to set output channel %s to state %s", output, str(state)) - self.__sc__.set_out_state(output, state) + if message.topic.find("set") >= 0: + state = message.payload == b"true" + logger.info("Received request to set output channel %s to state %s", output, str(state)) + self.__sc__.set_out_state(output, state) + elif message.topic.find("toggle") >= 0: + logger.info("Received request to toggle output channel %s", output) + if message.payload != b"false": + self.__sc__.toggle_out_state(output) else: logger.warning("Ignoring unknown mqtt topic %s", message.topic)