From f4ec092695361e366655bc9b837fa56e8958b485 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Sat, 21 Dec 2024 21:37:19 +0000 Subject: [PATCH 1/4] Web download debug feature --- apps/predbat/predbat.py | 3 +- apps/predbat/userinterface.py | 13 +++++--- apps/predbat/web.py | 58 +++++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/apps/predbat/predbat.py b/apps/predbat/predbat.py index 90f161049..1c9fdc818 100644 --- a/apps/predbat/predbat.py +++ b/apps/predbat/predbat.py @@ -38,7 +38,7 @@ import asyncio import json -THIS_VERSION = "v8.8.11" +THIS_VERSION = "v8.8.12" # fmt: off PREDBAT_FILES = ["predbat.py", "config.py", "prediction.py", "gecloud.py","utils.py", "inverter.py", "ha.py", "download.py", "unit_test.py", "web.py", "predheat.py", "futurerate.py", "octopus.py", "solcast.py","execute.py", "plan.py", "fetch.py", "output.py", "userinterface.py"] @@ -264,6 +264,7 @@ def reset(self): """ reset_prediction_globals() self.predheat = None + self.predbat_mode = "Monitor" self.soc_kwh_history = {} self.html_plan = "

Please wait calculating...

" self.unmatched_args = {} diff --git a/apps/predbat/userinterface.py b/apps/predbat/userinterface.py index 5422b13ab..67893d0e1 100644 --- a/apps/predbat/userinterface.py +++ b/apps/predbat/userinterface.py @@ -602,7 +602,7 @@ def read_debug_yaml(self, filename): current["value"] = item["value"] self.log("Restored debug settings - minutes now {}".format(self.minutes_now)) - def create_debug_yaml(self): + def create_debug_yaml(self, write_file=True): """ Write out a debug info yaml """ @@ -634,9 +634,14 @@ def create_debug_yaml(self): debug["inverters"] = inverters_debug debug["CONFIG_ITEMS"] = CONFIG_ITEMS - with open(filename, "w") as file: - yaml.dump(debug, file) - self.log("Wrote debug yaml to {}".format(filename_p)) + + if write_file: + with open(filename, "w") as file: + yaml.dump(debug, file) + self.log("Wrote debug yaml to {}".format(filename_p)) + else: + # Return the debug yaml as a string + return yaml.dump(debug) def create_entity_list(self): """ diff --git a/apps/predbat/web.py b/apps/predbat/web.py index f1b511fe9..d5f07dfe9 100644 --- a/apps/predbat/web.py +++ b/apps/predbat/web.py @@ -86,6 +86,10 @@ async def start(self): app.router.add_get("/config", self.html_config) app.router.add_post("/config", self.html_config_post) app.router.add_get("/dash", self.html_dash) + app.router.add_get("/debug_yaml", self.html_debug_yaml) + app.router.add_get("/debug_log", self.html_debug_log) + app.router.add_get("/debug_apps", self.html_debug_apps) + app.router.add_get("/debug_plan", self.html_debug_plan) runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, "0.0.0.0", 5052) @@ -122,7 +126,7 @@ def icon2html(self, icon): icon = ''.format(icon.replace("mdi:", "")) return icon - def get_status_html(self, level, status): + def get_status_html(self, level, status, debug_enable, read_only, mode): text = "" if not self.base.dashboard_index: text += "

Loading please wait...

" @@ -131,7 +135,14 @@ def get_status_html(self, level, status): text += "

Status

