Add reports fahrenheit to tuya climate (#4032)

This commit is contained in:
Jesse Hills 2022-12-07 07:29:18 +13:00 committed by GitHub
parent f34e797a0d
commit 2053b02c61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 2 deletions

View file

@ -25,6 +25,7 @@ CONF_CURRENT_TEMPERATURE_MULTIPLIER = "current_temperature_multiplier"
CONF_TARGET_TEMPERATURE_MULTIPLIER = "target_temperature_multiplier" CONF_TARGET_TEMPERATURE_MULTIPLIER = "target_temperature_multiplier"
CONF_ECO_DATAPOINT = "eco_datapoint" CONF_ECO_DATAPOINT = "eco_datapoint"
CONF_ECO_TEMPERATURE = "eco_temperature" CONF_ECO_TEMPERATURE = "eco_temperature"
CONF_REPORTS_FAHRENHEIT = "reports_fahrenheit"
TuyaClimate = tuya_ns.class_("TuyaClimate", climate.Climate, cg.Component) 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_TARGET_TEMPERATURE_MULTIPLIER): cv.positive_float,
cv.Optional(CONF_ECO_DATAPOINT): cv.uint8_t, cv.Optional(CONF_ECO_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_ECO_TEMPERATURE): cv.temperature, cv.Optional(CONF_ECO_TEMPERATURE): cv.temperature,
cv.Optional(CONF_REPORTS_FAHRENHEIT, default=False): cv.boolean,
} }
).extend(cv.COMPONENT_SCHEMA), ).extend(cv.COMPONENT_SCHEMA),
cv.has_at_least_one_key(CONF_TARGET_TEMPERATURE_DATAPOINT, CONF_SWITCH_DATAPOINT), 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])) cg.add(var.set_eco_id(config[CONF_ECO_DATAPOINT]))
if CONF_ECO_TEMPERATURE in config: if CONF_ECO_TEMPERATURE in config:
cg.add(var.set_eco_temperature(config[CONF_ECO_TEMPERATURE])) cg.add(var.set_eco_temperature(config[CONF_ECO_TEMPERATURE]))
if config[CONF_REPORTS_FAHRENHEIT]:
cg.add(var.set_reports_fahrenheit())

View file

@ -1,5 +1,5 @@
#include "esphome/core/log.h"
#include "tuya_climate.h" #include "tuya_climate.h"
#include "esphome/core/log.h"
namespace esphome { namespace esphome {
namespace tuya { namespace tuya {
@ -44,6 +44,10 @@ void TuyaClimate::setup() {
if (this->target_temperature_id_.has_value()) { if (this->target_temperature_id_.has_value()) {
this->parent_->register_listener(*this->target_temperature_id_, [this](const TuyaDatapoint &datapoint) { this->parent_->register_listener(*this->target_temperature_id_, [this](const TuyaDatapoint &datapoint) {
this->manual_temperature_ = datapoint.value_int * this->target_temperature_multiplier_; 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_); ESP_LOGV(TAG, "MCU reported manual target temperature is: %.1f", this->manual_temperature_);
this->compute_target_temperature_(); this->compute_target_temperature_();
this->compute_state_(); this->compute_state_();
@ -53,6 +57,10 @@ void TuyaClimate::setup() {
if (this->current_temperature_id_.has_value()) { if (this->current_temperature_id_.has_value()) {
this->parent_->register_listener(*this->current_temperature_id_, [this](const TuyaDatapoint &datapoint) { this->parent_->register_listener(*this->current_temperature_id_, [this](const TuyaDatapoint &datapoint) {
this->current_temperature = datapoint.value_int * this->current_temperature_multiplier_; 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); ESP_LOGV(TAG, "MCU reported current temperature is: %.1f", this->current_temperature);
this->compute_state_(); this->compute_state_();
this->publish_state(); this->publish_state();
@ -105,7 +113,10 @@ void TuyaClimate::control(const climate::ClimateCall &call) {
} }
if (call.get_target_temperature().has_value()) { 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); ESP_LOGV(TAG, "Setting target temperature: %.1f", target_temperature);
this->parent_->set_integer_datapoint_value(*this->target_temperature_id_, this->parent_->set_integer_datapoint_value(*this->target_temperature_id_,
(int) (target_temperature / this->target_temperature_multiplier_)); (int) (target_temperature / this->target_temperature_multiplier_));

View file

@ -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_id(uint8_t eco_id) { this->eco_id_ = eco_id; }
void set_eco_temperature(float eco_temperature) { this->eco_temperature_ = eco_temperature; } 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; } void set_tuya_parent(Tuya *parent) { this->parent_ = parent; }
protected: protected:
@ -77,6 +79,7 @@ class TuyaClimate : public climate::Climate, public Component {
bool cooling_state_{false}; bool cooling_state_{false};
float manual_temperature_; float manual_temperature_;
bool eco_; bool eco_;
bool reports_fahrenheit_{false};
}; };
} // namespace tuya } // namespace tuya

View file

@ -381,6 +381,7 @@ climate:
target_temperature_datapoint: 3 target_temperature_datapoint: 3
current_temperature_multiplier: 0.5 current_temperature_multiplier: 0.5
target_temperature_multiplier: 0.5 target_temperature_multiplier: 0.5
reports_fahrenheit: true
switch: switch:
- platform: tuya - platform: tuya