From 0abf4eaa7cfdf07c8f2cefd8160cad66e5a5a2fa Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 14:49:25 -0800 Subject: [PATCH 01/11] simplify passhash code --- solarman/helpers.py | 19 ------------------- solarman/solarmanpv.py | 8 +++++--- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/solarman/helpers.py b/solarman/helpers.py index 2a5a377..a6054fa 100644 --- a/solarman/helpers.py +++ b/solarman/helpers.py @@ -88,22 +88,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/solarmanpv.py b/solarman/solarmanpv.py index 8b6a432..6e0d04c 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -8,8 +8,9 @@ import time from .api import SolarmanApi, ConstructData -from .helpers import ConfigCheck, HashPassword +from .helpers import ConfigCheck from .mqtt import Mqtt +from hashlib import sha256 logging.basicConfig(level=logging.INFO) @@ -195,5 +196,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 From d232ceac09b656e575d20595ee899257c13d4439 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:00:17 -0800 Subject: [PATCH 02/11] don't load config over and over --- solarman/solarmanpv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 6e0d04c..7a88152 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -164,8 +164,7 @@ def single_run_loop(self, file): """ 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): From 3a80666aed47c584cb27760b6d312be85f54b1b8 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:08:17 -0800 Subject: [PATCH 03/11] run loop on self instead of class --- solarman/solarmanpv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 7a88152..b3006e2 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -180,7 +180,7 @@ def daemon(self, file, interval): ) while True: try: - SolarmanPV.single_run_loop(self, file) + self.single_run_loop(self, file) time.sleep(interval) except Exception as error: # pylint: disable=broad-except logging.error("Error on start: %s", str(error)) From 6472bd6f3729f8b6b2eb39eb9d4da132d4979a48 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:09:15 -0800 Subject: [PATCH 04/11] KeyboardInterrupt is an Exception, so check first --- solarman/solarmanpv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index b3006e2..25c6ec3 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -182,12 +182,12 @@ def daemon(self, file, interval): try: self.single_run_loop(self, file) 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): """ From c6250fe48d3303866d1a864a9d87f12839805fe4 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:34:57 -0800 Subject: [PATCH 05/11] simplify with dict.get() --- solarman/solarmanpv.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 25c6ec3..6ca1f88 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -87,16 +87,10 @@ def single_run(self, config): 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) 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"]) From eead6335b9e9343e1b6963721d741781d5ce5726 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:35:18 -0800 Subject: [PATCH 06/11] simplify mqtt --- solarman/mqtt.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/solarman/mqtt.py b/solarman/mqtt.py index 1ccaee6..0ae0df2 100644 --- a/solarman/mqtt.py +++ b/solarman/mqtt.py @@ -23,7 +23,7 @@ def __init__(self, config): self.password = config["password"] self.qos = config.get("qos", 0) self.retain = config.get("retain", False) - self.client = Mqtt.connect(self) + self.client = self.connect(self) def connect(self): """ @@ -36,15 +36,14 @@ 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) + result = self.client.publish(topic, msg, self.qos, self.retain) # result: [0, 1] status = result[0] if status == 0: @@ -56,4 +55,4 @@ def message(self, topic, msg): """ MQTT Send message to selected topic """ - Mqtt.publish(self, self.client, topic, msg) + self.publish(self, topic, msg) From 08f5fccc2acac25b7ea3248e99cf4a412c956b12 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:44:40 -0800 Subject: [PATCH 07/11] remove extra arguments --- solarman/__init__.py | 2 +- solarman/solarmanpv.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/solarman/__init__.py b/solarman/__init__.py index 78ed4fa..860ed75 100644 --- a/solarman/__init__.py +++ b/solarman/__init__.py @@ -43,7 +43,7 @@ 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) elif args.validate: diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 6ca1f88..2be9f9a 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -154,7 +154,7 @@ 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 """ @@ -174,7 +174,7 @@ def daemon(self, file, interval): ) while True: try: - self.single_run_loop(self, file) + self.single_run_loop() time.sleep(interval) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") From 8145f93c06fa2c7d70baa7318574cabb20090160 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:47:08 -0800 Subject: [PATCH 08/11] remove extra argument in connect --- solarman/mqtt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solarman/mqtt.py b/solarman/mqtt.py index 0ae0df2..bf391d8 100644 --- a/solarman/mqtt.py +++ b/solarman/mqtt.py @@ -23,7 +23,7 @@ def __init__(self, config): self.password = config["password"] self.qos = config.get("qos", 0) self.retain = config.get("retain", False) - self.client = self.connect(self) + self.client = self.connect() def connect(self): """ From 649d81fe87127814ebe913e05215842bc526ea31 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 15:53:36 -0800 Subject: [PATCH 09/11] remove extra arguments --- solarman/__init__.py | 2 +- solarman/solarmanpv.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/solarman/__init__.py b/solarman/__init__.py index 860ed75..c69aa08 100644 --- a/solarman/__init__.py +++ b/solarman/__init__.py @@ -45,7 +45,7 @@ def main(): if args.single: 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/solarmanpv.py b/solarman/solarmanpv.py index 2be9f9a..0fd98ad 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -161,7 +161,7 @@ def single_run_loop(self): 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 From 9c14d02151e153c56620d17674baba869c70497f Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Wed, 22 Nov 2023 16:14:36 -0800 Subject: [PATCH 10/11] only touch topic prefix one time --- solarman/mqtt.py | 2 ++ solarman/solarmanpv.py | 19 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/solarman/mqtt.py b/solarman/mqtt.py index bf391d8..d63bc60 100644 --- a/solarman/mqtt.py +++ b/solarman/mqtt.py @@ -22,6 +22,7 @@ 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 = self.connect() @@ -43,6 +44,7 @@ def publish(self, topic, msg): :param msg: Message payload :return: """ + topic = self.topic_prefix + topic result = self.client.publish(topic, msg, self.qos, self.retain) # result: [0, 1] status = result[0] diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 0fd98ad..43a9b04 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -84,7 +84,6 @@ 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") inverter_device_state = inverter_data.get("deviceState", 128) @@ -100,9 +99,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: @@ -113,23 +112,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), ) @@ -142,10 +141,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" From 0b4d95597135f2a831b6c1940cef5b156bbce058 Mon Sep 17 00:00:00 2001 From: Austin Mroczek Date: Thu, 23 Nov 2023 09:59:13 -0800 Subject: [PATCH 11/11] fix lint errors --- solarman/helpers.py | 5 ++--- solarman/mqtt.py | 3 ++- solarman/solarmanpv.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/solarman/helpers.py b/solarman/helpers.py index a6054fa..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", diff --git a/solarman/mqtt.py b/solarman/mqtt.py index d63bc60..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) @@ -57,4 +58,4 @@ def message(self, topic, msg): """ MQTT Send message to selected topic """ - self.publish(self, topic, msg) + self.publish(topic, msg) diff --git a/solarman/solarmanpv.py b/solarman/solarmanpv.py index 43a9b04..21fe638 100644 --- a/solarman/solarmanpv.py +++ b/solarman/solarmanpv.py @@ -6,11 +6,11 @@ import logging import sys import time +from hashlib import sha256 -from .api import SolarmanApi, ConstructData +from .api import ConstructData, SolarmanApi from .helpers import ConfigCheck from .mqtt import Mqtt -from hashlib import sha256 logging.basicConfig(level=logging.INFO)