diff --git a/lib/global_state.py b/lib/global_state.py index cd4f5ae..4e818b7 100644 --- a/lib/global_state.py +++ b/lib/global_state.py @@ -1,6 +1,6 @@ import sqlite3 -from lib.helpers import publish_message +from lib.helpers import publish_message, reduce_decimal from lib.constants import logging @@ -51,13 +51,14 @@ def get(self, key): return bool(False) else: return int(result_value) - except Exception as e: + except Exception as e: # noqa return str(result_value) else: return 0 def set(self, key, value): - self.cursor.execute("INSERT OR REPLACE INTO data VALUES (?, ?)", (key, value)) - publish_message(f"Cerbomoticzgx/GlobalState/{key}", message=value, retain=True) + _value = reduce_decimal(value) + self.cursor.execute("INSERT OR REPLACE INTO data VALUES (?, ?)", (key, _value)) + publish_message(f"Cerbomoticzgx/GlobalState/{key}", message=_value, retain=True) self.connection.commit() diff --git a/lib/helpers/__init__.py b/lib/helpers/__init__.py index bb93cab..55c43ad 100644 --- a/lib/helpers/__init__.py +++ b/lib/helpers/__init__.py @@ -78,5 +78,17 @@ def get_seasonally_adjusted_max_charge_slots(batt_soc: float) -> int: return 0 +def reduce_decimal(value): + if '.' in value: + try: + float_value = float(value) + rounded_value = round(float_value, 4) + return str(rounded_value) + except ValueError: + return value + else: + return value + + # Function Aliases retrieve_message = get_current_value_from_mqtt