diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/config.yml.example b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/config.yml.example index f4213917..7ff9dcbc 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/config.yml.example +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/config.yml.example @@ -1,7 +1,6 @@ nspanels: tasmota_nspdev2: name: test - mac: AA:BB:CC:DD:EE:FF panelRecvTopic: "tele/tasmota_nspdev2/RESULT" panelSendTopic: "cmnd/tasmota_nspdev2/CustomSend" timeFormat: "%H:%M" diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py index cb57fb6d..fb70ff66 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py @@ -21,16 +21,17 @@ def on_off(entity_id, value): def button_press(entity_id, value): - etype = entity_id.split["."][0] + etype = entity_id.split(".")[0] match etype: - case 'scene': + case 'scene' | 'script': libs.home_assistant.call_service( entity_name=entity_id, - domain="scene", + domain=etype, service="turn_on", service_data={} ) + # apis.ha_api.log( # f"Button Press Event; entity_id: {entity_id}; button_type: {button_type}; value: {value} ") # # buttons with actions on HA @@ -73,41 +74,6 @@ def button_press(entity_id, value): # "set_cover_tilt_position", tilt_position=pos) # # if button_type == "button": - # if entity_id.startswith('navigate'): - # # internal navigation for next/prev - # if entity_id.startswith('navigate.uuid'): - # dstCard = self._config.get_card_by_uuid( - # entity_id.replace('navigate.', '')) - # # internal for navigation to nested pages - # else: - # dstCard = self._config.search_card(entity_id) - # if dstCard is not None: - # if dstCard.hidden: - # self._previous_cards.append(self._current_card) - # self._current_card = dstCard - # self._pages_gen.render_card(self._current_card) - # else: - # apis.ha_api.log(f"No page with key {entity_id} found") - # if entity_id.startswith('navUp'): - # if self._previous_cards: - # self._current_card = self._previous_cards.pop() - # else: - # self._current_card = self._config.get_default_card() - # self._pages_gen.render_card(self._current_card) - # if entity_id.startswith('navPrev'): - # if self._current_card.uuid_prev: - # self._current_card = self._config.get_card_by_uuid( - # self._current_card.uuid_prev) - # self._pages_gen.render_card(self._current_card) - # if entity_id.startswith('navNext'): - # if self._current_card.uuid_next: - # self._current_card = self._config.get_card_by_uuid( - # self._current_card.uuid_next) - # self._pages_gen.render_card(self._current_card) - # elif entity_id.startswith('scene'): - # apis.ha_api.get_entity(entity_id).call_service("turn_on") - # elif entity_id.startswith('script'): - # apis.ha_api.get_entity(entity_id).call_service("turn_on") # elif entity_id.startswith('light') or entity_id.startswith('switch') or entity_id.startswith('input_boolean') or entity_id.startswith('automation') or entity_id.startswith('fan'): # apis.ha_api.get_entity(entity_id).call_service("toggle") # elif entity_id.startswith('lock'): 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 52c607b6..366e5713 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 @@ -19,10 +19,10 @@ ON_CONNECT_HANDLER = None ON_DISCONNECT_HANDLER = None -def init(settings_from_manager, mqtt_client_from_manager): - global home_assistant_url, home_assistant_token, settings, mqtt_client +def init(settings_from_manager, on_ha_update_from_manager): + global home_assistant_url, home_assistant_token, settings, on_ha_update settings = settings_from_manager - mqtt_client = mqtt_client_from_manager + on_ha_update = on_ha_update_from_manager home_assistant_url = settings["home_assistant_address"] home_assistant_token = settings["home_assistant_token"] # Disable logging from underlying "websocket" @@ -92,11 +92,10 @@ def connect(): def _do_connection(): - global home_assistant_url, ws + global home_assistant_url, ws, settings ws_url = home_assistant_url.replace( "https://", "wss://").replace("http://", "ws://") - environment = environ.Env() - if "IS_HOME_ASSISTANT_ADDON" in environment and environment("IS_HOME_ASSISTANT_ADDON") == "true": + if settings["is_addon"]: ws_url += "/core/websocket" else: ws_url += "/api/websocket" @@ -142,11 +141,9 @@ def _get_all_states(): # Got new value from Home Assistant, publish to MQTT -def send_entity_update(json_msg): - global mqtt_client - # Check if the light is used on any nspanel and if so, send MQTT state update - # mqtt_client.publish(f"cmnd/tasmota_nspdev2/CustomSend", "page~screensaver") - +def send_entity_update(entity_id): + global on_ha_update + on_ha_update(entity_id) def set_entity_brightness(home_assistant_name: str, light_level: int, color_temp: int) -> bool: """Set entity brightness""" diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/main.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/main.py index d750e7f5..225ab698 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/main.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/main.py @@ -30,6 +30,8 @@ def on_connect(client, userdata, flags, rc): # client.subscribe(panel["panelRecvTopic"]) client.subscribe("tele/tasmota_nspdev2/RESULT") +def on_ha_update(entity_id): + logging.debug(f"{entity_id} updated/state changed") def on_message(client, userdata, msg): try: @@ -78,9 +80,13 @@ def get_config(): if not settings.get("mqtt_server"): settings["mqtt_server"] = environment('MQTT_SERVER') + settings["is_addon"] = False if "SUPERVISOR_TOKEN" in environment: settings["home_assistant_token"] = environment('SUPERVISOR_TOKEN') + print(settings["home_assistant_token"]) settings["home_assistant_address"] = "http://supervisor" + settings["is_addon"] = True + def connect_and_loop(): @@ -106,7 +112,7 @@ def connect_and_loop(): # MQTT Connected, start APIs if configured if settings["home_assistant_address"] != "" and settings["home_assistant_token"] != "": - libs.home_assistant.init(settings, client) + libs.home_assistant.init(settings, on_ha_update) libs.home_assistant.connect() else: logging.info("Home Assistant values not configured, will not connect.") @@ -122,7 +128,6 @@ def connect_and_loop(): # Loop MQTT client.loop_forever() - if __name__ == '__main__': get_config() # print(settings)