#include "boot_target_select.h" #include "esphome/core/log.h" namespace esphome { namespace grub_boot_switch { static const char *const TAG = "grub_boot_switch.select"; void BootTargetSelect::setup() { // Publish the current state from the parent so HA reflects whatever was // restored from NVS / set_initial_target. if (parent_ != nullptr) { auto label = parent_->label_for_digit(parent_->get_target()); if (!label.empty()) this->publish_state(label); // Mirror future external set_target() calls (REST/automation/MQTT) into // the select's reported state. parent_->add_on_target_change([this](uint8_t digit) { auto l = this->parent_->label_for_digit(digit); if (!l.empty()) this->publish_state(l); }); } } void BootTargetSelect::control(const std::string &value) { if (parent_ == nullptr) return; uint8_t digit = 0; if (!parent_->digit_for_label(value, &digit)) { ESP_LOGW(TAG, "control: unknown option '%s'", value.c_str()); return; } parent_->set_target(digit); // The parent will fire add_on_target_change → publish_state, but if the // target was already at this digit no callback fires; publish anyway so // HA's optimistic state stays in sync. this->publish_state(value); } } // namespace grub_boot_switch } // namespace esphome