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