ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
tuya_select.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tuya_select.h"
3
4namespace esphome {
5namespace tuya {
6
7static const char *const TAG = "tuya.select";
8
10 this->parent_->register_listener(this->select_id_, [this](const TuyaDatapoint &datapoint) {
11 uint8_t enum_value = datapoint.value_enum;
12 ESP_LOGV(TAG, "MCU reported select %u value %u", this->select_id_, enum_value);
13 auto mappings = this->mappings_;
14 auto it = std::find(mappings.cbegin(), mappings.cend(), enum_value);
15 if (it == mappings.end()) {
16 ESP_LOGW(TAG, "Invalid value %u", enum_value);
17 return;
18 }
19 size_t mapping_idx = std::distance(mappings.cbegin(), it);
20 this->publish_state(mapping_idx);
21 });
22}
23
24void TuyaSelect::control(size_t index) {
25 if (this->optimistic_)
26 this->publish_state(index);
27
28 uint8_t mapping = this->mappings_.at(index);
29 ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, this->option_at(index));
30 if (this->is_int_) {
31 this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
32 } else {
33 this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
34 }
35}
36
38 LOG_SELECT("", "Tuya Select", this);
39 ESP_LOGCONFIG(TAG,
40 " Select has datapoint ID %u\n"
41 " Data type: %s\n"
42 " Options are:",
43 this->select_id_, this->is_int_ ? "int" : "enum");
44 const auto &options = this->traits.get_options();
45 for (size_t i = 0; i < this->mappings_.size(); i++) {
46 ESP_LOGCONFIG(TAG, " %i: %s", this->mappings_.at(i), options.at(i));
47 }
48}
49
50} // namespace tuya
51} // namespace esphome
const char * option_at(size_t index) const
Return the option value at the provided index offset (as const char* from flash).
Definition select.cpp:87
SelectTraits traits
Definition select.h:33
void publish_state(const std::string &state)
Definition select.cpp:12
const FixedVector< const char * > & get_options() const
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:606
void register_listener(uint8_t datapoint_id, const std::function< void(TuyaDatapoint)> &func)
Definition tuya.cpp:722
void set_integer_datapoint_value(uint8_t datapoint_id, uint32_t value)
Definition tuya.cpp:598
void dump_config() override
void control(size_t index) override
std::vector< uint8_t > mappings_
Definition tuya_select.h:31
uint8_t options
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7