validate and publish always with all channels; simplifications
This commit is contained in:
parent
c796d1d47a
commit
1ccc230b50
44
powerplug.py
44
powerplug.py
@ -26,35 +26,48 @@ class sispmctl(object):
|
|||||||
logger.error("sispm error code %d", grepexc.returncode)
|
logger.error("sispm error code %d", grepexc.returncode)
|
||||||
else:
|
else:
|
||||||
logger.info('sispmctl all channels switched off')
|
logger.info('sispmctl all channels switched off')
|
||||||
|
self.publish_states()
|
||||||
|
|
||||||
|
def __filter_output_parameter__(self, output):
|
||||||
|
try:
|
||||||
|
return int(output)
|
||||||
|
except ValueError:
|
||||||
|
return output
|
||||||
|
|
||||||
def set_out_state(self, output, state):
|
def set_out_state(self, output, state):
|
||||||
if output in range(1,5):
|
if output == "all":
|
||||||
|
state = [state, state, state, state]
|
||||||
|
|
||||||
if self.get_out_state(output) != state:
|
if self.get_out_state(output) != state:
|
||||||
try:
|
try:
|
||||||
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
|
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True or state == [True, True, True, 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", grepexc.returncode)
|
||||||
else:
|
else:
|
||||||
logger.info('sispmctl channel %d changed to %s', output, state)
|
logger.info('sispmctl channel %s changed to %s', output, state)
|
||||||
cnt = 0
|
self.publish_states()
|
||||||
while cnt < 3 and self.__state__[output - 1] != state:
|
|
||||||
cnt += 1
|
def publish_states(self):
|
||||||
try:
|
try:
|
||||||
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", str(output)])
|
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
|
||||||
except subprocess.CalledProcessError as grepexc:
|
except subprocess.CalledProcessError as grepexc:
|
||||||
logger.error("sispm error code %d", grepexc.returncode)
|
logger.error("sispm error code %d", grepexc.returncode)
|
||||||
else:
|
else:
|
||||||
self.__state__[output - 1] = out_txt.decode("UTF-8").split("\n")[1].split("\t")[1] == "on"
|
print(out_txt)
|
||||||
time.sleep(.2)
|
for i in range(1, 5):
|
||||||
self.send_state(output)
|
self.__state__[i-1] = out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on"
|
||||||
|
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 + "/status/" + str(output)
|
||||||
logger.info("Sending Powerplug status information of plug %d to mqtt %s = %s", output, topic, str(self.get_out_state(output)))
|
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.get_out_state(output)))
|
||||||
self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
|
self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
|
||||||
|
|
||||||
def get_out_state(self, output):
|
def get_out_state(self, output):
|
||||||
return self.__state__[output - 1]
|
if output in range(1,5):
|
||||||
|
return self.__state__[int(output) - 1]
|
||||||
|
else:
|
||||||
|
return self.__state__
|
||||||
|
|
||||||
|
|
||||||
class mqtt_powerplug(object):
|
class mqtt_powerplug(object):
|
||||||
@ -62,7 +75,8 @@ class mqtt_powerplug(object):
|
|||||||
"set/1",
|
"set/1",
|
||||||
"set/2",
|
"set/2",
|
||||||
"set/3",
|
"set/3",
|
||||||
"set/4"
|
"set/4",
|
||||||
|
"set/all"
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -79,9 +93,9 @@ 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 = int(message.topic.split("/")[-1])
|
output = message.topic.split("/")[-1]
|
||||||
state = message.payload == b"true"
|
state = message.payload == b"true"
|
||||||
logger.info("Received request to set output channel %d to state %s", output, str(state))
|
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
||||||
self.__sc__.set_out_state(output, state)
|
self.__sc__.set_out_state(output, state)
|
||||||
else:
|
else:
|
||||||
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user