From 7caac2e62c1c27b25d552b1093359bcc0217b836 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Wed, 29 Nov 2023 16:25:42 +0100 Subject: [PATCH] Don't update button if page changed Solves #1358 It might be a bit slower to change between button pages now, as the blueprint checks for the page before sending to ESPHome and this one also checks before sending to Nextion, but it reduces a lot the chances of `Nextion reported variable name invalid!` and broken buttons on quick page changes. --- nspanel_blueprint.yaml | 45 +++++++++++++++--------------- nspanel_esphome_core.yaml | 58 ++++++++++++++++++++++----------------- 2 files changed, 55 insertions(+), 48 deletions(-) diff --git a/nspanel_blueprint.yaml b/nspanel_blueprint.yaml index 4b0d98b..51a5c2e 100644 --- a/nspanel_blueprint.yaml +++ b/nspanel_blueprint.yaml @@ -7644,7 +7644,8 @@ action: {{ repeat.item.entity is string and repeat.item.entity | length > 0 and - repeat.item.entity.split(".") | default([]) | count > 0 + repeat.item.entity.split(".") | default([]) | count > 0 and + repeat.item.page == states(currentpage) }} then: - variables: @@ -7755,17 +7756,16 @@ action: - *delay-default - service: '{{ nextion.command.set_button }}' data: - btn_id: '{{ repeat.item.page }}.{{ repeat.item.component }}' - btn_pic: '{{ btn_pic }}' - btn_bg: '{{ [ btn_bg ] if is_number(btn_bg) else btn_bg }}' - btn_icon_font: '{{ [ btn_icon_font ] if is_number(btn_icon_font) else btn_icon_font }}' - btn_txt_font: '{{ [ btn_txt_font ] if is_number(btn_txt_font) else btn_txt_font }}' - btn_bri_font: '{{ [ btn_bri_font ] if is_number(btn_bri_font) else btn_bri_font }}' - btn_icon: '{{ btn_icon }}' - btn_label: '{{ btn_label }}' - btn_bri_txt: '{{ btn_bri_txt }}' - #btn_confirm: '{{ repeat.item.confirm }}' - #entity: '{{ repeat.item.entity }}' + page: '{{ repeat.item.page }}' + id: '{{ repeat.item.component }}' + pic: '{{ btn_pic }}' + bg: '{{ [ btn_bg ] if is_number(btn_bg) else btn_bg }}' + icon: '{{ btn_icon }}' + icon_color: '{{ [ btn_icon_font ] if is_number(btn_icon_font) else btn_icon_font }}' + bri: '{{ btn_bri_txt }}' + bri_color: '{{ [ btn_bri_font ] if is_number(btn_bri_font) else btn_bri_font }}' + label: '{{ btn_label }}' + label_color: '{{ [ btn_txt_font ] if is_number(btn_txt_font) else btn_txt_font }}' continue_on_error: true - if: > {{ @@ -7777,17 +7777,16 @@ action: milliseconds: 800 - service: '{{ nextion.command.set_button }}' data: - btn_id: '{{ repeat.item.page }}.{{ repeat.item.component }}' - btn_pic: '{{ nextion.pic.button.off }}' - btn_bg: '{{ nextion.color.grey_dark }}' - btn_icon_font: '{{ nextion.color.grey_light }}' - btn_txt_font: '{{ nextion.color.white }}' - btn_bri_font: '{{ [ btn_bri_font ] if is_number(btn_bri_font) else btn_bri_font }}' - btn_icon: '{{ btn_icon }}' - btn_label: '{{ btn_label }}' - btn_bri_txt: '{{ btn_bri_txt }}' - #btn_confirm: '{{ repeat.item.confirm }}' - #entity: '{{ repeat.item.entity }}' + page: '{{ repeat.item.page }}' + id: '{{ repeat.item.component }}' + pic: '{{ nextion.pic.button.off }}' + bg: '{{ nextion.color.grey_dark }}' + icon: '{{ btn_icon }}' + icon_color: '{{ nextion.color.grey_light }}' + bri: '{{ btn_bri_txt }}' + bri_color: '{{ [ btn_bri_font ] if is_number(btn_bri_font) else btn_bri_font }}' + label: '{{ btn_label }}' + label_color: '{{ nextion.color.white }}' continue_on_error: true ###### SHOW All component when page loading done ##### - if: '{{ not show_while_loading }}' diff --git a/nspanel_esphome_core.yaml b/nspanel_esphome_core.yaml index 67e44c7..411356f 100644 --- a/nspanel_esphome_core.yaml +++ b/nspanel_esphome_core.yaml @@ -11,9 +11,10 @@ substitutions: ############################# external_components: - - source: github://pr#5825 # Remove this when that pr is merged and released + - source: github://pr#5825 #5683 # Remove this when that pr is merged and released components: - nextion + refresh: 1s ##### ESPHOME CONFIGURATION ##### esphome: @@ -32,7 +33,7 @@ esphome: timeout: 60s - wait_until: condition: - - lambda: !lambda return disp1->is_setup(); + - lambda: !lambda return disp1->is_detected(); timeout: 20s - script.execute: exit_reparse - wait_until: @@ -485,30 +486,37 @@ api: #### Service to set the buttons #### - service: set_button variables: - btn_id: string - btn_pic: int - btn_bg: int[] - btn_icon_font: int[] - btn_txt_font: int[] - btn_bri_font: int[] - btn_icon: string - btn_label: string - btn_bri_txt: string + page: string + id: string + pic: int + bg: int[] + icon: string + icon_color: int[] + bri: string + bri_color: int[] + label: string + label_color: int[] then: - lambda: |- - std::string btnicon = btn_id.c_str() + std::string("icon"); - std::string btntext = btn_id.c_str() + std::string("text"); - std::string btnbri = btn_id.c_str() + std::string("bri"); - disp1->send_command_printf("%spic.pic=%" PRIu32, btn_id.c_str(), btn_pic); - set_component_color->execute(btnicon.c_str(), btn_icon_font, btn_bg); - set_component_color->execute(btntext.c_str(), btn_txt_font, btn_bg); - set_component_color->execute(btnbri.c_str(), btn_bri_font, btn_bg); - disp1->set_component_text_printf(btnicon.c_str(), "%s", btn_icon.c_str()); - display_wrapped_text->execute(btntext.c_str(), btn_label.c_str(), 10); - if (strcmp(btn_bri_txt.c_str(), "0") != 0) - disp1->set_component_text_printf(btnbri.c_str(), "%s", btn_bri_txt.c_str()); - else - disp1->set_component_text_printf(btnbri.c_str(), " "); + static const char *const TAG = "service.set_button"; + if (page == current_page->state) { + std::string btnicon = id.c_str() + std::string("icon"); + std::string btntext = id.c_str() + std::string("text"); + std::string btnbri = id.c_str() + std::string("bri"); + disp1->send_command_printf("%spic.pic=%" PRIu32, id.c_str(), pic); + set_component_color->execute(btnicon.c_str(), icon_color, bg); + set_component_color->execute(btntext.c_str(), label_color, bg); + set_component_color->execute(btnbri.c_str(), bri_color, bg); + disp1->set_component_text_printf(btnicon.c_str(), "%s", icon.c_str()); + display_wrapped_text->execute(btntext.c_str(), label.c_str(), 10); + if (strcmp(bri.c_str(), "0") != 0) + disp1->set_component_text_printf(btnbri.c_str(), "%s", bri.c_str()); + else + disp1->set_component_text_printf(btnbri.c_str(), " "); + } else { + ESP_LOGW(TAG, "Skipping button `%s.%s` as page has changed to %s.", page.c_str(), id.c_str(), current_page->state.c_str()); + } + ##### SERVICE TO WAKE UP THE DISPLAY ##### - service: wake_up @@ -1351,7 +1359,7 @@ switch: on_turn_on: - wait_until: condition: - - lambda: !lambda return disp1->is_detected(); + - lambda: !lambda return disp1->is_setup(); timeout: 20s - lambda: |- if (id(boot_sequence_completed)) {