powerplug-energenie/powerplug.py

113 lines
4.2 KiB
Python
Raw Normal View History

2022-07-22 09:14:52 +02:00
import config
import logging
2022-09-12 20:56:25 +01:00
import mqtt
2022-07-22 09:14:52 +02:00
import report
import socket
2022-07-22 09:14:52 +02:00
import subprocess
import time
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
class sispmctl(object):
AMP_CHANNEL = 0
2022-07-24 08:49:03 +02:00
def __init__(self, mqtt_client):
2022-07-22 09:14:52 +02:00
logger.info("Starting sispmctl module...")
2022-07-24 08:49:03 +02:00
self.__mqtt_client__ = mqtt_client
2022-07-22 09:14:52 +02:00
self.__state_requests__ = [{}, {}, {}, {}]
self.publish_states()
def __filter_output_parameter__(self, output):
try:
return int(output)
except ValueError:
return output
2022-07-22 09:14:52 +02:00
def set_out_state(self, output, state):
if output == "all":
if self.__state__ != [state, state, state, state]:
self.__set_out_state__(output, state)
else:
if self.__state__[int(output) - 1] != state:
self.__set_out_state__(output, state)
def __set_out_state__(self, output, state):
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True else "-f", output]).decode('UTF-8')
except subprocess.CalledProcessError as grepexc:
2022-12-19 06:49:57 +01:00
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()
2022-08-24 13:16:30 +01:00
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])
2022-07-24 14:08:49 +02:00
def get_out_states(self):
2022-12-19 06:49:57 +01:00
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 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__
2022-07-24 14:08:49 +02:00
def publish_states(self):
self.__state__ = self.get_out_states()
for i in range(1, 5):
self.send_state(i)
2022-07-24 08:49:03 +02:00
def send_state(self, output):
2023-11-03 17:02:45 +01:00
topic = config.MQTT_TOPIC + "/output/" + str(output)
2022-12-19 06:49:57 +01:00
logger.debug("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
2022-08-24 14:31:37 +01:00
try:
2022-09-12 20:56:25 +01:00
self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
2022-08-24 14:31:37 +01:00
except (socket.timeout, OSError) as e:
2022-12-19 06:49:57 +01:00
logger.warning("Error while sending state information via mqtt")
2022-07-22 09:14:52 +02:00
2022-09-12 20:56:25 +01:00
class mqtt_powerplug(mqtt.mqtt_client):
2022-07-22 09:14:52 +02:00
def __init__(self):
2022-09-12 20:56:25 +01:00
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"]:
2023-11-03 17:02:45 +01:00
self.add_callback(config.MQTT_TOPIC + "/output/%s/set" % target, self.__set__)
2022-09-12 20:56:25 +01:00
#
self.__sc__ = sispmctl(self)
def __set__(self, client, userdata, message):
2023-11-03 17:02:45 +01:00
output = message.topic.split("/")[-2]
if message.payload == b"true" or message.payload == b"false":
self.__sc__.set_out_state(output, message.payload == b"true")
elif message.payload.lower() == b'toggle':
2022-09-12 20:56:25 +01:00
self.__sc__.toggle_out_state(output)
2023-11-03 17:02:45 +01:00
else:
logger.warning("Ignoring set command due to payload: %s", message.payload)
2022-07-22 09:14:52 +02:00
def __del__(self):
self.__client__.loop_stop() # stop the loop
if __name__ == '__main__':
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
#
mp = mqtt_powerplug()
while True:
2022-07-24 14:08:49 +02:00
time.sleep(30)
mp.__sc__.publish_states()