1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-18 07:45:56 +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 {
protected:
void control(const std::string &value) override { this->publish_state(value); }
void control(size_t index) override { this->publish_state(index); }
};
} // namespace demo

View File

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

View File

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

View File

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

View File

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

View File

@@ -41,16 +41,16 @@ class LVGLSelect : public select::Select, public Component {
}
void publish() {
this->publish_state(this->widget_->get_selected_text());
auto index = this->widget_->get_selected_index();
this->publish_state(index);
if (this->restore_) {
auto index = this->widget_->get_selected_index();
this->pref_.save(&index);
}
}
protected:
void control(const std::string &value) override {
this->widget_->set_selected_text(value, this->anim_);
void control(size_t index) override {
this->widget_->set_selected_index(index, this->anim_);
this->publish();
}
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()) {
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", new_state->c_str(), value);
ESP_LOGV(TAG, "Found option %s for value %lld", this->option_at(idx), value);
this->publish_state(idx);
return;
} else {
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) {
auto idx = this->index_of(value);
if (!idx.has_value()) {
ESP_LOGW(TAG, "Invalid option '%s'", value.c_str());
return;
}
optional<int64_t> mapval = this->mapping_[idx.value()];
ESP_LOGD(TAG, "Found value %lld for option '%s'", *mapval, value.c_str());
void ModbusSelect::control(size_t index) {
optional<int64_t> mapval = this->mapping_[index];
const char *option = this->option_at(index);
ESP_LOGD(TAG, "Found value %lld for option '%s'", *mapval, option);
std::vector<uint16_t> data;
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()) {
mapval = *val;
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);
if (this->optimistic_)
this->publish_state(value);
this->publish_state(index);
}
} // namespace modbus_controller

View File

@@ -38,7 +38,7 @@ class ModbusSelect : public Component, public select::Select, public SensorItem
void dump_config() 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:
std::vector<int64_t> mapping_{};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,7 +11,7 @@ class SensitivitySelect : public select::Select, public Parented<MR60FDA2Compone
SensitivitySelect() = default;
protected:
void control(const std::string &value) override;
void control(size_t index) override;
};
} // 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_)
this->publish_state(value);
this->publish_state(index);
auto idx = this->index_of(value);
if (idx.has_value()) {
uint8_t mapping = this->mappings_.at(idx.value());
ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, value.c_str());
if (this->is_int_) {
this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
} else {
this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
}
return;
uint8_t mapping = this->mappings_.at(index);
ESP_LOGV(TAG, "Setting %u datapoint value to %u:%s", this->select_id_, mapping, this->option_at(index));
if (this->is_int_) {
this->parent_->set_integer_datapoint_value(this->select_id_, mapping);
} else {
this->parent_->set_enum_datapoint_value(this->select_id_, mapping);
}
ESP_LOGW(TAG, "Invalid value %s", value.c_str());
}
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); }
protected:
void control(const std::string &value) override;
void control(size_t index) override;
Tuya *parent_;
bool optimistic_ = false;