ESPHome  2024.2.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  this->parent_->register_listener(this->number_id_, [this](const TuyaDatapoint &datapoint) {
11  if (datapoint.type == TuyaDatapointType::INTEGER) {
12  ESP_LOGV(TAG, "MCU reported number %u is: %d", datapoint.id, datapoint.value_int);
13  this->publish_state(datapoint.value_int / multiply_by_);
14  } else if (datapoint.type == TuyaDatapointType::ENUM) {
15  ESP_LOGV(TAG, "MCU reported number %u is: %u", datapoint.id, datapoint.value_enum);
16  this->publish_state(datapoint.value_enum);
17  }
18  this->type_ = datapoint.type;
19  });
20 }
21 
22 void TuyaNumber::control(float value) {
23  ESP_LOGV(TAG, "Setting number %u: %f", this->number_id_, value);
24  if (this->type_ == TuyaDatapointType::INTEGER) {
25  int integer_value = lround(value * multiply_by_);
26  this->parent_->set_integer_datapoint_value(this->number_id_, integer_value);
27  } else if (this->type_ == TuyaDatapointType::ENUM) {
28  this->parent_->set_enum_datapoint_value(this->number_id_, value);
29  }
30  this->publish_state(value);
31 }
32 
34  LOG_NUMBER("", "Tuya Number", this);
35  ESP_LOGCONFIG(TAG, " Number has datapoint ID %u", this->number_id_);
36 }
37 
38 } // namespace tuya
39 } // namespace esphome
TuyaDatapointType type
Definition: tuya.h:30
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition: tuya.cpp:551
TuyaDatapointType type_
Definition: tuya_number.h:25
void setup() override
Definition: tuya_number.cpp:9
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:667
const char *const TAG
Definition: spi.cpp:8
void control(float value) override
Definition: tuya_number.cpp:22
void dump_config() override
Definition: tuya_number.cpp:33
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition: tuya.cpp:543
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7