Communication adaptions

This commit is contained in:
Dirk Alders 2023-11-03 17:02:45 +01:00
parent 4ecd37f1ca
commit 944824d502

View File

@ -73,7 +73,7 @@ class sispmctl(object):
self.send_state(i) self.send_state(i)
def send_state(self, output): def send_state(self, output):
topic = config.MQTT_TOPIC + "/status/" + str(output) topic = config.MQTT_TOPIC + "/output/" + str(output)
logger.debug("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1])) logger.debug("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
try: try:
self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false") self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
@ -85,23 +85,18 @@ class mqtt_powerplug(mqtt.mqtt_client):
def __init__(self): def __init__(self):
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS) mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
for target in ["1", "2", "3", "4", "all"]: for target in ["1", "2", "3", "4", "all"]:
self.add_callback(config.MQTT_TOPIC + "/set/" + target, self.__set__) self.add_callback(config.MQTT_TOPIC + "/output/%s/set" % target, self.__set__)
self.add_callback(config.MQTT_TOPIC + "/toggle/" + target, self.__toggle__)
# #
self.__sc__ = sispmctl(self) self.__sc__ = sispmctl(self)
def __set__(self, client, userdata, message): def __set__(self, client, userdata, message):
output = message.topic.split("/")[-1] output = message.topic.split("/")[-2]
if message.topic.find("set") >= 0: if message.payload == b"true" or message.payload == b"false":
state = message.payload == b"true" self.__sc__.set_out_state(output, message.payload == b"true")
logger.info("Received request to set output channel %s to state %s", output, str(state)) elif message.payload.lower() == b'toggle':
self.__sc__.set_out_state(output, state)
def __toggle__(self, client, userdata, message):
output = message.topic.split("/")[-1]
logger.info("Received request to toggle output channel %s", output)
if message.payload != b"false":
self.__sc__.toggle_out_state(output) self.__sc__.toggle_out_state(output)
else:
logger.warning("Ignoring set command due to payload: %s", message.payload)
def __del__(self): def __del__(self):
self.__client__.loop_stop() # stop the loop self.__client__.loop_stop() # stop the loop