ESPHome 2026.5.1
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::tuya {
5
6static const char *const TAG = "tuya.select";
7
9 this->parent_->register_listener(this->select_id_, [this](const TuyaDatapoint &datapoint) {
10 uint8_t enum_value = datapoint.value_enum;
11 ESP_LOGV(TAG, "MCU reported select %u value %u", this->select_id_, enum_value);
12 auto mappings = this->mappings_;
13 auto it = std::find(mappings.cbegin(), mappings.cend(), enum_value);
14 if (it == mappings.end()) {
15 ESP_LOGW(TAG, "Invalid value %u", enum_value);
16 return;
17 }
18 size_t mapping_idx = std::distance(mappings.cbegin(), it);
19 this->publish_state(mapping_idx);
20 });
21}
22
23void TuyaSelect::control(size_t index) {
24 if (this->optimistic_)
25 this->publish_state(index);
26
27 uint8_t mapping = this->mappings_.at(index);
28 ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, this->option_at(index));
29 if (this->is_int_) {
30 this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
31 } else {
32 this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
33 }
34}
35
37 LOG_SELECT("", "Tuya Select", this);
38 ESP_LOGCONFIG(TAG,
39 " Select has datapoint ID %u\n"
40 " Data type: %s\n"
41 " Options are:",
42 this->select_id_, this->is_int_ ? "int" : "enum");
43 const auto &options = this->traits.get_options();
44 for (size_t i = 0; i < this->mappings_.size(); i++) {
45 ESP_LOGCONFIG(TAG, " %i: %s", this->mappings_.at(i), options.at(i));
46 }
47}
48
49} // namespace esphome::tuya
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:81
SelectTraits traits
Definition select.h:31
void publish_state(const std::string &state)
Definition select.cpp:11
const FixedVector< const char * > & get_options() const
void set_enum_datapoint_value(uint8_t datapoint_id, uint8_t value)
Definition tuya.cpp:630
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
void dump_config() override
void control(size_t index) override
std::vector< uint8_t > mappings_
Definition tuya_select.h:30
uint8_t options
const char *const TAG
Definition spi.cpp:7