diff --git a/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/__finish b/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/__finish deleted file mode 100644 index 230d1792..00000000 --- a/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/__finish +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bashio -# ============================================================================== -# Take down the S6 supervision tree when example fails -# s6-overlay docs: https://github.com/just-containers/s6-overlay -# ============================================================================== - -declare APP_EXIT_CODE=${1} - -if [[ "${APP_EXIT_CODE}" -ne 0 ]] && [[ "${APP_EXIT_CODE}" -ne 256 ]]; then - bashio::log.warning "Halt add-on with exit code ${APP_EXIT_CODE}" - echo "${APP_EXIT_CODE}" > /run/s6-linux-init-container-results/exitcode - exec /run/s6/basedir/bin/halt -fi - -bashio::log.info "Service restart after closing" diff --git a/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/finish b/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/finish new file mode 100644 index 00000000..9b685471 --- /dev/null +++ b/nspanel-lovelace-ui/rootfs/etc/s6-overlay/s6-rc.d/mqtt-manager/finish @@ -0,0 +1,26 @@ +#!/command/with-contenv bashio +# shellcheck shell=bash +# ============================================================================== +# Take down the S6 supervision tree when mqtt manager fails +# ============================================================================== +declare exit_code +readonly exit_code_container=$( /run/s6-linux-init-container-results/exitcode + fi + [[ "${exit_code_signal}" -eq 15 ]] && exec /run/s6/basedir/bin/halt +elif [[ "${exit_code_service}" -ne 0 ]]; then + if [[ "${exit_code_container}" -eq 0 ]]; then + echo "${exit_code_service}" > /run/s6-linux-init-container-results/exitcode + fi + exec /run/s6/basedir/bin/halt +fi \ No newline at end of file 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 b80daad4..3f7c6336 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 @@ -3,7 +3,7 @@ import ha_icons import ha_colors from libs.localization import get_translation import panel_cards - +import logging class HAEntity(panel_cards.Entity): def __init__(self, locale, config, panel): @@ -183,21 +183,23 @@ class HAEntity(panel_cards.Entity): return f"~{entity_type_panel}~iid.{self.iid}~{icon_char}~{color}~{name}~{value}" - -class EntitiesCard(panel_cards.Card): +class HACard(panel_cards.Card): def __init__(self, locale, config, panel): super().__init__(locale, config, panel) + # Generate Entity for each Entity in Config self.entities = [] if "entities" in config: for e in config.get("entities"): - # print(e) iid, entity = entity_factory(locale, e, panel) self.entities.append(entity) def get_iid_entities(self): return [(e.iid, e.entity_id) for e in self.entities] - def render(self): + def get_entities(self): + return [e.entity_id for e in self.entities] + + def gen_nav(self): leftBtn = "delete~~~~~" if self.iid_prev: leftBtn = panel_cards.Entity(self.locale, @@ -216,11 +218,30 @@ class EntitiesCard(panel_cards.Card): 'color': [255, 255, 255], }, self.panel ).render()[1:] - result = f"{self.title}~{leftBtn}~{rightBtn}" + result = f"{leftBtn}~{rightBtn}" + return result + +class EntitiesCard(HACard): + def __init__(self, locale, config, panel): + super().__init__(locale, config, panel) + + def render(self): + result = f"{self.title}~{self.gen_nav()}" for e in self.entities: result += e.render() return result +class QRCard(HACard): + def __init__(self, locale, config, panel): + super().__init__(locale, config, panel) + self.qrcode = config.get("qrCode", "https://www.youtube.com/watch?v=dQw4w9WgXcQ") + def render(self): + # TODO: Render QRCode as HomeAssistant Template + #qrcode = apis.ha_api.render_template(qrcode) + result = f"{self.title}~{self.gen_nav()}~{self.qrcode}" + for e in self.entities: + result += e.render() + return result class Screensaver(panel_cards.Card): def __init__(self, locale, config, panel): @@ -239,8 +260,11 @@ def card_factory(locale, settings, panel): match settings["type"]: case 'cardEntities' | 'cardGrid': card = EntitiesCard(locale, settings, panel) + case 'cardQR': + card = QRCard(locale, settings, panel) case _: - card = panel_cards.Card(locale, settings, panel) + logging.error("card type %s not implemented", settings["type"]) + return "NotImplemented", None return card.iid, card diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py index 4bac1ebe..adebe3c1 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py @@ -29,13 +29,15 @@ class LovelaceUIPanel: # generate cards for input settings for c in self.settings.get("cards"): iid, card = card_factory(self.settings["locale"], c, self) - self.cards[iid] = card - # collect nav keys of cards - if card.navigate_key: - self.navigate_keys[card.navigate_key] = iid - # collect iids of entities - for e in card.get_iid_entities(): - self.entity_iids[e[0]] = e[1] + # Check if we acually got a card + if card: + self.cards[iid] = card + # collect nav keys of cards + if card.navigate_key: + self.navigate_keys[card.navigate_key] = iid + # collect iids of entities + for e in card.get_iid_entities(): + self.entity_iids[e[0]] = e[1] # setup prev and next iids top_level_cards = list(self.cards.values()) @@ -92,9 +94,9 @@ class LovelaceUIPanel: return self.hidden_cards[iid] def ha_event_callback(self, entity_id): - logging.debug(f"{entity_id} updated/state changed") - # TODO: Check if entity is on current card - libs.panel_cmd.entityUpd(self.sendTopic, self.current_card.render()) + #logging.debug(f"{entity_id} updated/state changed") + if entity_id in self.current_card.get_entities(): + libs.panel_cmd.entityUpd(self.sendTopic, self.current_card.render()) def customrecv_event_callback(self, msg): logging.debug("Recv Message from NsPanel: %s", msg)