"""Select sub-platform for grub_boot_switch. Exposes the parent's targets table as a Home Assistant select entity. The options list is derived at code-gen time from the parent's `targets:` map, so adding/removing entries there automatically updates the dropdown. """ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import select from esphome.const import CONF_ID from .. import ( CONF_GRUB_BOOT_SWITCH_ID, CONF_TARGETS, GrubBootSwitch, grub_boot_switch_ns, ) DEPENDENCIES = ["grub_boot_switch"] BootTargetSelect = grub_boot_switch_ns.class_( "BootTargetSelect", select.Select, cg.Component ) CONFIG_SCHEMA = select.select_schema(BootTargetSelect).extend( { cv.GenerateID(CONF_GRUB_BOOT_SWITCH_ID): cv.use_id(GrubBootSwitch), } ) async def to_code(config): parent = await cg.get_variable(config[CONF_GRUB_BOOT_SWITCH_ID]) # Reach back into the *full* validated config so we can pull the parent's # targets map. CORE.config holds the post-validation tree. from esphome.core import CORE parent_cfg = CORE.config.get("grub_boot_switch", {}) targets = parent_cfg.get(CONF_TARGETS, {}) if not targets: raise cv.Invalid( "select platform requires the grub_boot_switch component to define `targets:`" ) options = [label for _digit, label in sorted(targets.items())] var = await select.new_select(config, options=options) await cg.register_component(var, config) cg.add(var.set_parent(parent))