Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/predbat/predbat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import pytz
import asyncio

THIS_VERSION = "v8.41.0"
THIS_VERSION = "v8.41.1"

from download import predbat_update_move, predbat_update_download, check_install, DEFAULT_PREDBAT_REPOSITORY
from const import MINUTE_WATT
Expand Down
71 changes: 71 additions & 0 deletions apps/predbat/tests/test_web_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,76 @@ def set_exporting(on):

my_predbat.dashboard_index = original_dashboard_index

# -------------------------------------------------------------------------
# Currency unit display in the web config pages (issue #4071)
# The web UI must show the user's configured currency symbol, not the raw "p".
failed += run_currency_unit_tests(my_predbat, web)

print("**** Web functions tests completed ****")
return failed


def run_currency_unit_tests(my_predbat, web):
"""Verify config item units are converted to the user's currency symbols in the web UI."""
failed = 0
print("Test: web config pages convert currency units (issue #4071)")

original_symbols = my_predbat.currency_symbols
original_num_cars = my_predbat.num_cars

try:
my_predbat.currency_symbols = ["€", "c"]
my_predbat.num_cars = 1

# convert_currency_unit helper
if my_predbat.convert_currency_unit("p") != "c":
print(f" ERROR: 'p' should convert to 'c', got: {my_predbat.convert_currency_unit('p')}")
failed += 1
if my_predbat.convert_currency_unit("p/kWh") != "c/kWh":
print(f" ERROR: 'p/kWh' should convert to 'c/kWh', got: {my_predbat.convert_currency_unit('p/kWh')}")
failed += 1
if my_predbat.convert_currency_unit("£") != "€":
print(f" ERROR: '£' should convert to '€', got: {my_predbat.convert_currency_unit('£')}")
failed += 1
if my_predbat.convert_currency_unit("kWh") != "kWh":
print(f" ERROR: 'kWh' should be unchanged, got: {my_predbat.convert_currency_unit('kWh')}")
failed += 1
if my_predbat.convert_currency_unit("") != "":
print(f" ERROR: empty unit should stay empty, got: {my_predbat.convert_currency_unit('')}")
failed += 1

# Enable and locate the car charging max price config item
entity = None
original_item_value = None
original_item_ref = None
for item in my_predbat.CONFIG_ITEMS:
if item.get("name") == "car_charging_plan_max_price":
original_item_ref = item
original_item_value = item.get("value", None)
item["value"] = 14
entity = item.get("entity")
break

if entity is None:
print(" ERROR: car_charging_plan_max_price config item not found")
return failed + 1

# html_config_item_text (shown on the /entity page) must use the converted unit
item_html = web.html_config_item_text(entity)
if item_html is None:
print(" ERROR: html_config_item_text returned None for car_charging_plan_max_price")
failed += 1
else:
if "14 c" not in item_html:
print(f" ERROR: expected '14 c' in config item HTML, got: {item_html}")
failed += 1
if "14 p" in item_html:
print(f" ERROR: unexpected raw 'p' unit in config item HTML: {item_html}")
failed += 1
finally:
if original_item_ref is not None:
original_item_ref["value"] = original_item_value
my_predbat.currency_symbols = original_symbols
my_predbat.num_cars = original_num_cars
Comment thread
Copilot marked this conversation as resolved.

return failed
16 changes: 13 additions & 3 deletions apps/predbat/userinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ def get_ha_config(self, name, default):
return value, default
return None, default

def convert_currency_unit(self, unit):
"""
Convert a config item unit string (using the default £/p symbols) into the
user's configured currency symbols so displayed units match the rates.
"""
if not unit:
return unit
major = self.currency_symbols[0] if self.currency_symbols and len(self.currency_symbols) > 0 else "£"
minor = self.currency_symbols[1] if self.currency_symbols and len(self.currency_symbols) > 1 else "p"
unit = unit.replace("£", "%%CURR_MAJOR%%").replace("p", minor).replace("%%CURR_MAJOR%%", major)
return unit
Comment thread
Copilot marked this conversation as resolved.

async def async_expose_config(self, name, value, quiet=True, event=False, force=False, in_progress=False):
return await self.run_in_executor(self.expose_config, name, value, quiet, event, force, in_progress)

Expand Down Expand Up @@ -482,9 +494,7 @@ def expose_config(self, name, value, quiet=True, event=False, force=False, in_pr
if item["type"] == "input_number":
"""INPUT_NUMBER"""
icon = item.get("icon", "mdi:numeric")
unit = item["unit"]
unit = unit.replace("£", self.currency_symbols[0])
unit = unit.replace("p", self.currency_symbols[1])
unit = self.convert_currency_unit(item["unit"])
self.set_state_wrapper(
entity_id=entity,
state=value,
Expand Down
6 changes: 3 additions & 3 deletions apps/predbat/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def get_entity_list_data(self):
if self.base.user_config_item_enabled(item):
entity_id = item.get("entity", "")
entity_friendly_name = item.get("friendly_name", "")
unit = item.get("unit", "")
unit = self.base.convert_currency_unit(item.get("unit", ""))
if unit:
entity_friendly_name = f"{entity_friendly_name} ({unit})"
if entity_id:
Expand Down Expand Up @@ -3583,7 +3583,7 @@ def html_config_item_text(self, entity):
itemtype = item.get("type", "")
default = item.get("default", "")
icon = self.icon2html(item.get("icon", ""))
unit = item.get("unit", "")
unit = self.base.convert_currency_unit(item.get("unit", ""))
text += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td>".format(icon, friendly, entity, itemtype)
if value == default:
text += '<td class="cfg_default">{} {}</td><td>{} {}</td>\n'.format(value, unit, default, unit)
Expand Down Expand Up @@ -3634,7 +3634,7 @@ async def html_config(self, request):
itemtype = item.get("type", "")
default = item.get("default", "")
useid = entity.replace(".", "__")
unit = item.get("unit", "")
unit = self.base.convert_currency_unit(item.get("unit", ""))
icon = self.icon2html(item.get("icon", ""))

if itemtype in ["input_number", "number"] and item.get("step", 1) == 1:
Expand Down
Loading