Skip to content

Commit

Permalink
Fix currency format issue when page is loaded outside HA wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
springfall2008 authored Feb 15, 2025
1 parent e117436 commit 605648b
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions apps/predbat/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

TIME_FORMAT_DAILY = "%Y-%m-%d"


class WebInterface:
def __init__(self, base) -> None:
self.abort = False
Expand Down Expand Up @@ -73,7 +72,7 @@ def history_attribute(self, history, state_key="state", last_updated_key="last_u
try:
state = float(state) * scale
if pounds:
state = dp2(state / 100)
state = dp2(state/100)
last_updated_time = item[last_updated_key]
last_updated_stamp = str2time(last_updated_time)
except (ValueError, TypeError):
Expand Down Expand Up @@ -120,10 +119,10 @@ def history_update(self):
self.cost_yesterday_car_hist = self.history_attribute(self.base.get_history_wrapper(self.base.prefix + ".cost_yesterday_car", 28), daily=True, offset_days=-1, pounds=True)
self.cost_yesterday_no_car = self.subtract_daily(self.cost_yesterday_hist, self.cost_yesterday_car_hist)

compare_list = self.base.get_arg("compare_list", [])
compare_list = self.base.get_arg('compare_list', [])
for item in compare_list:
id = item.get("id", None)
if id and self.base.comparison:
if id and self.base.comparison:
self.compare_hist[id] = {}
result = self.base.comparison.get_comparison(id)
if result:
Expand Down Expand Up @@ -230,7 +229,7 @@ def get_header(self, title, refresh=0):
"""
Return the HTML header for a page
"""
text = "<!doctype html><html><head><title>Predbat Web Interface</title>"
text = "<!doctype html><html><head><meta charset=\"utf-8\"><title>Predbat Web Interface</title>"

text += """
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
Expand Down Expand Up @@ -344,7 +343,7 @@ def get_chart_series(self, name, results, chart_type, color):
text += " }\n"
return text

def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname="chart", daily_chart=True):
def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname='chart', daily_chart=True):
"""
Render a chart
"""
Expand Down Expand Up @@ -405,7 +404,7 @@ def render_chart(self, series_data, yaxis_name, chart_name, now_str, tagname="ch
start: 'day'
},
"""

text += " series: [\n"
first = True
opacity = []
Expand Down Expand Up @@ -849,7 +848,7 @@ async def html_charts(self, request):
args = request.query
chart = args.get("chart", "Battery")
self.default_page = "./charts?chart={}".format(chart)
text = self.get_header("Predbat Charts", refresh=60 * 5)
text = self.get_header("Predbat Charts", refresh=60*5)
text += "<body>\n"
text += "<h2>{} Chart</h2>\n".format(chart)
text += '- <a href="./charts?chart=Battery">Battery</a> '
Expand All @@ -870,7 +869,7 @@ async def html_apps(self, request):
Render apps.yaml as an HTML page
"""
self.default_page = "./apps"
text = self.get_header("Predbat Apps.yaml", refresh=60 * 5)
text = self.get_header("Predbat Apps.yaml", refresh=60*5)
text += "<body>\n"
text += "<a href='./debug_apps'>apps.yaml</a><br>\n"
text += "<table>\n"
Expand Down Expand Up @@ -991,7 +990,7 @@ async def html_compare(self, request):
"""
self.default_page = "./compare"

text = self.get_header("Predbat Compare", refresh=5 * 60)
text = self.get_header("Predbat Compare", refresh=5*60)

text += "<body>\n"
text += '<form class="form-inline" action="./compare" method="post" enctype="multipart/form-data" id="compareform">\n'
Expand All @@ -1013,7 +1012,7 @@ async def html_compare(self, request):
text += "<th>Carbon</th>"
text += "<th>Result</th>\n"

compare_list = self.base.get_arg("compare_list", [])
compare_list = self.base.get_arg('compare_list', [])

for compare in compare_list:
name = compare.get("name", "")
Expand Down Expand Up @@ -1050,7 +1049,7 @@ async def html_compare(self, request):
self.compare_hist[id]["metric"][stamp] = dp2(metric / 100)
self.compare_hist[id]["cost"][stamp] = dp2(cost / 100.0)

selected = '<font style="background-color:#FFaaaa;>"> Best </font>' if best else ""
selected = '<font style="background-color:#FFaaaa;>"> Best </font>' if best else ''
if existing_tariff:
selected += '<font style="background-color:#aaFFaa;"> Existing </font>'

Expand All @@ -1073,10 +1072,10 @@ async def html_compare(self, request):
for compare in compare_list:
name = compare.get("name", "")
id = compare.get("id", "")
series_data.append({"name": name, "data": self.compare_hist.get(id, {}).get("metric", {}), "chart_type": "bar"})
series_data.append({"name": "Actual", "data": self.cost_yesterday_hist, "chart_type": "line", "stroke_width": "2"})
series_data.append({"name" : name, "data" : self.compare_hist.get(id, {}).get("metric", {}), "chart_type": "bar"})
series_data.append({"name" : "Actual", "data" : self.cost_yesterday_hist, "chart_type": "line", "stroke_width": "2"})
if self.base.car_charging_hold:
series_data.append({"name": "Actual (no car)", "data": self.cost_yesterday_no_car, "chart_type": "line", "stroke_width": "2"})
series_data.append({"name" : "Actual (no car)", "data" : self.cost_yesterday_no_car, "chart_type": "line", "stroke_width": "2"})

now_str = self.base.now_utc.strftime(TIME_FORMAT)

Expand Down

0 comments on commit 605648b

Please sign in to comment.