1
0
mirror of https://github.com/esphome/esphome.git synced 2025-06-16 07:16:58 +02:00

[ruff] Apply various ruff suggestions

This commit is contained in:
Jesse Hills 2025-05-29 17:08:35 +12:00
parent 7ac5746e0d
commit a611c78a9b
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
14 changed files with 27 additions and 25 deletions

View File

@ -131,7 +131,7 @@ def position_range():
def validator(value):
is_negative_str = isinstance(value, str) and value.startswith("-")
is_negative_num = isinstance(value, (float, int)) and value < 0
is_negative_num = isinstance(value, float | int) and value < 0
if is_negative_str or is_negative_num:
return negative_validator(value)
return positive_validator(value)

View File

@ -1,4 +1,5 @@
import re
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv

View File

@ -1,10 +1,10 @@
"""CM1106 Sensor component for ESPHome."""
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
from esphome.components import sensor, uart
import esphome.config_validation as cv
from esphome.const import (
CONF_CO2,
CONF_ID,

View File

@ -325,7 +325,7 @@ def validate_value_type(value_config):
value_config[CONF_DATA] = VALUE_TYPES[type_].validate(
value, value_config[CONF_STRING_ENCODING]
)
elif isinstance(value, (float, int)):
elif isinstance(value, float | int):
raise cv.Invalid(
f'The "{CONF_TYPE}" property is required for the value "{value}"'
)

View File

@ -1,6 +1,6 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
import esphome.config_validation as cv
from esphome.const import CONF_ID
CODEOWNERS = ["@p1ngb4ck"]

View File

@ -1,8 +1,9 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import output
import esphome.config_validation as cv
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_INITIAL_VALUE
from .. import Mcp4461Component, CONF_MCP4461_ID, mcp4461_ns
from .. import CONF_MCP4461_ID, Mcp4461Component, mcp4461_ns
DEPENDENCIES = ["mcp4461"]

View File

@ -17,9 +17,9 @@ from .base_component import (
CONF_COMMAND_SPACING,
CONF_EXIT_REPARSE_ON_START,
CONF_ON_BUFFER_OVERFLOW,
CONF_ON_PAGE,
CONF_ON_SETUP,
CONF_ON_SLEEP,
CONF_ON_PAGE,
CONF_ON_WAKE,
CONF_SKIP_CONNECTION_HANDSHAKE,
CONF_START_UP_PAGE,

View File

@ -3,6 +3,8 @@ import esphome.codegen as cg
from esphome.components import i2c, sensirion_common, sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_AMBIENT_PRESSURE_COMPENSATION,
CONF_AUTOMATIC_SELF_CALIBRATION,
CONF_CO2,
CONF_HUMIDITY,
CONF_ID,
@ -18,8 +20,6 @@ from esphome.const import (
UNIT_CELSIUS,
UNIT_PARTS_PER_MILLION,
UNIT_PERCENT,
CONF_AUTOMATIC_SELF_CALIBRATION,
CONF_AMBIENT_PRESSURE_COMPENSATION,
)
DEPENDENCIES = ["i2c"]

View File

@ -4,9 +4,13 @@ import esphome.codegen as cg
from esphome.components import i2c, sensirion_common, sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_AMBIENT_PRESSURE_COMPENSATION,
CONF_AMBIENT_PRESSURE_COMPENSATION_SOURCE,
CONF_AUTOMATIC_SELF_CALIBRATION,
CONF_CO2,
CONF_HUMIDITY,
CONF_ID,
CONF_MEASUREMENT_MODE,
CONF_TEMPERATURE,
CONF_TEMPERATURE_OFFSET,
CONF_VALUE,
@ -20,10 +24,6 @@ from esphome.const import (
UNIT_CELSIUS,
UNIT_PARTS_PER_MILLION,
UNIT_PERCENT,
CONF_AUTOMATIC_SELF_CALIBRATION,
CONF_AMBIENT_PRESSURE_COMPENSATION,
CONF_AMBIENT_PRESSURE_COMPENSATION_SOURCE,
CONF_MEASUREMENT_MODE,
)
CODEOWNERS = ["@sjtrny", "@martgras"]

View File

@ -2,10 +2,10 @@ import esphome.codegen as cg
from esphome.components import i2c, sensirion_common, sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_MEASUREMENT_MODE,
DEVICE_CLASS_PRESSURE,
STATE_CLASS_MEASUREMENT,
UNIT_HECTOPASCAL,
CONF_MEASUREMENT_MODE,
)
DEPENDENCIES = ["i2c"]

View File

@ -105,7 +105,7 @@ def _substitute_item(substitutions, item, path, ignore_missing):
sub = _expand_substitutions(substitutions, item, path, ignore_missing)
if sub != item:
return sub
elif isinstance(item, (core.Lambda, Extend, Remove)):
elif isinstance(item, core.Lambda | Extend | Remove):
sub = _expand_substitutions(substitutions, item.value, path, ignore_missing)
if sub != item:
item.value = sub

View File

@ -964,7 +964,7 @@ def line_info(config, path, highlight=True):
def _print_on_next_line(obj):
if isinstance(obj, (list, tuple, dict)):
if isinstance(obj, list | tuple | dict):
return True
if isinstance(obj, str):
return len(obj) > 80
@ -985,7 +985,7 @@ def dump_dict(
if error is not None:
ret += f"\n{color(AnsiFore.BOLD_RED, _format_vol_invalid(error, config))}\n"
if isinstance(conf, (list, tuple)):
if isinstance(conf, list | tuple):
multiline = True
if not conf:
ret += "[]"

View File

@ -311,7 +311,7 @@ def string(value):
"60.0" (string). For values where this could be a problem `string_strict` has to be used.
"""
check_not_templatable(value)
if isinstance(value, (dict, list)):
if isinstance(value, dict | list):
raise Invalid("string value cannot be dictionary or list.")
if isinstance(value, bool):
raise Invalid(
@ -462,9 +462,9 @@ def hex_int_range(min=None, max=None, min_included=True, max_included=True):
def float_range(min=None, max=None, min_included=True, max_included=True):
"""Validate that the config option is a floating point number in the given range."""
if min is not None:
assert isinstance(min, (int, float))
assert isinstance(min, int | float)
if max is not None:
assert isinstance(max, (int, float))
assert isinstance(max, int | float)
return All(
float_,
Range(min=min, max=max, min_included=min_included, max_included=max_included),
@ -1348,7 +1348,7 @@ def valid(value):
@contextmanager
def prepend_path(path):
"""A contextmanager helper to prepend a path to all voluptuous errors."""
if not isinstance(path, (list, tuple)):
if not isinstance(path, list | tuple):
path = [path]
try:
yield
@ -1360,7 +1360,7 @@ def prepend_path(path):
@contextmanager
def remove_prepend_path(path):
"""A contextmanager helper to remove a path from a voluptuous error."""
if not isinstance(path, (list, tuple)):
if not isinstance(path, list | tuple):
path = [path]
try:
yield

View File

@ -173,7 +173,7 @@ def check_error(data, expect):
)
if dat == RESPONSE_ERROR_UNKNOWN:
raise OTAError("Unknown error from ESP")
if not isinstance(expect, (list, tuple)):
if not isinstance(expect, list | tuple):
expect = [expect]
if dat not in expect:
raise OTAError(f"Unexpected response from ESP: 0x{data[0]:02X}")
@ -181,7 +181,7 @@ def check_error(data, expect):
def send_check(sock, data, msg):
try:
if isinstance(data, (list, tuple)):
if isinstance(data, list | tuple):
data = bytes(data)
elif isinstance(data, int):
data = bytes([data])