formatting

This commit is contained in:
Gábor Poczkodi 2024-10-13 22:25:26 +02:00
parent 88465d624b
commit a25a08f5a6
8 changed files with 125 additions and 171 deletions

View file

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.components import i2c, sensor, text_sensor, binary_sensor from esphome.components import i2c, binary_sensor
from esphome.const import ( from esphome.const import (
CONF_ID, CONF_ID,
CONF_FREQUENCY, CONF_FREQUENCY,
@ -11,7 +11,15 @@ from esphome.const import (
CODEOWNERS = ["@gabest11"] CODEOWNERS = ["@gabest11"]
DEPENDENCIES = ["i2c"] DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "text_sensor", "binary_sensor", "number", "switch", "select", "text"] AUTO_LOAD = [
"sensor",
"text_sensor",
"binary_sensor",
"number",
"switch",
"select",
"text",
]
MULTI_CONF = True MULTI_CONF = True
UNIT_MEGA_HERTZ = "MHz" UNIT_MEGA_HERTZ = "MHz"
@ -153,7 +161,9 @@ SILENCE_LOW = {
"32mV": SilenceLow.SLNCTHL_32MV, "32mV": SilenceLow.SLNCTHL_32MV,
} }
SilenceLowAndHighLevelDurationTime = kt0803_ns.enum("SilenceLowAndHighLevelDurationTime", True) SilenceLowAndHighLevelDurationTime = kt0803_ns.enum(
"SilenceLowAndHighLevelDurationTime", True
)
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME = { SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME = {
"50ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_50MS, "50ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_50MS,
"100ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_100MS, "100ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_100MS,
@ -295,8 +305,12 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_MUTE, default=False): cv.boolean, cv.Optional(CONF_MUTE, default=False): cv.boolean,
cv.Optional(CONF_MONO, default=False): cv.boolean, cv.Optional(CONF_MONO, default=False): cv.boolean,
cv.Optional(CONF_PRE_EMPHASIS, default="75us"): cv.enum(PRE_EMPHASIS), cv.Optional(CONF_PRE_EMPHASIS, default="75us"): cv.enum(PRE_EMPHASIS),
cv.Optional(CONF_PILOT_TONE_AMPLITUDE, default="Low"): cv.enum(PILOT_TONE_AMPLITUDE), cv.Optional(CONF_PILOT_TONE_AMPLITUDE, default="Low"): cv.enum(
cv.Optional(CONF_BASS_BOOST_CONTROL, default="Disabled"): cv.enum(BASS_BOOST_CONTROL), PILOT_TONE_AMPLITUDE
),
cv.Optional(CONF_BASS_BOOST_CONTROL, default="Disabled"): cv.enum(
BASS_BOOST_CONTROL
),
cv.Optional(CONF_ALC_ENABLE, default=False): cv.boolean, cv.Optional(CONF_ALC_ENABLE, default=False): cv.boolean,
cv.Optional(CONF_AUTO_PA_DOWN, default=True): cv.boolean, cv.Optional(CONF_AUTO_PA_DOWN, default=True): cv.boolean,
cv.Optional(CONF_PA_DOWN, default=False): cv.boolean, cv.Optional(CONF_PA_DOWN, default=False): cv.boolean,
@ -304,18 +318,28 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_ALC_ATTACK_TIME, default="25us"): cv.enum(ALC_TIME), cv.Optional(CONF_ALC_ATTACK_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_ALC_DECAY_TIME, default="25us"): cv.enum(ALC_TIME), cv.Optional(CONF_ALC_DECAY_TIME, default="25us"): cv.enum(ALC_TIME),
cv.Optional(CONF_PA_BIAS, default=True): cv.boolean, cv.Optional(CONF_PA_BIAS, default=True): cv.boolean,
cv.Optional(CONF_AUDIO_LIMITER_LEVEL, default="0.875"): cv.enum(AUDIO_LIMITER_LEVEL), cv.Optional(CONF_AUDIO_LIMITER_LEVEL, default="0.875"): cv.enum(
AUDIO_LIMITER_LEVEL
),
cv.Optional(CONF_SWITCH_MODE, default="Mute"): cv.enum(SWITCH_MODE), cv.Optional(CONF_SWITCH_MODE, default="Mute"): cv.enum(SWITCH_MODE),
cv.Optional(CONF_SILENCE_HIGH, default="32mV"): cv.enum(SILENCE_HIGH), cv.Optional(CONF_SILENCE_HIGH, default="32mV"): cv.enum(SILENCE_HIGH),
cv.Optional(CONF_SILENCE_LOW, default="8mV"): cv.enum(SILENCE_LOW), cv.Optional(CONF_SILENCE_LOW, default="8mV"): cv.enum(SILENCE_LOW),
cv.Optional(CONF_SILENCE_DETECTION, default=False): cv.boolean, cv.Optional(CONF_SILENCE_DETECTION, default=False): cv.boolean,
cv.Optional(CONF_SILENCE_DURATION, default="100ms"): cv.enum(SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME), cv.Optional(CONF_SILENCE_DURATION, default="100ms"): cv.enum(
cv.Optional(CONF_SILENCE_HIGH_COUNTER, default='15'): cv.enum(SILENCE_HIGH_LEVEL_COUNTER), SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME
cv.Optional(CONF_SILENCE_LOW_COUNTER, default='1'): cv.enum(SILENCE_LOW_LEVEL_COUNTER), ),
cv.Optional(CONF_SILENCE_HIGH_COUNTER, default='15'): cv.enum(
SILENCE_HIGH_LEVEL_COUNTER
),
cv.Optional(CONF_SILENCE_LOW_COUNTER, default='1'): cv.enum(
SILENCE_LOW_LEVEL_COUNTER
),
cv.Optional(CONF_ALC_GAIN, default=-3): cv.float_range(-15, 6), cv.Optional(CONF_ALC_GAIN, default=-3): cv.float_range(-15, 6),
cv.Optional(CONF_XTAL_SEL, default="32.768kHz"): cv.enum(XTAL_SEL), cv.Optional(CONF_XTAL_SEL, default="32.768kHz"): cv.enum(XTAL_SEL),
cv.Optional(CONF_AU_ENHANCE, default=False): cv.boolean, cv.Optional(CONF_AU_ENHANCE, default=False): cv.boolean,
cv.Optional(CONF_FREQUENCY_DEVIATION, default="75kHz"): cv.enum(FREQUENCY_DEVIATION), cv.Optional(CONF_FREQUENCY_DEVIATION, default="75kHz"): cv.enum(
FREQUENCY_DEVIATION
),
cv.Optional(CONF_REF_CLK, default="32.768kHz"): cv.enum(REFERENCE_CLOCK), cv.Optional(CONF_REF_CLK, default="32.768kHz"): cv.enum(REFERENCE_CLOCK),
cv.Optional(CONF_XTAL_ENABLE, default=True): cv.boolean, cv.Optional(CONF_XTAL_ENABLE, default=True): cv.boolean,
cv.Optional(CONF_REF_CLK_ENABLE, default=False): cv.boolean, cv.Optional(CONF_REF_CLK_ENABLE, default=False): cv.boolean,

View file

@ -673,7 +673,7 @@ float KT0803Component::get_alc_gain() { return (float) AlcGainMap[this->state_.A
void KT0803Component::set_xtal_sel(XtalSel value) { void KT0803Component::set_xtal_sel(XtalSel value) {
if (value >= XtalSel::LAST) { if (value >= XtalSel::LAST) {
ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", value); ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", (int) value);
return; return;
} }
this->state_.XTAL_SEL = (uint8_t) value; this->state_.XTAL_SEL = (uint8_t) value;

View file

@ -57,13 +57,44 @@ CONFIG_SCHEMA = cv.Schema(
async def new_number(config, id, setter, min_value, max_value, step): async def new_number(config, id, setter, min_value, max_value, step):
if c := config.get(id): if c := config.get(id):
n = await number.new_number(c, min_value=min_value, max_value=max_value, step=step) n = await number.new_number(
c, min_value=min_value, max_value=max_value, step=step
)
await cg.register_parented(n, config[CONF_KT0803_ID]) await cg.register_parented(n, config[CONF_KT0803_ID])
cg.add(setter(n)) cg.add(setter(n))
async def to_code(config): async def to_code(config):
kt0803_component = await cg.get_variable(config[CONF_KT0803_ID]) kt0803_component = await cg.get_variable(config[CONF_KT0803_ID])
await new_number(config, CONF_FREQUENCY, kt0803_component.set_frequency_number, kt0803_ns.CHSEL_MIN, kt0803_ns.CHSEL_MAX, kt0803_ns.CHSEL_STEP) await new_number(
await new_number(config, CONF_PGA, kt0803_component.set_pga_number, kt0803_ns.PGA_MIN, kt0803_ns.PGA_MAX, kt0803_ns.PGA_STEP) config,
await new_number(config, CONF_RFGAIN, kt0803_component.set_rfgain_number, kt0803_ns.RFGAIN_MIN, kt0803_ns.RFGAIN_MAX, 0.1) CONF_FREQUENCY,
await new_number(config, CONF_ALC_GAIN, kt0803_component.set_alc_gain_number, kt0803_ns.ALC_GAIN_MIN, kt0803_ns.ALC_GAIN_MAX, 3) kt0803_component.set_frequency_number,
kt0803_ns.CHSEL_MIN,
kt0803_ns.CHSEL_MAX,
kt0803_ns.CHSEL_STEP,
)
await new_number(
config,
CONF_PGA,
kt0803_component.set_pga_number,
kt0803_ns.PGA_MIN,
kt0803_ns.PGA_MAX,
kt0803_ns.PGA_STEP,
)
await new_number(
config,
CONF_RFGAIN,
kt0803_component.set_rfgain_number,
kt0803_ns.RFGAIN_MIN,
kt0803_ns.RFGAIN_MAX,
0.1,
)
await new_number(
config,
CONF_ALC_GAIN,
kt0803_component.set_alc_gain_number,
kt0803_ns.ALC_GAIN_MIN,
kt0803_ns.ALC_GAIN_MAX,
3,
)

View file

@ -9,7 +9,6 @@ from .. import (
CONF_KT0803_ID, CONF_KT0803_ID,
KT0803Component, KT0803Component,
kt0803_ns, kt0803_ns,
CONF_CHIP_ID,
CONF_PRE_EMPHASIS, CONF_PRE_EMPHASIS,
CONF_PILOT_TONE_AMPLITUDE, CONF_PILOT_TONE_AMPLITUDE,
CONF_BASS_BOOST_CONTROL, CONF_BASS_BOOST_CONTROL,
@ -31,7 +30,6 @@ from .. import (
ICON_SLEEP, ICON_SLEEP,
ICON_SINE_WAVE, ICON_SINE_WAVE,
ICON_SPEAKER, ICON_SPEAKER,
ICON_EAR_HEARING,
PRE_EMPHASIS, PRE_EMPHASIS,
PILOT_TONE_AMPLITUDE, PILOT_TONE_AMPLITUDE,
BASS_BOOST_CONTROL, BASS_BOOST_CONTROL,
@ -177,20 +175,59 @@ async def new_select(config, id, setter, options):
async def to_code(config): async def to_code(config):
c = await cg.get_variable(config[CONF_KT0803_ID]) c = await cg.get_variable(config[CONF_KT0803_ID])
await new_select(config, CONF_PRE_EMPHASIS, c.set_pre_emphasis_select, PRE_EMPHASIS) await new_select(config, CONF_PRE_EMPHASIS, c.set_pre_emphasis_select, PRE_EMPHASIS)
await new_select(config, CONF_PILOT_TONE_AMPLITUDE, c.set_pilot_tone_amplitude_select, PILOT_TONE_AMPLITUDE) await new_select(
await new_select(config, CONF_BASS_BOOST_CONTROL, c.set_bass_boost_control_select, BASS_BOOST_CONTROL) config,
await new_select(config, CONF_ALC_ATTACK_TIME, c.set_alc_attack_time_select, ALC_TIME) CONF_PILOT_TONE_AMPLITUDE,
c.set_pilot_tone_amplitude_select,
PILOT_TONE_AMPLITUDE,
)
await new_select(
config,
CONF_BASS_BOOST_CONTROL,
c.set_bass_boost_control_select,
BASS_BOOST_CONTROL,
)
await new_select(
config, CONF_ALC_ATTACK_TIME, c.set_alc_attack_time_select, ALC_TIME
)
await new_select(config, CONF_ALC_DECAY_TIME, c.set_alc_decay_time_select, ALC_TIME) await new_select(config, CONF_ALC_DECAY_TIME, c.set_alc_decay_time_select, ALC_TIME)
await new_select(config, CONF_AUDIO_LIMITER_LEVEL, c.set_audio_limiter_level_select, AUDIO_LIMITER_LEVEL) await new_select(
config,
CONF_AUDIO_LIMITER_LEVEL,
c.set_audio_limiter_level_select,
AUDIO_LIMITER_LEVEL,
)
await new_select(config, CONF_SWITCH_MODE, c.set_switch_mode_select, SWITCH_MODE) await new_select(config, CONF_SWITCH_MODE, c.set_switch_mode_select, SWITCH_MODE)
await new_select(config, CONF_SILENCE_HIGH, c.set_silence_high_select, SILENCE_HIGH) await new_select(config, CONF_SILENCE_HIGH, c.set_silence_high_select, SILENCE_HIGH)
await new_select(config, CONF_SILENCE_LOW, c.set_silence_low_select, SILENCE_LOW) await new_select(config, CONF_SILENCE_LOW, c.set_silence_low_select, SILENCE_LOW)
await new_select(config, CONF_SILENCE_DURATION, c.set_silence_duration_select, SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME) await new_select(
await new_select(config, CONF_SILENCE_HIGH_COUNTER, c.set_silence_high_counter_select, SILENCE_HIGH_LEVEL_COUNTER) config,
await new_select(config, CONF_SILENCE_LOW_COUNTER, c.set_silence_low_counter_select, SILENCE_LOW_LEVEL_COUNTER) CONF_SILENCE_DURATION,
c.set_silence_duration_select,
SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME,
)
await new_select(
config,
CONF_SILENCE_HIGH_COUNTER,
c.set_silence_high_counter_select,
SILENCE_HIGH_LEVEL_COUNTER,
)
await new_select(
config,
CONF_SILENCE_LOW_COUNTER,
c.set_silence_low_counter_select,
SILENCE_LOW_LEVEL_COUNTER,
)
await new_select(config, CONF_XTAL_SEL, c.set_xtal_sel_select, XTAL_SEL) await new_select(config, CONF_XTAL_SEL, c.set_xtal_sel_select, XTAL_SEL)
await new_select(config, CONF_FREQUENCY_DEVIATION, c.set_frequency_deviation_select, FREQUENCY_DEVIATION) await new_select(
config,
CONF_FREQUENCY_DEVIATION,
c.set_frequency_deviation_select,
FREQUENCY_DEVIATION,
)
await new_select(config, CONF_REF_CLK, c.set_ref_clk_select, REFERENCE_CLOCK) await new_select(config, CONF_REF_CLK, c.set_ref_clk_select, REFERENCE_CLOCK)
await new_select(config, CONF_ALC_HIGH, c.set_alc_high_select, ALC_HIGH) await new_select(config, CONF_ALC_HIGH, c.set_alc_high_select, ALC_HIGH)
await new_select(config, CONF_ALC_HOLD_TIME, c.set_alc_hold_time_select, ALC_HOLD_TIME) await new_select(
config, CONF_ALC_HOLD_TIME, c.set_alc_hold_time_select, ALC_HOLD_TIME
)
await new_select(config, CONF_ALC_LOW, c.set_alc_low_select, ALC_LOW) await new_select(config, CONF_ALC_LOW, c.set_alc_low_select, ALC_LOW)

View file

@ -1,138 +0,0 @@
from typing import Optional
import esphome.codegen as cg
from esphome.components import text
from esphome.components import mqtt, web_server
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
CONF_ENTITY_CATEGORY,
CONF_ICON,
CONF_MQTT_ID,
CONF_WEB_SERVER,
ENTITY_CATEGORY_CONFIG,
)
from esphome.core import CORE
from esphome.cpp_generator import MockObjClass
from esphome.cpp_helpers import setup_entity
from .. import (
CONF_KT0803_ID,
KT0803Component,
kt0803_ns,
#CONF_..,
ICON_FORMAT_TEXT,
)
#...Text = kt0803_ns.class_("...Text", text.Text)
# The text component isn't implemented the same way as switch, select, number.
# It is not possible to create our own text platform as it is now, so I adopted
# the necessary code from those.
_TEXT_SCHEMA = (
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
.extend(
{
cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTTextComponent),
}
)
)
_UNDEF = object()
def text_schema(
class_: MockObjClass = _UNDEF,
*,
entity_category: str = _UNDEF,
device_class: str = _UNDEF,
icon: str = _UNDEF,
):
schema = _TEXT_SCHEMA
if class_ is not _UNDEF:
schema = schema.extend({cv.GenerateID(): cv.declare_id(class_)})
if entity_category is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_ENTITY_CATEGORY, default=entity_category
): cv.entity_category
}
)
if icon is not _UNDEF:
schema = schema.extend({cv.Optional(CONF_ICON, default=icon): cv.icon})
return schema
TEXT_SCHEMA = text_schema() # for compatibility
async def setup_text_core_(
var,
config,
*,
min_length: Optional[int],
max_length: Optional[int],
pattern: Optional[str],
):
await setup_entity(var, config)
cg.add(var.traits.set_min_length(min_length))
cg.add(var.traits.set_max_length(max_length))
if pattern is not None:
cg.add(var.traits.set_pattern(pattern))
if (mqtt_id := config.get(CONF_MQTT_ID)) is not None:
mqtt_ = cg.new_Pvariable(mqtt_id, var)
await mqtt.register_mqtt_component(mqtt_, config)
if web_server_config := config.get(CONF_WEB_SERVER):
await web_server.add_entity_config(var, web_server_config)
async def register_text(
var,
config,
min_length: Optional[int] = 0,
max_length: Optional[int] = 255,
pattern: Optional[str] = None,
):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
cg.add(cg.App.register_text(var))
await setup_text_core_(
var, config, min_length=min_length, max_length=max_length, pattern=pattern
)
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
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_KT0803_ID): cv.use_id(KT0803Component),
#cv.Optional(CONF_..): text_schema(..Text, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_FORMAT_TEXT),
}
)
async def new_text_simple(config, id, setter, min_length, max_length, *args):
if c := config.get(id):
t = await new_text(c, *args, min_length=min_length, max_length=max_length)
await cg.register_parented(t, config[CONF_KT0803_ID])
cg.add(setter(t))
async def to_code(config):
c = await cg.get_variable(config[CONF_KT0803_ID])
#await new_text_simple(config, CONF_.., c.set_.._text, 0, kt0803_ns..._MAX)