diff --git a/esphome/components/display/__init__.py b/esphome/components/display/__init__.py index 12c63231e7..81f2536e96 100644 --- a/esphome/components/display/__init__.py +++ b/esphome/components/display/__init__.py @@ -51,8 +51,7 @@ DISPLAY_ROTATIONS = { def validate_rotation(value): value = cv.string(value) - if value.endswith("°"): - value = value[:-1] + value = value.removesuffix("°") return cv.enum(DISPLAY_ROTATIONS, int=True)(value) diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index d718a7a612..d8c95d75f2 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -409,7 +409,7 @@ def validate_printf(value): [cCdiouxXeEfgGaAnpsSZ] # type ) """ # noqa - matches = re.findall(cfmt, value[CONF_FORMAT], flags=re.X) + matches = re.findall(cfmt, value[CONF_FORMAT], flags=re.VERBOSE) if len(matches) != len(value[CONF_ARGS]): raise cv.Invalid( f"Found {len(matches)} printf-patterns ({', '.join(matches)}), but {len(value[CONF_ARGS])} args were given!" diff --git a/esphome/components/lvgl/helpers.py b/esphome/components/lvgl/helpers.py index e04a0105d5..8d5b6354bb 100644 --- a/esphome/components/lvgl/helpers.py +++ b/esphome/components/lvgl/helpers.py @@ -33,7 +33,7 @@ def validate_printf(value): [cCdiouxXeEfgGaAnpsSZ] # type ) """ # noqa - matches = re.findall(cfmt, value[CONF_FORMAT], flags=re.X) + matches = re.findall(cfmt, value[CONF_FORMAT], flags=re.VERBOSE) if len(matches) != len(value[CONF_ARGS]): raise cv.Invalid( f"Found {len(matches)} printf-patterns ({', '.join(matches)}), but {len(value[CONF_ARGS])} args were given!" diff --git a/esphome/components/speaker/media_player/__init__.py b/esphome/components/speaker/media_player/__init__.py index 16dcc855c3..1c2e7dc0e1 100644 --- a/esphome/components/speaker/media_player/__init__.py +++ b/esphome/components/speaker/media_player/__init__.py @@ -155,8 +155,7 @@ def _read_audio_file_and_type(file_config): import puremagic file_type: str = puremagic.from_string(data) - if file_type.startswith("."): - file_type = file_type[1:] + file_type = file_type.removeprefix(".") media_file_type = audio.AUDIO_FILE_TYPE_ENUM["NONE"] if file_type in ("wav"): diff --git a/esphome/components/stepper/__init__.py b/esphome/components/stepper/__init__.py index 66fe88c6c4..c234388e7e 100644 --- a/esphome/components/stepper/__init__.py +++ b/esphome/components/stepper/__init__.py @@ -27,8 +27,7 @@ SetDecelerationAction = stepper_ns.class_("SetDecelerationAction", automation.Ac def validate_acceleration(value): value = cv.string(value) for suffix in ("steps/s^2", "steps/s*s", "steps/s/s", "steps/ss", "steps/(s*s)"): - if value.endswith(suffix): - value = value[: -len(suffix)] + value = value.removesuffix(suffix) if value == "inf": return 1e6 @@ -48,8 +47,7 @@ def validate_acceleration(value): def validate_speed(value): value = cv.string(value) for suffix in ("steps/s", "steps/s"): - if value.endswith(suffix): - value = value[: -len(suffix)] + value = value.removesuffix(suffix) if value == "inf": return 1e6 diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 72aadcb139..34e4eec1ee 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -771,8 +771,7 @@ class MockObj(Expression): if attr.startswith("P") and self.op not in ["::", ""]: attr = attr[1:] next_op = "->" - if attr.startswith("_"): - attr = attr[1:] + attr = attr.removeprefix("_") return MockObj(f"{self.base}{self.op}{attr}", next_op) def __call__(self, *args: SafeExpType) -> "MockObj": diff --git a/esphome/writer.py b/esphome/writer.py index 2c2e00b513..b5c834722a 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -76,7 +76,7 @@ def get_include_text(): def replace_file_content(text, pattern, repl): - content_new, count = re.subn(pattern, repl, text, flags=re.M) + content_new, count = re.subn(pattern, repl, text, flags=re.MULTILINE) return content_new, count diff --git a/pyproject.toml b/pyproject.toml index 742cd6e83d..200f51a873 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,6 +113,7 @@ exclude = ['generated'] select = [ "E", # pycodestyle "F", # pyflakes/autoflake + "FURB", # refurb "I", # isort "PERF", # performance "PL", # pylint diff --git a/script/generate-esp32-boards.py b/script/generate-esp32-boards.py index 83d0f0c3e0..3f444ed455 100755 --- a/script/generate-esp32-boards.py +++ b/script/generate-esp32-boards.py @@ -64,8 +64,10 @@ def main(): for line in lines: if line.startswith("BOARDS = {"): f.write("BOARDS = {\n") - for board, info in sorted(boards.items()): - f.write(TEMPLATE % (board, info["name"], info["variant"])) + f.writelines( + TEMPLATE % (board, info["name"], info["variant"]) + for board, info in sorted(boards.items()) + ) f.write("}\n") break