1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-19 08:15:49 +00:00

[select] Convert remaining components to use index-based control() (#11693)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
J. Nick Koston
2025-11-05 20:55:26 -06:00
committed by GitHub
parent 822eacfd77
commit 74187845b7
24 changed files with 61 additions and 90 deletions

View File

@@ -8,7 +8,7 @@ namespace demo {
class DemoSelect : public select::Select, public Component { class DemoSelect : public select::Select, public Component {
protected: protected:
void control(const std::string &value) override { this->publish_state(value); } void control(size_t index) override { this->publish_state(index); }
}; };
} // namespace demo } // namespace demo

View File

@@ -3,9 +3,9 @@
namespace esphome { namespace esphome {
namespace es8388 { namespace es8388 {
void ADCInputMicSelect::control(const std::string &value) { void ADCInputMicSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
this->parent_->set_adc_input_mic(static_cast<AdcInputMicLine>(this->index_of(value).value())); this->parent_->set_adc_input_mic(static_cast<AdcInputMicLine>(index));
} }
} // namespace es8388 } // namespace es8388

View File

@@ -8,7 +8,7 @@ namespace es8388 {
class ADCInputMicSelect : public select::Select, public Parented<ES8388> { class ADCInputMicSelect : public select::Select, public Parented<ES8388> {
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace es8388 } // namespace es8388

View File

@@ -3,9 +3,9 @@
namespace esphome { namespace esphome {
namespace es8388 { namespace es8388 {
void DacOutputSelect::control(const std::string &value) { void DacOutputSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
this->parent_->set_dac_output(static_cast<DacOutputLine>(this->index_of(value).value())); this->parent_->set_dac_output(static_cast<DacOutputLine>(index));
} }
} // namespace es8388 } // namespace es8388

View File

@@ -8,7 +8,7 @@ namespace es8388 {
class DacOutputSelect : public select::Select, public Parented<ES8388> { class DacOutputSelect : public select::Select, public Parented<ES8388> {
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace es8388 } // namespace es8388

View File

@@ -41,16 +41,16 @@ class LVGLSelect : public select::Select, public Component {
} }
void publish() { void publish() {
this->publish_state(this->widget_->get_selected_text()); auto index = this->widget_->get_selected_index();
this->publish_state(index);
if (this->restore_) { if (this->restore_) {
auto index = this->widget_->get_selected_index();
this->pref_.save(&index); this->pref_.save(&index);
} }
} }
protected: protected:
void control(const std::string &value) override { void control(size_t index) override {
this->widget_->set_selected_text(value, this->anim_); this->widget_->set_selected_index(index, this->anim_);
this->publish(); this->publish();
} }
void set_options_() { void set_options_() {

View File

@@ -28,8 +28,9 @@ void ModbusSelect::parse_and_publish(const std::vector<uint8_t> &data) {
if (map_it != this->mapping_.cend()) { if (map_it != this->mapping_.cend()) {
size_t idx = std::distance(this->mapping_.cbegin(), map_it); size_t idx = std::distance(this->mapping_.cbegin(), map_it);
new_state = std::string(this->option_at(idx)); ESP_LOGV(TAG, "Found option %s for value %lld", this->option_at(idx), value);
ESP_LOGV(TAG, "Found option %s for value %lld", new_state->c_str(), value); this->publish_state(idx);
return;
} else { } else {
ESP_LOGE(TAG, "No option found for mapping %lld", value); ESP_LOGE(TAG, "No option found for mapping %lld", value);
} }
@@ -40,19 +41,16 @@ void ModbusSelect::parse_and_publish(const std::vector<uint8_t> &data) {
} }
} }
void ModbusSelect::control(const std::string &value) { void ModbusSelect::control(size_t index) {
auto idx = this->index_of(value); optional<int64_t> mapval = this->mapping_[index];
if (!idx.has_value()) { const char *option = this->option_at(index);
ESP_LOGW(TAG, "Invalid option '%s'", value.c_str()); ESP_LOGD(TAG, "Found value %lld for option '%s'", *mapval, option);
return;
}
optional<int64_t> mapval = this->mapping_[idx.value()];
ESP_LOGD(TAG, "Found value %lld for option '%s'", *mapval, value.c_str());
std::vector<uint16_t> data; std::vector<uint16_t> data;
if (this->write_transform_func_.has_value()) { if (this->write_transform_func_.has_value()) {
auto val = (*this->write_transform_func_)(this, value, *mapval, data); // Transform func requires string parameter for backward compatibility
auto val = (*this->write_transform_func_)(this, std::string(option), *mapval, data);
if (val.has_value()) { if (val.has_value()) {
mapval = *val; mapval = *val;
ESP_LOGV(TAG, "write_lambda returned mapping value %lld", *mapval); ESP_LOGV(TAG, "write_lambda returned mapping value %lld", *mapval);
@@ -85,7 +83,7 @@ void ModbusSelect::control(const std::string &value) {
this->parent_->queue_command(write_cmd); this->parent_->queue_command(write_cmd);
if (this->optimistic_) if (this->optimistic_)
this->publish_state(value); this->publish_state(index);
} }
} // namespace modbus_controller } // namespace modbus_controller

View File

@@ -38,7 +38,7 @@ class ModbusSelect : public Component, public select::Select, public SensorItem
void dump_config() override; void dump_config() override;
void parse_and_publish(const std::vector<uint8_t> &data) override; void parse_and_publish(const std::vector<uint8_t> &data) override;
void control(const std::string &value) override; void control(size_t index) override;
protected: protected:
std::vector<int64_t> mapping_{}; std::vector<int64_t> mapping_{};

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr24hpc1 { namespace seeed_mr24hpc1 {
void ExistenceBoundarySelect::control(const std::string &value) { void ExistenceBoundarySelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_existence_boundary(index);
if (index.has_value()) {
this->parent_->set_existence_boundary(index.value());
}
} }
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -11,7 +11,7 @@ class ExistenceBoundarySelect : public select::Select, public Parented<MR24HPC1C
ExistenceBoundarySelect() = default; ExistenceBoundarySelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr24hpc1 { namespace seeed_mr24hpc1 {
void MotionBoundarySelect::control(const std::string &value) { void MotionBoundarySelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_motion_boundary(index);
if (index.has_value()) {
this->parent_->set_motion_boundary(index.value());
}
} }
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -11,7 +11,7 @@ class MotionBoundarySelect : public select::Select, public Parented<MR24HPC1Comp
MotionBoundarySelect() = default; MotionBoundarySelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr24hpc1 { namespace seeed_mr24hpc1 {
void SceneModeSelect::control(const std::string &value) { void SceneModeSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_scene_mode(index);
if (index.has_value()) {
this->parent_->set_scene_mode(index.value());
}
} }
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -11,7 +11,7 @@ class SceneModeSelect : public select::Select, public Parented<MR24HPC1Component
SceneModeSelect() = default; SceneModeSelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr24hpc1 { namespace seeed_mr24hpc1 {
void UnmanTimeSelect::control(const std::string &value) { void UnmanTimeSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_unman_time(index);
if (index.has_value()) {
this->parent_->set_unman_time(index.value());
}
} }
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -11,7 +11,7 @@ class UnmanTimeSelect : public select::Select, public Parented<MR24HPC1Component
UnmanTimeSelect() = default; UnmanTimeSelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr24hpc1 } // namespace seeed_mr24hpc1

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr60fda2 { namespace seeed_mr60fda2 {
void HeightThresholdSelect::control(const std::string &value) { void HeightThresholdSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_height_threshold(index);
if (index.has_value()) {
this->parent_->set_height_threshold(index.value());
}
} }
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -11,7 +11,7 @@ class HeightThresholdSelect : public select::Select, public Parented<MR60FDA2Com
HeightThresholdSelect() = default; HeightThresholdSelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr60fda2 { namespace seeed_mr60fda2 {
void InstallHeightSelect::control(const std::string &value) { void InstallHeightSelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_install_height(index);
if (index.has_value()) {
this->parent_->set_install_height(index.value());
}
} }
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -11,7 +11,7 @@ class InstallHeightSelect : public select::Select, public Parented<MR60FDA2Compo
InstallHeightSelect() = default; InstallHeightSelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -3,12 +3,9 @@
namespace esphome { namespace esphome {
namespace seeed_mr60fda2 { namespace seeed_mr60fda2 {
void SensitivitySelect::control(const std::string &value) { void SensitivitySelect::control(size_t index) {
this->publish_state(value); this->publish_state(index);
auto index = this->index_of(value); this->parent_->set_sensitivity(index);
if (index.has_value()) {
this->parent_->set_sensitivity(index.value());
}
} }
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -11,7 +11,7 @@ class SensitivitySelect : public select::Select, public Parented<MR60FDA2Compone
SensitivitySelect() = default; SensitivitySelect() = default;
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
}; };
} // namespace seeed_mr60fda2 } // namespace seeed_mr60fda2

View File

@@ -21,23 +21,17 @@ void TuyaSelect::setup() {
}); });
} }
void TuyaSelect::control(const std::string &value) { void TuyaSelect::control(size_t index) {
if (this->optimistic_) if (this->optimistic_)
this->publish_state(value); this->publish_state(index);
auto idx = this->index_of(value); uint8_t mapping = this->mappings_.at(index);
if (idx.has_value()) { ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, this->option_at(index));
uint8_t mapping = this->mappings_.at(idx.value()); if (this->is_int_) {
ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, value.c_str()); this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
if (this->is_int_) { } else {
this->parent_->set_integer_datapoint_value(this->select_id_, mapping); this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
} else {
this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
}
return;
} }
ESP_LOGW(TAG, "Invalid value %s", value.c_str());
} }
void TuyaSelect::dump_config() { void TuyaSelect::dump_config() {

View File

@@ -23,7 +23,7 @@ class TuyaSelect : public select::Select, public Component {
void set_select_mappings(std::vector<uint8_t> mappings) { this->mappings_ = std::move(mappings); } void set_select_mappings(std::vector<uint8_t> mappings) { this->mappings_ = std::move(mappings); }
protected: protected:
void control(const std::string &value) override; void control(size_t index) override;
Tuya *parent_; Tuya *parent_;
bool optimistic_ = false; bool optimistic_ = false;