ESPHome 2026.3.2
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 auto speed_id = this->speed_id_;
11 if (speed_id.has_value()) {
12 this->parent_->register_listener(*speed_id, [this](const TuyaDatapoint &datapoint) {
13 if (datapoint.type == TuyaDatapointType::ENUM) {
14 ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_enum);
15 if (datapoint.value_enum >= this->speed_count_) {
16 ESP_LOGE(TAG, "Speed has invalid value %d", datapoint.value_enum);
17 } else {
18 this->speed = datapoint.value_enum + 1;
19 this->publish_state();
20 }
21 } else if (datapoint.type == TuyaDatapointType::INTEGER) {
22 ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_int);
23 this->speed = datapoint.value_int;
24 this->publish_state();
25 }
26 this->speed_type_ = datapoint.type;
27 });
28 }
29 auto switch_id = this->switch_id_;
30 if (switch_id.has_value()) {
31 this->parent_->register_listener(*switch_id, [this](const TuyaDatapoint &datapoint) {
32 ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
33 this->state = datapoint.value_bool;
34 this->publish_state();
35 });
36 }
37 auto oscillation_id = this->oscillation_id_;
38 if (oscillation_id.has_value()) {
39 this->parent_->register_listener(*oscillation_id, [this](const TuyaDatapoint &datapoint) {
40 // Whether data type is BOOL or ENUM, it will still be a 1 or a 0, so the functions below are valid in both
41 // scenarios
42 ESP_LOGV(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
43 this->oscillating = datapoint.value_bool;
44 this->publish_state();
45
46 this->oscillation_type_ = datapoint.type;
47 });
48 }
49 auto direction_id = this->direction_id_;
50 if (direction_id.has_value()) {
51 this->parent_->register_listener(*direction_id, [this](const TuyaDatapoint &datapoint) {
52 ESP_LOGD(TAG, "MCU reported reverse direction is: %s", ONOFF(datapoint.value_bool));
54 this->publish_state();
55 });
56 }
57
58 this->parent_->add_on_initialized_callback([this]() {
59 auto restored = this->restore_state_();
60 if (restored)
61 restored->to_call(*this).perform();
62 });
63}
64
66 LOG_FAN("", "Tuya Fan", this);
67 auto speed_dp_id = this->speed_id_;
68 if (speed_dp_id.has_value()) {
69 ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *speed_dp_id);
70 }
71 auto switch_dp_id = this->switch_id_;
72 if (switch_dp_id.has_value()) {
73 ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *switch_dp_id);
74 }
75 auto oscillation_dp_id = this->oscillation_id_;
76 if (oscillation_dp_id.has_value()) {
77 ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *oscillation_dp_id);
78 }
79 auto direction_dp_id = this->direction_id_;
80 if (direction_dp_id.has_value()) {
81 ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *direction_dp_id);
82 }
83}
84
86 return fan::FanTraits(this->oscillation_id_.has_value(), this->speed_id_.has_value(), this->direction_id_.has_value(),
87 this->speed_count_);
88}
89
90void TuyaFan::control(const fan::FanCall &call) {
91 auto switch_id = this->switch_id_;
92 if (switch_id.has_value()) {
93 auto state = call.get_state();
94 if (state.has_value()) {
95 this->parent_->set_boolean_datapoint_value(*switch_id, *state);
96 }
97 }
98 auto osc_id = this->oscillation_id_;
99 if (osc_id.has_value()) {
100 auto oscillating = call.get_oscillating();
101 if (oscillating.has_value()) {
104 } else if (this->oscillation_type_ == TuyaDatapointType::BOOLEAN) {
106 }
107 }
108 }
109 auto dir_id = this->direction_id_;
110 if (dir_id.has_value()) {
111 auto direction = call.get_direction();
112 if (direction.has_value()) {
113 bool enable = *direction == fan::FanDirection::REVERSE;
114 this->parent_->set_enum_datapoint_value(*dir_id, enable);
115 }
116 }
117 auto spd_id = this->speed_id_;
118 if (spd_id.has_value()) {
119 auto speed = call.get_speed();
120 if (speed.has_value()) {
122 this->parent_->set_enum_datapoint_value(*spd_id, *speed - 1);
123 } else if (this->speed_type_ == TuyaDatapointType::INTEGER) {
124 this->parent_->set_integer_datapoint_value(*spd_id, *speed);
125 }
126 }
127 }
128}
129
130} // namespace tuya
131} // namespace esphome
void publish_state()
Definition fan.cpp:197
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:225
optional< uint8_t > speed_id_
Definition tuya_fan.h:26
void dump_config() override
Definition tuya_fan.cpp:65
TuyaDatapointType speed_type_
Definition tuya_fan.h:31
fan::FanTraits get_traits() override
Definition tuya_fan.cpp:85
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:90
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:618
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:630
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:747
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:622
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
TuyaDatapointType type
Definition tuya.h:30