1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-04 09:01:49 +00:00

Compare commits

...

41 Commits

Author SHA1 Message Date
J. Nick Koston
772ebe9db2 [select] Convert remaining components to use index-based control() 2025-11-03 08:34:35 -06:00
J. Nick Koston
014722bcbb Merge branch 'dev' into select_options 2025-11-03 08:23:34 -06:00
J. Nick Koston
a6d98cec0b address review comments 2025-11-02 23:41:24 -06:00
J. Nick Koston
8e3d53cb03 address review comments 2025-11-02 23:36:42 -06:00
J. Nick Koston
1150f8e6ba address review comments 2025-11-02 23:33:08 -06:00
J. Nick Koston
55b89faac8 Merge branch 'dev' into select_options 2025-11-02 23:17:04 -06:00
J. Nick Koston
19e1427d92 wip 2025-10-30 15:40:10 -05:00
J. Nick Koston
2a73fd3fd6 esp8266 2025-10-30 15:38:40 -05:00
J. Nick Koston
10b9ec32a8 preen 2025-10-30 15:33:19 -05:00
J. Nick Koston
774cdd33bc cleaner 2025-10-30 15:27:44 -05:00
J. Nick Koston
394d50a328 esphom prefers this-> 2025-10-30 15:24:02 -05:00
J. Nick Koston
f86c74ff02 preen 2025-10-30 15:20:50 -05:00
J. Nick Koston
3552d29167 preen 2025-10-30 14:30:58 -05:00
J. Nick Koston
567672171a force inline 2025-10-30 14:28:09 -05:00
J. Nick Koston
f447aaed8d force inline 2025-10-30 14:26:37 -05:00
J. Nick Koston
1a9aa23ae9 force inline 2025-10-30 14:25:35 -05:00
J. Nick Koston
6cab143db2 break it out, logic was too hard to follow 2025-10-30 14:20:28 -05:00
J. Nick Koston
867ff200ce break it out, logic was too hard to follow 2025-10-30 14:18:56 -05:00
J. Nick Koston
9f62df1456 break it out, logic was too hard to follow 2025-10-30 14:16:56 -05:00
J. Nick Koston
c02d316866 tidy 2025-10-30 14:07:49 -05:00
J. Nick Koston
54c536cbe2 missed some 2025-10-30 13:40:33 -05:00
J. Nick Koston
849483eb3b silience warning 2025-10-30 13:35:35 -05:00
J. Nick Koston
d496676c84 preen 2025-10-30 13:30:22 -05:00
J. Nick Koston
7d2ebabec7 give people time to migrate since we can 2025-10-30 13:28:27 -05:00
J. Nick Koston
ad5752f68e give people time to migrate since we can 2025-10-30 13:25:31 -05:00
J. Nick Koston
2e6dab89ff preen 2025-10-30 13:19:45 -05:00
J. Nick Koston
6dff2d6240 cleanups 2025-10-30 13:17:25 -05:00
J. Nick Koston
b6d178b8c1 cleanups 2025-10-30 13:12:28 -05:00
J. Nick Koston
fd8726b479 comment it 2025-10-30 13:07:03 -05:00
J. Nick Koston
f6aee64ec1 preen 2025-10-30 13:02:37 -05:00
J. Nick Koston
58a517afa6 preen 2025-10-30 13:01:32 -05:00
J. Nick Koston
a02b90129d preen 2025-10-30 13:00:02 -05:00
J. Nick Koston
d1adf79fc3 preen 2025-10-30 12:45:41 -05:00
J. Nick Koston
29887e1da5 preen 2025-10-30 12:43:50 -05:00
J. Nick Koston
5f4f6ced32 preen 2025-10-30 12:39:18 -05:00
J. Nick Koston
cf99bab87b preen 2025-10-30 12:38:12 -05:00
J. Nick Koston
c2902c9671 preen 2025-10-30 12:33:10 -05:00
J. Nick Koston
1c0a5a9765 preen 2025-10-30 12:32:37 -05:00
J. Nick Koston
df014f0217 preen 2025-10-30 12:28:19 -05:00
J. Nick Koston
18783ff20b preen 2025-10-30 12:26:47 -05:00
J. Nick Koston
0db55ef2dd select by index 2025-10-30 12:14:53 -05:00
67 changed files with 329 additions and 294 deletions

View File

@@ -877,7 +877,7 @@ uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection
bool is_single) {
auto *select = static_cast<select::Select *>(entity);
SelectStateResponse resp;
resp.set_state(StringRef(select->state));
resp.set_state(StringRef(select->current_option()));
resp.missing_state = !select->has_state();
return fill_and_encode_entity_state(select, resp, SelectStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
}

View File

@@ -7,19 +7,19 @@ namespace copy {
static const char *const TAG = "copy.select";
void CopySelect::setup() {
source_->add_on_state_callback([this](const std::string &value, size_t index) { this->publish_state(value); });
source_->add_on_state_callback([this](const std::string &value, size_t index) { this->publish_state(index); });
traits.set_options(source_->traits.get_options());
if (source_->has_state())
this->publish_state(source_->state);
this->publish_state(source_->active_index().value());
}
void CopySelect::dump_config() { LOG_SELECT("", "Copy Select", this); }
void CopySelect::control(const std::string &value) {
void CopySelect::control(size_t index) {
auto call = source_->make_call();
call.set_option(value);
call.set_index(index);
call.perform();
}

View File

@@ -13,7 +13,7 @@ class CopySelect : public select::Select, public Component {
void dump_config() override;
protected:
void control(const std::string &value) override;
void control(size_t index) override;
select::Select *source_;
};

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

@@ -42,7 +42,7 @@ std::string MenuItemSelect::get_value_text() const {
result = this->value_getter_.value()(this);
} else {
if (this->select_var_ != nullptr) {
result = this->select_var_->state;
result = this->select_var_->current_option();
}
}

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

@@ -121,9 +121,9 @@ constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[] = {
};
// Helper functions for lookups
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const std::string &str) {
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const char *str) {
for (const auto &entry : arr) {
if (str == entry.str)
if (strcmp(str, entry.str) == 0)
return entry.value;
}
return 0xFF; // Not found
@@ -441,7 +441,7 @@ bool LD2410Component::handle_ack_data_() {
ESP_LOGV(TAG, "Baud rate change");
#ifdef USE_SELECT
if (this->baud_rate_select_ != nullptr) {
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
}
#endif
break;
@@ -626,14 +626,14 @@ void LD2410Component::set_bluetooth(bool enable) {
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
}
void LD2410Component::set_distance_resolution(const std::string &state) {
void LD2410Component::set_distance_resolution(const char *state) {
this->set_config_mode_(true);
const uint8_t cmd_value[2] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00};
this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value));
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
}
void LD2410Component::set_baud_rate(const std::string &state) {
void LD2410Component::set_baud_rate(const char *state) {
this->set_config_mode_(true);
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
@@ -759,10 +759,10 @@ void LD2410Component::set_light_out_control() {
#endif
#ifdef USE_SELECT
if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state);
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option());
}
if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->has_state()) {
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state);
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option());
}
#endif
this->set_config_mode_(true);

View File

@@ -98,8 +98,8 @@ class LD2410Component : public Component, public uart::UARTDevice {
void read_all_info();
void restart_and_read_all_info();
void set_bluetooth(bool enable);
void set_distance_resolution(const std::string &state);
void set_baud_rate(const std::string &state);
void set_distance_resolution(const char *state);
void set_baud_rate(const char *state);
void factory_reset();
protected:

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2410 {
void BaudRateSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_baud_rate(state);
void BaudRateSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_baud_rate(this->option_at(index));
}
} // namespace ld2410

View File

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

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2410 {
void DistanceResolutionSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_distance_resolution(state);
void DistanceResolutionSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_distance_resolution(this->option_at(index));
}
} // namespace ld2410

View File

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

View File

@@ -3,8 +3,8 @@
namespace esphome {
namespace ld2410 {
void LightOutControlSelect::control(const std::string &value) {
this->publish_state(value);
void LightOutControlSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_light_out_control();
}

View File

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

View File

@@ -132,9 +132,9 @@ constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[] = {
};
// Helper functions for lookups
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const std::string &str) {
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const char *str) {
for (const auto &entry : arr) {
if (str == entry.str) {
if (strcmp(str, entry.str) == 0) {
return entry.value;
}
}
@@ -485,7 +485,7 @@ bool LD2412Component::handle_ack_data_() {
ESP_LOGV(TAG, "Baud rate change");
#ifdef USE_SELECT
if (this->baud_rate_select_ != nullptr) {
ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
}
#endif
break;
@@ -699,14 +699,14 @@ void LD2412Component::set_bluetooth(bool enable) {
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
}
void LD2412Component::set_distance_resolution(const std::string &state) {
void LD2412Component::set_distance_resolution(const char *state) {
this->set_config_mode_(true);
const uint8_t cmd_value[6] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00, 0x00, 0x00, 0x00, 0x00};
this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value));
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
}
void LD2412Component::set_baud_rate(const std::string &state) {
void LD2412Component::set_baud_rate(const char *state) {
this->set_config_mode_(true);
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
@@ -783,7 +783,7 @@ void LD2412Component::set_basic_config() {
1, TOTAL_GATES, DEFAULT_PRESENCE_TIMEOUT, 0,
#endif
#ifdef USE_SELECT
find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state),
find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option()),
#else
0x01, // Default value if not using select
#endif
@@ -837,7 +837,7 @@ void LD2412Component::set_light_out_control() {
#endif
#ifdef USE_SELECT
if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state);
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option());
}
#endif
uint8_t value[2] = {this->light_function_, this->light_threshold_};

View File

@@ -99,8 +99,8 @@ class LD2412Component : public Component, public uart::UARTDevice {
void read_all_info();
void restart_and_read_all_info();
void set_bluetooth(bool enable);
void set_distance_resolution(const std::string &state);
void set_baud_rate(const std::string &state);
void set_distance_resolution(const char *state);
void set_baud_rate(const char *state);
void factory_reset();
void start_dynamic_background_correction();

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2412 {
void BaudRateSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_baud_rate(state);
void BaudRateSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_baud_rate(this->option_at(index));
}
} // namespace ld2412

View File

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

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2412 {
void DistanceResolutionSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_distance_resolution(state);
void DistanceResolutionSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_distance_resolution(this->option_at(index));
}
} // namespace ld2412

View File

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

View File

@@ -3,8 +3,8 @@
namespace esphome {
namespace ld2412 {
void LightOutControlSelect::control(const std::string &value) {
this->publish_state(value);
void LightOutControlSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_light_out_control();
}

View File

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

View File

@@ -131,8 +131,8 @@ static const uint8_t CMD_FRAME_STATUS = 7;
static const uint8_t CMD_ERROR_WORD = 8;
static const uint8_t ENERGY_SENSOR_START = 9;
static const uint8_t CALIBRATE_REPORT_INTERVAL = 4;
static const std::string OP_NORMAL_MODE_STRING = "Normal";
static const std::string OP_SIMPLE_MODE_STRING = "Simple";
static const char *const OP_NORMAL_MODE_STRING = "Normal";
static const char *const OP_SIMPLE_MODE_STRING = "Simple";
// Memory-efficient lookup tables
struct StringToUint8 {
@@ -379,7 +379,7 @@ void LD2420Component::report_gate_data() {
ESP_LOGI(TAG, "Total samples: %d", this->total_sample_number_counter);
}
void LD2420Component::set_operating_mode(const std::string &state) {
void LD2420Component::set_operating_mode(const char *state) {
// If unsupported firmware ignore mode select
if (ld2420::get_firmware_int(firmware_ver_) >= CALIBRATE_VERSION_MIN) {
this->current_operating_mode = find_uint8(OP_MODE_BY_STR, state);

View File

@@ -107,7 +107,7 @@ class LD2420Component : public Component, public uart::UARTDevice {
int send_cmd_from_array(CmdFrameT cmd_frame);
void report_gate_data();
void handle_cmd_error(uint8_t error);
void set_operating_mode(const std::string &state);
void set_operating_mode(const char *state);
void auto_calibrate_sensitivity();
void update_radar_data(uint16_t const *gate_energy, uint8_t sample_number);
uint8_t set_config_mode(bool enable);

View File

@@ -7,9 +7,9 @@ namespace ld2420 {
static const char *const TAG = "ld2420.select";
void LD2420Select::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_operating_mode(value);
void LD2420Select::control(size_t index) {
this->publish_state(index);
this->parent_->set_operating_mode(this->option_at(index));
}
} // namespace ld2420

View File

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

View File

@@ -380,7 +380,7 @@ void LD2450Component::read_all_info() {
this->set_config_mode_(false);
#ifdef USE_SELECT
const auto baud_rate = std::to_string(this->parent_->get_baud_rate());
if (this->baud_rate_select_ != nullptr && this->baud_rate_select_->state != baud_rate) {
if (this->baud_rate_select_ != nullptr && strcmp(this->baud_rate_select_->current_option(), baud_rate.c_str()) != 0) {
this->baud_rate_select_->publish_state(baud_rate);
}
this->publish_zone_type();
@@ -635,7 +635,7 @@ bool LD2450Component::handle_ack_data_() {
ESP_LOGV(TAG, "Baud rate change");
#ifdef USE_SELECT
if (this->baud_rate_select_ != nullptr) {
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
}
#endif
break;
@@ -716,7 +716,7 @@ bool LD2450Component::handle_ack_data_() {
this->publish_zone_type();
#ifdef USE_SELECT
if (this->zone_type_select_ != nullptr) {
ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->state.c_str());
ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->current_option());
}
#endif
if (this->buffer_data_[10] == 0x00) {
@@ -790,7 +790,7 @@ void LD2450Component::set_bluetooth(bool enable) {
}
// Set Baud rate
void LD2450Component::set_baud_rate(const std::string &state) {
void LD2450Component::set_baud_rate(const char *state) {
this->set_config_mode_(true);
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
@@ -798,8 +798,8 @@ void LD2450Component::set_baud_rate(const std::string &state) {
}
// Set Zone Type - one of: Disabled, Detection, Filter
void LD2450Component::set_zone_type(const std::string &state) {
ESP_LOGV(TAG, "Set zone type: %s", state.c_str());
void LD2450Component::set_zone_type(const char *state) {
ESP_LOGV(TAG, "Set zone type: %s", state);
uint8_t zone_type = find_uint8(ZONE_TYPE_BY_STR, state);
this->zone_type_ = zone_type;
this->send_set_zone_command_();

View File

@@ -115,8 +115,8 @@ class LD2450Component : public Component, public uart::UARTDevice {
void restart_and_read_all_info();
void set_bluetooth(bool enable);
void set_multi_target(bool enable);
void set_baud_rate(const std::string &state);
void set_zone_type(const std::string &state);
void set_baud_rate(const char *state);
void set_zone_type(const char *state);
void publish_zone_type();
void factory_reset();
#ifdef USE_TEXT_SENSOR

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2450 {
void BaudRateSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_baud_rate(state);
void BaudRateSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_baud_rate(this->option_at(index));
}
} // namespace ld2450

View File

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

View File

@@ -3,9 +3,9 @@
namespace esphome {
namespace ld2450 {
void ZoneTypeSelect::control(const std::string &value) {
this->publish_state(value);
this->parent_->set_zone_type(state);
void ZoneTypeSelect::control(size_t index) {
this->publish_state(index);
this->parent_->set_zone_type(this->option_at(index));
}
} // namespace ld2450

View File

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

View File

@@ -3,10 +3,10 @@
namespace esphome::logger {
void LoggerLevelSelect::publish_state(int level) {
const auto &option = this->at(level_to_index(level));
if (!option)
auto index = level_to_index(level);
if (!this->has_index(index))
return;
Select::publish_state(option.value());
Select::publish_state(index);
}
void LoggerLevelSelect::setup() {
@@ -14,11 +14,6 @@ void LoggerLevelSelect::setup() {
this->publish_state(this->parent_->get_log_level());
}
void LoggerLevelSelect::control(const std::string &value) {
const auto index = this->index_of(value);
if (!index)
return;
this->parent_->set_log_level(index_to_level(index.value()));
}
void LoggerLevelSelect::control(size_t index) { this->parent_->set_log_level(index_to_level(index)); }
} // namespace esphome::logger

View File

@@ -9,7 +9,7 @@ class LoggerLevelSelect : public Component, public select::Select, public Parent
public:
void publish_state(int level);
void setup() override;
void control(const std::string &value) override;
void control(size_t index) override;
protected:
// Convert log level to option index (skip CONFIG at level 4)

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

@@ -21,7 +21,8 @@ void MQTTSelectComponent::setup() {
call.set_option(state);
call.perform();
});
this->select_->add_on_state_callback([this](const std::string &state, size_t index) { this->publish_state(state); });
this->select_->add_on_state_callback(
[this](const std::string &state, size_t index) { this->publish_state(this->select_->option_at(index)); });
}
void MQTTSelectComponent::dump_config() {
@@ -44,7 +45,7 @@ void MQTTSelectComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon
}
bool MQTTSelectComponent::send_initial_state() {
if (this->select_->has_state()) {
return this->publish_state(this->select_->state);
return this->publish_state(this->select_->current_option());
} else {
return true;
}

View File

@@ -435,12 +435,12 @@ void MR24HPC1Component::r24_frame_parse_open_underlying_information_(uint8_t *da
} else if ((this->existence_boundary_select_ != nullptr) &&
((data[FRAME_COMMAND_WORD_INDEX] == 0x0a) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8a))) {
if (this->existence_boundary_select_->has_index(data[FRAME_DATA_INDEX] - 1)) {
this->existence_boundary_select_->publish_state(S_BOUNDARY_STR[data[FRAME_DATA_INDEX] - 1]);
this->existence_boundary_select_->publish_state(data[FRAME_DATA_INDEX] - 1);
}
} else if ((this->motion_boundary_select_ != nullptr) &&
((data[FRAME_COMMAND_WORD_INDEX] == 0x0b) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8b))) {
if (this->motion_boundary_select_->has_index(data[FRAME_DATA_INDEX] - 1)) {
this->motion_boundary_select_->publish_state(S_BOUNDARY_STR[data[FRAME_DATA_INDEX] - 1]);
this->motion_boundary_select_->publish_state(data[FRAME_DATA_INDEX] - 1);
}
} else if ((this->motion_trigger_number_ != nullptr) &&
((data[FRAME_COMMAND_WORD_INDEX] == 0x0c) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8c))) {
@@ -515,7 +515,7 @@ void MR24HPC1Component::r24_frame_parse_work_status_(uint8_t *data) {
ESP_LOGD(TAG, "Reply: get radar init status 0x%02X", data[FRAME_DATA_INDEX]);
} else if (data[FRAME_COMMAND_WORD_INDEX] == 0x07) {
if ((this->scene_mode_select_ != nullptr) && (this->scene_mode_select_->has_index(data[FRAME_DATA_INDEX]))) {
this->scene_mode_select_->publish_state(S_SCENE_STR[data[FRAME_DATA_INDEX]]);
this->scene_mode_select_->publish_state(data[FRAME_DATA_INDEX]);
} else {
ESP_LOGD(TAG, "Select has index offset %d Error", data[FRAME_DATA_INDEX]);
}
@@ -538,7 +538,7 @@ void MR24HPC1Component::r24_frame_parse_work_status_(uint8_t *data) {
ESP_LOGD(TAG, "Reply: get radar init status 0x%02X", data[FRAME_DATA_INDEX]);
} else if (data[FRAME_COMMAND_WORD_INDEX] == 0x87) {
if ((this->scene_mode_select_ != nullptr) && (this->scene_mode_select_->has_index(data[FRAME_DATA_INDEX]))) {
this->scene_mode_select_->publish_state(S_SCENE_STR[data[FRAME_DATA_INDEX]]);
this->scene_mode_select_->publish_state(data[FRAME_DATA_INDEX]);
} else {
ESP_LOGD(TAG, "Select has index offset %d Error", data[FRAME_DATA_INDEX]);
}
@@ -581,7 +581,7 @@ void MR24HPC1Component::r24_frame_parse_human_information_(uint8_t *data) {
((data[FRAME_COMMAND_WORD_INDEX] == 0x0A) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8A))) {
// none:0x00 1s:0x01 30s:0x02 1min:0x03 2min:0x04 5min:0x05 10min:0x06 30min:0x07 1hour:0x08
if (data[FRAME_DATA_INDEX] < 9) {
this->unman_time_select_->publish_state(S_UNMANNED_TIME_STR[data[FRAME_DATA_INDEX]]);
this->unman_time_select_->publish_state(data[FRAME_DATA_INDEX]);
}
} else if ((this->keep_away_text_sensor_ != nullptr) &&
((data[FRAME_COMMAND_WORD_INDEX] == 0x0B) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8B))) {

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

@@ -292,7 +292,7 @@ void MR60FDA2Component::process_frame_() {
install_height_float = bit_cast<float>(current_install_height_int);
uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
this->install_height_select_->publish_state(select_index);
}
if (this->height_threshold_select_ != nullptr) {
@@ -301,7 +301,7 @@ void MR60FDA2Component::process_frame_() {
height_threshold_float = bit_cast<float>(current_height_threshold_int);
size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
this->height_threshold_select_->publish_state(select_index);
}
if (this->sensitivity_select_ != nullptr) {
@@ -309,7 +309,7 @@ void MR60FDA2Component::process_frame_() {
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
this->sensitivity_select_->publish_state(select_index);
}
ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,

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

@@ -7,24 +7,43 @@ namespace select {
static const char *const TAG = "select";
void Select::publish_state(const std::string &state) {
void Select::publish_state(const std::string &state) { this->publish_state(state.c_str()); }
void Select::publish_state(const char *state) {
auto index = this->index_of(state);
const auto *name = this->get_name().c_str();
if (index.has_value()) {
this->set_has_state(true);
this->state = state;
ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", name, state.c_str(), index.value());
this->state_callback_.call(state, index.value());
this->publish_state(index.value());
} else {
ESP_LOGE(TAG, "'%s': invalid state for publish_state(): %s", name, state.c_str());
ESP_LOGE(TAG, "'%s': Invalid option %s", this->get_name().c_str(), state);
}
}
void Select::publish_state(size_t index) {
if (!this->has_index(index)) {
ESP_LOGE(TAG, "'%s': Invalid index %zu", this->get_name().c_str(), index);
return;
}
const char *option = this->option_at(index);
this->set_has_state(true);
this->active_index_ = index;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
this->state = option; // Update deprecated member for backward compatibility
#pragma GCC diagnostic pop
ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", this->get_name().c_str(), option, index);
// Callback signature requires std::string, create temporary for compatibility
this->state_callback_.call(std::string(option), index);
}
const char *Select::current_option() const { return this->has_state() ? this->option_at(this->active_index_) : ""; }
void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
this->state_callback_.add(std::move(callback));
}
bool Select::has_option(const std::string &option) const { return this->index_of(option).has_value(); }
bool Select::has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
bool Select::has_option(const char *option) const { return this->index_of(option).has_value(); }
bool Select::has_index(size_t index) const { return index < this->size(); }
@@ -33,10 +52,12 @@ size_t Select::size() const {
return options.size();
}
optional<size_t> Select::index_of(const std::string &option) const {
optional<size_t> Select::index_of(const std::string &option) const { return this->index_of(option.c_str()); }
optional<size_t> Select::index_of(const char *option) const {
const auto &options = traits.get_options();
for (size_t i = 0; i < options.size(); i++) {
if (strcmp(options[i], option.c_str()) == 0) {
if (strcmp(options[i], option) == 0) {
return i;
}
}
@@ -45,19 +66,17 @@ optional<size_t> Select::index_of(const std::string &option) const {
optional<size_t> Select::active_index() const {
if (this->has_state()) {
return this->index_of(this->state);
} else {
return {};
return this->active_index_;
}
return {};
}
optional<std::string> Select::at(size_t index) const {
if (this->has_index(index)) {
const auto &options = traits.get_options();
return std::string(options.at(index));
} else {
return {};
}
return {};
}
const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }

View File

@@ -30,16 +30,31 @@ namespace select {
*/
class Select : public EntityBase {
public:
std::string state;
SelectTraits traits;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/// @deprecated Use current_option() instead. This member will be removed in ESPHome 2026.5.0.
__attribute__((deprecated("Use current_option() instead of .state. Will be removed in 2026.5.0")))
std::string state{};
Select() = default;
~Select() = default;
#pragma GCC diagnostic pop
void publish_state(const std::string &state);
void publish_state(const char *state);
void publish_state(size_t index);
/// Return the currently selected option (as const char* from flash).
const char *current_option() const;
/// Instantiate a SelectCall object to modify this select component's state.
SelectCall make_call() { return SelectCall(this); }
/// Return whether this select component contains the provided option.
bool has_option(const std::string &option) const;
bool has_option(const char *option) const;
/// Return whether this select component contains the provided index offset.
bool has_index(size_t index) const;
@@ -49,6 +64,7 @@ class Select : public EntityBase {
/// Find the (optional) index offset of the provided option value.
optional<size_t> index_of(const std::string &option) const;
optional<size_t> index_of(const char *option) const;
/// Return the (optional) index offset of the currently active option.
optional<size_t> active_index() const;
@@ -64,13 +80,36 @@ class Select : public EntityBase {
protected:
friend class SelectCall;
/** Set the value of the select, this is a virtual method that each select integration must implement.
size_t active_index_{0};
/** Set the value of the select by index, this is an optional virtual method.
*
* This method is called by the SelectCall.
* IMPORTANT: At least ONE of the two control() methods must be overridden by derived classes.
* Overriding this index-based version is PREFERRED as it avoids string conversions.
*
* @param value The value as validated by the SelectCall.
* This method is called by the SelectCall when the index is already known.
* Default implementation converts to string and calls control(const std::string&).
*
* @param index The index as validated by the SelectCall.
*/
virtual void control(const std::string &value) = 0;
virtual void control(size_t index) { this->control(this->option_at(index)); }
/** Set the value of the select, this is a virtual method that each select integration can implement.
*
* IMPORTANT: At least ONE of the two control() methods must be overridden by derived classes.
* Overriding control(size_t) is PREFERRED as it avoids string conversions.
*
* This method is called by control(size_t) when not overridden, or directly by external code.
* Default implementation converts to index and calls control(size_t).
*
* @param value The value as validated by the caller.
*/
virtual void control(const std::string &value) {
auto index = this->index_of(value);
if (index.has_value()) {
this->control(index.value());
}
}
CallbackManager<void(std::string, size_t)> state_callback_;
};

View File

@@ -7,19 +7,21 @@ namespace select {
static const char *const TAG = "select";
SelectCall &SelectCall::set_option(const std::string &option) {
return with_operation(SELECT_OP_SET).with_option(option);
SelectCall &SelectCall::set_option(const std::string &option) { return this->with_option(option); }
SelectCall &SelectCall::set_option(const char *option) { return this->with_option(option); }
SelectCall &SelectCall::set_index(size_t index) { return this->with_index(index); }
SelectCall &SelectCall::select_next(bool cycle) { return this->with_operation(SELECT_OP_NEXT).with_cycle(cycle); }
SelectCall &SelectCall::select_previous(bool cycle) {
return this->with_operation(SELECT_OP_PREVIOUS).with_cycle(cycle);
}
SelectCall &SelectCall::set_index(size_t index) { return with_operation(SELECT_OP_SET_INDEX).with_index(index); }
SelectCall &SelectCall::select_first() { return this->with_operation(SELECT_OP_FIRST); }
SelectCall &SelectCall::select_next(bool cycle) { return with_operation(SELECT_OP_NEXT).with_cycle(cycle); }
SelectCall &SelectCall::select_previous(bool cycle) { return with_operation(SELECT_OP_PREVIOUS).with_cycle(cycle); }
SelectCall &SelectCall::select_first() { return with_operation(SELECT_OP_FIRST); }
SelectCall &SelectCall::select_last() { return with_operation(SELECT_OP_LAST); }
SelectCall &SelectCall::select_last() { return this->with_operation(SELECT_OP_LAST); }
SelectCall &SelectCall::with_operation(SelectOperation operation) {
this->operation_ = operation;
@@ -31,89 +33,96 @@ SelectCall &SelectCall::with_cycle(bool cycle) {
return *this;
}
SelectCall &SelectCall::with_option(const std::string &option) {
this->option_ = option;
SelectCall &SelectCall::with_option(const std::string &option) { return this->with_option(option.c_str()); }
SelectCall &SelectCall::with_option(const char *option) {
this->operation_ = SELECT_OP_SET;
// Find the option index - this validates the option exists
this->index_ = this->parent_->index_of(option);
return *this;
}
SelectCall &SelectCall::with_index(size_t index) {
this->index_ = index;
this->operation_ = SELECT_OP_SET;
if (index >= this->parent_->size()) {
ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", this->parent_->get_name().c_str(), index);
this->index_ = {}; // Store nullopt for invalid index
} else {
this->index_ = index;
}
return *this;
}
optional<size_t> SelectCall::calculate_target_index_(const char *name) {
const auto &options = this->parent_->traits.get_options();
if (options.empty()) {
ESP_LOGW(TAG, "'%s' - Select has no options", name);
return {};
}
if (this->operation_ == SELECT_OP_FIRST) {
return 0;
}
if (this->operation_ == SELECT_OP_LAST) {
return options.size() - 1;
}
if (this->operation_ == SELECT_OP_SET) {
ESP_LOGD(TAG, "'%s' - Setting", name);
if (!this->index_.has_value()) {
ESP_LOGW(TAG, "'%s' - No option set", name);
return {};
}
return this->index_.value();
}
// SELECT_OP_NEXT or SELECT_OP_PREVIOUS
ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name,
this->operation_ == SELECT_OP_NEXT ? LOG_STR_LITERAL("next") : LOG_STR_LITERAL("previous"),
this->cycle_ ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("out"));
const auto size = options.size();
if (!this->parent_->has_state()) {
return this->operation_ == SELECT_OP_NEXT ? 0 : size - 1;
}
// Use cached active_index_ instead of index_of() lookup
const auto active_index = this->parent_->active_index_;
if (this->cycle_) {
return (size + active_index + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
}
if (this->operation_ == SELECT_OP_PREVIOUS && active_index > 0) {
return active_index - 1;
}
if (this->operation_ == SELECT_OP_NEXT && active_index < size - 1) {
return active_index + 1;
}
return {}; // Can't navigate further without cycling
}
void SelectCall::perform() {
auto *parent = this->parent_;
const auto *name = parent->get_name().c_str();
const auto &traits = parent->traits;
const auto &options = traits.get_options();
if (this->operation_ == SELECT_OP_NONE) {
ESP_LOGW(TAG, "'%s' - SelectCall performed without selecting an operation", name);
return;
}
if (options.empty()) {
ESP_LOGW(TAG, "'%s' - Cannot perform SelectCall, select has no options", name);
// Calculate target index (with_index() and with_option() already validate bounds/existence)
auto target_index = this->calculate_target_index_(name);
if (!target_index.has_value()) {
return;
}
std::string target_value;
if (this->operation_ == SELECT_OP_SET) {
ESP_LOGD(TAG, "'%s' - Setting", name);
if (!this->option_.has_value()) {
ESP_LOGW(TAG, "'%s' - No option value set for SelectCall", name);
return;
}
target_value = this->option_.value();
} else if (this->operation_ == SELECT_OP_SET_INDEX) {
if (!this->index_.has_value()) {
ESP_LOGW(TAG, "'%s' - No index value set for SelectCall", name);
return;
}
if (this->index_.value() >= options.size()) {
ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", name, this->index_.value());
return;
}
target_value = options[this->index_.value()];
} else if (this->operation_ == SELECT_OP_FIRST) {
target_value = options.front();
} else if (this->operation_ == SELECT_OP_LAST) {
target_value = options.back();
} else if (this->operation_ == SELECT_OP_NEXT || this->operation_ == SELECT_OP_PREVIOUS) {
auto cycle = this->cycle_;
ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name, this->operation_ == SELECT_OP_NEXT ? "next" : "previous",
cycle ? "" : "out");
if (!parent->has_state()) {
target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
} else {
auto index = parent->index_of(parent->state);
if (index.has_value()) {
auto size = options.size();
if (cycle) {
auto use_index = (size + index.value() + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
target_value = options[use_index];
} else {
if (this->operation_ == SELECT_OP_PREVIOUS && index.value() > 0) {
target_value = options[index.value() - 1];
} else if (this->operation_ == SELECT_OP_NEXT && index.value() < options.size() - 1) {
target_value = options[index.value() + 1];
} else {
return;
}
}
} else {
target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
}
}
}
if (!parent->has_option(target_value)) {
ESP_LOGW(TAG, "'%s' - Option %s is not a valid option", name, target_value.c_str());
return;
}
ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, target_value.c_str());
parent->control(target_value);
auto idx = target_index.value();
// All operations use indices, call control() by index to avoid string conversion
ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, parent->option_at(idx));
parent->control(idx);
}
} // namespace select

View File

@@ -10,7 +10,6 @@ class Select;
enum SelectOperation {
SELECT_OP_NONE,
SELECT_OP_SET,
SELECT_OP_SET_INDEX,
SELECT_OP_NEXT,
SELECT_OP_PREVIOUS,
SELECT_OP_FIRST,
@@ -23,6 +22,7 @@ class SelectCall {
void perform();
SelectCall &set_option(const std::string &option);
SelectCall &set_option(const char *option);
SelectCall &set_index(size_t index);
SelectCall &select_next(bool cycle);
@@ -33,11 +33,13 @@ class SelectCall {
SelectCall &with_operation(SelectOperation operation);
SelectCall &with_cycle(bool cycle);
SelectCall &with_option(const std::string &option);
SelectCall &with_option(const char *option);
SelectCall &with_index(size_t index);
protected:
__attribute__((always_inline)) inline optional<size_t> calculate_target_index_(const char *name);
Select *const parent_;
optional<std::string> option_;
optional<size_t> index_;
SelectOperation operation_{SELECT_OP_NONE};
bool cycle_;

View File

@@ -24,7 +24,7 @@ void TemplateSelect::setup() {
ESP_LOGD(TAG, "State from initial: %s", this->option_at(index));
}
this->publish_state(this->at(index).value());
this->publish_state(index);
}
void TemplateSelect::update() {
@@ -41,16 +41,14 @@ void TemplateSelect::update() {
}
}
void TemplateSelect::control(const std::string &value) {
this->set_trigger_->trigger(value);
void TemplateSelect::control(size_t index) {
this->set_trigger_->trigger(std::string(this->option_at(index)));
if (this->optimistic_)
this->publish_state(value);
this->publish_state(index);
if (this->restore_value_) {
auto index = this->index_of(value);
this->pref_.save(&index.value());
}
if (this->restore_value_)
this->pref_.save(&index);
}
void TemplateSelect::dump_config() {

View File

@@ -24,7 +24,7 @@ class TemplateSelect : public select::Select, public PollingComponent {
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
protected:
void control(const std::string &value) override;
void control(size_t index) override;
bool optimistic_ = false;
size_t initial_option_index_{0};
bool restore_value_ = false;

View File

@@ -17,28 +17,21 @@ void TuyaSelect::setup() {
return;
}
size_t mapping_idx = std::distance(mappings.cbegin(), it);
auto value = this->at(mapping_idx);
this->publish_state(value.value());
this->publish_state(mapping_idx);
});
}
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;

View File

@@ -1188,7 +1188,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
if (request->method() == HTTP_GET && match.method_empty()) {
auto detail = get_request_detail(request);
std::string data = this->select_json(obj, obj->state, detail);
std::string data = this->select_json(obj, obj->has_state() ? obj->current_option() : "", detail);
request->send(200, "application/json", data.c_str());
return;
}
@@ -1208,12 +1208,14 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
request->send(404);
}
std::string WebServer::select_state_json_generator(WebServer *web_server, void *source) {
return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_STATE);
auto *obj = (select::Select *) (source);
return web_server->select_json(obj, obj->has_state() ? obj->current_option() : "", DETAIL_STATE);
}
std::string WebServer::select_all_json_generator(WebServer *web_server, void *source) {
return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_ALL);
auto *obj = (select::Select *) (source);
return web_server->select_json(obj, obj->has_state() ? obj->current_option() : "", DETAIL_ALL);
}
std::string WebServer::select_json(select::Select *obj, const std::string &value, JsonDetail start_config) {
std::string WebServer::select_json(select::Select *obj, const char *value, JsonDetail start_config) {
json::JsonBuilder builder;
JsonObject root = builder.root();

View File

@@ -410,7 +410,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
static std::string select_state_json_generator(WebServer *web_server, void *source);
static std::string select_all_json_generator(WebServer *web_server, void *source);
/// Dump the select state with its value as a JSON string.
std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
std::string select_json(select::Select *obj, const char *value, JsonDetail start_config);
#endif
#ifdef USE_CLIMATE