From c9ff45f2b97d5536b0e300ab94769f5fb54336f8 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:58:47 +0200 Subject: [PATCH] Pages constructors on scripts This will simplify the calls for new add-ons. --- nspanel_esphome.yaml | 505 +++++++++++++++++++++++++++++-------------- 1 file changed, 345 insertions(+), 160 deletions(-) diff --git a/nspanel_esphome.yaml b/nspanel_esphome.yaml index bc6291a..059a83d 100644 --- a/nspanel_esphome.yaml +++ b/nspanel_esphome.yaml @@ -54,15 +54,14 @@ esphome: timeout: 20s - lambda: |- static const char *const TAG = "on_boot"; - if (not id(tf_uart).available() or not id(disp1).is_setup()) - { - ESP_LOGE(TAG, "No response from Nextion display"); - ESP_LOGD(TAG, "Turn off Nextion"); - id(screen_power).turn_off(); - delay(1500); - ESP_LOGD(TAG, "Turn on Nextion"); - id(screen_power).turn_on(); - } + if (not id(disp1).is_setup()) { + ESP_LOGE(TAG, "No response from Nextion display"); + ESP_LOGD(TAG, "Turn off Nextion"); + id(screen_power).turn_off(); + delay(1500); + ESP_LOGD(TAG, "Turn on Nextion"); + id(screen_power).turn_on(); + } ##### TYPE OF ESP BOARD ##### esp32: @@ -325,7 +324,7 @@ api: // Update home page ESP_LOGV("service.global_settings", "Update home page"); - id(update_page_home).execute(); + id(page_home).execute(false); ESP_LOGV("service.global_settings", "Current page: %s", id(current_page).state.c_str()); @@ -1110,117 +1109,9 @@ text_sensor: then: - lambda: |- static const char *const TAG = "text_sensor.current_page"; - - // Reset globals - if (x != "climate" and x != "cover" and x != "fan" and x != "light" and x != "media_player" and x != "confirm" and x != "keyb_num") id(entity_id) = ""; - if (x != "media_player") - { - id(last_volume_level) = -1; - id(last_media_duration) = -1; - id(last_media_position) = -1; - } - - // Report new page to logs - ESP_LOGD(TAG, "New page: %s", x.c_str()); - if (!id(entity_id).empty()) ESP_LOGD(TAG, "Entity shown: %s", id(entity_id).c_str()); - - // Reset timers - id(timer_reset_all).execute(x.c_str()); - - // Report new page to Home Assistant - ESP_LOGV(TAG, "Trigger HA event"); - auto ha_event = new esphome::api::CustomAPIDevice(); - ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint", - { - {"type", "page_changed"}, - {"page", x.c_str()}, - {"entity", id(entity_id)} - }); - - // Report new page to add-ons - ESP_LOGV(TAG, "Call add-ons scripts for new page"); - id(addon_climate_set_climate).execute(x == "climate" and id(entity_id) == "embedded_climate"); - // Construct new page ESP_LOGV(TAG, "Construct new page"); - if (x == "home") - { - ESP_LOGV(TAG, "Construct home page"); - id(update_page_home).execute(); - } - else if (x == "screensaver") - { - ESP_LOGV(TAG, "Construct screensaver page"); - id(update_page_screensaver).execute(); - } - else if (x == "climate") - { - ESP_LOGV(TAG, "Construct climate page"); - id(disp1).set_component_text_printf("climate.button01_icon", "%s", "\uEE8D"); //mdi:calendar-sync - id(disp1).set_component_text_printf("climate.button02_icon", "%s", "\uE069"); //mdi:autorenew - id(disp1).set_component_text_printf("climate.button03_icon", "%s", "\uE237"); //mdi:fire - id(disp1).set_component_text_printf("climate.button04_icon", "%s", "\uE716"); //mdi:snowflake - id(disp1).set_component_text_printf("climate.button05_icon", "%s", "\uE58D"); //mdi:water-percent - id(disp1).set_component_text_printf("climate.button06_icon", "%s", "\uE20F"); //mdi:fan - id(disp1).set_component_text_printf("climate.button07_icon", "%s", "\uE424"); //mdi:power - id(addon_climate_update_page_climate).execute(); - } - else if (x == "cover") // To do: Should be moved to Blueprint - { - ESP_LOGV(TAG, "Construct cover page"); - id(disp1).set_component_text_printf("cover.cover_stop", "%s", "\uE666"); //mdi:stop-circle-outline - // In the future this will be dynamically contructed based on the device_class - id(disp1).set_component_text_printf("cover.cover_open", "%s", "\uF11D"); //mdi:window-shutter-open - id(disp1).set_component_text_printf("cover.cover_close", "%s", "\uF11B"); //mdi:window-shutter - } - else if (x == "fan") // To do: Should be moved to Blueprint - { - ESP_LOGV(TAG, "Construct fan page"); - id(disp1).set_component_text_printf("fan.button_on", "%s", "\uE20F"); //mdi:fan - id(disp1).set_component_text_printf("fan.button_off", "%s", "\uE81C"); //mdi:fan-off - id(disp1).set_component_text_printf("fan.button_up", "%s", "\uF46D"); //mdi:fan-chevron-up - id(disp1).set_component_text_printf("fan.button_down", "%s", "\uF46C"); //mdi:fan-chevron-down - } - else if (x == "keyb_num") - { - ESP_LOGV(TAG, "Construct keyb_num page"); - id(disp1).set_component_text_printf("keyb_num.bview", "%s", "\uE207"); //mdi:eye - id(disp1).set_component_text_printf("keyb_num.bclose", "%s", "\uE158"); //mdi:close-circle - id(disp1).set_component_text_printf("keyb_num.bclear", "%s", "\uE641"); //mdi:eraser-variant - id(disp1).set_component_text_printf("keyb_num.benter", "%s", "\uE12B"); //mdi:check - } - else if (x == "weather01") id(disp1).set_component_text_printf("page_index", "%s", "●○○○○"); // 1/5 // To do: Should be moved to Blueprint - else if (x == "weather02") id(disp1).set_component_text_printf("page_index", "%s", "○●○○○"); // 2/5 // To do: Should be moved to Blueprint - else if (x == "weather03") id(disp1).set_component_text_printf("page_index", "%s", "○○●○○"); // 3/5 // To do: Should be moved to Blueprint - else if (x == "weather04") id(disp1).set_component_text_printf("page_index", "%s", "○○○●○"); // 4/5 // To do: Should be moved to Blueprint - else if (x == "weather05") id(disp1).set_component_text_printf("page_index", "%s", "○○○○●"); // 5/5 // To do: Should be moved to Blueprint - else if (x == "buttonpage01" or x == "entitypage01") id(disp1).set_component_text_printf("page_index", "%s", "●○○○"); // 1/4 // To do: Should be moved to Blueprint - else if (x == "buttonpage02" or x == "entitypage02") id(disp1).set_component_text_printf("page_index", "%s", "○●○○"); // 2/4 // To do: Should be moved to Blueprint - else if (x == "buttonpage03" or x == "entitypage03") id(disp1).set_component_text_printf("page_index", "%s", "○○●○"); // 3/4 // To do: Should be moved to Blueprint - else if (x == "buttonpage04" or x == "entitypage04") id(disp1).set_component_text_printf("page_index", "%s", "○○○●"); // 4/4 // To do: Should be moved to Blueprint - else if (x == "settings") // To do: Add timers on TFT? - { - //id(disp1).set_component_text_printf("bt_sleep", "%s", (id(sleep_mode).state) ? "\uEA19" : "\uEA18"); //mdi:toggle-switch-outline or mdi:toggle-switch-off-outline - id(disp1).hide_component("lbl_sleep"); - id(disp1).hide_component("bt_sleep"); - } - else if (x == "notification") - { - id(disp1).set_component_text_printf("notification.notifi_label", "%s", id(notification_label).state.c_str()); - id(display_wrapped_text).execute("notification.notifi_text01", id(notification_text).state.c_str(), id(display_mode) == 2 ? 23 : 32); - } - else if (x == "media_player") // To do: Should be moved to Blueprint - { - ESP_LOGV(TAG, "Construct media_player page"); - id(disp1).set_component_text_printf("bt_on_off", "%s", "\uE424"); //mdi:power - id(disp1).set_component_text_printf("bt_prev", "%s", "\uE4AD"); //mdi:skip-previous - id(disp1).set_component_text_printf("bt_next", "%s", "\uE4AC"); //mdi:skip-next - id(disp1).set_component_text_printf("bt_play_pause", "%s", "\uE40D"); //mdi:play-pause - //id(disp1).set_component_text_printf("bt_stop", "%s", "\uE4DA"); //mdi:stop - id(disp1).set_component_text_printf("bt_mute", "%s", "\uE75E"); //mdi:volume-mute - id(disp1).set_component_text_printf("bt_vol_down", "%s", "\uE75D"); //mdi:volume-minus - id(disp1).set_component_text_printf("bt_vol_up", "%s", "\uE75C"); //mdi:volume-plus - } + id(page_changed).execute(x.c_str()); - name: ${device_name} Notification Label platform: template @@ -1629,7 +1520,9 @@ select: entity_category: config icon: mdi:page-next-outline set_action: - - script.execute: update_page_screensaver + - script.execute: + id: page_screensaver + construct_page: false ##### START - DISPLAY START CONFIGURATION ##### display: @@ -1891,23 +1784,6 @@ script: - lambda: id(disp1).set_component_text_printf("home.wifi_icon", "%s", "\uE5A9"); - lambda: id(disp1).set_component_font_color("home.wifi_icon", 63488); - - id: update_page_home - mode: restart - then: - - if: - condition: - - text_sensor.state: # Is home page visible? - id: current_page - state: 'home' - then: - - script.execute: refresh_relays - - script.execute: refresh_wifi_icon - - lambda: |- - id(disp1).send_command_printf("is_notification=%i", (id(notification_text).state.empty() and id(notification_label).state.empty()) ? 0 : 1); - id(set_component_color).execute("home.bt_notific", id(notification_unread).state ? id(home_notify_icon_color_unread) : id(home_notify_icon_color_normal), {}); - - script.execute: refresh_datetime - - script.execute: addon_climate_update_page_home - - id: service_call_alarm_control_panel mode: restart parameters: @@ -2233,29 +2109,6 @@ script: {"blueprint", id(version_blueprint).c_str()} }); - - id: update_page_screensaver - mode: restart - then: - - if: - condition: - text_sensor.state: - id: current_page - state: screensaver - then: - - lambda: |- - int wakeup_page_id = 0; - if (id(wakeup_page_name).state == "buttonpage01") wakeup_page_id = 12; - else if (id(wakeup_page_name).state == "buttonpage02") wakeup_page_id = 13; - else if (id(wakeup_page_name).state == "buttonpage03") wakeup_page_id = 14; - else if (id(wakeup_page_name).state == "buttonpage04") wakeup_page_id = 15; - else if (id(wakeup_page_name).state == "entitypage01") wakeup_page_id = 18; - else if (id(wakeup_page_name).state == "entitypage02") wakeup_page_id = 19; - else if (id(wakeup_page_name).state == "entitypage03") wakeup_page_id = 20; - else if (id(wakeup_page_name).state == "entitypage04") wakeup_page_id = 21; - else if (id(wakeup_page_name).state == "qrcode") wakeup_page_id = 17; - else if (id(wakeup_page_name).state == "alarm") wakeup_page_id = 23; - id(disp1).set_component_value("orign", wakeup_page_id); - - id: upload_tft mode: single parameters: @@ -2917,6 +2770,338 @@ script: ESP_LOGD(TAG, "Finished!"); + - id: page_changed + mode: restart + parameters: + page: string + then: + - lambda: |- + static const char *const TAG = "script.page_changed"; + + // Reset globals + if (page != "climate" && + page != "cover" && + page != "fan" && + page != "light" && + page != "media_player" && + page != "confirm" && + page != "keyb_num") { + id(entity_id) = ""; + } + if (page != "media_player") { + id(last_volume_level) = -1; + id(last_media_duration) = -1; + id(last_media_position) = -1; + } + + // Report new page to logs + ESP_LOGD(TAG, "New page: %s", page.c_str()); + if (!id(entity_id).empty()) ESP_LOGD(TAG, "Entity shown: %s", id(entity_id).c_str()); + + // Reset timers + id(timer_reset_all).execute(page.c_str()); + + // Report new page to Home Assistant + ESP_LOGV(TAG, "Trigger HA event"); + auto ha_event = new esphome::api::CustomAPIDevice(); + ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint", + { + {"type", "page_changed"}, + {"page", page.c_str()}, + {"entity", id(entity_id)} + }); + + // Report new page to add-ons + ESP_LOGV(TAG, "Call add-ons scripts for new page"); + id(addon_climate_set_climate).execute(page == "climate" and id(entity_id) == "embedded_climate"); + + // Call page constructor + if (page == "boot") id(page_boot).execute(true); + else if (page == "buttonpage01") id(page_buttonpage).execute(true, 1); + else if (page == "buttonpage02") id(page_buttonpage).execute(true, 2); + else if (page == "buttonpage03") id(page_buttonpage).execute(true, 3); + else if (page == "buttonpage04") id(page_buttonpage).execute(true, 4); + else if (page == "climate") id(page_climate).execute(true); + else if (page == "confirm") id(page_confirm).execute(true); + else if (page == "cover") id(page_cover).execute(true); + else if (page == "entitypage01") id(page_entitypage).execute(true, 1); + else if (page == "entitypage02") id(page_entitypage).execute(true, 2); + else if (page == "entitypage03") id(page_entitypage).execute(true, 3); + else if (page == "entitypage04") id(page_entitypage).execute(true, 4); + else if (page == "fan") id(page_fan).execute(true); + else if (page == "home") id(page_home).execute(true); + else if (page == "keyb_num") id(page_keyb_num).execute(true); + else if (page == "light") id(page_light).execute(true); + else if (page == "media_player") id(page_media_player).execute(true); + else if (page == "notification") id(page_notification).execute(true); + else if (page == "qrcode") id(page_qrcode).execute(true); + else if (page == "screensaver") id(page_screensaver).execute(true); + else if (page == "settings") id(page_settings).execute(true); + else if (page == "weather01") id(page_weather).execute(true, 1); + else if (page == "weather02") id(page_weather).execute(true, 2); + else if (page == "weather03") id(page_weather).execute(true, 3); + else if (page == "weather04") id(page_weather).execute(true, 4); + else if (page == "weather05") id(page_weather).execute(true, 5); + + - id: page_boot + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_boot"; + if (construct_page) { + ESP_LOGV(TAG, "Construct boot page"); + } + + - id: page_buttonpage + mode: restart + parameters: + construct_page: bool + page_number: uint + then: + - lambda: |- + static const char *const TAG = "script.page_buttonpage"; + if (construct_page) { + ESP_LOGV(TAG, "Construct button page"); + id(page_index_indicator).execute(page_number, 4); + } + + - id: page_climate + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_climate"; + if (construct_page) { + ESP_LOGV(TAG, "Construct climate page"); + id(disp1).set_component_text_printf("climate.button01_icon", "%s", "\uEE8D"); //mdi:calendar-sync + id(disp1).set_component_text_printf("climate.button02_icon", "%s", "\uE069"); //mdi:autorenew + id(disp1).set_component_text_printf("climate.button03_icon", "%s", "\uE237"); //mdi:fire + id(disp1).set_component_text_printf("climate.button04_icon", "%s", "\uE716"); //mdi:snowflake + id(disp1).set_component_text_printf("climate.button05_icon", "%s", "\uE58D"); //mdi:water-percent + id(disp1).set_component_text_printf("climate.button06_icon", "%s", "\uE20F"); //mdi:fan + id(disp1).set_component_text_printf("climate.button07_icon", "%s", "\uE424"); //mdi:power + } + id(addon_climate_update_page_climate).execute(); + + - id: page_confirm + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_confirm"; + if (construct_page) { + ESP_LOGV(TAG, "Construct confirm page"); + } + + - id: page_cover + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_cover"; + if (construct_page) { // To do: Should be moved to Blueprint + ESP_LOGV(TAG, "Construct cover page"); + id(disp1).set_component_text_printf("cover.cover_stop", "%s", "\uE666"); //mdi:stop-circle-outline + // In the future this will be dynamically contructed based on the device_class + id(disp1).set_component_text_printf("cover.cover_open", "%s", "\uF11D"); //mdi:window-shutter-open + id(disp1).set_component_text_printf("cover.cover_close", "%s", "\uF11B"); //mdi:window-shutter + } + + - id: page_entitypage + mode: restart + parameters: + construct_page: bool + page_number: uint + then: + - lambda: |- + static const char *const TAG = "script.page_entitypage"; + if (construct_page) { + ESP_LOGV(TAG, "Construct entity page"); + id(page_index_indicator).execute(page_number, 4); + } + + - id: page_fan + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_fan"; + if (construct_page) { // To do: Should be moved to Blueprint + ESP_LOGV(TAG, "Construct fan page"); + id(disp1).set_component_text_printf("fan.button_on", "%s", "\uE20F"); //mdi:fan + id(disp1).set_component_text_printf("fan.button_off", "%s", "\uE81C"); //mdi:fan-off + id(disp1).set_component_text_printf("fan.button_up", "%s", "\uF46D"); //mdi:fan-chevron-up + id(disp1).set_component_text_printf("fan.button_down", "%s", "\uF46C"); //mdi:fan-chevron-down + } + + - id: page_home + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_home"; + if (construct_page) { + ESP_LOGV(TAG, "Construct home page"); + } + if (id(current_page).state == "home") { // Is home page visible? + ESP_LOGV(TAG, "Update home page"); + id(refresh_relays).execute(); + id(refresh_wifi_icon).execute(); + id(disp1).send_command_printf("is_notification=%i", (id(notification_text).state.empty() and id(notification_label).state.empty()) ? 0 : 1); + id(set_component_color).execute("home.bt_notific", id(notification_unread).state ? id(home_notify_icon_color_unread) : id(home_notify_icon_color_normal), {}); + id(refresh_datetime).execute(); + id(addon_climate_update_page_home).execute(); + } + + - id: page_keyb_num + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_keyb_num"; + if (construct_page) { + ESP_LOGV(TAG, "Construct keyb_num page"); + id(disp1).set_component_text_printf("keyb_num.bview", "%s", "\uE207"); //mdi:eye + id(disp1).set_component_text_printf("keyb_num.bclose", "%s", "\uE158"); //mdi:close-circle + id(disp1).set_component_text_printf("keyb_num.bclear", "%s", "\uE641"); //mdi:eraser-variant + id(disp1).set_component_text_printf("keyb_num.benter", "%s", "\uE12B"); //mdi:check + } + + - id: page_light + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_light"; + if (construct_page) { + ESP_LOGV(TAG, "Construct light page"); + } + + - id: page_media_player + mode: restart + parameters: + construct_page: bool + then: + - logger.log: Page media_player + - lambda: |- + static const char *const TAG = "script.page_media_player"; + if (construct_page) { // To do: Should be moved to Blueprint + ESP_LOGV(TAG, "Construct media_player page"); + id(disp1).set_component_text_printf("bt_on_off", "%s", "\uE424"); //mdi:power + id(disp1).set_component_text_printf("bt_prev", "%s", "\uE4AD"); //mdi:skip-previous + id(disp1).set_component_text_printf("bt_next", "%s", "\uE4AC"); //mdi:skip-next + id(disp1).set_component_text_printf("bt_play_pause", "%s", "\uE40D"); //mdi:play-pause + //id(disp1).set_component_text_printf("bt_stop", "%s", "\uE4DA"); //mdi:stop + id(disp1).set_component_text_printf("bt_mute", "%s", "\uE75E"); //mdi:volume-mute + id(disp1).set_component_text_printf("bt_vol_down", "%s", "\uE75D"); //mdi:volume-minus + id(disp1).set_component_text_printf("bt_vol_up", "%s", "\uE75C"); //mdi:volume-plus + } + + - id: page_notification + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_notification"; + if (construct_page) { + ESP_LOGV(TAG, "Construct notification page"); + id(disp1).set_component_text_printf("notification.notifi_label", "%s", id(notification_label).state.c_str()); + id(display_wrapped_text).execute("notification.notifi_text01", id(notification_text).state.c_str(), id(display_mode) == 2 ? 23 : 32); + } + + - id: page_qrcode + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_qrcode"; + if (construct_page) { + ESP_LOGV(TAG, "Construct qrcode page"); + } + + - id: page_screensaver + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_screensaver"; + if (construct_page) { + ESP_LOGV(TAG, "Construct screensaver page"); + } + if (id(current_page).state == "screensaver") { // Is screensaver page visible? + ESP_LOGV(TAG, "Update screensaver page"); + int wakeup_page_id = 0; + if (id(wakeup_page_name).state == "buttonpage01") wakeup_page_id = 12; + else if (id(wakeup_page_name).state == "buttonpage02") wakeup_page_id = 13; + else if (id(wakeup_page_name).state == "buttonpage03") wakeup_page_id = 14; + else if (id(wakeup_page_name).state == "buttonpage04") wakeup_page_id = 15; + else if (id(wakeup_page_name).state == "entitypage01") wakeup_page_id = 18; + else if (id(wakeup_page_name).state == "entitypage02") wakeup_page_id = 19; + else if (id(wakeup_page_name).state == "entitypage03") wakeup_page_id = 20; + else if (id(wakeup_page_name).state == "entitypage04") wakeup_page_id = 21; + else if (id(wakeup_page_name).state == "qrcode") wakeup_page_id = 17; + else if (id(wakeup_page_name).state == "alarm") wakeup_page_id = 23; + id(disp1).set_component_value("orign", wakeup_page_id); + } + + - id: page_settings + mode: restart + parameters: + construct_page: bool + then: + - lambda: |- + static const char *const TAG = "script.page_settings"; + if (construct_page) { // To do: Add timers on TFT? + ESP_LOGV(TAG, "Construct settings page"); + //id(disp1).set_component_text_printf("bt_sleep", "%s", (id(sleep_mode).state) ? "\uEA19" : "\uEA18"); //mdi:toggle-switch-outline or mdi:toggle-switch-off-outline + id(disp1).hide_component("lbl_sleep"); + id(disp1).hide_component("bt_sleep"); + } + + - id: page_weather + mode: restart + parameters: + construct_page: bool + page_number: uint + then: + - lambda: |- + static const char *const TAG = "script.page_weather"; + if (construct_page) { + ESP_LOGV(TAG, "Construct weather page"); + id(page_index_indicator).execute(page_number, 5); + } + + - id: page_index_indicator + mode: restart + parameters: + page_number: uint + page_total: uint + then: + - lambda: |- + static const char *const TAG = "script.page_index_indicator"; + ESP_LOGV(TAG, "Show page number indicator"); + std::string indicator = ""; + for (int i = 0; i < page_total; ++i) { + if (i == page_number - 1) { + indicator += "●"; + } else { + indicator += "○"; + } + } + id(disp1).set_component_text_printf("page_index", "%s", indicator.c_str()); + ##### ADD-ONS ############################################################ ##### Add-on - Climate ##### - id: addon_climate_service_call