Files
esphome-dev/esphome/components/tuya/fan/tuya_fan.cpp
T
Jim Ekman 7708b81ef5 Support fan speed levels (#1541)
* Add fan speed percentage support to the API

* Add float fan speed percentage

* Add percentage support to automation and configuration

* Update Tuya fan

* Fix pylint warning

* Update API to use speed levels instead of percentage

* Use speed levels

* Fix type warnings

* MQTT component now converts between speed levels and enums

* Webserver now supports speed_level

* Update prometheus

* Remove low/medium/high settings from speed fan

* Remove unused enum

* Configurable speed levels for speed fan

* Remove unused import

* Rename speed_level->speed and speed_levels->speed_count

* Rename supported_speed_levels -> supported_speed_count in API and FanTraits

Field id stays the same in the protocol, so the change is not breaking for aioesphome.
2021-03-17 10:40:02 -03:00

87 lines
3.1 KiB
C++

#include "esphome/core/log.h"
#include "esphome/components/fan/fan_helpers.h"
#include "tuya_fan.h"
namespace esphome {
namespace tuya {
static const char *TAG = "tuya.fan";
void TuyaFan::setup() {
auto traits = fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), false, 3);
this->fan_->set_traits(traits);
if (this->speed_id_.has_value()) {
this->parent_->register_listener(*this->speed_id_, [this](TuyaDatapoint datapoint) {
auto call = this->fan_->make_call();
if (datapoint.value_enum == 0x0)
call.set_speed(1);
else if (datapoint.value_enum == 0x1)
call.set_speed(2);
else if (datapoint.value_enum == 0x2)
call.set_speed(3);
else
ESP_LOGCONFIG(TAG, "Speed has invalid value %d", datapoint.value_enum);
ESP_LOGD(TAG, "MCU reported speed of: %d", datapoint.value_enum);
call.perform();
});
}
if (this->switch_id_.has_value()) {
this->parent_->register_listener(*this->switch_id_, [this](TuyaDatapoint datapoint) {
auto call = this->fan_->make_call();
call.set_state(datapoint.value_bool);
call.perform();
ESP_LOGD(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
});
}
if (this->oscillation_id_.has_value()) {
this->parent_->register_listener(*this->oscillation_id_, [this](TuyaDatapoint datapoint) {
auto call = this->fan_->make_call();
call.set_oscillating(datapoint.value_bool);
call.perform();
ESP_LOGD(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
});
}
this->fan_->add_on_state_callback([this]() { this->write_state(); });
}
void TuyaFan::dump_config() {
ESP_LOGCONFIG(TAG, "Tuya Fan:");
if (this->speed_id_.has_value())
ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *this->speed_id_);
if (this->switch_id_.has_value())
ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
if (this->oscillation_id_.has_value())
ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *this->oscillation_id_);
}
void TuyaFan::write_state() {
if (this->switch_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->switch_id_;
datapoint.type = TuyaDatapointType::BOOLEAN;
datapoint.value_bool = this->fan_->state;
this->parent_->set_datapoint_value(datapoint);
ESP_LOGD(TAG, "Setting switch: %s", ONOFF(this->fan_->state));
}
if (this->oscillation_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->oscillation_id_;
datapoint.type = TuyaDatapointType::BOOLEAN;
datapoint.value_bool = this->fan_->oscillating;
this->parent_->set_datapoint_value(datapoint);
ESP_LOGD(TAG, "Setting oscillating: %s", ONOFF(this->fan_->oscillating));
}
if (this->speed_id_.has_value()) {
TuyaDatapoint datapoint{};
datapoint.id = *this->speed_id_;
datapoint.type = TuyaDatapointType::ENUM;
datapoint.value_enum = this->fan_->speed - 1;
ESP_LOGD(TAG, "Setting speed: %d", datapoint.value_enum);
this->parent_->set_datapoint_value(datapoint);
}
}
} // namespace tuya
} // namespace esphome