diff --git a/solarman/__init__.py b/solarman/__init__.py index 78ed4fa..c69aa08 100644 --- a/solarman/__init__.py +++ b/solarman/__init__.py @@ -43,9 +43,9 @@ def main(): args = parser.parse_args() solarman = SolarmanPV(args.file) if args.single: - solarman.single_run_loop(args.file) + solarman.single_run_loop() elif args.daemon: - solarman.daemon(args.file, args.interval) + solarman.daemon(args.interval) elif args.validate: solarman.validate_config(args.file) elif args.create_passhash: diff --git a/solarman/helpers.py b/solarman/helpers.py index 2a5a377..f8b30ba 100644 --- a/solarman/helpers.py +++ b/solarman/helpers.py @@ -2,11 +2,10 @@ Validate the JSON schema and contents used for the config file. """ -import hashlib import sys + from jsonschema import validate -from jsonschema.exceptions import ValidationError -from jsonschema.exceptions import SchemaError +from jsonschema.exceptions import SchemaError, ValidationError _SCHEMA = { "$schema": "https://json-schema.org/draft/2020-12/schema", @@ -88,22 +87,3 @@ def __init__(self, config): print(err.message) sys.exit(1) print(_VALID) - - -class HashPassword: # pylint: disable=too-few-public-methods - """ - Hash the password - """ - - def __init__(self, password): - self.password = password - self.hashed = "" - HashPassword.hash(self) - - def hash(self): - """ - Return hashed string - :return: - """ - self.hashed = hashlib.sha256(self.password.encode("utf-8")).hexdigest() - return self.hashed diff --git a/solarman/mqtt.py b/solarman/mqtt.py index 1ccaee6..5f57019 100644 --- a/solarman/mqtt.py +++ b/solarman/mqtt.py @@ -4,6 +4,7 @@ import logging import random + from paho.mqtt import client as mqtt_client logging.basicConfig(level=logging.INFO) @@ -22,8 +23,9 @@ def __init__(self, config): self.username = config["username"] self.password = config["password"] self.qos = config.get("qos", 0) + self.topic_prefix = config["topic"] self.retain = config.get("retain", False) - self.client = Mqtt.connect(self) + self.client = self.connect() def connect(self): """ @@ -36,15 +38,15 @@ def connect(self): client.connect(self.broker, self.port) return client - def publish(self, client, topic, msg): + def publish(self, topic, msg): """ Publish a message on a MQTT topic - :param client: Connect parameters :param topic: MQTT topic :param msg: Message payload :return: """ - result = client.publish(topic, msg, self.qos, self.retain) + topic = self.topic_prefix + topic + result = self.client.publish(topic, msg, self.qos, self.retain) # result: [0, 1] status = result[0] if status == 0: @@ -56,4 +58,4 @@ def message(self, topic, msg): """ MQTT Send message to selected topic """ - Mqtt.publish(self, self.client, topic, msg) + self.publish(topic, msg) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 718a0bc..5f65e8d 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -6,9 +6,10 @@ import logging import sys import time +from hashlib import sha256 -from .api import SolarmanApi, ConstructData -from .helpers import ConfigCheck, HashPassword +from .api import ConstructData, SolarmanApi +from .helpers import ConfigCheck from .mqtt import Mqtt logging.basicConfig(level=logging.INFO) @@ -83,20 +84,13 @@ def single_run(self, config): logging.info(json.dumps(meter_data_list, indent=4, sort_keys=True)) discard = ["code", "msg", "requestId", "success"] - topic = config["mqtt"]["topic"] _t = time.strftime("%Y-%m-%d %H:%M:%S") - try: - inverter_device_state = inverter_data["deviceState"] - except KeyError: - inverter_device_state = 128 + inverter_device_state = inverter_data.get("deviceState", 128) meter_state = None if meter_data: - try: - meter_state = meter_data["deviceState"] - except KeyError: - meter_state = 128 + meter_state = meter_data.get("deviceState", 128) mqtt_connection = Mqtt(config["mqtt"]) @@ -106,9 +100,9 @@ def single_run(self, config): ) for i in meter_data: if meter_data[i]: - mqtt_connection.message(topic + "/meter/" + i, meter_data[i]) + mqtt_connection.message("/meter/" + i, meter_data[i]) mqtt_connection.message( - topic + "/meter/attributes", json.dumps(meter_data_list) + "/meter/attributes", json.dumps(meter_data_list) ) if inverter_device_state == 1: @@ -119,23 +113,23 @@ def single_run(self, config): ) for i in station_data: if station_data[i] and i not in discard: - mqtt_connection.message(topic + "/station/" + i, station_data[i]) + mqtt_connection.message("/station/" + i, station_data[i]) for i in inverter_data: if inverter_data[i] and i not in discard: - mqtt_connection.message(topic + "/inverter/" + i, inverter_data[i]) + mqtt_connection.message("/inverter/" + i, inverter_data[i]) mqtt_connection.message( - topic + "/inverter/attributes", + "/inverter/attributes", json.dumps(inverter_data_list), ) for i in logger_data: if logger_data[i] and i not in discard: - mqtt_connection.message(topic + "/logger/" + i, logger_data[i]) + mqtt_connection.message("/logger/" + i, logger_data[i]) mqtt_connection.message( - topic + "/logger/attributes", + "/logger/attributes", json.dumps(logger_data_list), ) @@ -148,10 +142,10 @@ def single_run(self, config): ) else: mqtt_connection.message( - topic + "/inverter/deviceState", inverter_data.get("deviceState") + "/inverter/deviceState", inverter_data.get("deviceState") ) mqtt_connection.message( - topic + "/logger/deviceState", logger_data.get("deviceState") + "/logger/deviceState", logger_data.get("deviceState") ) logging.info( "%s - Inverter DeviceState: %s" @@ -160,15 +154,14 @@ def single_run(self, config): inverter_device_state, ) - def single_run_loop(self, file): + def single_run_loop(self): """ Perform single runs for all config instances """ - config = self.load_config(file) - for conf in config: + for conf in self.config: self.single_run(conf) - def daemon(self, file, interval): + def daemon(self, interval): """ Run as a daemon process :param file: Config file @@ -181,14 +174,14 @@ def daemon(self, file, interval): ) while True: try: - SolarmanPV.single_run_loop(self, file) + self.single_run_loop() time.sleep(interval) - except Exception as error: # pylint: disable=broad-except - logging.error("Error on start: %s", str(error)) - sys.exit(1) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") sys.exit(0) + except Exception as error: # pylint: disable=broad-except + logging.error("Error on start: %s", str(error)) + sys.exit(1) def create_passhash(self, password): """ @@ -196,5 +189,6 @@ def create_passhash(self, password): :param password: Password :return: """ - pwstring = HashPassword(password) - print(pwstring.hashed) + passhash = sha256(password.encode()).hexdigest() + print(passhash) + return passhash