fixed default=0 not being set

This commit is contained in:
Gábor Poczkodi 2024-10-26 00:03:04 +02:00
parent 64c8b4a23b
commit 556cb74d5f
4 changed files with 34 additions and 23 deletions

View file

@ -492,14 +492,17 @@ SENSORS = {
async def for_each_conf(config, vars, callback): async def for_each_conf(config, vars, callback):
for section in vars: 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]: for args in vars[section]:
setter = "set_" setter = "set_"
if section is not None and section != CONF_SENSOR: if section is not None and section != CONF_SENSOR:
setter += section + "_" setter += section + "_"
setter += args[0] setter += args[0]
if cc := c.get(args[0]): if args[0] in c:
await callback(cc, args, setter) # print(setter + "(" + repr(c[args[0]]) + ")")
await callback(c[args[0]], args, setter)
async def to_code(config): async def to_code(config):
@ -534,25 +537,32 @@ FREQUENCY_SCHEMA = automation.maybe_simple_id(
} }
) )
SetFrequencyAction = si4713_ns.class_( SetTunerFrequencyAction = si4713_ns.class_(
"SetFrequencyAction", automation.Action, cg.Parented.template(Si4713Component) "SetTunerFrequencyAction", automation.Action, cg.Parented.template(Si4713Component)
) )
MeasureFrequencyAction = si4713_ns.class_(
"MeasureFrequencyAction", automation.Action, cg.Parented.template(Si4713Component)
)
@automation.register_action( @automation.register_action(
"si4713.set_tuner_frequency", SetFrequencyAction, FREQUENCY_SCHEMA "si4713.set_tuner_frequency", SetTunerFrequencyAction, FREQUENCY_SCHEMA
) )
@automation.register_action( async def set_tuner_frequency_action_to_code(config, action_id, template_arg, args):
"si4713.measure_frequency", MeasureFrequencyAction, FREQUENCY_SCHEMA
)
async def tune_frequency_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg) var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID]) await cg.register_parented(var, config[CONF_ID])
if frequency := config.get(CONF_FREQUENCY): 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_) template_ = await cg.templatable(frequency, args, cg.float_)
cg.add(var.set_frequency(template_)) cg.add(var.set_frequency(template_))
return var return var

View file

@ -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); uint16_t f = (uint16_t) clamp((int) std::lround(value * 20) * 5, FREQ_RAW_MIN, FREQ_RAW_MAX);
if (!this->send_cmd_(CmdTxTuneMeasure(f))) { if (!this->send_cmd_(CmdTxTuneMeasure(f))) {
return; return;

View file

@ -173,17 +173,17 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
bool get_output_gpio(uint8_t pin); bool get_output_gpio(uint8_t pin);
// used by automation // used by automation
void measure_freq(float value); void measure(float value);
}; };
template<typename... Ts> class SetFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> { template<typename... Ts> class SetTunerFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> {
TEMPLATABLE_VALUE(float, frequency) TEMPLATABLE_VALUE(float, frequency)
void play(Ts... x) override { this->parent_->set_tuner_frequency(this->frequency_.value(x...)); } void play(Ts... x) override { this->parent_->set_tuner_frequency(this->frequency_.value(x...)); }
}; };
template<typename... Ts> class MeasureFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> { template<typename... Ts> class MeasureAction : public Action<Ts...>, public Parented<Si4713Component> {
TEMPLATABLE_VALUE(float, frequency) 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 } // namespace si4713_i2c

View file

@ -147,6 +147,7 @@ OUTPUT_SCHEMA = cv.Schema(
), ),
} }
) )
CONFIG_SCHEMA = cv.Schema( CONFIG_SCHEMA = cv.Schema(
{ {
cv.GenerateID(CONF_SI4713_ID): cv.use_id(Si4713Component), cv.GenerateID(CONF_SI4713_ID): cv.use_id(Si4713Component),