sispm improvements

This commit is contained in:
Dirk Alders 2022-12-19 06:49:57 +01:00
parent 4e3b0cd945
commit 4ecd37f1ca

View File

@ -40,7 +40,7 @@ class sispmctl(object):
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True else "-f", output]).decode('UTF-8')
except subprocess.CalledProcessError as grepexc:
logger.error("sispm error code %d", grepexc.returncode)
logger.error("sispm error code %d while changing state of channel %s", grepexc.returncode, output)
else:
logger.info('sispmctl channel %s changed to %s', output, state)
self.publish_states()
@ -53,15 +53,19 @@ class sispmctl(object):
self.set_out_state(output, not self.__state__[int(output) - 1])
def get_out_states(self):
i = 0
while i < 10: # number of tries
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
except subprocess.CalledProcessError as grepexc:
logger.error("sispm error code %d", grepexc.returncode)
logger.error("sispm error code %d while getting states", grepexc.returncode)
else:
rv = []
for i in range(1, 5):
rv.append(out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on")
return rv
logger.warning("Could not get state of powerplugs")
return self.__state__
def publish_states(self):
self.__state__ = self.get_out_states()
@ -70,11 +74,11 @@ class sispmctl(object):
def send_state(self, output):
topic = config.MQTT_TOPIC + "/status/" + str(output)
logger.info("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:
self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending state information information")
logger.warning("Error while sending state information via mqtt")
class mqtt_powerplug(mqtt.mqtt_client):