ESPHome 2026.6.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
6static const char *const 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
89void TuyaFan::control(const fan::FanCall &call) {
90 auto switch_id = this->switch_id_;
91 if (switch_id.has_value()) {
92 auto state = call.get_state();
93 if (state.has_value()) {
94 this->parent_->set_boolean_datapoint_value(*switch_id, *state);
95 }
96 }
97 auto osc_id = this->oscillation_id_;
98 if (osc_id.has_value()) {
99 auto oscillating = call.get_oscillating();
100 if (oscillating.has_value()) {
103 } else if (this->oscillation_type_ == TuyaDatapointType::BOOLEAN) {
105 }
106 }
107 }
108 auto dir_id = this->direction_id_;
109 if (dir_id.has_value()) {
110 auto direction = call.get_direction();
111 if (direction.has_value()) {
112 bool enable = *direction == fan::FanDirection::REVERSE;
113 this->parent_->set_enum_datapoint_value(*dir_id, enable);
114 }
115 }
116 auto spd_id = this->speed_id_;
117 if (spd_id.has_value()) {
118 auto speed = call.get_speed();
119 if (speed.has_value()) {
121 this->parent_->set_enum_datapoint_value(*spd_id, *speed - 1);
122 } else if (this->speed_type_ == TuyaDatapointType::INTEGER) {
123 this->parent_->set_integer_datapoint_value(*spd_id, *speed);
124 }
125 }
126 }
127}
128
129} // namespace esphome::tuya
void publish_state()
Definition fan.cpp:223
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:251
optional< uint8_t > speed_id_
Definition tuya_fan.h:25
void dump_config() override
Definition tuya_fan.cpp:64
TuyaDatapointType speed_type_
Definition tuya_fan.h:30
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:27
optional< uint8_t > switch_id_
Definition tuya_fan.h:26
optional< uint8_t > direction_id_
Definition tuya_fan.h:28
TuyaDatapointType oscillation_type_
Definition tuya_fan.h:31
void control(const fan::FanCall &call) override
Definition tuya_fan.cpp:89
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition tuya.cpp:618
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:630
void add_on_initialized_callback(F &&callback)
Definition tuya.h:114
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:749
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:622
const char *const TAG
Definition spi.cpp:7
TuyaDatapointType type
Definition tuya.h:29