Skip to content

Commit

Permalink
fix: MQTT/Domoticz summary.py items not send when language is NOT Eng…
Browse files Browse the repository at this point in the history
…lish
  • Loading branch information
ZuinigeRijder committed Nov 27, 2024
1 parent 777f035 commit a13e32b
Show file tree
Hide file tree
Showing 34 changed files with 11,747 additions and 96 deletions.
53 changes: 27 additions & 26 deletions domoticz_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == domoticz_utils.py Author: Zuinige Rijder =========
""" domoticz utils """
# pylint:disable=logging-fstring-interpolation
# pylint:disable=logging-fstring-interpolation, consider-using-enumerate

import configparser
import logging
Expand All @@ -19,6 +19,7 @@
get_items_monitor_dailystats_csv,
get_items_monitor_tripinfo_csv,
get_items_summary,
get_summary_headers,
)

PARSER = configparser.ConfigParser()
Expand All @@ -29,31 +30,31 @@
SEND_TO_DOMOTICZ = get_bool(domoticz_settings, "send_to_domoticz", False)
DOMOTICZ_URL = get(domoticz_settings, "domot_url")

ITEMS_MONITOR_CSV = get_items_monitor_csv()
for IDX in range(len(ITEMS_MONITOR_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_MONITOR_CSV[IDX] = f"monitor_monitor_{ITEMS_MONITOR_CSV[IDX]}"
if SEND_TO_DOMOTICZ:
ITEMS_MONITOR_CSV = get_items_monitor_csv()
for IDX in range(len(ITEMS_MONITOR_CSV)):
ITEMS_MONITOR_CSV[IDX] = f"monitor_monitor_{ITEMS_MONITOR_CSV[IDX]}"

ITEMS_TRIPINFO_CSV = get_items_monitor_tripinfo_csv()
for IDX in range(len(ITEMS_TRIPINFO_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_TRIPINFO_CSV[IDX] = f"monitor_tripinfo_{ITEMS_TRIPINFO_CSV[IDX]}"
ITEMS_TRIPINFO_CSV = get_items_monitor_tripinfo_csv()
for IDX in range(len(ITEMS_TRIPINFO_CSV)):
ITEMS_TRIPINFO_CSV[IDX] = f"monitor_tripinfo_{ITEMS_TRIPINFO_CSV[IDX]}"

ITEMS_DAILYSTATS_CSV = get_items_monitor_dailystats_csv()
for IDX in range(len(ITEMS_DAILYSTATS_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_DAILYSTATS_CSV[IDX] = f"monitor_dailystats_{ITEMS_DAILYSTATS_CSV[IDX]}"
ITEMS_DAILYSTATS_CSV = get_items_monitor_dailystats_csv()
for IDX in range(len(ITEMS_DAILYSTATS_CSV)):
ITEMS_DAILYSTATS_CSV[IDX] = f"monitor_dailystats_{ITEMS_DAILYSTATS_CSV[IDX]}"

ITEMS_SUMMARY = get_items_summary()

ITEMS_DAILYSTATS_DAY = get_items_dailystats_day()

ITEMS_DAILYSTATS_TRIP = get_items_dailystat_trip()
ITEMS_SUMMARY = get_items_summary()
ITEMS_DAILYSTATS_DAY = get_items_dailystats_day()
ITEMS_DAILYSTATS_TRIP = get_items_dailystat_trip()


# == send to Domoticz ========================================================
def send_to_domoticz(header: str, value: str) -> None:
"""send_to_Domoticz"""
reference_test = DOMOTICZ_URL == "domoticz_reference_test"
idx = get(domoticz_settings, header, "0")
if idx == "0" and not reference_test:
idx = get(domoticz_settings, header.lower(), "0")
_ = d() and dbg(f"send_to_domoticz: idx = {idx}, {header} = {value}")
if idx == "0":
return # nothing to do

url = (
Expand All @@ -63,10 +64,7 @@ def send_to_domoticz(header: str, value: str) -> None:
+ "&svalue="
+ value
)
if reference_test:
_ = d() and dbg(f"send_to_domoticz: {header} = {value}")
else:
_ = d() and dbg(url)
_ = d() and dbg(f"send_to_domoticz: {url}")
retry = 0
while not reference_test:
retry += 1
Expand Down Expand Up @@ -138,12 +136,15 @@ def send_summary_line_to_domoticz(line: str) -> None:
if SEND_TO_DOMOTICZ:
splitted = [x.strip() for x in line.split(",")]
period = splitted[0].replace(" ", "")
if (
period
in "TRIP, DAY, WEEK, MONTH, YEAR, TRIPAVG, DAYAVG, WEEKAVG, MONTHAVG, YEARLY" # noqa
):
summary_headers_dict = get_summary_headers()
if period in summary_headers_dict:
key = summary_headers_dict[period]
send_splitted_line(
get_items(f"summary_{period}", ITEMS_SUMMARY), splitted, True, True
get_items(f"summary_{key}", ITEMS_SUMMARY), splitted, True, True
)
else:
_ = d() and dbg(
f"Skipping: period={period}, headers={summary_headers_dict}"
)


Expand Down
92 changes: 58 additions & 34 deletions monitor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

D = False

TR_HELPER: dict[str, str] = {}
TR_SUMMARY_HEADERS_DICT: dict[str, str] = {}


def d() -> bool:
"""return D"""
Expand Down Expand Up @@ -380,40 +383,61 @@ def reverse_read_next_line(

def read_translations() -> dict:
"""read translations"""
translations: dict = {}
parser = configparser.ConfigParser()
parser.read(get_filepath("monitor.cfg"))
monitor_settings = dict(parser.items("monitor"))
language = monitor_settings["language"].lower().strip()
translations_csv_file = Path(get_filepath("monitor.translations.csv"))
with translations_csv_file.open("r", encoding="utf-8") as inputfile:
linecount = 0
column = 1
for line in inputfile:
linecount += 1
split = split_on_comma(line)
if len(split) < 15:
logging.error(
f"Error: unexpected translation csvline {linecount}: {line}"
)
continue
key = split[0]
translation = split[1]
if linecount == 1:
continue # skip first line
elif linecount == 2:
# determine column index
for index, value in enumerate(split):
if value.lower() == language:
column = index
break
else:
current = split[column]
if current != "":
translation = current

translations[key] = translation
return translations
if len(TR_HELPER) == 0:
parser = configparser.ConfigParser()
parser.read(get_filepath("monitor.cfg"))
monitor_settings = dict(parser.items("monitor"))
language = monitor_settings["language"].lower().strip()
translations_csv_file = Path(get_filepath("monitor.translations.csv"))
with translations_csv_file.open("r", encoding="utf-8") as inputfile:
linecount = 0
column = 1
for line in inputfile:
linecount += 1
split = split_on_comma(line)
if len(split) < 15:
logging.error(
f"Error: unexpected translation csvline {linecount}: {line}"
)
continue
key = split[0]
translation = split[1]
if linecount == 1:
continue # skip first line
elif linecount == 2:
# determine column index
for index, value in enumerate(split):
if value.lower() == language:
column = index
break
else:
current = split[column]
if current != "":
translation = current

TR_HELPER[key] = translation
return TR_HELPER


def get_summary_headers() -> dict:
"""get summary headers"""
_ = read_translations()
if len(TR_SUMMARY_HEADERS_DICT) == 0:
keys = [
"TRIP",
"DAY",
"WEEK",
"MONTH",
"YEAR",
"TRIPAVG",
"DAYAVG",
"WEEKAVG",
"MONTHAVG",
"YEARLY",
]
for key in keys:
TR_SUMMARY_HEADERS_DICT[TR_HELPER[key].replace(" ", "")] = key
return TR_SUMMARY_HEADERS_DICT


def get_translation(translations: dict[str, str], text: str) -> str:
Expand Down
53 changes: 28 additions & 25 deletions mqtt_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == mqtt_utils.py Author: Zuinige Rijder =========
""" mqtt utils """
# pylint:disable=logging-fstring-interpolation
# pylint:disable=logging-fstring-interpolation, consider-using-enumerate

import configparser
import logging
Expand All @@ -22,27 +22,10 @@
get_items_monitor_dailystats_csv,
get_items_monitor_tripinfo_csv,
get_items_summary,
get_summary_headers,
get_vin,
)

ITEMS_MONITOR_CSV = get_items_monitor_csv()
for IDX in range(len(ITEMS_MONITOR_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_MONITOR_CSV[IDX] = f"monitor/monitor/{ITEMS_MONITOR_CSV[IDX]}"

ITEMS_TRIPINFO_CSV = get_items_monitor_tripinfo_csv()
for IDX in range(len(ITEMS_TRIPINFO_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_TRIPINFO_CSV[IDX] = f"monitor/tripinfo/{ITEMS_TRIPINFO_CSV[IDX]}"

ITEMS_DAILYSTATS_CSV = get_items_monitor_dailystats_csv()
for IDX in range(len(ITEMS_DAILYSTATS_CSV)): # pylint:disable=consider-using-enumerate
ITEMS_DAILYSTATS_CSV[IDX] = f"monitor/dailystats/{ITEMS_DAILYSTATS_CSV[IDX]}"

ITEMS_SUMMARY = get_items_summary()

ITEMS_DAILYSTATS_DAY = get_items_dailystats_day()

ITEMS_DAILYSTATS_TRIP = get_items_dailystat_trip()

PARSER = configparser.ConfigParser()
PARSER.read(get_filepath("monitor.cfg"))

Expand All @@ -57,6 +40,23 @@

MQTT_CLIENT = None # will be filled at MQTT connect if configured

if SEND_TO_MQTT:
ITEMS_MONITOR_CSV = get_items_monitor_csv()
for IDX in range(len(ITEMS_MONITOR_CSV)):
ITEMS_MONITOR_CSV[IDX] = f"monitor/monitor/{ITEMS_MONITOR_CSV[IDX]}"

ITEMS_TRIPINFO_CSV = get_items_monitor_tripinfo_csv()
for IDX in range(len(ITEMS_TRIPINFO_CSV)):
ITEMS_TRIPINFO_CSV[IDX] = f"monitor/tripinfo/{ITEMS_TRIPINFO_CSV[IDX]}"

ITEMS_DAILYSTATS_CSV = get_items_monitor_dailystats_csv()
for IDX in range(len(ITEMS_DAILYSTATS_CSV)):
ITEMS_DAILYSTATS_CSV[IDX] = f"monitor/dailystats/{ITEMS_DAILYSTATS_CSV[IDX]}"

ITEMS_SUMMARY = get_items_summary()
ITEMS_DAILYSTATS_DAY = get_items_dailystats_day()
ITEMS_DAILYSTATS_TRIP = get_items_dailystat_trip()


# == connect MQTT ========================================================
def connect_mqtt() -> mqtt_client.Client:
Expand Down Expand Up @@ -106,7 +106,7 @@ def on_disconnect(client, userdata, rc): # pylint: disable=unused-argument
def start_mqtt_loop() -> None:
"""start_mqtt_loop"""
global MQTT_CLIENT # pylint: disable=global-statement
if not MQTT_CLIENT:
if not MQTT_CLIENT and SEND_TO_MQTT:
while True:
try:
_ = d() and dbg("Trying to connected to MQTT Broker")
Expand Down Expand Up @@ -229,12 +229,15 @@ def send_summary_line_to_mqtt(line: str) -> None:
if SEND_TO_MQTT:
splitted = [x.strip() for x in line.split(",")]
period = splitted[0].replace(" ", "")
if (
period
in "TRIP, DAY, WEEK, MONTH, YEAR, TRIPAVG, DAYAVG, WEEKAVG, MONTHAVG, YEARLY" # noqa
):
summary_headers_dict = get_summary_headers()
if period in summary_headers_dict:
key = summary_headers_dict[period]
send_splitted_line(
get_items(f"summary/{period}", ITEMS_SUMMARY), splitted, True, True
get_items(f"summary/{key}", ITEMS_SUMMARY), splitted, True, True
)
else:
_ = d() and dbg(
f"Skipping: period={period}, headers={summary_headers_dict}"
)


Expand Down
Loading

0 comments on commit a13e32b

Please sign in to comment.