diff --git a/esphome/components/kt0803/__init__.py b/esphome/components/kt0803/__init__.py index 5e001e9bcf..bff70ef95e 100644 --- a/esphome/components/kt0803/__init__.py +++ b/esphome/components/kt0803/__init__.py @@ -26,7 +26,7 @@ UNIT_MEGA_HERTZ = "MHz" UNIT_KILO_HERTZ = "kHz" UNIT_MILLI_VOLT = "mV" UNIT_MICRO_AMPERE = "mA" -UNIT_DECIBEL_MICRO_VOLT = "dBµV" +UNIT_DECIBEL_MICRO_VOLT = "dBuV" ICON_VOLUME_MUTE = "mdi:volume-mute" ICON_EAR_HEARING = "mdi:ear-hearing" @@ -361,6 +361,24 @@ CONFIG_SCHEMA = ( ) +FREQUENCY_SCHEMA = automation.maybe_simple_id( + { + cv.GenerateID(): cv.use_id(KT0803Component), + cv.Required(CONF_FREQUENCY): cv.float_range(min = 70, max = 108), + } +) + + +@automation.register_action("kt0803.set_frequency", SetFrequencyAction, FREQUENCY_SCHEMA) +async def tune_frequency_action_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + if frequency := config.get(CONF_FREQUENCY): + template_ = await cg.templatable(frequency, args, cg.float_) + cg.add(var.set_frequency(template_)) + return var + + async def set_var(config, id, setter): if c := config.get(id): cg.add(setter(c)) diff --git a/esphome/components/kt0803/kt0803.h b/esphome/components/kt0803/kt0803.h index 6c7617c4cf..a1fd1b9a9e 100644 --- a/esphome/components/kt0803/kt0803.h +++ b/esphome/components/kt0803/kt0803.h @@ -198,5 +198,12 @@ class KT0803Component : public PollingComponent, public i2c::I2CDevice { AlcLow get_alc_low(); }; +template class SetFrequencyAction : public Action, public Parented { + TEMPLATABLE_VALUE(float, frequency) + void play(Ts... x) override { + this->parent_->set_frequency(this->frequency_.value(x...)); + } +}; + } // namespace kt0803 } // namespace esphome diff --git a/esphome/components/kt0803/kt0803defs.h b/esphome/components/kt0803/kt0803defs.h index a6d7eb589b..ccbe5dec48 100644 --- a/esphome/components/kt0803/kt0803defs.h +++ b/esphome/components/kt0803/kt0803defs.h @@ -22,6 +22,7 @@ enum class ChipId { KT0803K, KT0803M, KT0803L, + LAST, }; enum class PreEmphasis {