\n" text += "\n" text += "\n".format(status) + text += "\n".format(mode) text += "\n".format(level) + text += "\n".format(debug_enable) + text += "\n".format(read_only) + text += "<\n" + text += "\n" + text += "<\n" + text += "<\n" text += "
Status{}
Mode{}
SOC{}%
Debug Enable{}
Set Read Only{}
Downloadapps.yaml
predbat_debug.yaml
predbat.log
predbat_plan.html
\n" text += "
\n" @@ -428,7 +439,8 @@ async def html_log(self, request): text += '- All ' text += 'Warnings ' - text += 'Errors
\n' + text += 'Errors ' + text += 'Download
\n' text += "\n" @@ -546,6 +558,45 @@ def render_type(self, arg, value): text = str(value) return text + async def html_file(self, filename, data): + if data is None: + return web.Response(content_type="text/html", text="{} not found".format(filename), status=404) + else: + return web.Response( + content_type="application/octet-stream", + body=data.encode('utf-8'), # Convert text to binary if needed + headers={'Content-Disposition': 'attachment; filename={}'.format(filename)} + ) + + async def html_debug_yaml(self, request): + """ + Return the Predbat debug yaml data + """ + yaml_debug = self.base.create_debug_yaml(write_file=False) + return self.html_file("predbat_debug.yaml", yaml_debug) + + async def html_file_load(self, filename): + """ + Load a file and servce it up + """ + data = None + if os.path.exists(filename): + with open(filename, "r") as f: + data = f.read() + return self.html_file(filename, data) + + async def html_debug_log(self, request): + return self.html_file_load("predbat.log") + + async def html_debug_apps(self, request): + return self.html_file_load("apps.yaml") + + async def html_debug_plan(self, request): + html_plan = self.base.html_plan + if not html_plan: + html_plan = None + return self.html_file("predbat_plan.html", html_plan) + async def html_dash(self, request): """ Render apps.yaml as an HTML page @@ -554,7 +605,7 @@ async def html_dash(self, request): text = self.get_header("Predbat Dashboard") text += "\n" soc_perc = calc_percent_limit(self.base.soc_kw, self.base.soc_max) - text += self.get_status_html(soc_perc, self.base.current_status) + text += self.get_status_html(soc_perc, self.base.current_status, self.base.debug_enable, self.base.set_read_only, self.base.predbat_mode) text += "\n" return web.Response(content_type="text/html", text=text) @@ -732,6 +783,7 @@ async def html_apps(self, request): self.default_page = "./apps" text = self.get_header("Predbat Config") text += "\n" + text += "apps.yaml
\n" text += "
\n" text += "\n".format(level) text += "\n".format(debug_enable) text += "\n".format(read_only) - text += "<\n" - text += "\n" - text += "<\n" - text += "<\n" + text += "\n" + text += "\n" + text += "\n" + text += "\n" text += "
NameValue\n" From 3362d1cf6877e63b2c659e054e6781adf1f67e62 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Sat, 21 Dec 2024 21:42:46 +0000 Subject: [PATCH 2/4] Format fix --- apps/predbat/web.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/predbat/web.py b/apps/predbat/web.py index d5f07dfe9..90455737f 100644 --- a/apps/predbat/web.py +++ b/apps/predbat/web.py @@ -139,10 +139,10 @@ def get_status_html(self, level, status, debug_enable, read_only, mode): text += "
SOC{}%
Debug Enable{}
Set Read Only{}
Downloadapps.yaml
predbat_debug.yaml
predbat.log
predbat_plan.html
Downloadapps.yaml
predbat_debug.yaml
predbat.log
predbat_plan.html
\n" text += "
\n" @@ -573,7 +573,7 @@ async def html_debug_yaml(self, request): Return the Predbat debug yaml data """ yaml_debug = self.base.create_debug_yaml(write_file=False) - return self.html_file("predbat_debug.yaml", yaml_debug) + return await self.html_file("predbat_debug.yaml", yaml_debug) async def html_file_load(self, filename): """ @@ -583,19 +583,19 @@ async def html_file_load(self, filename): if os.path.exists(filename): with open(filename, "r") as f: data = f.read() - return self.html_file(filename, data) + return await self.html_file(filename, data) async def html_debug_log(self, request): - return self.html_file_load("predbat.log") + return await self.html_file_load("predbat.log") async def html_debug_apps(self, request): - return self.html_file_load("apps.yaml") + return await self.html_file_load("apps.yaml") async def html_debug_plan(self, request): html_plan = self.base.html_plan if not html_plan: html_plan = None - return self.html_file("predbat_plan.html", html_plan) + return await self.html_file("predbat_plan.html", html_plan) async def html_dash(self, request): """ From f3339cd981f93ec4348b1fbf4a73253c06697c6b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 21 Dec 2024 21:43:56 +0000 Subject: [PATCH 3/4] [pre-commit.ci lite] apply automatic fixes --- apps/predbat/web.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/apps/predbat/web.py b/apps/predbat/web.py index 90455737f..07ac33c6b 100644 --- a/apps/predbat/web.py +++ b/apps/predbat/web.py @@ -562,19 +562,15 @@ async def html_file(self, filename, data): if data is None: return web.Response(content_type="text/html", text="{} not found".format(filename), status=404) else: - return web.Response( - content_type="application/octet-stream", - body=data.encode('utf-8'), # Convert text to binary if needed - headers={'Content-Disposition': 'attachment; filename={}'.format(filename)} - ) - + return web.Response(content_type="application/octet-stream", body=data.encode("utf-8"), headers={"Content-Disposition": "attachment; filename={}".format(filename)}) # Convert text to binary if needed + async def html_debug_yaml(self, request): """ Return the Predbat debug yaml data """ yaml_debug = self.base.create_debug_yaml(write_file=False) return await self.html_file("predbat_debug.yaml", yaml_debug) - + async def html_file_load(self, filename): """ Load a file and servce it up From 0c66725f66be85a8cd6b5c75f4a318473d1a44c5 Mon Sep 17 00:00:00 2001 From: Trefor Southwell <48591903+springfall2008@users.noreply.github.com> Date: Sat, 21 Dec 2024 21:46:13 +0000 Subject: [PATCH 4/4] Update web.py --- apps/predbat/web.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/predbat/web.py b/apps/predbat/web.py index 07ac33c6b..1eeedb4d3 100644 --- a/apps/predbat/web.py +++ b/apps/predbat/web.py @@ -573,7 +573,7 @@ async def html_debug_yaml(self, request): async def html_file_load(self, filename): """ - Load a file and servce it up + Load a file and serve it up """ data = None if os.path.exists(filename):