diff --git a/nspanel-lovelace-ui/config.yaml b/nspanel-lovelace-ui/config.yaml index 0f00a333..7353c7d1 100644 --- a/nspanel-lovelace-ui/config.yaml +++ b/nspanel-lovelace-ui/config.yaml @@ -1,6 +1,6 @@ # https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config name: NSPanel Lovelace UI Addon -version: "4.7.34" +version: "4.7.35" slug: nspanel-lovelace-ui description: NSPanel Lovelace UI Addon services: diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_cards.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_cards.py index 163f0994..ab3d113c 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_cards.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_cards.py @@ -8,6 +8,7 @@ import dateutil.parser as dp import babel from libs.icon_mapping import get_icon_char from libs.helper import rgb_dec565, scale +import ha_template class HAEntity(panel_cards.Entity): def __init__(self, locale, config, panel): @@ -15,10 +16,6 @@ class HAEntity(panel_cards.Entity): def render(self, cardType=""): - if self.icon_overwrite and self.icon_overwrite.startswith("ha:"): - out = libs.home_assistant.render_template(self.icon_overwrite[3:]) - self.icon_overwrite = out - if self.etype in ["delete", "navigate", "iText"]: out = super().render() if self.etype == "navigate" and "status" in self.config: @@ -44,9 +41,7 @@ class HAEntity(panel_cards.Entity): entity_type_panel = "text" icon_char = ha_icons.get_icon_ha(self.etype, self.state, device_class=self.attributes.get("device_class", None), media_content_type=self.attributes.get("media_content_type", None), overwrite=self.config.get("icon")) color = ha_colors.get_entity_color( - self.etype, self.state, self.attributes) - if self.color_overwrite: - color = rgb_dec565(self.color_overwrite) + self.etype, self.state, self.attributes, self.color_overwrite) name = self.config.get("name", self.attributes.get("friendly_name", "unknown")) if self.name_overwrite: name = self.name_overwrite @@ -217,6 +212,14 @@ class HAEntity(panel_cards.Entity): case _: name = "unsupported" + + if self.icon_overwrite and self.icon_overwrite.startswith("ha:"): + #icon_char = libs.home_assistant.render_template(self.icon_overwrite[3:]) + icon_char = ha_template.render(self.icon_overwrite[3:]) + + if self.value_overwrite and self.value_overwrite.startswith("ha:"): + value = ha_template.render(self.value_overwrite[3:]) + return f"~{entity_type_panel}~iid.{self.iid}~{icon_char}~{color}~{name}~{value}" class HACard(panel_cards.Card): diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_template.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_template.py new file mode 100644 index 00000000..7e2219f4 --- /dev/null +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_template.py @@ -0,0 +1,30 @@ +from jinja2 import Environment, BaseLoader +from typing import ( + Any +) +import libs.home_assistant + +def states(entity_id): + return libs.home_assistant.get_entity_data(entity_id).get("state") + +def state_attr(entity_id, attr): + return libs.home_assistant.get_entity_data(entity_id).get('attributes', []).get(attr) + +def iif(value: Any, if_true: Any = True, if_false: Any = False, if_none: Any = False) -> Any: + if value is None: + return if_none + if bool(value): + return if_true + return if_false + +func_dict = { + "states": states, + "state_attr": state_attr, + "iif": iif +} + +def render(template): + jinja_template = Environment(loader=BaseLoader()).from_string(template) + jinja_template.globals.update(func_dict) + template_string = jinja_template.render() + return template_string \ No newline at end of file diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py index 223b825b..29e3d117 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py @@ -193,7 +193,7 @@ def execute_script(entity_name: str, domain: str, service: str, service_data: di } send_message(json.dumps(msg)) # busy waiting for response with a timeout of 0.2 seconds - maybe there's a better way of doing this - mustend = time.time() + 0.2 + mustend = time.time() + 0.4 while time.time() < mustend: if response_buffer[call_id] == True: #print(f'loooooooooop {time.time()}') diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel_cards.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel_cards.py index fd3f975b..0c21b898 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel_cards.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel_cards.py @@ -31,6 +31,7 @@ class Entity: self.icon_overwrite = config.get("icon", None) self.name_overwrite = config.get("name", None) self.color_overwrite = config.get("color", None) + self.value_overwrite = config.get("value", None) font_mapping = { "small": "1", "medium": "2", diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/requirements.txt b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/requirements.txt index 7558b4e3..b05114aa 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/requirements.txt +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/requirements.txt @@ -6,4 +6,5 @@ django-environ python-dateutil scheduler babel -watchdog \ No newline at end of file +watchdog +jinja2 \ No newline at end of file