From 8404a6e62c9b8afd66457ea237be50a7bf54b31f Mon Sep 17 00:00:00 2001 From: Edward Firmo <94725493+edwardtfn@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:03:31 +0100 Subject: [PATCH] Converts betwwen C/F before display embedded climate Solves #1678 --- .../nspanel_esphome_addon_climate_base.yaml | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/advanced/esphome/nspanel_esphome_addon_climate_base.yaml b/advanced/esphome/nspanel_esphome_addon_climate_base.yaml index 9899786..c4d862a 100644 --- a/advanced/esphome/nspanel_esphome_addon_climate_base.yaml +++ b/advanced/esphome/nspanel_esphome_addon_climate_base.yaml @@ -82,20 +82,31 @@ script: - id: !extend change_climate_state then: - lambda: |- + auto FahrenheitToCelsius = [](float fahrenheit) -> float { + return (fahrenheit - 32.0) * 5.0 / 9.0; + }; + if (embedded) { static const char *const TAG = "addon_climate_base.script.change_climate_state"; id(is_addon_climate_visible) = true; disp1->set_component_value("climate.embedded", 1); auto call = thermostat_embedded->make_call(); + float temperature; if (key == "temperature") { - ESP_LOGD(TAG, "set_target_temperature(%f)", (stof(value) / 10)); - call.set_target_temperature(stof(value) / 10); + temperature = stof(value) / 10; + if (id(temp_unit_fahrenheit)) temperature = FahrenheitToCelsius(temperature); + ESP_LOGD(TAG, "set_target_temperature(%f)", temperature); + call.set_target_temperature(temperature); } else if (key == "target_temp_high") { - ESP_LOGD(TAG, "set_target_temperature_high(%f)", (stof(value) / 10)); - call.set_target_temperature_high(stof(value) / 10); + temperature = stof(value) / 10; + if (id(temp_unit_fahrenheit)) temperature = FahrenheitToCelsius(temperature); + ESP_LOGD(TAG, "set_target_temperature_high(%f)", temperature); + call.set_target_temperature_high(temperature); } else if (key == "target_temp_low") { - ESP_LOGD(TAG, "set_target_temperature_low(%f)", (stof(value) / 10)); - call.set_target_temperature_low(stof(value) / 10); + temperature = stof(value) / 10; + if (id(temp_unit_fahrenheit)) temperature = FahrenheitToCelsius(temperature); + ESP_LOGD(TAG, "set_target_temperature_low(%f)", temperature); + call.set_target_temperature_low(temperature); } else if (key == "hvac_mode") { ESP_LOGD(TAG, "hvac_mode(%s)", value.c_str()); call.set_mode(value); @@ -122,28 +133,48 @@ script: - id: !extend page_climate then: - lambda: |- + auto CelsiusToFahrenheit = [](float celsius) -> float { + return (celsius * 9 / 5) + 32; + }; + id(is_addon_climate_visible) = (current_page->state == "climate" and detailed_entity->state == "embedded_climate"); if (id(is_addon_climate_visible)) { static const char *const TAG = "addon_climate_base.script.page_climate"; ESP_LOGV(TAG, "Climate page constructor:"); ESP_LOGV(TAG, " Add-on mode: %s", (${addon_climate_dual}) ? "Dual" : ((${addon_climate_heat}) ? "Heat" : ((${addon_climate_cool}) ? "Cool" : "Unknown"))); + ESP_LOGV(TAG, " Temp. units: %s", id(temp_unit_fahrenheit) ? "Fahrenheit" : "Celsius"); 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}; + ClimateTraits traits = thermostat_embedded->get_traits(); + float temp_step = traits.get_visual_target_temperature_step(); + float temp_offset = traits.get_visual_min_temperature(); + float temp_max = traits.get_visual_max_temperature(); + float temp_target = thermostat_embedded->target_temperature; + float temp_target_high = thermostat_embedded->target_temperature_high; + float temp_target_low = thermostat_embedded->target_temperature_low; + float temp_current = thermostat_embedded->current_temperature; + if (id(temp_unit_fahrenheit)) { + //temp_step = CelsiusToFahrenheit(temp_step); + temp_step = temp_step * 1.8; + temp_offset = CelsiusToFahrenheit(temp_offset); + temp_max = CelsiusToFahrenheit(temp_max); + temp_target = CelsiusToFahrenheit(temp_target); + temp_target_high = CelsiusToFahrenheit(temp_target_high); + temp_target_low = CelsiusToFahrenheit(temp_target_low); + temp_current = CelsiusToFahrenheit(temp_current); + } float total_steps = (temp_max-temp_offset)/temp_step; set_climate->execute ( - thermostat_embedded->current_temperature, // current_temp - 0, // supported_features - ((${addon_climate_dual}) ? -999 : thermostat_embedded->target_temperature), // target_temp - ((${addon_climate_dual}) ? thermostat_embedded->target_temperature_high : -999), // target_temp_high - ((${addon_climate_dual}) ? thermostat_embedded->target_temperature_low : -999), // target_temp_low - int(round(${temp_step}*10)), // temp_step - int(round(total_steps)), // total_steps - int(round(${temp_min}*10)), // temp_offset - "", // climate_icon - true // embedded_climate + temp_current, // current_temp + 0, // supported_features + ((${addon_climate_dual}) ? -999 : temp_target), // target_temp + ((${addon_climate_dual}) ? temp_target_high : -999), // target_temp_high + ((${addon_climate_dual}) ? temp_target_low : -999), // target_temp_low + int(round(temp_step*10)), // temp_step + int(round(total_steps)), // total_steps + int(round(temp_offset*10)), // temp_offset + "", // climate_icon + true // embedded_climate ); // Update target temp icon