ESPHome  2023.5.5
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  ESP_LOGV(TAG, "MCU reported speed of: %d", datapoint.value_enum);
13  if (datapoint.value_enum >= this->speed_count_) {
14  ESP_LOGE(TAG, "Speed has invalid value %d", datapoint.value_enum);
15  } else {
16  this->speed = datapoint.value_enum + 1;
17  this->publish_state();
18  }
19  });
20  }
21  if (this->switch_id_.has_value()) {
22  this->parent_->register_listener(*this->switch_id_, [this](const TuyaDatapoint &datapoint) {
23  ESP_LOGV(TAG, "MCU reported switch is: %s", ONOFF(datapoint.value_bool));
24  this->state = datapoint.value_bool;
25  this->publish_state();
26  });
27  }
28  if (this->oscillation_id_.has_value()) {
29  this->parent_->register_listener(*this->oscillation_id_, [this](const TuyaDatapoint &datapoint) {
30  ESP_LOGV(TAG, "MCU reported oscillation is: %s", ONOFF(datapoint.value_bool));
31  this->oscillating = datapoint.value_bool;
32  this->publish_state();
33  });
34  }
35  if (this->direction_id_.has_value()) {
36  this->parent_->register_listener(*this->direction_id_, [this](const TuyaDatapoint &datapoint) {
37  ESP_LOGD(TAG, "MCU reported reverse direction is: %s", ONOFF(datapoint.value_bool));
39  this->publish_state();
40  });
41  }
42 
43  this->parent_->add_on_initialized_callback([this]() {
44  auto restored = this->restore_state_();
45  if (restored)
46  restored->to_call(*this).perform();
47  });
48 }
49 
51  LOG_FAN("", "Tuya Fan", this);
52  if (this->speed_id_.has_value()) {
53  ESP_LOGCONFIG(TAG, " Speed has datapoint ID %u", *this->speed_id_);
54  }
55  if (this->switch_id_.has_value()) {
56  ESP_LOGCONFIG(TAG, " Switch has datapoint ID %u", *this->switch_id_);
57  }
58  if (this->oscillation_id_.has_value()) {
59  ESP_LOGCONFIG(TAG, " Oscillation has datapoint ID %u", *this->oscillation_id_);
60  }
61  if (this->direction_id_.has_value()) {
62  ESP_LOGCONFIG(TAG, " Direction has datapoint ID %u", *this->direction_id_);
63  }
64 }
65 
68  this->speed_count_);
69 }
70 
72  if (this->switch_id_.has_value() && call.get_state().has_value()) {
74  }
75  if (this->oscillation_id_.has_value() && call.get_oscillating().has_value()) {
77  }
78  if (this->direction_id_.has_value() && call.get_direction().has_value()) {
79  bool enable = *call.get_direction() == fan::FanDirection::REVERSE;
80  this->parent_->set_enum_datapoint_value(*this->direction_id_, enable);
81  }
82  if (this->speed_id_.has_value() && call.get_speed().has_value()) {
83  this->parent_->set_enum_datapoint_value(*this->speed_id_, *call.get_speed() - 1);
84  }
85 }
86 
87 } // namespace tuya
88 } // namespace esphome
bool state
The current on/off state of the fan.
Definition: fan.h:103
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:105
fan::FanTraits get_traits() override
Definition: tuya_fan.cpp:66
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition: tuya.cpp:551
FanDirection direction
The current direction of the fan.
Definition: fan.h:109
optional< FanDirection > get_direction() const
Definition: fan.h:74
void publish_state()
Definition: fan.cpp:89
optional< int > get_speed() const
Definition: fan.h:65
optional< FanRestoreState > restore_state_()
Definition: fan.cpp:110
bool has_value() const
Definition: optional.h:87
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:667
void add_on_initialized_callback(std::function< void()> callback)
Definition: tuya.h:104
optional< uint8_t > switch_id_
Definition: tuya_fan.h:27
int speed
The current fan speed level.
Definition: fan.h:107
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:71
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:539
void dump_config() override
Definition: tuya_fan.cpp:50
Definition: a4988.cpp:4
optional< uint8_t > direction_id_
Definition: tuya_fan.h:29
optional< uint8_t > speed_id_
Definition: tuya_fan.h:26