ESPHome  2024.11.1
tuya_number.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tuya_number.h"
3 
4 namespace esphome {
5 namespace tuya {
6 
7 static const char *const TAG = "tuya.number";
8 
10  if (this->restore_value_) {
12  }
13 
14  this->parent_->register_listener(this->number_id_, [this](const TuyaDatapoint &datapoint) {
15  if (datapoint.type == TuyaDatapointType::INTEGER) {
16  ESP_LOGV(TAG, "MCU reported number %u is: %d", datapoint.id, datapoint.value_int);
17  float value = datapoint.value_int / multiply_by_;
18  this->publish_state(value);
19  if (this->restore_value_)
20  this->pref_.save(&value);
21  } else if (datapoint.type == TuyaDatapointType::ENUM) {
22  ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum);
23  float value = datapoint.value_enum;
24  this->publish_state(value);
25  if (this->restore_value_)
26  this->pref_.save(&value);
27  } else {
28  ESP_LOGW(TAG, "Reported type (%d) is not a number!", static_cast<int>(datapoint.type));
29  return;
30  }
31 
32  if ((this->type_) && (this->type_ != datapoint.type)) {
33  ESP_LOGW(TAG, "Reported type (%d) different than previously set (%d)!", static_cast<int>(datapoint.type),
34  static_cast<int>(*this->type_));
35  }
36  this->type_ = datapoint.type;
37  });
38 
39  this->parent_->add_on_initialized_callback([this] {
40  if (this->type_) {
41  float value;
42  if (!this->restore_value_) {
43  if (this->initial_value_) {
44  value = *this->initial_value_;
45  } else {
46  return;
47  }
48  } else {
49  if (!this->pref_.load(&value)) {
50  if (this->initial_value_) {
51  value = *this->initial_value_;
52  } else {
53  value = this->traits.get_min_value();
54  ESP_LOGW(TAG, "Failed to restore and there is no initial value defined. Setting min_value (%f)", value);
55  }
56  }
57  }
58 
59  this->control(value);
60  }
61  });
62 }
63 
64 void TuyaNumber::control(float value) {
65  ESP_LOGV(TAG, "Setting number %u: %f", this->number_id_, value);
66  if (this->type_ == TuyaDatapointType::INTEGER) {
67  int integer_value = lround(value * multiply_by_);
68  this->parent_->set_integer_datapoint_value(this->number_id_, integer_value);
69  } else if (this->type_ == TuyaDatapointType::ENUM) {
70  this->parent_->set_enum_datapoint_value(this->number_id_, value);
71  }
72  this->publish_state(value);
73 
74  if (this->restore_value_)
75  this->pref_.save(&value);
76 }
77 
79  LOG_NUMBER("", "Tuya Number", this);
80  ESP_LOGCONFIG(TAG, " Number has datapoint ID %u", this->number_id_);
81  if (this->type_) {
82  ESP_LOGCONFIG(TAG, " Datapoint type is %d", static_cast<int>(*this->type_));
83  } else {
84  ESP_LOGCONFIG(TAG, " Datapoint type is unknown");
85  }
86 
87  if (this->initial_value_) {
88  ESP_LOGCONFIG(TAG, " Initial Value: %f", *this->initial_value_);
89  }
90 
91  ESP_LOGCONFIG(TAG, " Restore Value: %s", YESNO(this->restore_value_));
92 }
93 
94 } // namespace tuya
95 } // namespace esphome
TuyaDatapointType type
Definition: tuya.h:30
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition: tuya.cpp:581
void setup() override
Definition: tuya_number.cpp:9
ESPPreferenceObject pref_
Definition: tuya_number.h:34
void publish_state(float state)
Definition: number.cpp:9
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition: tuya.cpp:697
bool save(const T *src)
Definition: preferences.h:21
void add_on_initialized_callback(std::function< void()> callback)
Definition: tuya.h:115
const char *const TAG
Definition: spi.cpp:8
void control(float value) override
Definition: tuya_number.cpp:64
ESPPreferences * global_preferences
void dump_config() override
Definition: tuya_number.cpp:78
NumberTraits traits
Definition: number.h:49
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition: tuya.cpp:573
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
optional< float > initial_value_
Definition: tuya_number.h:31
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
optional< TuyaDatapointType > type_
Definition: tuya_number.h:30
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76