TFT upload selectors

This commit is contained in:
Edward Firmo
2024-01-03 20:16:33 +01:00
parent 00d8cc9c94
commit 30ed450a4f
12 changed files with 229 additions and 131 deletions

View File

@@ -174,35 +174,35 @@ script:
- id: !extend watchdog
then:
- lambda: |-
static const char *const TAG = "script.watchdog (add-on climate)";
static const char *const TAG = "addon_climate_base.script.watchdog";
bool addon_climate_cool = ${addon_climate_cool};
bool addon_climate_heat = ${addon_climate_heat};
bool addon_climate_dual = ${addon_climate_dual};
uint cooler_relay = ${cooler_relay};
uint heater_relay = ${heater_relay};
ESP_LOGD(TAG, "Add-on climate:");
ESP_LOGI(TAG, "Add-on climate:");
if (addon_climate_cool) {
ESP_LOGD(TAG, " Cool: %s", addon_climate_cool ? "Enabled" : "Disabled");
ESP_LOGI(TAG, " Cool: %s", addon_climate_cool ? "Enabled" : "Disabled");
if (cooler_relay == 1 or cooler_relay == 2)
ESP_LOGD(TAG, " Relay: %i", cooler_relay);
ESP_LOGI(TAG, " Relay: %i", cooler_relay);
else
ESP_LOGE(TAG, " Relay: %i", cooler_relay);
}
if (addon_climate_heat) {
ESP_LOGD(TAG, " Heat: %s", addon_climate_heat ? "Enabled" : "Disabled");
ESP_LOGI(TAG, " Heat: %s", addon_climate_heat ? "Enabled" : "Disabled");
if (heater_relay == 1 or heater_relay == 2)
ESP_LOGD(TAG, " Relay: %i", heater_relay);
ESP_LOGI(TAG, " Relay: %i", heater_relay);
else
ESP_LOGE(TAG, " Relay: %i", heater_relay);
}
if (addon_climate_dual) {
ESP_LOGD(TAG, " Dual: %s", addon_climate_dual ? "Enabled" : "Disabled");
ESP_LOGI(TAG, " Dual: %s", addon_climate_dual ? "Enabled" : "Disabled");
if (cooler_relay == 1 or cooler_relay == 2)
ESP_LOGD(TAG, " Relay (cooler): %i", cooler_relay);
ESP_LOGI(TAG, " Relay (cooler): %i", cooler_relay);
else
ESP_LOGE(TAG, " Relay (cooler): %i", cooler_relay);
if (heater_relay == 1 or heater_relay == 2)
ESP_LOGD(TAG, " Relay (heater): %i", heater_relay);
ESP_LOGI(TAG, " Relay (heater): %i", heater_relay);
else
ESP_LOGE(TAG, " Relay (heater): %i", heater_relay);
if (cooler_relay == heater_relay)

View File

@@ -10,16 +10,27 @@
substitutions:
################## Defaults ##################
# Just in case user forgets to set something #
nextion_update_url: "http://github.com/Blackymas/NSPanel_HA_Blueprint/raw/main/custom_configuration/nspanel_eu.tft"
nextion_blank_url: "http://github.com/Blackymas/NSPanel_HA_Blueprint/raw/main/custom_configuration/nspanel_blank.tft"
nextion_update_url: "https://raw.githubusercontent.com/Blackymas/NSPanel_HA_Blueprint/main/nspanel_eu.tft"
nextion_update_base_url: "https://raw.githubusercontent.com/Blackymas/NSPanel_HA_Blueprint/"
##############################################
##### DON'T CHANGE THIS #####
upload_tft_chunk_size_max: "32768"
upload_tft_baud_rate: "0"
upload_tft_baud_rate: "921600"
#############################
api:
on_client_connected:
- delay: 15s
- lambda: |-
static const char *const TAG = "addon_upload_tft.esphome.on_boot";
ESP_LOGI(TAG, "TFT URL: %s", id(tft_url).c_str());
ESP_LOGI(TAG, "Substitutions:");
ESP_LOGI(TAG, " nextion_update_url: ${nextion_update_url}");
ESP_LOGI(TAG, " nextion_update_base_url: ${nextion_update_base_url}");
ESP_LOGI(TAG, " upload_tft_chunk_size_max: ${upload_tft_chunk_size_max}");
ESP_LOGI(TAG, " upload_tft_baud_rate: ${upload_tft_baud_rate}");
services:
##### SERVICE TO UPDATE THE TFT FILE from URL #####
##### It will use the default url if url is empty or "default"
@@ -28,7 +39,7 @@ api:
url: string
then:
- lambda: |-
static const char *const TAG = "service.upload_tft_url";
static const char *const TAG = "addon_upload_tft.service.upload_tft_url";
ESP_LOGVV(TAG, "Starting...");
std::string clean_url = url;
@@ -41,7 +52,7 @@ api:
clean_url = clean_url.substr(0, endPos + 1);
}
if (clean_url.empty() or clean_url == "default") url = "${nextion_update_url}";
if (clean_url.empty() or clean_url == "default") url = id(tft_url);
upload_tft->execute(url.c_str());
button:
@@ -53,9 +64,9 @@ button:
entity_category: config
on_press:
- lambda: |-
static const char *const TAG = "button.tft_update.on_press";
static const char *const TAG = "addon_upload_tft.button.tft_update.on_press";
ESP_LOGD(TAG, "Update TFT display button pressed");
upload_tft->execute("${nextion_update_url}");
upload_tft->execute(id(tft_url).c_str());
display:
- id: !extend disp1
@@ -83,6 +94,11 @@ globals:
restore_value: false
initial_value: '0'
- id: tft_url
type: std::string
restore_value: false
initial_value: '"${nextion_update_url}"'
script:
- id: exit_reparse_v414
mode: restart
@@ -101,7 +117,7 @@ script:
command: string
then:
- lambda: |-
static const char *const TAG = "script.nextion_uart_command";
static const char *const TAG = "addon_upload_tft.script.nextion_uart_command";
if (disp1->is_setup()) {
ESP_LOGD(TAG, "Sending `%s` directly to Nextion", command.c_str());
disp1->send_command_printf(command.c_str());
@@ -113,28 +129,11 @@ script:
}
App.feed_wdt();
- id: report_upload_progress
mode: restart
parameters:
message: string
then:
- lambda: |-
static const char *const TAG = "script.report_upload_progress";
ESP_LOGD(TAG, "%s", message.c_str());
if (id(tft_is_valid)) {
if (page_id->state != 26) {
open_upload_dialog->execute();
}
display_wrapped_text->execute("confirm.body", message.c_str(), 18);
disp1->set_backlight_brightness(1);
App.feed_wdt();
}
- id: nextion_upload
mode: single
then:
- lambda: |-
static const char *const TAG = "script.nextion_upload";
static const char *const TAG = "addon_upload_tft.script.nextion_upload";
ESP_LOGD(TAG, "Waiting for empty UART and Nextion queues");
- wait_until:
condition:
@@ -143,7 +142,7 @@ script:
timeout: 10s
- delay: 2s
- lambda: |-
static const char *const TAG = "script.nextion_upload";
static const char *const TAG = "addon_upload_tft.script.nextion_upload";
ESP_LOGD(TAG, "Starting TFT upload...");
if (disp1->upload_tft()) App.safe_reboot();
@@ -151,7 +150,7 @@ script:
mode: restart
then:
- lambda: |-
static const char *const TAG = "script.open_upload_dialog";
static const char *const TAG = "addon_upload_tft.script.open_upload_dialog";
ESP_LOGD(TAG, "Showing upload dialog page");
disp1->goto_page("confirm");
disp1->hide_component("bt_close");
@@ -165,6 +164,63 @@ script:
#endif
page_id->update();
- id: report_upload_progress
mode: restart
parameters:
message: string
then:
- lambda: |-
static const char *const TAG = "addon_upload_tft.script.report_upload_progress";
ESP_LOGD(TAG, "%s", message.c_str());
if (id(tft_is_valid)) {
if (page_id->state != 26) {
open_upload_dialog->execute();
}
display_wrapped_text->execute("confirm.body", message.c_str(), 18);
disp1->set_backlight_brightness(1);
App.feed_wdt();
}
- id: set_tft_file
mode: restart
parameters:
model: string
branch: string
then:
- lambda: |-
static const char *const TAG = "addon_upload_tft.script.set_tft_file";
ESP_LOGD(TAG, "A new TFT was selected:");
ESP_LOGD(TAG, " Model: %s", model.c_str());
ESP_LOGD(TAG, " Branch: %s", branch.c_str());
if (id(is_uploading_tft))
ESP_LOGW(TAG, "TFT Upload in progress.");
else {
std::string url;
std::string file_name;
if (model == "NSPanel Blank") file_name = "nspanel_blank.tft";
else if (model == "NSPanel EU") file_name = "nspanel_eu.tft";
else if (model == "NSPanel US") file_name = "nspanel_us.tft";
else if (model == "NSPanel US Landscape") file_name = "nspanel_us_land.tft";
else if (model == "NSPanel EU (CJK languages)") file_name = "advanced/hmi/nspanel_CJK_eu.tft";
else if (model == "NSPanel US (CJK languages)") file_name = "advanced/hmi/nspanel_CJK_us.tft";
else if (model == "NSPanel US Landscape (CJK languages)") file_name = "advanced/hmi/nspanel_CJK_us_land.tft";
if (file_name.empty()) url = "${nextion_update_url}";
else {
std::string base_url("${nextion_update_base_url}");
url = base_url + branch + "/" + file_name;
// Remove trailing slashes or spaces
auto endPos = url.find_last_not_of(" /");
if (std::string::npos != endPos) {
url = url.substr(0, endPos + 1);
}
ESP_LOGD(TAG, "Full URL: %s", url.c_str());
id(tft_url) = url;
disp1->set_tft_url(url.c_str());
}
}
- id: upload_tft # I've changed this to use ESPHome commands to avoid the parallelism from lambdas
mode: single
parameters:
@@ -177,9 +233,11 @@ script:
then:
- switch.turn_on: screen_power
- delay: 5s
# Wait for recent changes to TFT url
- script.wait: set_tft_file
# Then start the upload
- lambda: |-
static const char *const TAG = "script.upload_tft";
static const char *const TAG = "addon_upload_tft.script.upload_tft";
ESP_LOGD(TAG, "Starting...");
id(is_uploading_tft) = true;
@@ -449,7 +507,7 @@ script:
### This code should never run
- delay: 2s
- lambda: |-
static const char *const TAG = "script.upload_tft";
static const char *const TAG = "addon_upload_tft.script.upload_tft";
ESP_LOGD(TAG, "Finishing...");
id(is_uploading_tft) = false;
screen_power->publish_state(true);
@@ -461,7 +519,7 @@ script:
url: string
then:
- lambda: |-
static const char *const TAG = "script.upload_tft_v414";
static const char *const TAG = "addon_upload_tft.script.upload_tft_v414";
ESP_LOGD(TAG, "Trying to upload TFT");
id(tft_upload_try)++;
char update_msg[128];
@@ -473,7 +531,7 @@ script:
- lambda: !lambda return (!id(tft_is_valid));
timeout: 1s
- lambda: |-
static const char *const TAG = "script.upload_tft_v414";
static const char *const TAG = "addon_upload_tft.script.upload_tft_v414";
ESP_LOGD(TAG, "Starting...");
std::vector<uint8_t> buffer_;
@@ -509,7 +567,7 @@ script:
auto send_nextion_command = [](const std::string &command) -> bool
{
static const char *const TAG = "script.upload_tft.send_nextion_command";
static const char *const TAG = "addon_upload_tft.script.upload_tft.send_nextion_command";
ESP_LOGD(TAG, "Sending: %s", command.c_str());
id(tf_uart).write_str(command.c_str());
const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
@@ -704,7 +762,7 @@ script:
};
auto upload_tft_ = [&](const std::string &url, unsigned int update_baud_rate_) -> bool
{
static const char *const TAG = "script.upload_tft.upload_tft_arduino";
static const char *const TAG = "addon_upload_tft.script.upload_tft.upload_tft_arduino";
ESP_LOGD(TAG, "Nextion TFT upload requested");
ESP_LOGD(TAG, " url: %s", url.c_str());
ESP_LOGD(TAG, " baud_rate: %i", update_baud_rate_);
@@ -864,7 +922,7 @@ script:
};
#elif defined(ESP_PLATFORM) // esp-idf # To do: Move to Nextion component on ESPHome
auto upload_range_esp_idf_ = [&](const std::string &url, int range_start) -> int {
static const char *const TAG = "script.upload_tft.upload_range_esp_idf_";
static const char *const TAG = "addon_upload_tft.script.upload_tft.upload_range_esp_idf_";
ESP_LOGVV(TAG, "url: %s", url.c_str());
uint range_size_ = tft_size_ - range_start;
ESP_LOGVV(TAG, "tft_size_: %i", tft_size_);
@@ -970,7 +1028,7 @@ script:
return range_end + 1;
};
auto upload_tft_ = [&](const std::string &url, unsigned int update_baud_rate_) -> bool {
static const char *const TAG = "script.upload_tft.upload_tft_esp_idf";
static const char *const TAG = "addon_upload_tft.script.upload_tft.upload_tft_esp_idf";
ESP_LOGD(TAG, "Nextion TFT upload requested");
ESP_LOGD(TAG, " url: %s", url.c_str());
ESP_LOGD(TAG, " baud_rate: %i", update_baud_rate_);
@@ -1132,6 +1190,48 @@ script:
report_upload_progress->execute(update_msg);
- script.wait: report_upload_progress
select:
- id: tft_file
name: ${device_name} Update TFT display - Model
platform: template
options:
- "Use nextion_update_url"
- "NSPanel Blank"
- "NSPanel EU"
- "NSPanel US"
- "NSPanel US Landscape"
- "NSPanel EU (CJK languages)"
- "NSPanel US (CJK languages)"
- "NSPanel US Landscape (CJK languages)"
initial_option: "Use nextion_update_url"
optimistic: false
restore_value: true
internal: false
entity_category: config
disabled_by_default: false
icon: mdi:file-sync
set_action:
- lambda: set_tft_file->execute(x.c_str(), tft_file_branch->state.c_str());
- script.wait: set_tft_file
- id: tft_file_branch
name: ${device_name} Update TFT display - Branch
platform: template
options:
- "main"
- "beta"
- "dev"
initial_option: "main"
optimistic: false
restore_value: true
internal: false
entity_category: config
disabled_by_default: true
icon: mdi:file-sync
set_action:
- lambda: set_tft_file->execute(tft_file->state.c_str(), x.c_str());
- script.wait: set_tft_file
sensor:
- id: !extend display_mode
on_value:

View File

@@ -19,7 +19,7 @@ substitutions:
##### External components #####
external_components:
- source: github://pr#6029 # Remove this when that PR is released (Hopefully v2024.1.0)
- source: github://pr#6051 # Remove this when that PR is released (Hopefully v2024.1.0)
components:
- nextion
@@ -3031,7 +3031,7 @@ script:
else if (rssi > -70) rssi_status = "Fair";
else if (rssi > -80) rssi_status = "Weak";
else rssi_status = "Poor";
if (rssi > -70) ESP_LOGD(TAG, "Wi-Fi: %s (%.0f dBm)", rssi_status.c_str(), rssi);
if (rssi > -70) ESP_LOGI(TAG, "Wi-Fi: %s (%.0f dBm)", rssi_status.c_str(), rssi);
else if (rssi > -80) ESP_LOGW(TAG, "Wi-Fi: %s (%.0f dBm)", rssi_status.c_str(), rssi);
else ESP_LOGE(TAG, "Wi-Fi: %s (%.0f dBm)", rssi_status.c_str(), rssi);
}
@@ -3039,7 +3039,7 @@ script:
ESP_LOGW(TAG, "Wi-Fi: DISCONNECTED");
if (id(wifi_timeout) > 0) {
id(wifi_timeout)--;
ESP_LOGD(TAG, "Retrying Wi-Fi connection");
ESP_LOGI(TAG, "Retrying Wi-Fi connection");
wifi_component->retry_connect();
} else {
ESP_LOGE(TAG, "Restarting ESP due to a Wi-Fi timeout...");
@@ -3051,7 +3051,7 @@ script:
bool previous_blueprint_status = id(is_blueprint_updated);
bool api_connected = api_server->is_connected();
if (api_connected) {
ESP_LOGD(TAG, "API: Connected");
ESP_LOGI(TAG, "API: Connected");
} else {
ESP_LOGW(TAG, "API: DISCONNECTED");
id(is_blueprint_updated) = false;
@@ -3061,7 +3061,7 @@ script:
current_page->state != "screensaver" and
current_page->state != "settings" and
current_page->state != "qrcode") {
ESP_LOGD(TAG, "Fallback to page Home");
ESP_LOGI(TAG, "Fallback to page Home");
disp1->goto_page("home");
}
}
@@ -3069,10 +3069,10 @@ script:
// Report blueprint version
id(is_blueprint_updated) = id(is_blueprint_updated) and wifi_connected and api_connected and (not id(version_blueprint).empty());
if (id(is_blueprint_updated))
ESP_LOGD(TAG, "Blueprint: %s", id(version_blueprint).c_str());
ESP_LOGI(TAG, "Blueprint: %s", id(version_blueprint).c_str());
else {
ESP_LOGW(TAG, "Blueprint: %s", (wifi_connected and api_connected) ? "Pending" : "DISCONNECTED");
ESP_LOGD(TAG, "Requesting blueprint settings");
ESP_LOGI(TAG, "Requesting blueprint settings");
auto ha_event = new esphome::api::CustomAPIDevice();
ha_event->fire_homeassistant_event("esphome.nspanel_ha_blueprint",
{
@@ -3084,33 +3084,33 @@ script:
refresh_wifi_icon->execute();
// Report ESPHome
ESP_LOGD(TAG, "ESPHome:");
ESP_LOGD(TAG, " Version: ${version}");
ESP_LOGI(TAG, "ESPHome:");
ESP_LOGI(TAG, " Version: ${version}");
// Report framework
#ifdef ARDUINO
ESP_LOGD(TAG, " Framework: arduino");
ESP_LOGI(TAG, " Framework: arduino");
#elif defined(USE_ESP_IDF)
ESP_LOGD(TAG, " Framework: esp-idf");
ESP_LOGI(TAG, " Framework: esp-idf");
#endif
// Report UART
ESP_LOGD(TAG, "UART:");
ESP_LOGD(TAG, " Baud rate: %d bps", tf_uart->get_baud_rate());
ESP_LOGD(TAG, " Queue size: %d", tf_uart->available());
ESP_LOGI(TAG, "UART:");
ESP_LOGI(TAG, " Baud rate: %d bps", tf_uart->get_baud_rate());
ESP_LOGI(TAG, " Queue size: %d", tf_uart->available());
// Report Nextion status
nextion_init->publish_state(nextion_init->state and disp1->is_setup());
ESP_LOGD(TAG, "Nextion:");
ESP_LOGD(TAG, " Queue size: %d", disp1->queue_size());
ESP_LOGI(TAG, "Nextion:");
ESP_LOGI(TAG, " Queue size: %d", disp1->queue_size());
if (disp1->is_setup())
ESP_LOGD(TAG, " Is setup: True");
ESP_LOGI(TAG, " Is setup: True");
else {
ESP_LOGW(TAG, " Is setup: False");
ESP_LOGW(TAG, " Is detected: %s", disp1->is_detected() ? "True" : "False");
//exit_reparse->execute();
}
if (nextion_init->state) {
ESP_LOGD(TAG, " Init: True");
ESP_LOGI(TAG, " Init: True");
// Update api value on Nextion
if (id(is_blueprint_updated) != previous_blueprint_status) disp1->send_command_printf("api=%i", id(is_blueprint_updated) ? 1 : 0);
} else
@@ -3118,7 +3118,7 @@ script:
if (version_tft->state.empty())
ESP_LOGW(TAG, " TFT: UNKNOWN");
else
ESP_LOGD(TAG, " TFT: %s", version_tft->state.c_str());
ESP_LOGI(TAG, " TFT: %s", version_tft->state.c_str());
}
ESP_LOGV(TAG, "Finished");

View File

@@ -9,7 +9,6 @@ substitutions:
device_name: nspanel
wifi_ssid: nspanel
wifi_password: NSPanel_Blueprint
nextion_update_url: "http://github.com/Blackymas/NSPanel_HA_Blueprint/raw/main/nspanel_blank.tft"
pre_built: "true"
packages:
@@ -17,8 +16,6 @@ packages:
upload_tft_package: !include ./nspanel_esphome_addon_upload_tft.yaml # Core package
button:
- id: !remove tft_update
- platform: factory_reset
name: Factory reset
disabled_by_default: true
@@ -56,6 +53,27 @@ logger:
ota:
password: ""
script:
- id: !extend watchdog
then:
- lambda: |-
static const char *const TAG = "prebuilt.script.watchdog";
ESP_LOGI(TAG, "Pre-built version: ${pre_built}");
select:
- id: !extend tft_file
platform: template
options:
- "NSPanel Blank"
- "NSPanel EU"
- "NSPanel US"
- "NSPanel US Landscape"
- "NSPanel EU (CJK languages)"
- "NSPanel US (CJK languages)"
- "NSPanel US Landscape (CJK languages)"
initial_option: "NSPanel Blank"
wifi:
ap: {}
power_save_mode: LIGHT # To make it compatible with BLE