reworked to_code in all __init__.py

This commit is contained in:
Gábor Poczkodi 2024-10-19 08:35:22 +02:00
parent 6d5fe9b3c3
commit c25bc13708
37 changed files with 548 additions and 501 deletions

View file

@ -55,7 +55,7 @@ CONF_TUNER = "tuner"
CONF_DIGITAL = "digital" CONF_DIGITAL = "digital"
CONF_PILOT = "pilot" CONF_PILOT = "pilot"
CONF_REFCLK = "refclk" CONF_REFCLK = "refclk"
CONF_COMPRESSOR = "compressor" CONF_ACOMP = "acomp"
CONF_LIMITER = "limiter" CONF_LIMITER = "limiter"
CONF_ASQ = "asq" CONF_ASQ = "asq"
CONF_RDS = "rds" CONF_RDS = "rds"
@ -87,7 +87,7 @@ CONF_DEVIATION = "deviation"
# CONF_FREQUENCY = "frequency" # CONF_FREQUENCY = "frequency"
# CONF_SOURCE = "source" # CONF_SOURCE = "source"
CONF_PRESCALER = "prescaler" CONF_PRESCALER = "prescaler"
# compressor # acomp
CONF_ENABLE = "enable" CONF_ENABLE = "enable"
# CONF_THRESHOLD = "threshold" # CONF_THRESHOLD = "threshold"
CONF_ATTACK = "attack" CONF_ATTACK = "attack"
@ -262,7 +262,7 @@ REFCLK_SCHEMA = cv.Schema(
} }
) )
COMPRESSOR_SCHEMA = cv.Schema( ACOMP_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_ENABLE, default="False"): cv.boolean, cv.Optional(CONF_ENABLE, default="False"): cv.boolean,
cv.Optional(CONF_THRESHOLD, default=-40): cv.int_range(-40, 0), cv.Optional(CONF_THRESHOLD, default=-40): cv.int_range(-40, 0),
@ -367,7 +367,7 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_DIGITAL): DIGITAL_SCHEMA, cv.Optional(CONF_DIGITAL): DIGITAL_SCHEMA,
cv.Optional(CONF_PILOT): PILOT_SCHEMA, cv.Optional(CONF_PILOT): PILOT_SCHEMA,
cv.Optional(CONF_REFCLK): REFCLK_SCHEMA, cv.Optional(CONF_REFCLK): REFCLK_SCHEMA,
cv.Optional(CONF_COMPRESSOR): COMPRESSOR_SCHEMA, cv.Optional(CONF_ACOMP): ACOMP_SCHEMA,
cv.Optional(CONF_LIMITER): LIMITER_SCHEMA, cv.Optional(CONF_LIMITER): LIMITER_SCHEMA,
cv.Optional(CONF_ASQ): ASQ_SCHEMA, cv.Optional(CONF_ASQ): ASQ_SCHEMA,
cv.Optional(CONF_RDS): RDS_SCHEMA, cv.Optional(CONF_RDS): RDS_SCHEMA,
@ -378,6 +378,122 @@ CONFIG_SCHEMA = (
.extend(i2c.i2c_device_schema(0x63)) .extend(i2c.i2c_device_schema(0x63))
) )
VARIABLES = {
None: [
[CONF_OP_MODE],
[CONF_MUTE],
[CONF_MONO],
[CONF_PRE_EMPHASIS],
],
CONF_TUNER: [
[CONF_ENABLE],
[CONF_FREQUENCY],
[CONF_DEVIATION],
[CONF_POWER],
[CONF_ANTCAP],
],
CONF_ANALOG: [
[CONF_LEVEL],
[CONF_ATTENUATION],
],
CONF_DIGITAL: [
[CONF_SAMPLE_RATE],
[CONF_SAMPLE_BITS],
[CONF_CHANNELS],
[CONF_MODE],
[CONF_CLOCK_EDGE],
],
CONF_PILOT: [
[CONF_ENABLE],
[CONF_FREQUENCY],
[CONF_DEVIATION],
],
CONF_REFCLK: [
[CONF_FREQUENCY],
[CONF_SOURCE],
[CONF_PRESCALER],
],
CONF_ACOMP: [
[CONF_ENABLE],
[CONF_THRESHOLD],
[CONF_ATTACK],
[CONF_RELEASE],
[CONF_GAIN],
[CONF_PRESET],
],
CONF_LIMITER: [
[CONF_ENABLE],
[CONF_RELEASE_TIME],
],
CONF_ASQ: [
[CONF_IALL],
[CONF_IALH],
[CONF_OVERMOD],
[CONF_LEVEL_LOW],
[CONF_DURATION_LOW],
[CONF_LEVEL_HIGH],
[CONF_DURATION_HIGH],
],
CONF_RDS: [
[CONF_ENABLE],
[CONF_DEVIATION],
[CONF_STATION],
[CONF_TEXT],
],
}
SENSORS = {
CONF_SENSOR: [
[CONF_CHIP_ID, "text_sensor"],
[CONF_READ_FREQUENCY, "sensor"],
[CONF_READ_POWER, "sensor"],
[CONF_READ_ANTCAP, "sensor"],
[CONF_READ_NOISE_LEVEL, "sensor"],
[CONF_IALL, "binary_sensor"],
[CONF_IALH, "binary_sensor"],
[CONF_OVERMOD, "binary_sensor"],
[CONF_INLEVEL, "sensor"],
]
}
async def for_each_conf(config, vars, callback):
for section in vars:
c = config[section] if section in config 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)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
reset_pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN])
cg.add(var.set_reset_pin(reset_pin))
async def set_var(c, a, s):
cg.add(getattr(var, s)(c))
await for_each_conf(config, VARIABLES, set_var)
async def new_sensor(c, args, setter):
s = None
match args[1]:
case "sensor":
s = await sensor.new_sensor(c)
case "binary_sensor":
s = await binary_sensor.new_binary_sensor(c)
case "text_sensor":
s = await text_sensor.new_text_sensor(c)
cg.add(getattr(var, setter + "_" + args[1])(s))
await for_each_conf(config, SENSORS, new_sensor)
FREQUENCY_SCHEMA = automation.maybe_simple_id( FREQUENCY_SCHEMA = automation.maybe_simple_id(
{ {
@ -388,7 +504,7 @@ FREQUENCY_SCHEMA = automation.maybe_simple_id(
@automation.register_action( @automation.register_action(
"si4713.set_frequency", SetFrequencyAction, FREQUENCY_SCHEMA "si4713.set_tuner_frequency", SetFrequencyAction, FREQUENCY_SCHEMA
) )
@automation.register_action( @automation.register_action(
"si4713.measure_frequency", MeasureFrequencyAction, FREQUENCY_SCHEMA "si4713.measure_frequency", MeasureFrequencyAction, FREQUENCY_SCHEMA
@ -400,100 +516,3 @@ async def tune_frequency_action_to_code(config, action_id, template_arg, args):
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
async def set_var(config, id, setter):
if c := config.get(id):
cg.add(setter(c))
async def new_sensor(config, id, setter):
if c := config.get(id):
s = await sensor.new_sensor(c)
cg.add(setter(s))
async def new_binary_sensor(config, id, setter):
if c := config.get(id):
s = await binary_sensor.new_binary_sensor(c)
cg.add(setter(s))
async def new_text_sensor(config, id, setter):
if c := config.get(id):
s = await text_sensor.new_text_sensor(c)
cg.add(setter(s))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
reset_pin = await cg.gpio_pin_expression(config[CONF_RESET_PIN])
cg.add(var.set_reset_pin(reset_pin))
await set_var(config, CONF_OP_MODE, var.set_op_mode)
await set_var(config, CONF_MUTE, var.set_mute)
await set_var(config, CONF_MONO, var.set_mono)
await set_var(config, CONF_PRE_EMPHASIS, var.set_pre_emphasis)
if tuner_config := config.get(CONF_TUNER):
await set_var(tuner_config, CONF_ENABLE, var.set_power_enable)
await set_var(tuner_config, CONF_FREQUENCY, var.set_frequency)
await set_var(tuner_config, CONF_DEVIATION, var.set_audio_deviation)
await set_var(tuner_config, CONF_POWER, var.set_power)
await set_var(tuner_config, CONF_ANTCAP, var.set_antcap)
if analog_config := config.get(CONF_ANALOG):
await set_var(analog_config, CONF_LEVEL, var.set_analog_level)
await set_var(analog_config, CONF_ATTENUATION, var.set_analog_attenuation)
if digital_config := config.get(CONF_DIGITAL):
await set_var(digital_config, CONF_SAMPLE_RATE, var.set_digital_sample_rate)
await set_var(digital_config, CONF_SAMPLE_BITS, var.set_digital_sample_bits)
await set_var(digital_config, CONF_CHANNELS, var.set_digital_channels)
await set_var(digital_config, CONF_MODE, var.set_digital_mode)
await set_var(digital_config, CONF_CLOCK_EDGE, var.set_digital_clock_edge)
if pilot_config := config.get(CONF_PILOT):
await set_var(pilot_config, CONF_ENABLE, var.set_pilot_enable)
await set_var(pilot_config, CONF_FREQUENCY, var.set_pilot_frequency)
await set_var(pilot_config, CONF_DEVIATION, var.set_pilot_deviation)
if refclk_config := config.get(CONF_REFCLK):
await set_var(refclk_config, CONF_FREQUENCY, var.set_refclk_frequency)
await set_var(refclk_config, CONF_SOURCE, var.set_refclk_source)
await set_var(refclk_config, CONF_PRESCALER, var.set_refclk_prescaler)
if compressor_config := config.get(CONF_COMPRESSOR):
await set_var(compressor_config, CONF_ENABLE, var.set_acomp_enable)
await set_var(compressor_config, CONF_THRESHOLD, var.set_acomp_threshold)
await set_var(compressor_config, CONF_ATTACK, var.set_acomp_attack)
await set_var(compressor_config, CONF_RELEASE, var.set_acomp_release)
await set_var(compressor_config, CONF_GAIN, var.set_acomp_gain)
await set_var(compressor_config, CONF_PRESET, var.set_acomp_preset)
if limiter_config := config.get(CONF_LIMITER):
await set_var(limiter_config, CONF_ENABLE, var.set_limiter_enable)
await set_var(limiter_config, CONF_RELEASE_TIME, var.set_limiter_release_time)
if asq_config := config.get(CONF_ASQ):
await set_var(asq_config, CONF_IALL, var.set_asq_iall_enable)
await set_var(asq_config, CONF_IALH, var.set_asq_ialh_enable)
await set_var(asq_config, CONF_OVERMOD, var.set_asq_overmod_enable)
await set_var(asq_config, CONF_LEVEL_LOW, var.set_asq_level_low)
await set_var(asq_config, CONF_DURATION_LOW, var.set_asq_duration_low)
await set_var(asq_config, CONF_LEVEL_HIGH, var.set_asq_level_high)
await set_var(asq_config, CONF_DURATION_HIGH, var.set_asq_duration_high)
if rds_config := config.get(CONF_RDS):
await set_var(rds_config, CONF_ENABLE, var.set_rds_enable)
await set_var(rds_config, CONF_DEVIATION, var.set_rds_deviation)
await set_var(rds_config, CONF_STATION, var.set_rds_station)
await set_var(rds_config, CONF_TEXT, var.set_rds_text)
if sensor_config := config.get(CONF_SENSOR):
await new_text_sensor(sensor_config, CONF_CHIP_ID, var.set_chip_id_text_sensor)
await new_sensor(
sensor_config, CONF_READ_FREQUENCY, var.set_read_frequency_sensor
)
await new_sensor(sensor_config, CONF_READ_POWER, var.set_read_power_sensor)
await new_sensor(sensor_config, CONF_READ_ANTCAP, var.set_read_antcap_sensor)
await new_sensor(
sensor_config, CONF_READ_NOISE_LEVEL, var.set_read_noise_level_sensor
)
await new_binary_sensor(sensor_config, CONF_IALL, var.set_iall_binary_sensor)
await new_binary_sensor(sensor_config, CONF_IALH, var.set_ialh_binary_sensor)
await new_binary_sensor(
sensor_config, CONF_OVERMOD, var.set_overmod_binary_sensor
)
await new_sensor(sensor_config, CONF_INLEVEL, var.set_inlevel_sensor)

View file

@ -1,5 +1,6 @@
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components import number from esphome.components import number
from esphome.components.number import NumberMode
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import ( from esphome.const import (
CONF_FREQUENCY, CONF_FREQUENCY,
@ -28,7 +29,7 @@ from .. import (
CONF_DIGITAL, CONF_DIGITAL,
CONF_PILOT, CONF_PILOT,
CONF_REFCLK, CONF_REFCLK,
CONF_COMPRESSOR, CONF_ACOMP,
CONF_LIMITER, CONF_LIMITER,
CONF_ASQ, CONF_ASQ,
CONF_RDS, CONF_RDS,
@ -46,12 +47,13 @@ from .. import (
UNIT_MILLI_VOLT, UNIT_MILLI_VOLT,
UNIT_DECIBEL_MICRO_VOLT, UNIT_DECIBEL_MICRO_VOLT,
UNIT_PICO_FARAD, UNIT_PICO_FARAD,
for_each_conf,
) )
FrequencyNumber = si4713_ns.class_("FrequencyNumber", number.Number) TunerFrequencyNumber = si4713_ns.class_("TunerFrequencyNumber", number.Number)
AudioDeviationNumber = si4713_ns.class_("AudioDeviationNumber", number.Number) TunerDeviationNumber = si4713_ns.class_("TunerDeviationNumber", number.Number)
PowerNumber = si4713_ns.class_("PowerNumber", number.Number) TunerPowerNumber = si4713_ns.class_("TunerPowerNumber", number.Number)
AntcapNumber = si4713_ns.class_("AntcapNumber", number.Number) TunerAntcapNumber = si4713_ns.class_("TunerAntcapNumber", number.Number)
AnalogLevelNumber = si4713_ns.class_("AnalogLevelNumber", number.Number) AnalogLevelNumber = si4713_ns.class_("AnalogLevelNumber", number.Number)
DigitalSampleRateNumber = si4713_ns.class_("DigitalSampleRateNumber", number.Number) DigitalSampleRateNumber = si4713_ns.class_("DigitalSampleRateNumber", number.Number)
PilotFrequencyNumber = si4713_ns.class_("PilotFrequencyNumber", number.Number) PilotFrequencyNumber = si4713_ns.class_("PilotFrequencyNumber", number.Number)
@ -73,25 +75,25 @@ CONFIG_SCHEMA = cv.Schema(
cv.Optional(CONF_TUNER): cv.Schema( cv.Optional(CONF_TUNER): cv.Schema(
{ {
cv.Optional(CONF_FREQUENCY): number.number_schema( cv.Optional(CONF_FREQUENCY): number.number_schema(
FrequencyNumber, TunerFrequencyNumber,
unit_of_measurement=UNIT_MEGA_HERTZ, unit_of_measurement=UNIT_MEGA_HERTZ,
device_class=DEVICE_CLASS_FREQUENCY, device_class=DEVICE_CLASS_FREQUENCY,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
), ),
cv.Optional(CONF_DEVIATION): number.number_schema( cv.Optional(CONF_DEVIATION): number.number_schema(
AudioDeviationNumber, TunerDeviationNumber,
unit_of_measurement=UNIT_KILO_HERTZ, unit_of_measurement=UNIT_KILO_HERTZ,
device_class=DEVICE_CLASS_FREQUENCY, device_class=DEVICE_CLASS_FREQUENCY,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
), ),
cv.Optional(CONF_POWER): number.number_schema( cv.Optional(CONF_POWER): number.number_schema(
PowerNumber, TunerPowerNumber,
unit_of_measurement=UNIT_DECIBEL_MICRO_VOLT, unit_of_measurement=UNIT_DECIBEL_MICRO_VOLT,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH, device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
), ),
cv.Optional(CONF_ANTCAP): number.number_schema( cv.Optional(CONF_ANTCAP): number.number_schema(
AntcapNumber, TunerAntcapNumber,
unit_of_measurement=UNIT_PICO_FARAD, unit_of_measurement=UNIT_PICO_FARAD,
device_class=DEVICE_CLASS_EMPTY, device_class=DEVICE_CLASS_EMPTY,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
@ -150,7 +152,7 @@ CONFIG_SCHEMA = cv.Schema(
), ),
}, },
), ),
cv.Optional(CONF_COMPRESSOR): cv.Schema( cv.Optional(CONF_ACOMP): cv.Schema(
{ {
cv.Optional(CONF_THRESHOLD): number.number_schema( cv.Optional(CONF_THRESHOLD): number.number_schema(
AcompThresholdNumber, AcompThresholdNumber,
@ -218,45 +220,58 @@ CONFIG_SCHEMA = cv.Schema(
) )
async def new_number(p, config, id, setter, min_value, max_value, step, *args, **kwargs): VARIABLES = {
if c := config.get(id): CONF_TUNER: [
if CONF_MODE in kwargs: [CONF_FREQUENCY, 76, 108, 0.05, None],
if CONF_MODE not in c or c[CONF_MODE] == number.NumberMode.NUMBER_MODE_AUTO: [CONF_DEVIATION, 0, 90, 0.01, None],
c[CONF_MODE] = kwargs.get(CONF_MODE) [CONF_POWER, 88, 120, 1, NumberMode.NUMBER_MODE_SLIDER],
n = await number.new_number( [CONF_ANTCAP, 0, 47.75, 0.25, None],
c, *args, min_value=min_value, max_value=max_value, step=step ],
) CONF_ANALOG: [
await cg.register_parented(n, p) [CONF_LEVEL, 0, 1023, 1, NumberMode.NUMBER_MODE_SLIDER],
cg.add(setter(n)) ],
return n CONF_DIGITAL: [
[CONF_SAMPLE_RATE, 32000, 48000, 1, None],
],
CONF_PILOT: [
[CONF_FREQUENCY, 0, 19, 0.001, None],
[CONF_DEVIATION, 0, 90, 0.001, None],
],
CONF_REFCLK: [
[CONF_FREQUENCY, 31130, 34406, 1, None],
[CONF_PRESCALER, 0, 4095, 1, None],
],
CONF_ACOMP: [
[CONF_THRESHOLD, -40, 0, 1, None],
[CONF_GAIN, 0, 20, 1, None],
],
CONF_LIMITER: [
[CONF_RELEASE_TIME, 0.25, 102.4, 0.01, NumberMode.NUMBER_MODE_SLIDER],
],
CONF_ASQ: [
[CONF_LEVEL_LOW, -70, 0, 1, None],
[CONF_DURATION_LOW, 0, 65535, 1, None],
[CONF_LEVEL_HIGH, -70, 0, 1, None],
[CONF_DURATION_HIGH, 0, 65535, 1, None],
],
CONF_RDS: [
[CONF_DEVIATION, 0, 7.5, 0.01, None],
],
}
async def to_code(config): async def to_code(config):
p = await cg.get_variable(config[CONF_SI4713_ID]) parent = await cg.get_variable(config[CONF_SI4713_ID])
if tuner_config := config.get(CONF_TUNER):
await new_number(p, tuner_config, CONF_FREQUENCY, p.set_frequency_number, 76, 108, 0.05) async def new_number(c, args, setter):
await new_number(p, tuner_config, CONF_DEVIATION, p.set_audio_deviation_number, 0, 90, 0.01) # only override mode when it's set to auto in user config
await new_number(p, tuner_config, CONF_POWER, p.set_power_number, 88, 120, 1, mode=number.NumberMode.NUMBER_MODE_SLIDER) if CONF_MODE not in c or c[CONF_MODE] == number.NumberMode.NUMBER_MODE_AUTO:
await new_number(p, tuner_config, CONF_ANTCAP, p.set_antcap_number, 0, 47.75, 0.25) if args[4] is not None:
if analog_config := config.get(CONF_ANALOG): c[CONF_MODE] = args[4]
await new_number(p, analog_config, CONF_LEVEL, p.set_analog_level_number, 0, 1023, 1, mode=number.NumberMode.NUMBER_MODE_SLIDER) n = await number.new_number(
if digital_config := config.get(CONF_DIGITAL): c, min_value=args[1], max_value=args[2], step=args[3]
await new_number(p, digital_config, CONF_SAMPLE_RATE, p.set_digital_sample_rate_number, 32000, 48000, 1) )
if pilot_config := config.get(CONF_PILOT): await cg.register_parented(n, parent)
await new_number(p, pilot_config, CONF_FREQUENCY, p.set_pilot_frequency_number, 0, 19, 0.001) cg.add(getattr(parent, setter + "_number")(n))
await new_number(p, pilot_config, CONF_DEVIATION, p.set_pilot_deviation_number, 0, 90, 0.001)
if refclk_config := config.get(CONF_REFCLK): await for_each_conf(config, VARIABLES, new_number)
await new_number(p, refclk_config, CONF_FREQUENCY, p.set_refclk_frequency_number, 31130, 34406, 1)
await new_number(p, refclk_config, CONF_PRESCALER, p.set_refclk_prescaler_number, 0, 4095, 1)
if compressor_config := config.get(CONF_COMPRESSOR):
await new_number(p, compressor_config, CONF_THRESHOLD, p.set_acomp_threshold_number, -40, 0, 1)
await new_number(p, compressor_config, CONF_GAIN, p.set_acomp_gain_number, 0, 20, 1)
if limiter_config := config.get(CONF_LIMITER):
await new_number(p, limiter_config, CONF_RELEASE_TIME, p.set_limiter_release_time_number, 0.25, 102.4, 0.01, mode=number.NumberMode.NUMBER_MODE_SLIDER)
if asq_config := config.get(CONF_ASQ):
await new_number(p, asq_config, CONF_LEVEL_LOW, p.set_asq_level_low_number, -70, 0, 1)
await new_number(p, asq_config, CONF_DURATION_LOW, p.set_asq_duration_low_number, 0, 65535, 1)
await new_number(p, asq_config, CONF_LEVEL_HIGH, p.set_asq_level_high_number, -70, 0, 1)
await new_number(p, asq_config, CONF_DURATION_HIGH, p.set_asq_duration_high_number, 0, 65535, 1)
if rds_config := config.get(CONF_RDS):
await new_number(p, rds_config, CONF_DEVIATION, p.set_rds_deviation_number, 0, 7.5, 0.01)

View file

@ -1,12 +0,0 @@
#include "antcap_number.h"
namespace esphome {
namespace si4713 {
void AntcapNumber::control(float value) {
this->publish_state(value);
this->parent_->set_antcap(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -1,12 +0,0 @@
#include "audio_deviation_number.h"
namespace esphome {
namespace si4713 {
void AudioDeviationNumber::control(float value) {
this->publish_state(value);
this->parent_->set_audio_deviation(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -1,12 +0,0 @@
#include "frequency_number.h"
namespace esphome {
namespace si4713 {
void FrequencyNumber::control(float value) {
this->publish_state(value);
this->parent_->set_frequency(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -1,12 +0,0 @@
#include "power_number.h"
namespace esphome {
namespace si4713 {
void PowerNumber::control(float value) {
this->publish_state(value);
this->parent_->set_power(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "tuner_antcap_number.h"
namespace esphome {
namespace si4713 {
void TunerAntcapNumber::control(float value) {
this->publish_state(value);
this->parent_->set_tuner_antcap(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class PowerNumber : public number::Number, public Parented<Si4713Component> { class TunerAntcapNumber : public number::Number, public Parented<Si4713Component> {
public: public:
PowerNumber() = default; TunerAntcapNumber() = default;
protected: protected:
void control(float value) override; void control(float value) override;

View file

@ -0,0 +1,12 @@
#include "tuner_deviation_number.h"
namespace esphome {
namespace si4713 {
void TunerDeviationNumber::control(float value) {
this->publish_state(value);
this->parent_->set_tuner_deviation(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class AudioDeviationNumber : public number::Number, public Parented<Si4713Component> { class TunerDeviationNumber : public number::Number, public Parented<Si4713Component> {
public: public:
AudioDeviationNumber() = default; TunerDeviationNumber() = default;
protected: protected:
void control(float value) override; void control(float value) override;

View file

@ -0,0 +1,12 @@
#include "tuner_frequency_number.h"
namespace esphome {
namespace si4713 {
void TunerFrequencyNumber::control(float value) {
this->publish_state(value);
this->parent_->set_tuner_frequency(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class FrequencyNumber : public number::Number, public Parented<Si4713Component> { class TunerFrequencyNumber : public number::Number, public Parented<Si4713Component> {
public: public:
FrequencyNumber() = default; TunerFrequencyNumber() = default;
protected: protected:
void control(float value) override; void control(float value) override;

View file

@ -0,0 +1,12 @@
#include "tuner_power_number.h"
namespace esphome {
namespace si4713 {
void TunerPowerNumber::control(float value) {
this->publish_state(value);
this->parent_->set_tuner_power(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class AntcapNumber : public number::Number, public Parented<Si4713Component> { class TunerPowerNumber : public number::Number, public Parented<Si4713Component> {
public: public:
AntcapNumber() = default; TunerPowerNumber() = default;
protected: protected:
void control(float value) override; void control(float value) override;

View file

@ -13,7 +13,7 @@ void BinaryOutput::dump_config() {
} }
void BinaryOutput::write_state(bool state) { void BinaryOutput::write_state(bool state) {
this->parent_->set_gpio(this->pin_, state); this->parent_->set_output_gpio(this->pin_, state);
} }
} // namespace si4713 } // namespace si4713

View file

@ -17,7 +17,7 @@ from .. import (
si4713_ns, si4713_ns,
CONF_DIGITAL, CONF_DIGITAL,
CONF_REFCLK, CONF_REFCLK,
CONF_COMPRESSOR, CONF_ACOMP,
CONF_PRE_EMPHASIS, CONF_PRE_EMPHASIS,
CONF_SAMPLE_BITS, CONF_SAMPLE_BITS,
CONF_CLOCK_EDGE, CONF_CLOCK_EDGE,
@ -36,6 +36,7 @@ from .. import (
ACOMP_ATTACK, ACOMP_ATTACK,
ACOMP_RELEASE, ACOMP_RELEASE,
ACOMP_PRESET, ACOMP_PRESET,
for_each_conf,
) )
PreEmphasisSelect = si4713_ns.class_("PreEmphasisSelect", select.Select) PreEmphasisSelect = si4713_ns.class_("PreEmphasisSelect", select.Select)
@ -99,7 +100,7 @@ CONFIG_SCHEMA = cv.Schema(
), ),
} }
), ),
cv.Optional(CONF_COMPRESSOR): cv.Schema( cv.Optional(CONF_ACOMP): cv.Schema(
{ {
cv.Optional(CONF_ATTACK): select.select_schema( cv.Optional(CONF_ATTACK): select.select_schema(
AcompAttackSelect, AcompAttackSelect,
@ -121,28 +122,36 @@ CONFIG_SCHEMA = cv.Schema(
} }
) )
VARIABLES = {
async def new_select(p, config, id, setter, options): None: [
if c := config.get(id): [CONF_PRE_EMPHASIS, PRE_EMPHASIS],
s = await select.new_select(c, options=list(options.keys())) ],
await cg.register_parented(s, p) CONF_ANALOG: [
cg.add(setter(s)) [CONF_ATTENUATION, LINE_ATTENUATION],
return s ],
CONF_DIGITAL: [
[CONF_SAMPLE_BITS, SAMPLE_BITS],
[CONF_CHANNELS, SAMPLE_CHANNELS],
[CONF_MODE, DIGITAL_MODE],
[CONF_CLOCK_EDGE, DIGITAL_CLOCK_EDGE],
],
CONF_REFCLK: [
[CONF_SOURCE, REFCLK_SOURCE],
],
CONF_ACOMP: [
[CONF_ATTACK, ACOMP_ATTACK],
[CONF_RELEASE, ACOMP_RELEASE],
[CONF_PRESET, ACOMP_PRESET],
],
}
async def to_code(config): async def to_code(config):
p = await cg.get_variable(config[CONF_SI4713_ID]) parent = await cg.get_variable(config[CONF_SI4713_ID])
await new_select(p, config, CONF_PRE_EMPHASIS, p.set_pre_emphasis_select, PRE_EMPHASIS)
if analog_config := config.get(CONF_ANALOG): async def new_select(c, args, setter):
await new_select(p, analog_config, CONF_ATTENUATION, p.set_analog_attenuation_select, LINE_ATTENUATION) s = await select.new_select(c, options=list(args[1].keys()))
if digital_config := config.get(CONF_DIGITAL): await cg.register_parented(s, parent)
await new_select(p, digital_config, CONF_SAMPLE_BITS, p.set_digital_sample_bits_select, SAMPLE_BITS) cg.add(getattr(parent, setter + "_select")(s))
await new_select(p, digital_config, CONF_CHANNELS, p.set_digital_channels_select, SAMPLE_CHANNELS)
await new_select(p, digital_config, CONF_MODE, p.set_digital_mode_select, DIGITAL_MODE) await for_each_conf(config, VARIABLES, new_select)
await new_select(p, digital_config, CONF_CLOCK_EDGE, p.set_digital_clock_edge_select, DIGITAL_CLOCK_EDGE)
if refclk_config := config.get(CONF_REFCLK):
await new_select(p, refclk_config, CONF_SOURCE, p.set_refclk_source_select, REFCLK_SOURCE)
if compressor_config := config.get(CONF_COMPRESSOR):
await new_select(p, compressor_config, CONF_ATTACK, p.set_acomp_attack_select, ACOMP_ATTACK)
await new_select(p, compressor_config, CONF_RELEASE, p.set_acomp_release_select, ACOMP_RELEASE)
await new_select(p, compressor_config, CONF_PRESET, p.set_acomp_preset_select, ACOMP_PRESET)

View file

@ -219,11 +219,11 @@ void Si4713Component::setup() {
this->publish_mute(); this->publish_mute();
this->publish_mono(); this->publish_mono();
this->publish_pre_emphasis(); this->publish_pre_emphasis();
this->publish_power_enable(); this->publish_tuner_enable();
this->publish_frequency(); this->publish_tuner_frequency();
this->publish_audio_deviation(); this->publish_tuner_deviation();
this->publish_power(); this->publish_tuner_power();
this->publish_antcap(); this->publish_tuner_antcap();
this->publish_analog_level(); this->publish_analog_level();
this->publish_analog_attenuation(); this->publish_analog_attenuation();
this->publish_digital_sample_rate(); this->publish_digital_sample_rate();
@ -245,9 +245,9 @@ void Si4713Component::setup() {
this->publish_acomp_preset(); this->publish_acomp_preset();
this->publish_limiter_enable(); this->publish_limiter_enable();
this->publish_limiter_release_time(); this->publish_limiter_release_time();
this->publish_asq_iall_enable(); this->publish_asq_iall();
this->publish_asq_ialh_enable(); this->publish_asq_ialh();
this->publish_asq_overmod_enable(); this->publish_asq_overmod();
this->publish_asq_level_low(); this->publish_asq_level_low();
this->publish_asq_duration_low(); this->publish_asq_duration_low();
this->publish_asq_level_high(); this->publish_asq_level_high();
@ -256,18 +256,18 @@ void Si4713Component::setup() {
this->publish_rds_deviation(); this->publish_rds_deviation();
this->publish_rds_station(); this->publish_rds_station();
this->publish_rds_text(); this->publish_rds_text();
this->publish_gpio1(); this->publish_output_gpio1();
this->publish_gpio2(); this->publish_output_gpio2();
this->publish_gpio3(); this->publish_output_gpio3();
this->publish_chip_id(); this->publish_chip_id_text_sensor();
this->publish_read_frequency(); this->publish_frequency_sensor();
this->publish_read_power(); this->publish_power_sensor();
this->publish_read_antcap(); this->publish_antcap_sensor();
this->publish_read_noise_level(); this->publish_noise_level_sensor();
this->publish_iall(); this->publish_iall_binary_sensor();
this->publish_ialh(); this->publish_ialh_binary_sensor();
this->publish_overmod(); this->publish_overmod_binary_sensor();
this->publish_inlevel(); this->publish_inlevel_sensor();
this->set_interval(1000, [this]() { this->rds_update_(); }); this->set_interval(1000, [this]() { this->rds_update_(); });
} }
@ -279,7 +279,7 @@ void Si4713Component::dump_config() {
ESP_LOGE(TAG, "failed!"); ESP_LOGE(TAG, "failed!");
} }
ESP_LOGCONFIG(TAG, " Chip: %s", this->chip_id_.c_str()); ESP_LOGCONFIG(TAG, " Chip: %s", this->chip_id_.c_str());
ESP_LOGCONFIG(TAG, " Frequency: %.2f MHz", this->get_frequency()); ESP_LOGCONFIG(TAG, " Frequency: %.2f MHz", this->get_tuner_frequency());
ESP_LOGCONFIG(TAG, " RDS station: %s", this->rds_station_.c_str()); ESP_LOGCONFIG(TAG, " RDS station: %s", this->rds_station_.c_str());
ESP_LOGCONFIG(TAG, " RDS text: %s", this->rds_text_.c_str()); ESP_LOGCONFIG(TAG, " RDS text: %s", this->rds_text_.c_str());
// TODO: ...and everything else... // TODO: ...and everything else...
@ -288,8 +288,8 @@ void Si4713Component::dump_config() {
void Si4713Component::update() { void Si4713Component::update() {
// these might be changing too fast for loop() // these might be changing too fast for loop()
this->publish_read_noise_level(); this->publish_noise_level_sensor();
this->publish_inlevel(); this->publish_inlevel_sensor();
} }
void Si4713Component::loop() { void Si4713Component::loop() {
@ -301,10 +301,10 @@ void Si4713Component::loop() {
float f = (float) ((this->tune_status_.READFREQH << 8) | this->tune_status_.READFREQL) / 100; float f = (float) ((this->tune_status_.READFREQH << 8) | this->tune_status_.READFREQL) / 100;
ESP_LOGD(TAG, "ResTxTuneStatus FREQ %.2f RFdBuV %d ANTCAP %d NL %d", f, this->tune_status_.READRFdBuV, ESP_LOGD(TAG, "ResTxTuneStatus FREQ %.2f RFdBuV %d ANTCAP %d NL %d", f, this->tune_status_.READRFdBuV,
this->tune_status_.READANTCAP, this->tune_status_.RNL); this->tune_status_.READANTCAP, this->tune_status_.RNL);
this->publish_read_frequency(); this->publish_frequency_sensor();
this->publish_read_power(); this->publish_power_sensor();
this->publish_read_antcap(); this->publish_antcap_sensor();
// this->publish_read_noise_level(); // this->publish_noise_level_sensor();
} }
} }
if (res.ASQINT == 1) { if (res.ASQINT == 1) {
@ -312,10 +312,10 @@ void Si4713Component::loop() {
// ESP_LOGD(TAG, "ResTxAsqStatus IALL %d IALH %d OVERMOD %d INLEVEL %d", // ESP_LOGD(TAG, "ResTxAsqStatus IALL %d IALH %d OVERMOD %d INLEVEL %d",
// this->asq_status_.IALL, this->asq_status_.IALH, this->asq_status_.OVERMOD, // this->asq_status_.IALL, this->asq_status_.IALH, this->asq_status_.OVERMOD,
// this->asq_status_.INLEVEL); // this->asq_status_.INLEVEL);
this->publish_iall(); this->publish_iall_binary_sensor();
this->publish_ialh(); this->publish_ialh_binary_sensor();
this->publish_overmod(); this->publish_overmod_binary_sensor();
// this->publish_inlevel(); // this->publish_inlevel_sensor();
} }
} }
// TODO: if (res.RDSINT == 1) {} // TODO: if (res.RDSINT == 1) {}
@ -386,50 +386,50 @@ void Si4713Component::set_pre_emphasis(PreEmphasis value) {
PreEmphasis Si4713Component::get_pre_emphasis() { return (PreEmphasis) tx_pre_emphasis_.FMPE; } PreEmphasis Si4713Component::get_pre_emphasis() { return (PreEmphasis) tx_pre_emphasis_.FMPE; }
void Si4713Component::set_power_enable(bool value) { void Si4713Component::set_tuner_enable(bool value) {
this->power_enable_ = value; this->power_enable_ = value;
this->tune_power(this->power_, this->antcap_); this->tune_power(this->power_, this->antcap_);
// this->set_prop(this->tx_component_enable_); // this->set_prop(this->tx_component_enable_);
this->publish_power_enable(); this->publish_tuner_enable();
} }
bool Si4713Component::get_power_enable() { return this->power_enable_; } bool Si4713Component::get_tuner_enable() { return this->power_enable_; }
void Si4713Component::set_frequency(float value) { void Si4713Component::set_tuner_frequency(float value) {
CHECK_FLOAT_RANGE(value, FREQ_MIN, FREQ_MAX) CHECK_FLOAT_RANGE(value, FREQ_MIN, FREQ_MAX)
this->frequency_ = (uint16_t) clamp((int) std::lround(value * 20) * 5, FREQ_RAW_MIN, FREQ_RAW_MAX); this->frequency_ = (uint16_t) clamp((int) std::lround(value * 20) * 5, FREQ_RAW_MIN, FREQ_RAW_MAX);
this->tune_freq(this->frequency_); this->tune_freq(this->frequency_);
this->publish_frequency(); this->publish_tuner_frequency();
} }
float Si4713Component::get_frequency() { return (float) this->frequency_ / 100; } float Si4713Component::get_tuner_frequency() { return (float) this->frequency_ / 100; }
void Si4713Component::set_audio_deviation(float value) { void Si4713Component::set_tuner_deviation(float value) {
CHECK_FLOAT_RANGE(value, TXADEV_MIN, TXADEV_MAX) CHECK_FLOAT_RANGE(value, TXADEV_MIN, TXADEV_MAX)
this->tx_audio_deviation_.TXADEV = (uint16_t) clamp((int) std::lround(value * 100), TXADEV_RAW_MIN, TXADEV_RAW_MAX); this->tx_audio_deviation_.TXADEV = (uint16_t) clamp((int) std::lround(value * 100), TXADEV_RAW_MIN, TXADEV_RAW_MAX);
this->set_prop(this->tx_audio_deviation_); this->set_prop(this->tx_audio_deviation_);
this->publish_audio_deviation(); this->publish_tuner_deviation();
} }
float Si4713Component::get_audio_deviation() { return (float) this->tx_audio_deviation_.TXADEV / 100; } float Si4713Component::get_tuner_deviation() { return (float) this->tx_audio_deviation_.TXADEV / 100; }
void Si4713Component::set_power(int value) { void Si4713Component::set_tuner_power(int value) {
CHECK_INT_RANGE(value, POWER_MIN, POWER_MAX) CHECK_INT_RANGE(value, POWER_MIN, POWER_MAX)
this->power_ = (uint8_t) value; this->power_ = (uint8_t) value;
this->tune_power(this->power_, this->antcap_); this->tune_power(this->power_, this->antcap_);
this->publish_power(); this->publish_tuner_power();
} }
int Si4713Component::get_power() { return (int) this->power_; } int Si4713Component::get_tuner_power() { return (int) this->power_; }
void Si4713Component::set_antcap(float value) { void Si4713Component::set_tuner_antcap(float value) {
CHECK_FLOAT_RANGE(value, ANTCAP_MIN, ANTCAP_MAX) CHECK_FLOAT_RANGE(value, ANTCAP_MIN, ANTCAP_MAX)
this->antcap_ = (uint8_t) lround(value * 4); this->antcap_ = (uint8_t) lround(value * 4);
this->tune_power(this->power_, this->antcap_); this->tune_power(this->power_, this->antcap_);
this->publish_antcap(); this->publish_tuner_antcap();
} }
float Si4713Component::get_antcap() { return (float) this->antcap_ * 0.25f; } float Si4713Component::get_tuner_antcap() { return (float) this->antcap_ * 0.25f; }
void Si4713Component::set_analog_level(int value) { void Si4713Component::set_analog_level(int value) {
CHECK_INT_RANGE(value, LILEVEL_MIN, LILEVEL_MAX) CHECK_INT_RANGE(value, LILEVEL_MIN, LILEVEL_MAX)
@ -634,29 +634,29 @@ void Si4713Component::set_limiter_release_time(float value) {
float Si4713Component::get_limiter_release_time() { return (float) 512.0f / this->tx_limiter_releasee_time_.LMITERTC; } float Si4713Component::get_limiter_release_time() { return (float) 512.0f / this->tx_limiter_releasee_time_.LMITERTC; }
void Si4713Component::set_asq_iall_enable(bool value) { void Si4713Component::set_asq_iall(bool value) {
this->tx_asq_interrupt_source_.IALLIEN = value ? 1 : 0; this->tx_asq_interrupt_source_.IALLIEN = value ? 1 : 0;
this->set_prop(this->tx_asq_interrupt_source_); this->set_prop(this->tx_asq_interrupt_source_);
this->publish_asq_iall_enable(); this->publish_asq_iall();
} }
bool Si4713Component::get_asq_iall_enable() { return this->tx_asq_interrupt_source_.IALLIEN != 0; } bool Si4713Component::get_asq_iall() { return this->tx_asq_interrupt_source_.IALLIEN != 0; }
void Si4713Component::set_asq_ialh_enable(bool value) { void Si4713Component::set_asq_ialh(bool value) {
this->tx_asq_interrupt_source_.IALHIEN = value ? 1 : 0; this->tx_asq_interrupt_source_.IALHIEN = value ? 1 : 0;
this->set_prop(this->tx_asq_interrupt_source_); this->set_prop(this->tx_asq_interrupt_source_);
this->publish_asq_ialh_enable(); this->publish_asq_ialh();
} }
bool Si4713Component::get_asq_ialh_enable() { return this->tx_asq_interrupt_source_.IALHIEN != 0; } bool Si4713Component::get_asq_ialh() { return this->tx_asq_interrupt_source_.IALHIEN != 0; }
void Si4713Component::set_asq_overmod_enable(bool value) { void Si4713Component::set_asq_overmod(bool value) {
this->tx_asq_interrupt_source_.OVERMODIEN = value ? 1 : 0; this->tx_asq_interrupt_source_.OVERMODIEN = value ? 1 : 0;
this->set_prop(this->tx_asq_interrupt_source_); this->set_prop(this->tx_asq_interrupt_source_);
this->publish_asq_overmod_enable(); this->publish_asq_overmod();
} }
bool Si4713Component::get_asq_overmod_enable() { return this->tx_asq_interrupt_source_.OVERMODIEN != 0; } bool Si4713Component::get_asq_overmod() { return this->tx_asq_interrupt_source_.OVERMODIEN != 0; }
void Si4713Component::set_asq_level_low(int value) { void Si4713Component::set_asq_level_low(int value) {
CHECK_INT_RANGE(value, IALTH_MIN, IALTH_MAX) CHECK_INT_RANGE(value, IALTH_MIN, IALTH_MAX)
@ -728,17 +728,17 @@ void Si4713Component::set_rds_text(const std::string &value) {
std::string Si4713Component::get_rds_text() { return this->rds_text_; } std::string Si4713Component::get_rds_text() { return this->rds_text_; }
void Si4713Component::set_gpio(uint8_t pin, bool value) { void Si4713Component::set_output_gpio(uint8_t pin, bool value) {
if (pin > 2) { if (pin > 2) {
ESP_LOGE(TAG, "%s(%d, %d) invalid pin number (0 to 2)", __func__, pin, value); ESP_LOGE(TAG, "%s(%d, %d) invalid pin number (0 to 2)", __func__, pin, value);
return; return;
} }
this->gpio_[pin] = value ? 1 : 0; this->gpio_[pin] = value ? 1 : 0;
this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2])); this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2]));
this->publish_gpio(pin); this->publish_output_gpio(pin);
} }
bool Si4713Component::get_gpio(uint8_t pin) { bool Si4713Component::get_output_gpio(uint8_t pin) {
if (pin > 2) { if (pin > 2) {
ESP_LOGE(TAG, "%s(%d) invalid pin number (0 to 2)", __func__, pin); ESP_LOGE(TAG, "%s(%d) invalid pin number (0 to 2)", __func__, pin);
return false; return false;
@ -746,62 +746,62 @@ bool Si4713Component::get_gpio(uint8_t pin) {
return this->gpio_[pin] != 0; return this->gpio_[pin] != 0;
} }
void Si4713Component::set_gpio1(bool value) { void Si4713Component::set_output_gpio1(bool value) {
this->gpio_[0] = value ? 1 : 0; this->gpio_[0] = value ? 1 : 0;
this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2])); this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2]));
this->publish_gpio1(); this->publish_output_gpio1();
} }
bool Si4713Component::get_gpio1() { return this->gpio_[0] != 0; } bool Si4713Component::get_output_gpio1() { return this->gpio_[0] != 0; }
void Si4713Component::set_gpio2(bool value) { void Si4713Component::set_output_gpio2(bool value) {
this->gpio_[1] = value ? 1 : 0; this->gpio_[1] = value ? 1 : 0;
this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2])); this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2]));
this->publish_gpio2(); this->publish_output_gpio2();
} }
bool Si4713Component::get_gpio2() { return this->gpio_[1] != 0; } bool Si4713Component::get_output_gpio2() { return this->gpio_[1] != 0; }
void Si4713Component::set_gpio3(bool value) { void Si4713Component::set_output_gpio3(bool value) {
this->gpio_[2] = value ? 1 : 0; this->gpio_[2] = value ? 1 : 0;
this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2])); this->send_cmd(CmdGpioSet(this->gpio_[0], this->gpio_[1], this->gpio_[2]));
this->publish_gpio2(); this->publish_output_gpio2();
} }
bool Si4713Component::get_gpio3() { return this->gpio_[2] != 0; } bool Si4713Component::get_output_gpio3() { return this->gpio_[2] != 0; }
std::string Si4713Component::get_chip_id() { return this->chip_id_; } std::string Si4713Component::get_chip_id_text_sensor() { return this->chip_id_; }
float Si4713Component::get_read_frequency() { float Si4713Component::get_frequency_sensor() {
return (float) ((this->tune_status_.READFREQH << 8) | this->tune_status_.READFREQL) / 100; return (float) ((this->tune_status_.READFREQH << 8) | this->tune_status_.READFREQL) / 100;
} }
float Si4713Component::get_read_power() { return (float) this->tune_status_.READRFdBuV; } float Si4713Component::get_power_sensor() { return (float) this->tune_status_.READRFdBuV; }
float Si4713Component::get_read_antcap() { return (float) this->tune_status_.READANTCAP; } float Si4713Component::get_antcap_sensor() { return (float) this->tune_status_.READANTCAP; }
float Si4713Component::get_read_noise_level() { return (float) this->tune_status_.RNL; } float Si4713Component::get_noise_level_sensor() { return (float) this->tune_status_.RNL; }
bool Si4713Component::get_iall() { return this->asq_status_.IALL != 0; } bool Si4713Component::get_iall_binary_sensor() { return this->asq_status_.IALL != 0; }
bool Si4713Component::get_ialh() { return this->asq_status_.IALH != 0; } bool Si4713Component::get_ialh_binary_sensor() { return this->asq_status_.IALH != 0; }
bool Si4713Component::get_overmod() { return this->asq_status_.OVERMOD != 0; } bool Si4713Component::get_overmod_binary_sensor() { return this->asq_status_.OVERMOD != 0; }
float Si4713Component::get_inlevel() { return (float) this->asq_status_.INLEVEL; } float Si4713Component::get_inlevel_sensor() { return (float) this->asq_status_.INLEVEL; }
// publish // publish
void Si4713Component::publish_gpio(uint8_t pin) { void Si4713Component::publish_output_gpio(uint8_t pin) {
switch (pin) { switch (pin) {
case 0: case 0:
this->publish_switch(this->gpio1_switch_, this->gpio_[pin] != 0); this->publish_switch(this->output_gpio1_switch_, this->gpio_[pin] != 0);
break; break;
case 1: case 1:
this->publish_switch(this->gpio2_switch_, this->gpio_[pin] != 0); this->publish_switch(this->output_gpio2_switch_, this->gpio_[pin] != 0);
break; break;
case 2: case 2:
this->publish_switch(this->gpio3_switch_, this->gpio_[pin] != 0); this->publish_switch(this->output_gpio3_switch_, this->gpio_[pin] != 0);
break; break;
default: default:
return; return;

View file

@ -52,18 +52,20 @@ namespace si4713 {
#define SUB_SENSOR_EX(name) \ #define SUB_SENSOR_EX(name) \
SUB_SENSOR(name) \ SUB_SENSOR(name) \
void publish_##name() { this->publish(this->name##_sensor_, (float) this->get_##name()); } \ void publish_##name##_sensor() { this->publish(this->name##_sensor_, (float) this->get_##name##_sensor()); } \
float get_##name(); float get_##name##_sensor();
#define SUB_BINARY_SENSOR_EX(name) \ #define SUB_BINARY_SENSOR_EX(name) \
SUB_BINARY_SENSOR(name) \ SUB_BINARY_SENSOR(name) \
void publish_##name() { this->publish(this->name##_binary_sensor_, this->get_##name()); } \ void publish_##name##_binary_sensor() { \
bool get_##name(); this->publish(this->name##_binary_sensor_, this->get_##name##_binary_sensor()); \
} \
bool get_##name##_binary_sensor();
#define SUB_TEXT_SENSOR_EX(name) \ #define SUB_TEXT_SENSOR_EX(name) \
SUB_TEXT_SENSOR(name) \ SUB_TEXT_SENSOR(name) \
void publish_##name() { this->publish(this->name##_text_sensor_, this->get_##name()); } \ void publish_##name##_text_sensor() { this->publish(this->name##_text_sensor_, this->get_##name##_text_sensor()); } \
std::string get_##name(); std::string get_##name##_text_sensor();
class Si4713Component : public PollingComponent, public i2c::I2CDevice { class Si4713Component : public PollingComponent, public i2c::I2CDevice {
std::string chip_id_; std::string chip_id_;
@ -161,11 +163,11 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
SUB_SWITCH_EX(mute) SUB_SWITCH_EX(mute)
SUB_SWITCH_EX(mono) SUB_SWITCH_EX(mono)
SUB_SELECT_EX(pre_emphasis, PreEmphasis) SUB_SELECT_EX(pre_emphasis, PreEmphasis)
SUB_SWITCH_EX(power_enable) SUB_SWITCH_EX(tuner_enable)
SUB_NUMBER_EX(frequency, float) SUB_NUMBER_EX(tuner_frequency, float)
SUB_NUMBER_EX(audio_deviation, float) SUB_NUMBER_EX(tuner_deviation, float)
SUB_NUMBER_EX(power, int) SUB_NUMBER_EX(tuner_power, int)
SUB_NUMBER_EX(antcap, float) SUB_NUMBER_EX(tuner_antcap, float)
SUB_NUMBER_EX(analog_level, int) SUB_NUMBER_EX(analog_level, int)
SUB_SELECT_EX(analog_attenuation, LineAttenuation) SUB_SELECT_EX(analog_attenuation, LineAttenuation)
SUB_NUMBER_EX(digital_sample_rate, int) SUB_NUMBER_EX(digital_sample_rate, int)
@ -187,9 +189,9 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
SUB_SELECT_EX(acomp_preset, AcompPreset) SUB_SELECT_EX(acomp_preset, AcompPreset)
SUB_SWITCH_EX(limiter_enable) SUB_SWITCH_EX(limiter_enable)
SUB_NUMBER_EX(limiter_release_time, float) SUB_NUMBER_EX(limiter_release_time, float)
SUB_SWITCH_EX(asq_iall_enable) SUB_SWITCH_EX(asq_iall)
SUB_SWITCH_EX(asq_ialh_enable) SUB_SWITCH_EX(asq_ialh)
SUB_SWITCH_EX(asq_overmod_enable) SUB_SWITCH_EX(asq_overmod)
SUB_NUMBER_EX(asq_level_low, int) SUB_NUMBER_EX(asq_level_low, int)
SUB_NUMBER_EX(asq_duration_low, int) SUB_NUMBER_EX(asq_duration_low, int)
SUB_NUMBER_EX(asq_level_high, int) SUB_NUMBER_EX(asq_level_high, int)
@ -198,23 +200,23 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
SUB_NUMBER_EX(rds_deviation, float) SUB_NUMBER_EX(rds_deviation, float)
SUB_TEXT_EX(rds_station) SUB_TEXT_EX(rds_station)
SUB_TEXT_EX(rds_text) SUB_TEXT_EX(rds_text)
SUB_SWITCH_EX(gpio1) SUB_SWITCH_EX(output_gpio1)
SUB_SWITCH_EX(gpio2) SUB_SWITCH_EX(output_gpio2)
SUB_SWITCH_EX(gpio3) SUB_SWITCH_EX(output_gpio3)
SUB_TEXT_SENSOR_EX(chip_id) SUB_TEXT_SENSOR_EX(chip_id)
SUB_SENSOR_EX(read_frequency) SUB_SENSOR_EX(frequency)
SUB_SENSOR_EX(read_power) SUB_SENSOR_EX(power)
SUB_SENSOR_EX(read_antcap) SUB_SENSOR_EX(antcap)
SUB_SENSOR_EX(read_noise_level) SUB_SENSOR_EX(noise_level)
SUB_BINARY_SENSOR_EX(iall) SUB_BINARY_SENSOR_EX(iall)
SUB_BINARY_SENSOR_EX(ialh) SUB_BINARY_SENSOR_EX(ialh)
SUB_BINARY_SENSOR_EX(overmod) SUB_BINARY_SENSOR_EX(overmod)
SUB_SENSOR_EX(inlevel) SUB_SENSOR_EX(inlevel)
// helper // helper
void publish_gpio(uint8_t pin); void publish_output_gpio(uint8_t pin);
void set_gpio(uint8_t pin, bool value); void set_output_gpio(uint8_t pin, bool value);
bool get_gpio(uint8_t pin); bool get_output_gpio(uint8_t pin);
// used by automation // used by automation
void measure_freq(float value); void measure_freq(float value);
@ -222,7 +224,7 @@ class Si4713Component : public PollingComponent, public i2c::I2CDevice {
template<typename... Ts> class SetFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> { template<typename... Ts> class SetFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> {
TEMPLATABLE_VALUE(float, frequency) TEMPLATABLE_VALUE(float, frequency)
void play(Ts... x) override { this->parent_->set_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 MeasureFrequencyAction : public Action<Ts...>, public Parented<Si4713Component> {

View file

@ -12,7 +12,7 @@ from .. import (
si4713_ns, si4713_ns,
CONF_TUNER, CONF_TUNER,
CONF_PILOT, CONF_PILOT,
CONF_COMPRESSOR, CONF_ACOMP,
CONF_LIMITER, CONF_LIMITER,
CONF_ASQ, CONF_ASQ,
CONF_RDS, CONF_RDS,
@ -30,25 +30,26 @@ from .. import (
ICON_VOLUME_MUTE, ICON_VOLUME_MUTE,
ICON_EAR_HEARING, ICON_EAR_HEARING,
ICON_FORMAT_TEXT, ICON_FORMAT_TEXT,
for_each_conf,
) )
MuteSwitch = si4713_ns.class_("MuteSwitch", switch.Switch) MuteSwitch = si4713_ns.class_("MuteSwitch", switch.Switch)
MonoSwitch = si4713_ns.class_("MonoSwitch", switch.Switch) MonoSwitch = si4713_ns.class_("MonoSwitch", switch.Switch)
PowerEnableSwitch = si4713_ns.class_("PowerEnableSwitch", switch.Switch) TunerEnableSwitch = si4713_ns.class_("TunerEnableSwitch", switch.Switch)
PilotEnableSwitch = si4713_ns.class_("PilotEnableSwitch", switch.Switch) PilotEnableSwitch = si4713_ns.class_("PilotEnableSwitch", switch.Switch)
AcompEnableSwitch = si4713_ns.class_("AcompEnableSwitch", switch.Switch) AcompEnableSwitch = si4713_ns.class_("AcompEnableSwitch", switch.Switch)
LimiterEnableSwitch = si4713_ns.class_("LimiterEnableSwitch", switch.Switch) LimiterEnableSwitch = si4713_ns.class_("LimiterEnableSwitch", switch.Switch)
AsqOvermodEnableSwitch = si4713_ns.class_("AsqOvermodEnableSwitch", switch.Switch) AsqIallSwitch = si4713_ns.class_("AsqIallSwitch", switch.Switch)
AsqIallEnableSwitch = si4713_ns.class_("AsqIallEnableSwitch", switch.Switch) AsqIalhSwitch = si4713_ns.class_("AsqIalhSwitch", switch.Switch)
AsqIalhEnableSwitch = si4713_ns.class_("AsqIalhEnableSwitch", switch.Switch) AsqOvermodSwitch = si4713_ns.class_("AsqOvermodSwitch", switch.Switch)
RdsEnable = si4713_ns.class_("RdsEnable", switch.Switch) RdsEnable = si4713_ns.class_("RdsEnable", switch.Switch)
RDSEnableSwitch = si4713_ns.class_("RDSEnableSwitch", switch.Switch) RDSEnableSwitch = si4713_ns.class_("RDSEnableSwitch", switch.Switch)
GPIOSwitch = si4713_ns.class_("GPIOSwitch", switch.Switch) OutputGpioSwitch = si4713_ns.class_("OutputGpioSwitch", switch.Switch)
TUNER_SCHEMA = cv.Schema( TUNER_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_ENABLE): switch.switch_schema( cv.Optional(CONF_ENABLE): switch.switch_schema(
PowerEnableSwitch, TunerEnableSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RADIO_TOWER, icon=ICON_RADIO_TOWER,
@ -68,7 +69,7 @@ PILOT_SCHEMA = cv.Schema(
) )
COMPRESSOR_SCHEMA = cv.Schema( ACOMP_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_ENABLE): switch.switch_schema( cv.Optional(CONF_ENABLE): switch.switch_schema(
AcompEnableSwitch, AcompEnableSwitch,
@ -92,20 +93,20 @@ LIMITER_SCHEMA = cv.Schema(
ASQ_SCHEMA = cv.Schema( ASQ_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_OVERMOD): switch.switch_schema(
AsqOvermodEnableSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
# icon=ICON_,
),
cv.Optional(CONF_IALL): switch.switch_schema( cv.Optional(CONF_IALL): switch.switch_schema(
AsqIallEnableSwitch, AsqIallSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
# icon=ICON_, # icon=ICON_,
), ),
cv.Optional(CONF_IALH): switch.switch_schema( cv.Optional(CONF_IALH): switch.switch_schema(
AsqIalhEnableSwitch, AsqIalhSwitch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
# icon=ICON_,
),
cv.Optional(CONF_OVERMOD): switch.switch_schema(
AsqOvermodSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
# icon=ICON_, # icon=ICON_,
@ -127,19 +128,19 @@ RDS_SCHEMA = cv.Schema(
OUTPUT_SCHEMA = cv.Schema( OUTPUT_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_GPIO1): switch.switch_schema( cv.Optional(CONF_GPIO1): switch.switch_schema(
GPIOSwitch, OutputGpioSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
# icon=, # icon=,
), ),
cv.Optional(CONF_GPIO2): switch.switch_schema( cv.Optional(CONF_GPIO2): switch.switch_schema(
GPIOSwitch, OutputGpioSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
# icon=, # icon=,
), ),
cv.Optional(CONF_GPIO3): switch.switch_schema( cv.Optional(CONF_GPIO3): switch.switch_schema(
GPIOSwitch, OutputGpioSwitch,
device_class=DEVICE_CLASS_SWITCH, device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG, entity_category=ENTITY_CATEGORY_CONFIG,
# icon=, # icon=,
@ -163,7 +164,7 @@ CONFIG_SCHEMA = cv.Schema(
), ),
cv.Optional(CONF_TUNER): TUNER_SCHEMA, cv.Optional(CONF_TUNER): TUNER_SCHEMA,
cv.Optional(CONF_PILOT): PILOT_SCHEMA, cv.Optional(CONF_PILOT): PILOT_SCHEMA,
cv.Optional(CONF_COMPRESSOR): COMPRESSOR_SCHEMA, cv.Optional(CONF_ACOMP): ACOMP_SCHEMA,
cv.Optional(CONF_LIMITER): LIMITER_SCHEMA, cv.Optional(CONF_LIMITER): LIMITER_SCHEMA,
cv.Optional(CONF_ASQ): ASQ_SCHEMA, cv.Optional(CONF_ASQ): ASQ_SCHEMA,
cv.Optional(CONF_RDS): RDS_SCHEMA, cv.Optional(CONF_RDS): RDS_SCHEMA,
@ -171,37 +172,47 @@ CONFIG_SCHEMA = cv.Schema(
} }
) )
VARIABLES = {
async def new_switch(p, config, id, setter): None: [
if c := config.get(id): [CONF_MUTE, None],
s = await switch.new_switch(c) [CONF_MONO, None],
await cg.register_parented(s, p) ],
cg.add(setter(s)) CONF_TUNER: [
return s [CONF_ENABLE, None],
],
CONF_PILOT: [
[CONF_ENABLE, None],
],
CONF_ACOMP: [
[CONF_ENABLE, None],
],
CONF_LIMITER: [
[CONF_ENABLE, None],
],
CONF_ASQ: [
[CONF_IALL, None],
[CONF_IALH, None],
[CONF_OVERMOD, None],
],
CONF_RDS: [
[CONF_ENABLE, None],
],
CONF_OUTPUT: [
[CONF_GPIO1, lambda sw: sw.set_pin(1)],
[CONF_GPIO2, lambda sw: sw.set_pin(2)],
[CONF_GPIO3, lambda sw: sw.set_pin(3)],
],
}
async def to_code(config): async def to_code(config):
p = await cg.get_variable(config[CONF_SI4713_ID]) parent = await cg.get_variable(config[CONF_SI4713_ID])
await new_switch(p, config, CONF_MUTE, p.set_mute_switch)
await new_switch(p, config, CONF_MONO, p.set_mono_switch) async def new_switch(c, args, setter):
if tuner_config := config.get(CONF_TUNER): s = await switch.new_switch(c)
await new_switch(p, tuner_config, CONF_ENABLE, p.set_power_enable_switch) await cg.register_parented(s, parent)
if pilot_config := config.get(CONF_PILOT): sw = cg.add(getattr(parent, setter + "_switch")(s))
await new_switch(p, pilot_config, CONF_ENABLE, p.set_pilot_enable_switch) if cb := s[1]:
if compressor_config := config.get(CONF_COMPRESSOR): cb(sw)
await new_switch(p, compressor_config, CONF_ENABLE, p.set_acomp_enable_switch)
if limiter_config := config.get(CONF_LIMITER): await for_each_conf(config, VARIABLES, new_switch)
await new_switch(p, limiter_config, CONF_ENABLE, p.set_limiter_enable_switch)
if asq_config := config.get(CONF_ASQ):
await new_switch(p, asq_config, CONF_IALL, p.set_asq_iall_enable_switch)
await new_switch(p, asq_config, CONF_IALH, p.set_asq_ialh_enable_switch)
await new_switch(p, asq_config, CONF_OVERMOD, p.set_asq_overmod_enable_switch)
if rds_config := config.get(CONF_RDS):
await new_switch(p, rds_config, CONF_ENABLE, p.set_rds_enable_switch)
if output_config := config.get(CONF_OUTPUT):
gpio1 = await new_switch(p, output_config, CONF_GPIO1, p.set_gpio1_switch)
gpio2 = await new_switch(p, output_config, CONF_GPIO2, p.set_gpio2_switch)
gpio3 = await new_switch(p, output_config, CONF_GPIO3, p.set_gpio3_switch)
cg.add(gpio1.set_pin(1))
cg.add(gpio2.set_pin(2))
cg.add(gpio3.set_pin(3))

View file

@ -1,12 +0,0 @@
#include "asq_ialh_enable_switch.h"
namespace esphome {
namespace si4713 {
void AsqIalhEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_ialh_enable(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "asq_ialh_switch.h"
namespace esphome {
namespace si4713 {
void AsqIalhSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_ialh(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class AsqIallEnableSwitch : public switch_::Switch, public Parented<Si4713Component> { class AsqIalhSwitch : public switch_::Switch, public Parented<Si4713Component> {
public: public:
AsqIallEnableSwitch() = default; AsqIalhSwitch() = default;
protected: protected:
void write_state(bool value) override; void write_state(bool value) override;

View file

@ -1,12 +0,0 @@
#include "asq_iall_enable_switch.h"
namespace esphome {
namespace si4713 {
void AsqIallEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_iall_enable(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "asq_iall_switch.h"
namespace esphome {
namespace si4713 {
void AsqIallSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_iall(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class AsqIalhEnableSwitch : public switch_::Switch, public Parented<Si4713Component> { class AsqIallSwitch : public switch_::Switch, public Parented<Si4713Component> {
public: public:
AsqIalhEnableSwitch() = default; AsqIallSwitch() = default;
protected: protected:
void write_state(bool value) override; void write_state(bool value) override;

View file

@ -1,12 +0,0 @@
#include "asq_overmod_enable_switch.h"
namespace esphome {
namespace si4713 {
void AsqOvermodEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_overmod_enable(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "asq_overmod_switch.h"
namespace esphome {
namespace si4713 {
void AsqOvermodSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_asq_overmod(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class AsqOvermodEnableSwitch : public switch_::Switch, public Parented<Si4713Component> { class AsqOvermodSwitch : public switch_::Switch, public Parented<Si4713Component> {
public: public:
AsqOvermodEnableSwitch() = default; AsqOvermodSwitch() = default;
protected: protected:
void write_state(bool value) override; void write_state(bool value) override;

View file

@ -1,12 +0,0 @@
#include "gpio_switch.h"
namespace esphome {
namespace si4713 {
void GPIOSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_gpio(this->pin_, value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "output_gpio_switch.h"
namespace esphome {
namespace si4713 {
void OutputGpioSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_output_gpio(this->pin_, value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class GPIOSwitch : public switch_::Switch, public Parented<Si4713Component> { class OutputGpioSwitch : public switch_::Switch, public Parented<Si4713Component> {
public: public:
GPIOSwitch() = default; OutputGpioSwitch() = default;
void set_pin(uint8_t pin) { this->pin_ = pin - 1; } void set_pin(uint8_t pin) { this->pin_ = pin - 1; }

View file

@ -1,12 +0,0 @@
#include "power_enable_switch.h"
namespace esphome {
namespace si4713 {
void PowerEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_power_enable(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -0,0 +1,12 @@
#include "tuner_enable_switch.h"
namespace esphome {
namespace si4713 {
void TunerEnableSwitch::write_state(bool value) {
this->publish_state(value);
this->parent_->set_tuner_enable(value);
}
} // namespace si4713
} // namespace esphome

View file

@ -6,9 +6,9 @@
namespace esphome { namespace esphome {
namespace si4713 { namespace si4713 {
class PowerEnableSwitch : public switch_::Switch, public Parented<Si4713Component> { class TunerEnableSwitch : public switch_::Switch, public Parented<Si4713Component> {
public: public:
PowerEnableSwitch() = default; TunerEnableSwitch() = default;
protected: protected:
void write_state(bool value) override; void write_state(bool value) override;

View file

@ -22,6 +22,7 @@ from .. import (
CONF_RDS, CONF_RDS,
CONF_STATION, CONF_STATION,
ICON_FORMAT_TEXT, ICON_FORMAT_TEXT,
for_each_conf,
) )
RDSStationText = si4713_ns.class_("RDSStationText", text.Text) RDSStationText = si4713_ns.class_("RDSStationText", text.Text)
@ -107,20 +108,6 @@ async def register_text(
) )
async def new_text(
config,
*args,
min_length: Optional[int] = 0,
max_length: Optional[int] = 255,
pattern: Optional[str] = None,
):
var = cg.new_Pvariable(config[CONF_ID], *args)
await register_text(
var, config, min_length=min_length, max_length=max_length, pattern=pattern
)
return var
RDS_SCHEMA = cv.Schema( RDS_SCHEMA = cv.Schema(
{ {
cv.Optional(CONF_STATION): text_schema( cv.Optional(CONF_STATION): text_schema(
@ -143,17 +130,21 @@ CONFIG_SCHEMA = cv.Schema(
} }
) )
VARIABLES = {
async def new_text_simple(p, config, id, setter, min_length, max_length, *args): CONF_RDS: [
if c := config.get(id): [CONF_STATION, 0, 8],
t = await new_text(c, *args, min_length=min_length, max_length=max_length) [CONF_TEXT, 0, 64],
await cg.register_parented(t, p) ],
cg.add(setter(t)) }
return t
async def to_code(config): async def to_code(config):
p = await cg.get_variable(config[CONF_SI4713_ID]) parent = await cg.get_variable(config[CONF_SI4713_ID])
if rds_config := config.get(CONF_RDS):
await new_text_simple(p, rds_config, CONF_STATION, p.set_rds_station_text, 0, 8) async def new_text(c, args, setter):
await new_text_simple(p, rds_config, CONF_TEXT, p.set_rds_text_text, 0, 64) var = cg.new_Pvariable(c[CONF_ID])
await register_text(var, c, min_length=args[1], max_length=args[2])
await cg.register_parented(var, parent)
cg.add(getattr(parent, setter + "_text")(var))
await for_each_conf(config, VARIABLES, new_text)

View file

@ -33,7 +33,7 @@ si4713:
frequency: 32768 frequency: 32768
source: 'RCLK' source: 'RCLK'
prescaler: 1 prescaler: 1
compressor: acomp:
enable: True enable: True
preset: 'Custom' preset: 'Custom'
threshold: -40 threshold: -40
@ -104,7 +104,7 @@ number:
name: Reference Clock Frequency name: Reference Clock Frequency
prescaler: prescaler:
name: Reference Clock Prescaler name: Reference Clock Prescaler
compressor: acomp:
threshold: threshold:
name: Dynamic Range Control Threshold name: Dynamic Range Control Threshold
gain: gain:
@ -137,7 +137,7 @@ switch:
pilot: pilot:
enable: enable:
name: Pilot Tone Enable name: Pilot Tone Enable
compressor: acomp:
enable: enable:
name: Dynamic Range Control Enable name: Dynamic Range Control Enable
limiter: limiter:
@ -180,7 +180,7 @@ select:
refclk: refclk:
source: source:
name: Reference Clock Source name: Reference Clock Source
compressor: acomp:
attack: attack:
name: Dynamic Range Control Attack Time name: Dynamic Range Control Attack Time
release: release:

View file

@ -33,7 +33,7 @@ si4713:
frequency: 32768 frequency: 32768
source: 'RCLK' source: 'RCLK'
prescaler: 1 prescaler: 1
compressor: acomp:
enable: True enable: True
preset: 'Custom' preset: 'Custom'
threshold: -40 threshold: -40
@ -104,7 +104,7 @@ number:
name: Reference Clock Frequency name: Reference Clock Frequency
prescaler: prescaler:
name: Reference Clock Prescaler name: Reference Clock Prescaler
compressor: acomp:
threshold: threshold:
name: Dynamic Range Control Threshold name: Dynamic Range Control Threshold
gain: gain:
@ -137,7 +137,7 @@ switch:
pilot: pilot:
enable: enable:
name: Pilot Tone Enable name: Pilot Tone Enable
compressor: acomp:
enable: enable:
name: Dynamic Range Control Enable name: Dynamic Range Control Enable
limiter: limiter:
@ -180,7 +180,7 @@ select:
refclk: refclk:
source: source:
name: Reference Clock Source name: Reference Clock Source
compressor: acomp:
attack: attack:
name: Dynamic Range Control Attack Time name: Dynamic Range Control Attack Time
release: release: