From 718ec12e3ccafa6d49a4a201148d6df8e666173a Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Fri, 23 Feb 2024 22:57:13 +0100 Subject: [PATCH] Fix home page missing components Solves #1833 --- .../__init__.py | 18 ++++++++ .../upload_tft.h | 45 +++++++++++++++++++ esphome/nspanel_esphome_core.yaml | 17 +++---- 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 components/nspanel_ha_blueprint_upload_tft/__init__.py create mode 100644 components/nspanel_ha_blueprint_upload_tft/upload_tft.h diff --git a/components/nspanel_ha_blueprint_upload_tft/__init__.py b/components/nspanel_ha_blueprint_upload_tft/__init__.py new file mode 100644 index 0000000..e521bf0 --- /dev/null +++ b/components/nspanel_ha_blueprint_upload_tft/__init__.py @@ -0,0 +1,18 @@ +# __init__.py +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.core import coroutine_with_priority + +CODEOWNERS = ["@edwardtfn"] + +nspanel_ha_blueprint_upload_tft_ns = cg.esphome_ns.namespace('nspanel_ha_blueprint_upload_tft') + +CONFIG_SCHEMA = cv.All( + cv.Schema({}), +) + +@coroutine_with_priority(1.0) + +async def to_code(config): + cg.add_define("USE_NSPANEL_HA_BLUEPRINT_UPLOAD_TFT") + cg.add_global(nspanel_ha_blueprint_upload_tft_ns.using) diff --git a/components/nspanel_ha_blueprint_upload_tft/upload_tft.h b/components/nspanel_ha_blueprint_upload_tft/upload_tft.h new file mode 100644 index 0000000..0582451 --- /dev/null +++ b/components/nspanel_ha_blueprint_upload_tft/upload_tft.h @@ -0,0 +1,45 @@ +// upload_tft.h +#pragma once + +#include + +namespace nspanel_ha_blueprint { + + /** + * Constructs the TFT file URL based on branch, model, default URL, and base URL. + * + * @param branchInput The input branch version, which might contain special keywords like "beta" or "dev". + * @param model The model of the device, used to determine the specific TFT file name. + * @param defaultUrl The default URL to use if no specific file is associated with the model. + * @param baseUrl The base URL to which branch and file names are appended to construct the full URL. + * @return The fully constructed URL as a string. + */ + std::string construct_tft_url(const std::string& branch, const std::string& model, + const std::string& defaultUrl, const std::string& baseUrl) { + // Determine the branch based on the input + std::string relative_branch = branch; + if (branch.find("beta") != std::string::npos) relative_branch = "beta"; + else if (branch.find("dev") != std::string::npos) relative_branch = "dev"; + + // Mapping model to the corresponding TFT file name + 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 = "nspanel_CJK_eu.tft"; + else if (model == "NSPanel US (CJK languages)") file_name = "nspanel_CJK_us.tft"; + else if (model == "NSPanel US Landscape (CJK languages)") file_name = "nspanel_CJK_us_land.tft"; + + // Construct the URL based on the determined file name + std::string url; + if (file_name.empty()) { + url = defaultUrl; // Use the default URL if no specific file name is matched + } else { + url = baseUrl + relative_branch + "/hmi/" + file_name; // Construct the full URL + } + + return url; // Return the constructed URL + } + +} // namespace nspanel_ha_blueprint diff --git a/esphome/nspanel_esphome_core.yaml b/esphome/nspanel_esphome_core.yaml index a9068f0..4e32bb1 100644 --- a/esphome/nspanel_esphome_core.yaml +++ b/esphome/nspanel_esphome_core.yaml @@ -27,6 +27,8 @@ external_components: type: git url: https://github.com/Blackymas/NSPanel_HA_Blueprint ref: dev # To do: Change it for releasing + components: + - nspanel_ha_blueprint refresh: 3s # To do: Change it for releasing - source: type: git @@ -466,13 +468,6 @@ api: visible: bool then: - lambda: |- - static const char *const TAG = "service.icon"; - ESP_LOGI(TAG, "id: %s", id.c_str()); - ESP_LOGI(TAG, "id_empty: %s", YESNO(id.empty())); - ESP_LOGI(TAG, "icon: %s", icon.c_str()); - ESP_LOGI(TAG, "icon_color: %i", icon_color.size()); - ESP_LOGI(TAG, "visible: %s", YESNO(visible)); - ESP_LOGI(TAG, "page: %s", current_page->state.c_str()); if (!id(is_uploading_tft) and !(id.empty())) { if (!(icon.empty())) disp1->set_component_text_printf(id.c_str(), "%s", icon.c_str()); if (icon_color.size() == 3) @@ -1298,14 +1293,14 @@ api: then: - lambda: |- if (!id(is_uploading_tft) and !(id.empty())) { - if (!(icon.empty())) disp1->set_component_text_printf("%s_icon", id.c_str(), icon.c_str()); + if (!(icon.empty())) disp1->set_component_text_printf((id + "_icon").c_str(), "%s", icon.c_str()); if (icon_color.size() == 3) disp1->set_component_font_color((id + "_icon").c_str(), esphome::display::ColorUtil::color_to_565(esphome::Color(icon_color[0], icon_color[1], icon_color[2]))); - if (!(name.empty())) disp1->set_component_text_printf("%s_label", id.c_str(), name.c_str()); - if (!(value.empty())) disp1->set_component_text_printf("%s", id.c_str(), value.c_str()); + if (!(name.empty())) disp1->set_component_text_printf((id + "_label").c_str(), "%s", name.c_str()); + if (!(value.empty())) disp1->set_component_text_printf(id.c_str(), "%s", value.c_str()); if (value_color.size() == 3) disp1->set_component_font_color(id.c_str(), esphome::display::ColorUtil::color_to_565(esphome::Color(value_color[0], value_color[1], @@ -2378,7 +2373,7 @@ script: std::string temp_units = "${temp_units}"; if (temp_units == "°F" || temp_units == "F" || temp_units == "°f" || temp_units == "f") unit_based_temperature = (unit_based_temperature * 9 / 5) + 32; - disp1->set_component_text_printf("home.current_temp", "%.1f${temp_units}", unit_based_temperature); + disp1->set_component_text_printf("home.indr_temp", "%.1f${temp_units}", unit_based_temperature); } - id: display_wrapped_text