From 2053b02c61cea203d2eb5bce8218647a978a27f1 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 7 Dec 2022 07:29:18 +1300 Subject: [PATCH] Add reports fahrenheit to tuya climate (#4032) --- esphome/components/tuya/climate/__init__.py | 5 +++++ esphome/components/tuya/climate/tuya_climate.cpp | 15 +++++++++++++-- esphome/components/tuya/climate/tuya_climate.h | 3 +++ tests/test4.yaml | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/esphome/components/tuya/climate/__init__.py b/esphome/components/tuya/climate/__init__.py index 7d4b37ad22..199c2eabeb 100644 --- a/esphome/components/tuya/climate/__init__.py +++ b/esphome/components/tuya/climate/__init__.py @@ -25,6 +25,7 @@ CONF_CURRENT_TEMPERATURE_MULTIPLIER = "current_temperature_multiplier" CONF_TARGET_TEMPERATURE_MULTIPLIER = "target_temperature_multiplier" CONF_ECO_DATAPOINT = "eco_datapoint" CONF_ECO_TEMPERATURE = "eco_temperature" +CONF_REPORTS_FAHRENHEIT = "reports_fahrenheit" TuyaClimate = tuya_ns.class_("TuyaClimate", climate.Climate, cg.Component) @@ -110,6 +111,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_TARGET_TEMPERATURE_MULTIPLIER): cv.positive_float, cv.Optional(CONF_ECO_DATAPOINT): cv.uint8_t, cv.Optional(CONF_ECO_TEMPERATURE): cv.temperature, + cv.Optional(CONF_REPORTS_FAHRENHEIT, default=False): cv.boolean, } ).extend(cv.COMPONENT_SCHEMA), cv.has_at_least_one_key(CONF_TARGET_TEMPERATURE_DATAPOINT, CONF_SWITCH_DATAPOINT), @@ -186,3 +188,6 @@ async def to_code(config): cg.add(var.set_eco_id(config[CONF_ECO_DATAPOINT])) if CONF_ECO_TEMPERATURE in config: cg.add(var.set_eco_temperature(config[CONF_ECO_TEMPERATURE])) + + if config[CONF_REPORTS_FAHRENHEIT]: + cg.add(var.set_reports_fahrenheit()) diff --git a/esphome/components/tuya/climate/tuya_climate.cpp b/esphome/components/tuya/climate/tuya_climate.cpp index 280e172c7a..687764e30f 100644 --- a/esphome/components/tuya/climate/tuya_climate.cpp +++ b/esphome/components/tuya/climate/tuya_climate.cpp @@ -1,5 +1,5 @@ -#include "esphome/core/log.h" #include "tuya_climate.h" +#include "esphome/core/log.h" namespace esphome { namespace tuya { @@ -44,6 +44,10 @@ void TuyaClimate::setup() { if (this->target_temperature_id_.has_value()) { this->parent_->register_listener(*this->target_temperature_id_, [this](const TuyaDatapoint &datapoint) { this->manual_temperature_ = datapoint.value_int * this->target_temperature_multiplier_; + if (this->reports_fahrenheit_) { + this->manual_temperature_ = (this->manual_temperature_ - 32) * 5 / 9; + } + ESP_LOGV(TAG, "MCU reported manual target temperature is: %.1f", this->manual_temperature_); this->compute_target_temperature_(); this->compute_state_(); @@ -53,6 +57,10 @@ void TuyaClimate::setup() { if (this->current_temperature_id_.has_value()) { this->parent_->register_listener(*this->current_temperature_id_, [this](const TuyaDatapoint &datapoint) { this->current_temperature = datapoint.value_int * this->current_temperature_multiplier_; + if (this->reports_fahrenheit_) { + this->current_temperature = (this->current_temperature - 32) * 5 / 9; + } + ESP_LOGV(TAG, "MCU reported current temperature is: %.1f", this->current_temperature); this->compute_state_(); this->publish_state(); @@ -105,7 +113,10 @@ void TuyaClimate::control(const climate::ClimateCall &call) { } if (call.get_target_temperature().has_value()) { - const float target_temperature = *call.get_target_temperature(); + float target_temperature = *call.get_target_temperature(); + if (this->reports_fahrenheit_) + target_temperature = (target_temperature * 9 / 5) + 32; + ESP_LOGV(TAG, "Setting target temperature: %.1f", target_temperature); this->parent_->set_integer_datapoint_value(*this->target_temperature_id_, (int) (target_temperature / this->target_temperature_multiplier_)); diff --git a/esphome/components/tuya/climate/tuya_climate.h b/esphome/components/tuya/climate/tuya_climate.h index ec19d05308..7c18625c4e 100644 --- a/esphome/components/tuya/climate/tuya_climate.h +++ b/esphome/components/tuya/climate/tuya_climate.h @@ -35,6 +35,8 @@ class TuyaClimate : public climate::Climate, public Component { void set_eco_id(uint8_t eco_id) { this->eco_id_ = eco_id; } void set_eco_temperature(float eco_temperature) { this->eco_temperature_ = eco_temperature; } + void set_reports_fahrenheit() { this->reports_fahrenheit_ = true; } + void set_tuya_parent(Tuya *parent) { this->parent_ = parent; } protected: @@ -77,6 +79,7 @@ class TuyaClimate : public climate::Climate, public Component { bool cooling_state_{false}; float manual_temperature_; bool eco_; + bool reports_fahrenheit_{false}; }; } // namespace tuya diff --git a/tests/test4.yaml b/tests/test4.yaml index a8077c625f..db7a123dbc 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -381,6 +381,7 @@ climate: target_temperature_datapoint: 3 current_temperature_multiplier: 0.5 target_temperature_multiplier: 0.5 + reports_fahrenheit: true switch: - platform: tuya