From a25a08f5a68fd2323b6f167f39af4c9a48b76ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Poczkodi?= Date: Sun, 13 Oct 2024 22:25:26 +0200 Subject: [PATCH] formatting --- esphome/components/kt0803/__init__.py | 46 ++++-- esphome/components/kt0803/kt0803.cpp | 2 +- esphome/components/kt0803/kt0803defs.h | 2 +- esphome/components/kt0803/number/__init__.py | 41 +++++- esphome/components/kt0803/select/__init__.py | 63 ++++++-- esphome/components/kt0803/text/__init__.py | 138 ------------------ .../components/kt0803/test.esp32-s3-ard.yaml | 2 +- .../components/kt0803/test.esp32-s3-idf.yaml | 2 +- 8 files changed, 125 insertions(+), 171 deletions(-) delete mode 100644 esphome/components/kt0803/text/__init__.py diff --git a/esphome/components/kt0803/__init__.py b/esphome/components/kt0803/__init__.py index 2a17277175..cb51c329ac 100644 --- a/esphome/components/kt0803/__init__.py +++ b/esphome/components/kt0803/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv 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 ( CONF_ID, CONF_FREQUENCY, @@ -11,7 +11,15 @@ from esphome.const import ( CODEOWNERS = ["@gabest11"] 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 UNIT_MEGA_HERTZ = "MHz" @@ -153,7 +161,9 @@ SILENCE_LOW = { "32mV": SilenceLow.SLNCTHL_32MV, } -SilenceLowAndHighLevelDurationTime = kt0803_ns.enum("SilenceLowAndHighLevelDurationTime", True) +SilenceLowAndHighLevelDurationTime = kt0803_ns.enum( + "SilenceLowAndHighLevelDurationTime", True +) SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME = { "50ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_50MS, "100ms": SilenceLowAndHighLevelDurationTime.SLNCTIME_100MS, @@ -295,8 +305,12 @@ CONFIG_SCHEMA = ( cv.Optional(CONF_MUTE, 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_PILOT_TONE_AMPLITUDE, default="Low"): cv.enum(PILOT_TONE_AMPLITUDE), - cv.Optional(CONF_BASS_BOOST_CONTROL, default="Disabled"): cv.enum(BASS_BOOST_CONTROL), + cv.Optional(CONF_PILOT_TONE_AMPLITUDE, default="Low"): cv.enum( + 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_AUTO_PA_DOWN, default=True): 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_DECAY_TIME, default="25us"): cv.enum(ALC_TIME), 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_SILENCE_HIGH, default="32mV"): cv.enum(SILENCE_HIGH), cv.Optional(CONF_SILENCE_LOW, default="8mV"): cv.enum(SILENCE_LOW), 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_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_SILENCE_DURATION, default="100ms"): cv.enum( + SILENCE_LOW_AND_HIGH_LEVEL_DURATION_TIME + ), + 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_XTAL_SEL, default="32.768kHz"): cv.enum(XTAL_SEL), 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_XTAL_ENABLE, default=True): cv.boolean, cv.Optional(CONF_REF_CLK_ENABLE, default=False): cv.boolean, @@ -360,7 +384,7 @@ async def to_code(config): await set_var(config, CONF_MONO, var.set_mono) await set_var(config, CONF_PRE_EMPHASIS, var.set_pre_emphasis) await set_var(config, CONF_PILOT_TONE_AMPLITUDE, var.set_pilot_tone_amplitude) - await set_var(config, CONF_BASS_BOOST_CONTROL, var.set_bass_boost_control) + await set_var(config, CONF_BASS_BOOST_CONTROL, var.set_bass_boost_control) await set_var(config, CONF_ALC_ENABLE, var.set_alc_enable) await set_var(config, CONF_AUTO_PA_DOWN, var.set_auto_pa_down) await set_var(config, CONF_PA_DOWN, var.set_pa_down) diff --git a/esphome/components/kt0803/kt0803.cpp b/esphome/components/kt0803/kt0803.cpp index d5a16402af..adb8cdb75e 100644 --- a/esphome/components/kt0803/kt0803.cpp +++ b/esphome/components/kt0803/kt0803.cpp @@ -673,7 +673,7 @@ float KT0803Component::get_alc_gain() { return (float) AlcGainMap[this->state_.A void KT0803Component::set_xtal_sel(XtalSel value) { if (value >= XtalSel::LAST) { - ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", value); + ESP_LOGE(TAG, "set_xtal_sel(%d) invalid", (int) value); return; } this->state_.XTAL_SEL = (uint8_t) value; diff --git a/esphome/components/kt0803/kt0803defs.h b/esphome/components/kt0803/kt0803defs.h index 8980655c6c..756983c6c1 100644 --- a/esphome/components/kt0803/kt0803defs.h +++ b/esphome/components/kt0803/kt0803defs.h @@ -344,7 +344,7 @@ struct KT0803State { uint8_t SLNCTIME1 : 1; // L uint8_t _14_1 : 1; uint8_t SLNCCNTHIGH : 3; // K L - uint8_t SLNCTIME0 : 3; // K L + uint8_t SLNCTIME0 : 3; // K L }; }; union { diff --git a/esphome/components/kt0803/number/__init__.py b/esphome/components/kt0803/number/__init__.py index 5c2e3a1e73..36c0d0d668 100644 --- a/esphome/components/kt0803/number/__init__.py +++ b/esphome/components/kt0803/number/__init__.py @@ -57,13 +57,44 @@ CONFIG_SCHEMA = cv.Schema( async def new_number(config, id, setter, min_value, max_value, step): 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]) cg.add(setter(n)) + async def to_code(config): 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(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) + 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( + 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, + ) diff --git a/esphome/components/kt0803/select/__init__.py b/esphome/components/kt0803/select/__init__.py index 7ada0a2b11..d1dd15a4eb 100644 --- a/esphome/components/kt0803/select/__init__.py +++ b/esphome/components/kt0803/select/__init__.py @@ -9,7 +9,6 @@ from .. import ( CONF_KT0803_ID, KT0803Component, kt0803_ns, - CONF_CHIP_ID, CONF_PRE_EMPHASIS, CONF_PILOT_TONE_AMPLITUDE, CONF_BASS_BOOST_CONTROL, @@ -31,7 +30,6 @@ from .. import ( ICON_SLEEP, ICON_SINE_WAVE, ICON_SPEAKER, - ICON_EAR_HEARING, PRE_EMPHASIS, PILOT_TONE_AMPLITUDE, BASS_BOOST_CONTROL, @@ -48,7 +46,7 @@ from .. import ( REFERENCE_CLOCK, ALC_HIGH, ALC_HOLD_TIME, - ALC_LOW, + ALC_LOW, ) PreEmphasisSelect = kt0803_ns.class_("PreEmphasisSelect", select.Select) @@ -106,7 +104,7 @@ CONFIG_SCHEMA = cv.Schema( cv.Optional(CONF_SWITCH_MODE): select.select_schema( SwitchModeSelect, entity_category=ENTITY_CATEGORY_CONFIG, - #icon=ICON_, + # icon=ICON_, ), cv.Optional(CONF_SILENCE_HIGH): select.select_schema( SilenceHighSelect, @@ -177,20 +175,59 @@ async def new_select(config, id, setter, options): async def to_code(config): 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_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_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_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_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_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_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_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_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) diff --git a/esphome/components/kt0803/text/__init__.py b/esphome/components/kt0803/text/__init__.py deleted file mode 100644 index 1b58945320..0000000000 --- a/esphome/components/kt0803/text/__init__.py +++ /dev/null @@ -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) diff --git a/tests/components/kt0803/test.esp32-s3-ard.yaml b/tests/components/kt0803/test.esp32-s3-ard.yaml index e7d8487140..e6daf6b57c 100644 --- a/tests/components/kt0803/test.esp32-s3-ard.yaml +++ b/tests/components/kt0803/test.esp32-s3-ard.yaml @@ -78,7 +78,7 @@ switch: au_enhance: name: Audio Frequency Response Enhancement xtal_enable: - name: Crystal Oscillator Enable + name: Crystal Oscillator Enable ref_clk_enable: name: Refence Clock Enable diff --git a/tests/components/kt0803/test.esp32-s3-idf.yaml b/tests/components/kt0803/test.esp32-s3-idf.yaml index e7d8487140..e6daf6b57c 100644 --- a/tests/components/kt0803/test.esp32-s3-idf.yaml +++ b/tests/components/kt0803/test.esp32-s3-idf.yaml @@ -78,7 +78,7 @@ switch: au_enhance: name: Audio Frequency Response Enhancement xtal_enable: - name: Crystal Oscillator Enable + name: Crystal Oscillator Enable ref_clk_enable: name: Refence Clock Enable