ESPHome 2026.11.0-dev
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::tuya {
5
6ESPHOME_LOG_TAG(TAG, "tuya.fan");
7
9 auto speed_id = this->speed_id_;
10 if (speed_id.has_value()) {
11 this->parent_->register_listener(*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 auto switch_id = this->switch_id_;
29 if (switch_id.has_value()) {
30 this->parent_->register_listener(*switch_id, [this](const TuyaDatapoint &datapoint) {
31 ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
32 this->state = datapoint.value_bool;
33 this->publish_state();
34 });
35 }
36 auto oscillation_id = this->oscillation_id_;
37 if (oscillation_id.has_value()) {
38 this->parent_->register_listener(*oscillation_id, [this](const TuyaDatapoint &datapoint) {
39 // Whether data type is BOOL or ENUM, it will still be a 1 or a 0, so the functions below are valid in both
40 // scenarios
41 ESP_LOGV(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
42 this->oscillating = datapoint.value_bool;
43 this->publish_state();
44
45 this->oscillation_type_ = datapoint.type;
46 });
47 }
48 auto direction_id = this->direction_id_;
49 if (direction_id.has_value()) {
50 this->parent_->register_listener(*direction_id, [this](const TuyaDatapoint &datapoint) {
51 ESP_LOGD(TAG, "MCU reported reverse direction is: %s", ONOFF(datapoint.value_bool));
53 this->publish_state();
54 });
55 }
56
57 this->parent_->add_on_initialized_callback([this]() {
58 auto restored = this->restore_state_();
59 if (restored)
60 restored->to_call(*this).perform();
61 });
62}
63
65 LOG_FAN("", "Tuya Fan", this);
66 auto speed_dp_id = this->speed_id_;
67 if (speed_dp_id.has_value()) {
68 ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *speed_dp_id);
69 }
70 auto switch_dp_id = this->switch_id_;
71 if (switch_dp_id.has_value()) {
72 ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *switch_dp_id);
73 }
74 auto oscillation_dp_id = this->oscillation_id_;
75 if (oscillation_dp_id.has_value()) {
76 ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *oscillation_dp_id);
77 }
78 auto direction_dp_id = this->direction_id_;
79 if (direction_dp_id.has_value()) {
80 ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *direction_dp_id);
81 }
82}
83
85 return fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), this->direction_id_.has_value(),
86 this->speed_count_);
87}
88
89// The parent skips a value equal to the last MCU report, which an MCU that does not report back leaves stale, so
90// optimistic mode always sends
91void TuyaFan::set_boolean_(uint8_t datapoint_id, bool value) {
92 if (this->optimistic_) {
93 this->parent_->force_set_boolean_datapoint_value(datapoint_id, value);
94 } else {
95 this->parent_->set_boolean_datapoint_value(datapoint_id, value);
96 }
97}
98
99void TuyaFan::set_enum_(uint8_t datapoint_id, uint8_t value) {
100 if (this->optimistic_) {
101 this->parent_->force_set_enum_datapoint_value(datapoint_id, value);
102 } else {
103 this->parent_->set_enum_datapoint_value(datapoint_id, value);
104 }
105}
106
107void TuyaFan::set_integer_(uint8_t datapoint_id, uint32_t value) {
108 if (this->optimistic_) {
109 this->parent_->force_set_integer_datapoint_value(datapoint_id, value);
110 } else {
111 this->parent_->set_integer_datapoint_value(datapoint_id, value);
112 }
113}
114
116 auto switch_id = this->switch_id_;
117 if (switch_id.has_value()) {
118 auto state = call.get_state();
119 if (state.has_value()) {
120 this->set_boolean_(*switch_id, *state);
121 }
122 }
123 auto osc_id = this->oscillation_id_;
124 if (osc_id.has_value()) {
125 auto oscillating = call.get_oscillating();
126 if (oscillating.has_value()) {
128 this->set_enum_(*osc_id, *oscillating);
129 } else if (this->oscillation_type_ == TuyaDatapointType::BOOLEAN || this->optimistic_) {
130 // Until the MCU reports the type, optimistic mode assumes a boolean datapoint
131 this->set_boolean_(*osc_id, *oscillating);
132 }
133 }
134 }
135 auto dir_id = this->direction_id_;
136 if (dir_id.has_value()) {
137 auto direction = call.get_direction();
138 if (direction.has_value()) {
139 bool enable = *direction == fan::FanDirection::REVERSE;
140 this->set_enum_(*dir_id, enable);
141 }
142 }
143 auto spd_id = this->speed_id_;
144 if (spd_id.has_value()) {
145 auto speed = call.get_speed();
146 if (speed.has_value()) {
148 this->set_enum_(*spd_id, *speed - 1);
149 } else if (this->speed_type_ == TuyaDatapointType::INTEGER || this->optimistic_) {
150 // Until the MCU reports the type, optimistic mode assumes an integer datapoint
151 this->set_integer_(*spd_id, *speed);
152 }
153 }
154 }
155
156 if (!this->optimistic_)
157 return;
158 // FanCall::validate_() already dropped the fields this fan's traits do not support; the on/off state is kept
159 // even without a switch datapoint, so a speed-only fan still reports turning on
160 auto state = call.get_state();
161 if (state.has_value())
162 this->state = *state;
163 auto oscillating = call.get_oscillating();
164 if (oscillating.has_value())
165 this->oscillating = *oscillating;
166 auto direction = call.get_direction();
167 if (direction.has_value())
168 this->direction = *direction;
169 auto speed = call.get_speed();
170 if (speed.has_value())
171 this->speed = *speed;
172 this->publish_state();
173}
174
175} // namespace esphome::tuya
optional< bool > get_state() const
Definition fan.h:50
optional< bool > get_oscillating() const
Definition fan.h:59
optional< int > get_speed() const
Definition fan.h:64
optional< FanDirection > get_direction() const
Definition fan.h:73
void publish_state()
Definition fan.cpp:205
FanDirection direction
The current direction of the fan.
Definition fan.h:117
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
optional< FanRestoreState > restore_state_()
Definition fan.cpp:233
optional< uint8_t > speed_id_
Definition tuya_fan.h:30
void dump_config() override
Definition tuya_fan.cpp:64
TuyaDatapointType speed_type_
Definition tuya_fan.h:35
fan::FanTraits get_traits() override
Definition tuya_fan.cpp:84
void setup() override
Definition tuya_fan.cpp:8
optional< uint8_t > oscillation_id_
Definition tuya_fan.h:32
optional< uint8_t > switch_id_
Definition tuya_fan.h:31
optional< uint8_t > direction_id_
Definition tuya_fan.h:33
TuyaDatapointType oscillation_type_
Definition tuya_fan.h:36
void control(const fan::FanCall &call) override
Definition tuya_fan.cpp:115
void set_enum_(uint8_t datapoint_id, uint8_t value)
Definition tuya_fan.cpp:99
void set_integer_(uint8_t datapoint_id, uint32_t value)
Definition tuya_fan.cpp:107
void set_boolean_(uint8_t datapoint_id, bool value)
Definition tuya_fan.cpp:91
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:698
void force_set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:726
void register_listener(uint8_t datapoint_id, F &&func)
Definition tuya.h:98
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:710
void add_on_initialized_callback(F &&callback)
Definition tuya.h:122
void force_set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:722
void force_set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:734
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:702
ESPHOME_LOG_TAG(TAG, "cover")
static void uint32_t
TuyaDatapointType type
Definition tuya.h:30