ESPHome  2024.11.0
tuya_fan.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tuya_fan.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.fan";
8 
9 void TuyaFan::setup() {
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 
79  this->speed_count_);
80 }
81 
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()) {
89  } else if (this->oscillation_type_ == TuyaDatapointType::BOOLEAN) {
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()) {
98  if (this->speed_type_ == TuyaDatapointType::ENUM) {
99  this->parent_->set_enum_datapoint_value(*this->speed_id_, *call.get_speed() - 1);
100  } else if (this->speed_type_ == TuyaDatapointType::INTEGER) {
101  this->parent_->set_integer_datapoint_value(*this->speed_id_, *call.get_speed());
102  }
103  }
104 }
105 
106 } // namespace tuya
107 } // namespace esphome
bool state
The current on/off state of the fan.
Definition: fan.h:110
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
TuyaDatapointType type
Definition: tuya.h:30
fan::FanTraits get_traits() override
Definition: tuya_fan.cpp:77
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition: tuya.cpp:581
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
optional< FanDirection > get_direction() const
Definition: fan.h:74
void publish_state()
Definition: fan.cpp:117
optional< int > get_speed() const
Definition: fan.h:65
optional< FanRestoreState > restore_state_()
Definition: fan.cpp:140
bool has_value() const
Definition: optional.h:87
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:697
void add_on_initialized_callback(std::function< void()> callback)
Definition: tuya.h:115
const char *const TAG
Definition: spi.cpp:8
optional< uint8_t > switch_id_
Definition: tuya_fan.h:27
optional< bool > get_state() const
Definition: fan.h:49
optional< bool > get_oscillating() const
Definition: fan.h:58
void control(const fan::FanCall &call) override
Definition: tuya_fan.cpp:82
optional< uint8_t > oscillation_id_
Definition: tuya_fan.h:28
void setup() override
Definition: tuya_fan.cpp:9
void set_boolean_datapoint_value(uint8_t datapoint_id, bool value)
Definition: tuya.cpp:569
void dump_config() override
Definition: tuya_fan.cpp:61
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition: tuya.cpp:573
TuyaDatapointType speed_type_
Definition: tuya_fan.h:31
TuyaDatapointType oscillation_type_
Definition: tuya_fan.h:32
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< uint8_t > direction_id_
Definition: tuya_fan.h:29
optional< uint8_t > speed_id_
Definition: tuya_fan.h:26