ESPHome 2025.5.0
Loading...
Searching...
No Matches
tuya_fan.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tuya_fan.h"
3
4namespace esphome {
5namespace tuya {
6
7static const char *const TAG = "tuya.fan";
8
10 if (this->speed_id_.has_value()) {
11 this->parent_->register_listener(*this->speed_id_, [this](const TuyaDatapoint &datapoint) {
12 if (datapoint.type == TuyaDatapointType::ENUM) {
13 ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_enum);
14 if (datapoint.value_enum >= this->speed_count_) {
15 ESP_LOGE(TAG, "Speed has invalid value %d", datapoint.value_enum);
16 } else {
17 this->speed = datapoint.value_enum + 1;
18 this->publish_state();
19 }
20 } else if (datapoint.type == TuyaDatapointType::INTEGER) {
21 ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_int);
22 this->speed = datapoint.value_int;
23 this->publish_state();
24 }
25 this->speed_type_ = datapoint.type;
26 });
27 }
28 if (this->switch_id_.has_value()) {
29 this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
30 ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
31 this->state = datapoint.value_bool;
32 this->publish_state();
33 });
34 }
35 if (this->oscillation_id_.has_value()) {
36 this->parent_->register_listener(*this->oscillation_id_, [this](const TuyaDatapoint &datapoint) {
37 // Whether data type is BOOL or ENUM, it will still be a 1 or a 0, so the functions below are valid in both
38 // scenarios
39 ESP_LOGV(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
40 this->oscillating = datapoint.value_bool;
41 this->publish_state();
42
43 this->oscillation_type_ = datapoint.type;
44 });
45 }
46 if (this->direction_id_.has_value()) {
47 this->parent_->register_listener(*this->direction_id_, [this](const TuyaDatapoint &datapoint) {
48 ESP_LOGD(TAG, "MCU reported reverse direction is: %s", ONOFF(datapoint.value_bool));
50 this->publish_state();
51 });
52 }
53
54 this->parent_->add_on_initialized_callback([this]() {
55 auto restored = this->restore_state_();
56 if (restored)
57 restored->to_call(*this).perform();
58 });
59}
60
62 LOG_FAN("", "Tuya Fan", this);
63 if (this->speed_id_.has_value()) {
64 ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *this->speed_id_);
65 }
66 if (this->switch_id_.has_value()) {
67 ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
68 }
69 if (this->oscillation_id_.has_value()) {
70 ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *this->oscillation_id_);
71 }
72 if (this->direction_id_.has_value()) {
73 ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *this->direction_id_);
74 }
75}
76
78 return fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), this->direction_id_.has_value(),
79 this->speed_count_);
80}
81
82void TuyaFan::control(const fan::FanCall &call) {
83 if (this->switch_id_.has_value() && call.get_state().has_value()) {
85 }
86 if (this->oscillation_id_.has_value() && call.get_oscillating().has_value()) {
91 }
92 }
93 if (this->direction_id_.has_value() && call.get_direction().has_value()) {
94 bool enable = *call.get_direction() == fan::FanDirection::REVERSE;
95 this->parent_->set_enum_datapoint_value(*this->direction_id_, enable);
96 }
97 if (this->speed_id_.has_value() && call.get_speed().has_value()) {
99 this->parent_->set_enum_datapoint_value(*this->speed_id_, *call.get_speed() - 1);
100 } else if (this->speed_type_ == TuyaDatapointType::INTEGER) {
102 }
103 }
104}
105
106} // namespace tuya
107} // namespace esphome
optional< bool > get_state() const
Definition fan.h:49
optional< bool > get_oscillating() const
Definition fan.h:58
optional< int > get_speed() const
Definition fan.h:65
optional< FanDirection > get_direction() const
Definition fan.h:74
void publish_state()
Definition fan.cpp:117
FanDirection direction
The current direction of the fan.
Definition fan.h:116
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
bool state
The current on/off state of the fan.
Definition fan.h:110
int speed
The current fan speed level.
Definition fan.h:114
optional< FanRestoreState > restore_state_()
Definition fan.cpp:140
bool has_value() const
Definition optional.h:87
optional< uint8_t > speed_id_
Definition tuya_fan.h:26
void dump_config() override
Definition tuya_fan.cpp:61
TuyaDatapointType speed_type_
Definition tuya_fan.h:31
fan::FanTraits get_traits() override
Definition tuya_fan.cpp:77
void setup() override
Definition tuya_fan.cpp:9
optional< uint8_t > oscillation_id_
Definition tuya_fan.h:28
optional< uint8_t > switch_id_
Definition tuya_fan.h:27
optional< uint8_t > direction_id_
Definition tuya_fan.h:29
TuyaDatapointType oscillation_type_
Definition tuya_fan.h:32
void control(const fan::FanCall &call) override
Definition tuya_fan.cpp:82
void add_on_initialized_callback(std::function< void()> callback)
Definition tuya.h:115
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:569
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:581
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:697
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:573
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
TuyaDatapointType type
Definition tuya.h:30