From 0a26861444995d85a322bbb27bfb551988fcfaf1 Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Fri, 3 Nov 2023 21:10:42 +0100 Subject: [PATCH] Code clean-up --- nspanel_esphome_addon_climate_base.yaml | 50 ++++++------- nspanel_esphome_addon_upload_tft.yaml | 96 ++++++++++--------------- nspanel_esphome_advanced.yaml | 7 +- nspanel_esphome_core.yaml | 19 +++-- 4 files changed, 74 insertions(+), 98 deletions(-) diff --git a/nspanel_esphome_addon_climate_base.yaml b/nspanel_esphome_addon_climate_base.yaml index d76ec57..556bbf5 100644 --- a/nspanel_esphome_addon_climate_base.yaml +++ b/nspanel_esphome_addon_climate_base.yaml @@ -71,17 +71,17 @@ script: - lambda: |- // Update home.climate_entity variable id(entity_id) = (id(is_embedded_thermostat)) ? "embedded_climate" : ""; - id(disp1).set_component_value("climate.embedded", (id(is_embedded_thermostat)) ? 1 : 0); + disp1->set_component_value("climate.embedded", (id(is_embedded_thermostat)) ? 1 : 0); // Update chips if (id(is_embedded_thermostat)) - id(update_climate_icon).execute("home.icon_top_03", int(id(thermostat_embedded).action), int(id(thermostat_embedded).mode)); + id(update_climate_icon).execute("home.icon_top_03", int(thermostat_embedded->action), int(thermostat_embedded->mode)); - id: !extend addon_climate_service_call then: - lambda: |- id(is_addon_climate_visible) = true; - id(disp1).set_component_value("climate.embedded", 1); - auto call = id(thermostat_embedded).make_call(); + disp1->set_component_value("climate.embedded", 1); + auto call = thermostat_embedded->make_call(); if (key == "set_temperature") call.set_target_temperature(stof(value) / 10); else if (key == "hvac_mode") @@ -96,44 +96,44 @@ script: - id: !extend addon_climate_update_page_climate then: - lambda: |- - if (id(current_page).state == "climate" and id(is_addon_climate_visible)) + if (current_page->state == "climate" and id(is_addon_climate_visible)) { - id(disp1).set_component_text_printf("page_label", id(addon_climate_friendly_name).c_str()); + disp1->set_component_text_printf("page_label", id(addon_climate_friendly_name).c_str()); float temp_step = ${temp_step}; float temp_offset = ${temp_min}; float temp_max = ${temp_max}; float total_steps = (temp_max-temp_offset)/temp_step; - id(set_climate)->execute + set_climate->execute ( - id(thermostat_embedded).current_temperature, // current_temp - id(thermostat_embedded).target_temperature, // target_temp + thermostat_embedded->current_temperature, // current_temp + thermostat_embedded->target_temperature, // target_temp int(round(${temp_step}*10)), // temp_step - int(round(total_steps)), // total_steps //int(round((10*id(thermostat_embedded).target_temperature-temp_offset)/temp_step)), // slider_val + int(round(total_steps)), // total_steps //int(round((10*thermostat_embedded->target_temperature-temp_offset)/temp_step)), // slider_val int(round(${temp_min}*10)), // temp_offset "", // climate_icon true // embedded_climate ); // Update target temp icon - id(update_climate_icon).execute("climate.target_icon", int(id(thermostat_embedded).action), int(id(thermostat_embedded).mode)); + update_climate_icon->execute("climate.target_icon", int(thermostat_embedded->action), int(thermostat_embedded->mode)); // Update buttons bar // Hide not supported hotspots - id(disp1).hide_component("button01"); - id(disp1).hide_component("button02"); - if (${addon_climate_heat}) id(disp1).show_component("button03"); else id(disp1).hide_component("button03"); //Heat - if (${addon_climate_cool}) id(disp1).show_component("button04"); else id(disp1).hide_component("button04"); //Cool - id(disp1).hide_component("button05"); - id(disp1).hide_component("button06"); - id(disp1).show_component("button07"); //Off + disp1->hide_component("button01"); + disp1->hide_component("button02"); + if (${addon_climate_heat}) disp1->show_component("button03"); else disp1->hide_component("button03"); //Heat + if (${addon_climate_cool}) disp1->show_component("button04"); else disp1->hide_component("button04"); //Cool + disp1->hide_component("button05"); + disp1->hide_component("button06"); + disp1->show_component("button07"); //Off // Set buttons colors - id(disp1).set_component_font_color("climate.button01_icon", 6339); - id(disp1).set_component_font_color("climate.button02_icon", 6339); - id(disp1).set_component_font_color("climate.button03_icon", (id(thermostat_embedded).mode==climate::CLIMATE_MODE_HEAT) ? 64164 : ((${addon_climate_heat}) ? 48631 : 6339)); - id(disp1).set_component_font_color("climate.button04_icon", (id(thermostat_embedded).mode==climate::CLIMATE_MODE_COOL) ? 1055 : ((${addon_climate_cool}) ? 48631 : 6339)); - id(disp1).set_component_font_color("climate.button05_icon", 6339); - id(disp1).set_component_font_color("climate.button06_icon", 6339); - id(disp1).set_component_font_color("climate.button07_icon", (id(thermostat_embedded).mode==climate::CLIMATE_MODE_OFF) ? 10597 : 35921); + disp1->set_component_font_color("climate.button01_icon", 6339); + disp1->set_component_font_color("climate.button02_icon", 6339); + disp1->set_component_font_color("climate.button03_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_HEAT) ? 64164 : ((${addon_climate_heat}) ? 48631 : 6339)); + disp1->set_component_font_color("climate.button04_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_COOL) ? 1055 : ((${addon_climate_cool}) ? 48631 : 6339)); + disp1->set_component_font_color("climate.button05_icon", 6339); + disp1->set_component_font_color("climate.button06_icon", 6339); + disp1->set_component_font_color("climate.button07_icon", (thermostat_embedded->mode==climate::CLIMATE_MODE_OFF) ? 10597 : 35921); } - id: !extend addon_climate_set_climate_friendly_name diff --git a/nspanel_esphome_addon_upload_tft.yaml b/nspanel_esphome_addon_upload_tft.yaml index 065b46a..7d4ae40 100644 --- a/nspanel_esphome_addon_upload_tft.yaml +++ b/nspanel_esphome_addon_upload_tft.yaml @@ -18,27 +18,10 @@ substitutions: ############################# external_components: -# - source: github://pr#3256 # adds esp-idf support to http_request -# components: -# - http_request - - source: github://pr#5667 #5484 #5660 # adds exit reparse to Nextion library + - source: github://pr#5667 # Remove this when that pr is merged components: - nextion refresh: 300s -# - source: -# type: git -# url: https://github.com/edwardtfn/esphome -# ref: nextion-esp-idf -# components: -# - nextion -# refresh: 1s - -##### HTTP REQUEST ##### -# Enables http client # -# for upload_tft. # -######################## -#http_request: -# id: httpclient button: ##### UPDATE TFT DISPLAY ##### @@ -49,11 +32,8 @@ button: entity_category: config on_press: - logger.log: "Button pressed: Update TFT display" - - binary_sensor.template.publish: - id: nextion_init - state: false - - delay: 16ms - - lambda: id(upload_tft).execute("${nextion_update_url}"); + - lambda: |- + upload_tft->execute("${nextion_update_url}"); api: services: @@ -61,10 +41,8 @@ api: - service: upload_tft then: - logger.log: "Service: upload_tft" - - binary_sensor.template.publish: - id: nextion_init - state: false - - lambda: 'id(upload_tft).execute("${nextion_update_url}");' + - lambda: |- + upload_tft->execute("${nextion_update_url}"); ##### SERVICE TO UPDATE THE TFT FILE from URL ##### - service: upload_tft_url @@ -72,10 +50,8 @@ api: url: string then: - logger.log: "Service: upload_tft_url" - - binary_sensor.template.publish: - id: nextion_init - state: false - - lambda: 'id(upload_tft).execute(url.c_str());' + - lambda: |- + upload_tft->execute(url.c_str()); display: - id: !extend disp1 @@ -90,51 +66,51 @@ script: - lambda: |- static const char *const TAG = "script.upload_tft"; ESP_LOGVV(TAG, "Starting..."); + + nextion_init->state = false; + + auto delay_seconds_ = [](int seconds) { + ESP_LOGVV(TAG, "Wait %i seconds", seconds); + for (int i = 0; i < (seconds*4); i++) { + #ifdef ARDUINO + delay(250); + #elif defined(ESP_PLATFORM) + vTaskDelay(pdMS_TO_TICKS(250)); + #endif + App.feed_wdt(); + } + }; + ESP_LOGV(TAG, "Setting TFT url: %s", url.c_str()); - id(disp1)->set_tft_url(url.c_str()); + disp1->set_tft_url(url.c_str()); unsigned int upload_tries = 0; while (upload_tries < 5) { upload_tries++; ESP_LOGD(TAG, "Try #%i", upload_tries); ESP_LOGD(TAG, "Setting Nextion protocol reparse mode to passive"); - id(disp1).set_protocol_reparse_mode(false); + exit_reparse->execute(); + delay_seconds_(2); ESP_LOGV(TAG, "Calling upload from Nextion component"); - if (id(disp1)->upload_tft()) id(restart_nspanel).press(); + if (disp1->upload_tft()) id(restart_nspanel).press(); ESP_LOGD(TAG, "Turn off Nextion"); - id(screen_power).turn_off(); - #ifdef ARDUINO - delay(1500); - #elif defined(ESP_PLATFORM) - vTaskDelay(pdMS_TO_TICKS(1500)); - #endif + screen_power->turn_off(); + delay_seconds_(3); ESP_LOGD(TAG, "Turn on Nextion"); - id(screen_power).turn_on(); - #ifdef ARDUINO - delay(1500); - #elif defined(ESP_PLATFORM) - vTaskDelay(pdMS_TO_TICKS(1500)); - #endif + screen_power->turn_on(); + delay_seconds_(2); } ESP_LOGE(TAG, "TFT upload failed."); ESP_LOGD(TAG, "Turn off Nextion"); - id(screen_power).turn_off(); - #ifdef ARDUINO - delay(1500); - #elif defined(ESP_PLATFORM) - vTaskDelay(pdMS_TO_TICKS(1500)); - #endif + screen_power->turn_off(); + delay_seconds_(2); ESP_LOGD(TAG, "Turn on Nextion"); - id(screen_power).turn_on(); + screen_power->turn_on(); ESP_LOGD(TAG, "Restarting esphome"); - #ifdef ARDUINO - delay(1500); - #elif defined(ESP_PLATFORM) - vTaskDelay(pdMS_TO_TICKS(1500)); - #endif - id(restart_nspanel).press(); + delay_seconds_(1); + restart_nspanel->press(); + nextion_init->state = true; ESP_LOGV(TAG, "Finished!"); - - id: upload_tft_legacy # Not in use - To be removed when Nextion component is updated mode: single parameters: diff --git a/nspanel_esphome_advanced.yaml b/nspanel_esphome_advanced.yaml index b8b408b..f743e54 100644 --- a/nspanel_esphome_advanced.yaml +++ b/nspanel_esphome_advanced.yaml @@ -36,12 +36,7 @@ button: entity_category: config on_press: - logger.log: "Button pressed: Exit reparse" - - uart.write: - id: tf_uart - data: "DRAKJHSUYDGBNCJHGJKSHBDN" - - uart.write: - id: tf_uart - data: [0xFF, 0xFF, 0xFF] + - script.execute: exit_reparse sensor: ##### Uptime Sensors ##### diff --git a/nspanel_esphome_core.yaml b/nspanel_esphome_core.yaml index ebe1cf9..a5dbff1 100644 --- a/nspanel_esphome_core.yaml +++ b/nspanel_esphome_core.yaml @@ -34,13 +34,7 @@ esphome: condition: - lambda: !lambda return id(disp1).is_setup(); timeout: 20s - - logger.log: "Exit reparse" - - uart.write: - id: tf_uart - data: "DRAKJHSUYDGBNCJHGJKSHBDN" - - uart.write: - id: tf_uart - data: [0xFF, 0xFF, 0xFF] + - script.execute: exit_reparse - wait_until: condition: - lambda: !lambda return id(disp1).is_setup(); @@ -2418,6 +2412,17 @@ script: } id(disp1).set_component_text_printf("page_index", "%s", indicator.c_str()); + - id: exit_reparse + mode: restart + then: + - logger.log: "Exit reparse" + - uart.write: + id: tf_uart + data: "DRAKJHSUYDGBNCJHGJKSHBDN" + - uart.write: + id: tf_uart + data: [0xFF, 0xFF, 0xFF] + ##### ADD-ONS ############################################################ ##### Add-on - Climate ##### - id: addon_climate_service_call