[core] Add type annotations to component Python (5/11) (#18342)

Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
This commit is contained in:
Jesse Hills
2026-08-21 07:56:02 +12:00
committed by GitHub
co-authored by Jonathan Swoboda
parent 2ab09e1a77
commit 6343c11873
35 changed files with 246 additions and 86 deletions
+4 -2
View File
@@ -2,6 +2,8 @@ import esphome.codegen as cg
from esphome.components import ble_client, time
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_RECEIVE_TIMEOUT, CONF_TIME_ID
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
CODEOWNERS = ["@jhansche"]
DEPENDENCIES = ["ble_client"]
@@ -32,12 +34,12 @@ BEDJET_CLIENT_SCHEMA = cv.Schema(
)
async def register_bedjet_child(var, config):
async def register_bedjet_child(var: MockObj, config: ConfigType) -> None:
parent = await cg.get_variable(config[CONF_BEDJET_ID])
cg.add(parent.register_child(var))
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await ble_client.register_ble_node(var, config)
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import climate
import esphome.config_validation as cv
from esphome.const import CONF_HEAT_MODE, CONF_TEMPERATURE_SOURCE
from esphome.types import ConfigType
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
@@ -37,7 +38,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await climate.new_climate(config)
await cg.register_component(var, config)
await register_bedjet_child(var, config)
+2 -1
View File
@@ -1,6 +1,7 @@
import esphome.codegen as cg
from esphome.components import fan
import esphome.config_validation as cv
from esphome.types import ConfigType
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
@@ -16,7 +17,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await fan.new_fan(config)
await cg.register_component(var, config)
await register_bedjet_child(var, config)
+2 -1
View File
@@ -9,6 +9,7 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
)
from esphome.types import ConfigType
from .. import BEDJET_CLIENT_SCHEMA, bedjet_ns, register_bedjet_child
@@ -38,7 +39,7 @@ CONFIG_SCHEMA = cv.Schema(
).extend(BEDJET_CLIENT_SCHEMA)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await register_bedjet_child(var, config)
+2 -1
View File
@@ -3,6 +3,7 @@ from esphome.components import esp32, i2c
from esphome.components.const import CONF_STATE_SAVE_INTERVAL
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_SAMPLE_RATE, CONF_TEMPERATURE_OFFSET, Framework
from esphome.types import ConfigType
CODEOWNERS = ["@trvrnrth"]
DEPENDENCIES = ["i2c"]
@@ -76,7 +77,7 @@ CONFIG_SCHEMA = cv.All(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
+4 -2
View File
@@ -29,6 +29,8 @@ from esphome.const import (
UNIT_PARTS_PER_MILLION,
UNIT_PERCENT,
)
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
from . import CONF_BME680_BSEC_ID, SAMPLE_RATE_OPTIONS, BME680BSECComponent
@@ -110,7 +112,7 @@ CONFIG_SCHEMA = cv.Schema(
)
async def setup_conf(config, key, hub):
async def setup_conf(config: ConfigType, key: str, hub: MockObj) -> None:
if sensor_config := config.get(key):
sens = await sensor.new_sensor(sensor_config)
cg.add(getattr(hub, f"set_{key}_sensor")(sens))
@@ -120,7 +122,7 @@ async def setup_conf(config, key, hub):
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_BME680_BSEC_ID])
for key in TYPES:
await setup_conf(config, key, hub)
@@ -2,6 +2,8 @@ import esphome.codegen as cg
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_IAQ_ACCURACY
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
from . import CONF_BME680_BSEC_ID, BME680BSECComponent
@@ -21,13 +23,13 @@ CONFIG_SCHEMA = cv.Schema(
)
async def setup_conf(config, key, hub):
async def setup_conf(config: ConfigType, key: str, hub: MockObj) -> None:
if sensor_config := config.get(key):
sens = await text_sensor.new_text_sensor(sensor_config)
cg.add(getattr(hub, f"set_{key}_text_sensor")(sens))
async def to_code(config):
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_BME680_BSEC_ID])
for key in TYPES:
await setup_conf(config, key, hub)
+11 -3
View File
@@ -17,6 +17,9 @@ from esphome.const import (
UNIT_VOLT,
UNIT_WATT,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@balrog-kun"]
DEPENDENCIES = ["spi"]
@@ -40,7 +43,7 @@ CONF_VOLTAGE_HPF = "voltage_hpf"
CONF_PULSE_ENERGY = "pulse_energy"
def validate_config(config):
def validate_config(config: ConfigType) -> ConfigType:
current_gain = abs(config[CONF_CURRENT_GAIN]) * (
1.0 if config[CONF_PGA_GAIN] == "10X" else 5.0
)
@@ -105,7 +108,7 @@ CONFIG_SCHEMA = cv.All(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await spi.register_spi_device(var, config)
@@ -138,6 +141,11 @@ async def to_code(config):
),
synchronous=True,
)
async def restart_action_to_code(config, action_id, template_arg, args):
async def restart_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
+8 -5
View File
@@ -1,4 +1,6 @@
from collections.abc import Callable, Iterable
import logging
from typing import Any
import esphome.codegen as cg
from esphome.components import esp32
@@ -23,6 +25,7 @@ from esphome.const import (
CONF_VOLTAGE_ATTENUATION,
)
from esphome.core import TimePeriod
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
@@ -181,7 +184,7 @@ EFFECTIVE_HIGH_VOLTAGE = {
}
def validate_touch_pad(value):
def validate_touch_pad(value: Any) -> int:
value = gpio.gpio_pin_number_validator(value)
variant = get_esp32_variant()
pads = TOUCH_PADS.get(variant)
@@ -192,7 +195,7 @@ def validate_touch_pad(value):
return pads[value] # Return integer channel ID
def validate_variant_vars(config):
def validate_variant_vars(config: ConfigType) -> ConfigType:
variant = get_esp32_variant()
invalid_vars = set()
if variant == VARIANT_ESP32:
@@ -219,8 +222,8 @@ def validate_variant_vars(config):
return config
def validate_voltage(values):
def validator(value):
def validate_voltage(values: Iterable[str]) -> Callable[[Any], str]:
def validator(value: Any) -> str:
if isinstance(value, float) and value.is_integer():
value = int(value)
value = cv.string(value)
@@ -300,7 +303,7 @@ CONFIG_SCHEMA = cv.All(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
# New unified touch sensor driver
include_builtin_idf_component("esp_driver_touch_sens")
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_PIN, CONF_THRESHOLD
from esphome.types import ConfigType
from . import ESP32TouchComponent, esp32_touch_ns, validate_touch_pad
@@ -24,7 +25,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(ESP32TouchBinarySensor).exten
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_ESP32_TOUCH_ID])
var = cg.new_Pvariable(
config[CONF_ID],
+11 -3
View File
@@ -4,11 +4,14 @@ from esphome.components import output
from esphome.components.esp8266.const import require_waveform
import esphome.config_validation as cv
from esphome.const import CONF_FREQUENCY, CONF_ID, CONF_NUMBER, CONF_PIN
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
DEPENDENCIES = ["esp8266"]
def valid_pwm_pin(value):
def valid_pwm_pin(value: ConfigType) -> ConfigType:
num = value[CONF_NUMBER]
cv.one_of(0, 1, 2, 3, 4, 5, 9, 10, 12, 13, 14, 15, 16)(num)
return value
@@ -35,7 +38,7 @@ CONFIG_SCHEMA = cv.All(
)
async def to_code(config) -> None:
async def to_code(config: ConfigType) -> None:
require_waveform()
var = cg.new_Pvariable(config[CONF_ID])
@@ -59,7 +62,12 @@ async def to_code(config) -> None:
),
synchronous=True,
)
async def esp8266_set_frequency_to_code(config, action_id, template_arg, args):
async def esp8266_set_frequency_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_FREQUENCY], args, cg.float_)
+4 -3
View File
@@ -12,6 +12,7 @@ from esphome.const import (
)
from esphome.core import CORE
from esphome.final_validate import full_config
from esphome.types import ConfigType
CODEOWNERS = ["@anatoly-savchenkov"]
@@ -23,7 +24,7 @@ CONF_RESETS_REQUIRED = "resets_required"
CONF_ON_INCREMENT = "on_increment"
def _validate(config):
def _validate(config: ConfigType) -> ConfigType:
if CONF_RESETS_REQUIRED in config:
return cv.only_on(
[
@@ -60,7 +61,7 @@ CONFIG_SCHEMA = cv.All(
)
def _final_validate(config) -> None:
def _final_validate(config: ConfigType) -> None:
if CORE.is_esp8266 and CONF_RESETS_REQUIRED in config:
fconfig = full_config.get()
if not fconfig.get_config_for_path([KEY_ESP8266, CONF_RESTORE_FROM_FLASH]):
@@ -81,7 +82,7 @@ _CALLBACK_AUTOMATIONS = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
if reset_count := config.get(CONF_RESETS_REQUIRED):
var = cg.new_Pvariable(
config[CONF_ID],
@@ -7,6 +7,7 @@ from esphome.const import (
ENTITY_CATEGORY_CONFIG,
ICON_RESTART_ALERT,
)
from esphome.types import ConfigType
from .. import factory_reset_ns
@@ -22,7 +23,7 @@ CONFIG_SCHEMA = button.button_schema(
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await button.register_button(var, config)
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_RESTART_ALERT
from esphome.types import ConfigType
from .. import factory_reset_ns
@@ -17,6 +18,6 @@ CONFIG_SCHEMA = switch.switch_schema(
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
+10 -2
View File
@@ -13,6 +13,9 @@ from esphome.const import (
CONF_PRESET_MODES,
CONF_SPEED_COUNT,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
from .. import hbridge_ns
@@ -54,12 +57,17 @@ CONFIG_SCHEMA = (
maybe_simple_id({cv.GenerateID(): cv.use_id(HBridgeFan)}),
synchronous=True,
)
async def fan_hbridge_brake_to_code(config, action_id, template_arg, args):
async def fan_hbridge_brake_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await fan.new_fan(
config,
config[CONF_SPEED_COUNT],
+2 -1
View File
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import light, output
import esphome.config_validation as cv
from esphome.const import CONF_OUTPUT_ID, CONF_PIN_A, CONF_PIN_B, CONF_UPDATE_INTERVAL
from esphome.types import ConfigType
from .. import hbridge_ns
@@ -21,7 +22,7 @@ CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
cg.add(var.set_update_interval(config.pop(CONF_UPDATE_INTERVAL)))
await cg.register_component(var, config)
@@ -3,6 +3,7 @@ import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_OPTIMISTIC, CONF_PULSE_LENGTH, CONF_WAIT_TIME
from esphome.types import ConfigType
from .. import hbridge_ns
@@ -30,7 +31,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_component(var, config)
+11 -4
View File
@@ -1,3 +1,6 @@
from collections.abc import Callable
from typing import Any
import esphome.codegen as cg
from esphome.components import i2c, sensor
import esphome.config_validation as cv
@@ -17,6 +20,8 @@ from esphome.const import (
UNIT_DEGREES,
UNIT_MICROTESLA,
)
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
DEPENDENCIES = ["i2c"]
@@ -59,14 +64,16 @@ HMC5883L_RANGES = {
}
def validate_enum(enum_values, units=None, int=True):
def validate_enum(
enum_values: dict[Any, Any], units: str | list[str] | None = None, int: bool = True
) -> Callable[[Any], Any]:
_units = []
if units is not None:
_units = units if isinstance(units, list) else [units]
_units = [str(x) for x in _units]
enum_bound = cv.enum(enum_values, int=int)
def validate_enum_bound(value):
def validate_enum_bound(value: Any) -> Any:
value = cv.string(value)
for unit in _units:
if value.endswith(unit):
@@ -112,7 +119,7 @@ CONFIG_SCHEMA = (
)
def auto_data_rate(config):
def auto_data_rate(config: ConfigType) -> MockObj:
interval_msec = config[CONF_UPDATE_INTERVAL].total_milliseconds
interval_hz = 1000.0 / interval_msec
for datarate in sorted(HMC5883LDatarates.keys()):
@@ -121,7 +128,7 @@ def auto_data_rate(config):
return HMC5883LDatarates[75]
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
+16 -3
View File
@@ -15,6 +15,9 @@ from esphome.const import (
UNIT_CELSIUS,
UNIT_PARTS_PER_MILLION,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
DEPENDENCIES = ["uart"]
@@ -78,7 +81,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
@@ -129,7 +132,12 @@ NO_ARGS_ACTION_SCHEMA = maybe_simple_id(
NO_ARGS_ACTION_SCHEMA,
synchronous=True,
)
async def mhz19_no_args_action_to_code(config, action_id, template_arg, args):
async def mhz19_no_args_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@@ -151,7 +159,12 @@ RANGE_ACTION_SCHEMA = maybe_simple_id(
RANGE_ACTION_SCHEMA,
synchronous=True,
)
async def mhz19_detection_range_set_to_code(config, action_id, template_arg, args):
async def mhz19_detection_range_set_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
detection_range = config.get(CONF_DETECTION_RANGE)
+9 -5
View File
@@ -12,7 +12,9 @@ from esphome.const import (
CONF_NUMBER,
CONF_OUTPUT,
)
from esphome.cpp_generator import MockObj
import esphome.final_validate as fv
from esphome.types import ConfigType
CONF_TOUCH_THRESHOLD = "touch_threshold"
CONF_RELEASE_THRESHOLD = "release_threshold"
@@ -49,7 +51,7 @@ CONFIG_SCHEMA = (
)
def _final_validate(config):
def _final_validate(config: ConfigType) -> None:
fconf = fv.full_config.get()
max_touch_channel = 3
if (binary_sensors := fconf.get(CONF_BINARY_SENSOR)) is not None:
@@ -71,7 +73,7 @@ def _final_validate(config):
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_touch_debounce(config[CONF_TOUCH_DEBOUNCE]))
cg.add(var.set_release_debounce(config[CONF_RELEASE_DEBOUNCE]))
@@ -82,7 +84,7 @@ async def to_code(config):
await i2c.register_i2c_device(var, config)
def validate_mode(value):
def validate_mode(value: ConfigType) -> ConfigType:
if bool(value[CONF_INPUT]) == bool(value[CONF_OUTPUT]):
raise cv.Invalid("Mode must be either input or output")
return value
@@ -105,7 +107,9 @@ MPR121_GPIO_PIN_SCHEMA = pins.gpio_base_schema(
)
def mpr121_pin_final_validate(pin_config, parent_config):
def mpr121_pin_final_validate(
pin_config: ConfigType, parent_config: ConfigType
) -> None:
if pin_config[CONF_NUMBER] <= parent_config[CONF_MAX_TOUCH_CHANNEL]:
raise cv.Invalid(
"Pin number must be higher than the max touch channel of the MPR121 component",
@@ -115,7 +119,7 @@ def mpr121_pin_final_validate(pin_config, parent_config):
@pins.PIN_SCHEMA_REGISTRY.register(
CONF_MPR121, MPR121_GPIO_PIN_SCHEMA, mpr121_pin_final_validate
)
async def mpr121_gpio_pin_to_code(config):
async def mpr121_gpio_pin_to_code(config: ConfigType) -> MockObj:
var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_MPR121])
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL
from esphome.types import ConfigType
from .. import (
CONF_MPR121_ID,
@@ -24,7 +25,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(MPR121BinarySensor).extend(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_MPR121_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))
+16 -3
View File
@@ -3,6 +3,9 @@ import esphome.codegen as cg
from esphome.components import i2c, time
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@brogon"]
DEPENDENCIES = ["i2c"]
@@ -31,7 +34,12 @@ CONFIG_SCHEMA = time.TIME_SCHEMA.extend(
),
synchronous=True,
)
async def pcf85063_write_time_to_code(config, action_id, template_arg, args):
async def pcf85063_write_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@@ -47,13 +55,18 @@ async def pcf85063_write_time_to_code(config, action_id, template_arg, args):
),
synchronous=True,
)
async def pcf85063_read_time_to_code(config, action_id, template_arg, args):
async def pcf85063_read_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
+16 -3
View File
@@ -3,6 +3,9 @@ import esphome.codegen as cg
from esphome.components import i2c, time
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@KoenBreeman"]
@@ -34,7 +37,12 @@ CONFIG_SCHEMA = time.TIME_SCHEMA.extend(
),
synchronous=True,
)
async def pcf8563_write_time_to_code(config, action_id, template_arg, args):
async def pcf8563_write_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@@ -50,13 +58,18 @@ async def pcf8563_write_time_to_code(config, action_id, template_arg, args):
),
synchronous=True,
)
async def pcf8563_read_time_to_code(config, action_id, template_arg, args):
async def pcf8563_read_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
+7 -5
View File
@@ -13,6 +13,8 @@ from esphome.const import (
CONF_NUMBER,
CONF_OUTPUT,
)
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
CODEOWNERS = ["@remcom"]
DEPENDENCIES = ["i2c"]
@@ -50,7 +52,7 @@ PCM5122_CHANNEL_MIX_ENUM = {
_validate_bits = cv.float_with_unit("bits", "bit")
def _validate_volume_range(config):
def _validate_volume_range(config: ConfigType) -> ConfigType:
if config[CONF_VOLUME_MIN_DB] >= config[CONF_VOLUME_MAX_DB]:
raise cv.Invalid(f"{CONF_VOLUME_MIN_DB} must be less than {CONF_VOLUME_MAX_DB}")
return config
@@ -90,7 +92,7 @@ CONFIG_SCHEMA = cv.All(
)
def _validate_pin_mode(value):
def _validate_pin_mode(value: ConfigType) -> ConfigType:
if not (value[CONF_INPUT] or value[CONF_OUTPUT]):
raise cv.Invalid("Mode must be either input or output")
if value[CONF_INPUT] and value[CONF_OUTPUT]:
@@ -98,7 +100,7 @@ def _validate_pin_mode(value):
return value
def _validate_pin(value):
def _validate_pin(value: ConfigType) -> ConfigType:
if value[CONF_MODE][CONF_INPUT] and value[CONF_NUMBER] == 6:
raise cv.Invalid("GPIO6 cannot be used as input on the PCM5122")
return value
@@ -120,7 +122,7 @@ PIN_SCHEMA = cv.All(
@pins.PIN_SCHEMA_REGISTRY.register(CONF_PCM5122, PIN_SCHEMA)
async def pcm5122_pin_to_code(config):
async def pcm5122_pin_to_code(config: ConfigType) -> MockObj:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_parented(var, config[CONF_PCM5122])
@@ -130,7 +132,7 @@ async def pcm5122_pin_to_code(config):
return var
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import CONF_POWER_MODE, ENTITY_CATEGORY_CONFIG
from esphome.types import ConfigType
from ..audio_dac import CONF_PCM5122, PCM5122, pcm5122_ns
@@ -26,7 +27,7 @@ CONFIG_SCHEMA = switch.switch_schema(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await switch.new_switch(config)
await cg.register_parented(var, config[CONF_PCM5122])
cg.add(var.set_power_mode(config[CONF_POWER_MODE]))
+16 -3
View File
@@ -10,6 +10,9 @@ from esphome.const import (
ICON_THERMOMETER,
STATE_CLASS_MEASUREMENT,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@SeByDocKy"]
DEPENDENCIES = ["i2c"]
@@ -72,7 +75,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
@@ -114,7 +117,12 @@ PMWCS3_CALIBRATION_SCHEMA = cv.Schema(
PMWCS3_CALIBRATION_SCHEMA,
synchronous=True,
)
async def pmwcs3_calibration_to_code(config, action_id, template_arg, args):
async def pmwcs3_calibration_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
parent = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, parent)
@@ -134,7 +142,12 @@ PMWCS3_NEW_I2C_ADDRESS_SCHEMA = cv.maybe_simple_value(
PMWCS3_NEW_I2C_ADDRESS_SCHEMA,
synchronous=True,
)
async def pmwcs3newi2caddress_to_code(config, action_id, template_arg, args):
async def pmwcs3newi2caddress_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
parent = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, parent)
address = await cg.templatable(config[CONF_ADDRESS], args, cg.int_)
+9 -4
View File
@@ -1,4 +1,6 @@
from collections.abc import Callable
import logging
from typing import Any
from esphome import pins
import esphome.codegen as cg
@@ -24,6 +26,7 @@ from esphome.const import (
UNIT_DEGREES,
UNIT_MICROTESLA,
)
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
@@ -60,7 +63,7 @@ QMC5883LOversamplings = {
}
def validate_config(config):
def validate_config(config: ConfigType) -> ConfigType:
if (
config[CONF_UPDATE_INTERVAL].total_milliseconds < 15
and CONF_DRDY_PIN not in config
@@ -72,14 +75,16 @@ def validate_config(config):
return config
def validate_enum(enum_values, units=None, int=True):
def validate_enum(
enum_values: dict[Any, Any], units: str | list[str] | None = None, int: bool = True
) -> Callable[[Any], Any]:
_units = []
if units is not None:
_units = units if isinstance(units, list) else [units]
_units = [str(x) for x in _units]
enum_bound = cv.enum(enum_values, int=int)
def validate_enum_bound(value):
def validate_enum_bound(value: Any) -> Any:
value = cv.string(value)
for unit in _units:
if value.endswith(unit):
@@ -137,7 +142,7 @@ CONFIG_SCHEMA = cv.All(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
@@ -18,7 +18,9 @@ from esphome.const import (
CONF_VALUE,
PlatformFramework,
)
from esphome.core import CORE
from esphome.core import CORE, ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
_LOGGER = logging.getLogger(__name__)
@@ -94,7 +96,7 @@ CONFIG_SCHEMA = (
)
def _validate_non_blocking(config):
def _validate_non_blocking(config: ConfigType) -> None:
if (
CORE.is_esp32
and esp32.get_esp32_variant() not in esp32_rmt.VARIANTS_NO_RMT
@@ -125,7 +127,12 @@ DIGITAL_WRITE_ACTION_SCHEMA = cv.maybe_simple_value(
DIGITAL_WRITE_ACTION_SCHEMA,
synchronous=True,
)
async def digital_write_action_to_code(config, action_id, template_arg, args):
async def digital_write_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_TRANSMITTER_ID])
template_ = await cg.templatable(config[CONF_VALUE], args, cg.bool_)
@@ -133,7 +140,7 @@ async def digital_write_action_to_code(config, action_id, template_arg, args):
return var
async def to_code(config):
async def to_code(config: ConfigType) -> None:
pin = await cg.gpio_pin_expression(config[CONF_PIN])
if CORE.is_esp32 and esp32.get_esp32_variant() not in esp32_rmt.VARIANTS_NO_RMT:
# Re-enable ESP-IDF's RMT driver (excluded by default to save compile time)
+11 -3
View File
@@ -15,6 +15,9 @@ from esphome.const import (
STATE_CLASS_MEASUREMENT,
UNIT_STEPS,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
rotary_encoder_ns = cg.esphome_ns.namespace("rotary_encoder")
@@ -44,7 +47,7 @@ RotaryEncoderSetValueAction = rotary_encoder_ns.class_(
)
def validate_min_max_value(config):
def validate_min_max_value(config: ConfigType) -> ConfigType:
if CONF_MIN_VALUE in config and CONF_MAX_VALUE in config:
min_val = config[CONF_MIN_VALUE]
max_val = config[CONF_MAX_VALUE]
@@ -92,7 +95,7 @@ _CALLBACK_AUTOMATIONS = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
@@ -126,7 +129,12 @@ async def to_code(config):
),
synchronous=True,
)
async def sensor_template_publish_to_code(config, action_id, template_arg, args):
async def sensor_template_publish_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALUE], args, cg.int_)
@@ -18,7 +18,7 @@ from esphome.types import ConfigType
from esphome.util import _LOGGER
def get_nops(timing):
def get_nops(timing: float) -> list[float | str]:
"""
Calculate the number of NOP instructions required to wait for a given amount of time.
"""
@@ -39,7 +39,7 @@ def get_nops(timing):
return nops
def generate_assembly_code(id, t0h, t0l, t1h, t1l):
def generate_assembly_code(id: str, t0h: int, t0l: int, t1h: int, t1l: int) -> str:
"""
Generate assembly code with the given timing values.
"""
@@ -125,7 +125,7 @@ writezero:
return assembly_template + const_csdk_code
def time_to_cycles(time_us):
def time_to_cycles(time_us: float) -> int:
cycles_per_us = 57.5
return round(float(time_us) * cycles_per_us)
@@ -172,7 +172,7 @@ CONF_BIT1_HIGH = "bit1_high"
CONF_BIT1_LOW = "bit1_low"
def _validate_timing(value):
def _validate_timing(value: str) -> float:
# if doesn't end with us, raise error
if not value.endswith("us"):
raise cv.Invalid("Timing must be in microseconds (us)")
+16 -3
View File
@@ -3,6 +3,9 @@ import esphome.codegen as cg
from esphome.components import i2c, time
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
CODEOWNERS = ["@beormund"]
DEPENDENCIES = ["i2c"]
@@ -29,7 +32,12 @@ CONFIG_SCHEMA = time.TIME_SCHEMA.extend(
),
synchronous=True,
)
async def rx8130_write_time_to_code(config, action_id, template_arg, args):
async def rx8130_write_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
@@ -45,13 +53,18 @@ async def rx8130_write_time_to_code(config, action_id, template_arg, args):
),
synchronous=True,
)
async def rx8130_read_time_to_code(config, action_id, template_arg, args):
async def rx8130_read_time_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
+16 -3
View File
@@ -13,6 +13,9 @@ from esphome.const import (
CONF_RESTORE,
CONF_TRANSITION_LENGTH,
)
from esphome.core import ID
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
servo_ns = cg.esphome_ns.namespace("servo")
Servo = servo_ns.class_("Servo", cg.Component)
@@ -39,7 +42,7 @@ CONFIG_SCHEMA = cv.Schema(
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
@@ -64,7 +67,12 @@ async def to_code(config):
),
synchronous=True,
)
async def servo_write_to_code(config, action_id, template_arg, args):
async def servo_write_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_LEVEL], args, cg.float_)
@@ -82,6 +90,11 @@ async def servo_write_to_code(config, action_id, template_arg, args):
),
synchronous=True,
)
async def servo_detach_to_code(config, action_id, template_arg, args):
async def servo_detach_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
+6 -4
View File
@@ -15,6 +15,8 @@ from esphome.const import (
CONF_PULLUP,
CONF_TRIGGER_ID,
)
from esphome.cpp_generator import MockObj
from esphome.types import ConfigType
CONF_KEYPAD = "keypad"
CONF_KEYS = "keys"
@@ -40,7 +42,7 @@ SX1509KeyTrigger = sx1509_ns.class_(
)
def check_keys(config):
def check_keys(config: ConfigType) -> ConfigType:
if (
CONF_KEYS in config
and len(config[CONF_KEYS]) != config[CONF_KEY_ROWS] * config[CONF_KEY_COLUMNS]
@@ -82,7 +84,7 @@ CONFIG_SCHEMA = (
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
@@ -104,7 +106,7 @@ async def to_code(config):
await automation.build_automation(trigger, [(cg.uint8, "x")], tconf)
def validate_mode(value):
def validate_mode(value: ConfigType) -> ConfigType:
if not (value[CONF_INPUT] or value[CONF_OUTPUT]):
raise cv.Invalid("Mode must be either input or output")
if value[CONF_INPUT] and value[CONF_OUTPUT]:
@@ -142,7 +144,7 @@ SX1509_PIN_SCHEMA = cv.All(
@pins.PIN_SCHEMA_REGISTRY.register(CONF_SX1509, SX1509_PIN_SCHEMA)
async def sx1509_pin_to_code(config):
async def sx1509_pin_to_code(config: ConfigType) -> MockObj:
var = cg.new_Pvariable(config[CONF_ID])
parent = await cg.get_variable(config[CONF_SX1509])
cg.add(var.set_parent(parent))
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_COL, CONF_ROW
from esphome.types import ConfigType
from .. import CONF_SX1509_ID, SX1509Component, sx1509_ns
@@ -18,7 +19,7 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(SX1509BinarySensor).extend(
)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
var = await binary_sensor.new_binary_sensor(config)
hub = await cg.get_variable(config[CONF_SX1509_ID])
cg.add(var.set_row_col(config[CONF_ROW], config[CONF_COL]))
+2 -1
View File
@@ -2,6 +2,7 @@ import esphome.codegen as cg
from esphome.components import output
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_PIN
from esphome.types import ConfigType
from .. import CONF_SX1509_ID, SX1509Component, sx1509_ns
@@ -20,7 +21,7 @@ CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
async def to_code(config: ConfigType) -> None:
parent = await cg.get_variable(config[CONF_SX1509_ID])
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)