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