From 556cb74d5fc1fbe97494180a34fd229332045c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Poczkodi?= Date: Sat, 26 Oct 2024 00:03:04 +0200 Subject: [PATCH] fixed default=0 not being set --- esphome/components/si4713_i2c/__init__.py | 46 +++++++++++-------- esphome/components/si4713_i2c/si4713.cpp | 2 +- esphome/components/si4713_i2c/si4713.h | 8 ++-- .../components/si4713_i2c/switch/__init__.py | 1 + 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/esphome/components/si4713_i2c/__init__.py b/esphome/components/si4713_i2c/__init__.py index fedaecb4d2..7b6060a714 100644 --- a/esphome/components/si4713_i2c/__init__.py +++ b/esphome/components/si4713_i2c/__init__.py @@ -492,14 +492,17 @@ SENSORS = { async def for_each_conf(config, vars, callback): for section in vars: - c = config[section] if section in config else config + if section is not None and section not in config: + continue + c = config[section] if section is not None else config for args in vars[section]: setter = "set_" if section is not None and section != CONF_SENSOR: setter += section + "_" setter += args[0] - if cc := c.get(args[0]): - await callback(cc, args, setter) + if args[0] in c: + # print(setter + "(" + repr(c[args[0]]) + ")") + await callback(c[args[0]], args, setter) async def to_code(config): @@ -534,25 +537,32 @@ FREQUENCY_SCHEMA = automation.maybe_simple_id( } ) -SetFrequencyAction = si4713_ns.class_( - "SetFrequencyAction", automation.Action, cg.Parented.template(Si4713Component) +SetTunerFrequencyAction = si4713_ns.class_( + "SetTunerFrequencyAction", automation.Action, cg.Parented.template(Si4713Component) ) -MeasureFrequencyAction = si4713_ns.class_( - "MeasureFrequencyAction", automation.Action, cg.Parented.template(Si4713Component) -) - - @automation.register_action( - "si4713.set_tuner_frequency", SetFrequencyAction, FREQUENCY_SCHEMA + "si4713.set_tuner_frequency", SetTunerFrequencyAction, FREQUENCY_SCHEMA ) -@automation.register_action( - "si4713.measure_frequency", MeasureFrequencyAction, FREQUENCY_SCHEMA -) -async def tune_frequency_action_to_code(config, action_id, template_arg, args): +async def set_tuner_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_)) + frequency = config.get(CONF_FREQUENCY) + template_ = await cg.templatable(frequency, args, cg.float_) + cg.add(var.set_frequency(template_)) + return var + +MeasureAction = si4713_ns.class_( + "MeasureAction", automation.Action, cg.Parented.template(Si4713Component) +) + +@automation.register_action( + "si4713.measure", MeasureAction, FREQUENCY_SCHEMA +) +async def measure_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]) + frequency = config.get(CONF_FREQUENCY) + template_ = await cg.templatable(frequency, args, cg.float_) + cg.add(var.set_frequency(template_)) return var diff --git a/esphome/components/si4713_i2c/si4713.cpp b/esphome/components/si4713_i2c/si4713.cpp index 95592c91ca..febfdc4287 100644 --- a/esphome/components/si4713_i2c/si4713.cpp +++ b/esphome/components/si4713_i2c/si4713.cpp @@ -914,7 +914,7 @@ void Si4713Component::publish_select(select::Select *s, size_t index) { } } -void Si4713Component::measure_freq(float value) { +void Si4713Component::measure(float value) { uint16_t f = (uint16_t) clamp((int) std::lround(value * 20) * 5, FREQ_RAW_MIN, FREQ_RAW_MAX); if (!this->send_cmd_(CmdTxTuneMeasure(f))) { return; diff --git a/esphome/components/si4713_i2c/si4713.h b/esphome/components/si4713_i2c/si4713.h index 6707b16cff..90908887f0 100644 --- a/esphome/components/si4713_i2c/si4713.h +++ b/esphome/components/si4713_i2c/si4713.h @@ -173,17 +173,17 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice { bool get_output_gpio(uint8_t pin); // used by automation - void measure_freq(float value); + void measure(float value); }; -template class SetFrequencyAction : public Action, public Parented { +template class SetTunerFrequencyAction : public Action, public Parented { TEMPLATABLE_VALUE(float, frequency) void play(Ts... x) override { this->parent_->set_tuner_frequency(this->frequency_.value(x...)); } }; -template class MeasureFrequencyAction : public Action, public Parented { +template class MeasureAction : public Action, public Parented { TEMPLATABLE_VALUE(float, frequency) - void play(Ts... x) override { this->parent_->measure_freq(this->frequency_.value(x...)); } + void play(Ts... x) override { this->parent_->measure(this->frequency_.value(x...)); } }; } // namespace si4713_i2c diff --git a/esphome/components/si4713_i2c/switch/__init__.py b/esphome/components/si4713_i2c/switch/__init__.py index 2f88057f56..69abd9fc15 100644 --- a/esphome/components/si4713_i2c/switch/__init__.py +++ b/esphome/components/si4713_i2c/switch/__init__.py @@ -147,6 +147,7 @@ OUTPUT_SCHEMA = cv.Schema( ), } ) + CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_SI4713_ID): cv.use_id(Si4713Component),