Toggle added

This commit is contained in:
Dirk Alders 2022-08-24 13:16:30 +01:00
parent cd9ab64d54
commit 33a68f3c0e

View File

@ -51,6 +51,13 @@ class sispmctl(object):
logger.info('sispmctl channel %s changed to %s', output, state) logger.info('sispmctl channel %s changed to %s', output, state)
self.publish_states() 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): def get_out_states(self):
try: try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"]) out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
@ -79,7 +86,12 @@ class mqtt_powerplug(object):
"set/2", "set/2",
"set/3", "set/3",
"set/4", "set/4",
"set/all" "set/all",
"toggle/1",
"toggle/2",
"toggle/3",
"toggle/4",
"toggle/all",
] ]
def __init__(self): def __init__(self):
@ -97,9 +109,14 @@ class mqtt_powerplug(object):
def __receive__(self, client, userdata, message): def __receive__(self, client, userdata, message):
if message.topic in self.__topics__: if message.topic in self.__topics__:
output = message.topic.split("/")[-1] output = message.topic.split("/")[-1]
state = message.payload == b"true" if message.topic.find("set") >= 0:
logger.info("Received request to set output channel %s to state %s", output, str(state)) state = message.payload == b"true"
self.__sc__.set_out_state(output, state) 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: else:
logger.warning("Ignoring unknown mqtt topic %s", message.topic) logger.warning("Ignoring unknown mqtt topic %s", message.topic)