1
0
mirror of https://github.com/esphome/esphome.git synced 2024-12-02 13:58:48 +01:00

[code-quality] Organise core imports (#7149)

This commit is contained in:
Jesse Hills 2024-07-29 14:07:44 +12:00 committed by GitHub
parent 20c2246533
commit e64709c37e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 190 additions and 186 deletions

View File

@ -1,12 +1,12 @@
# PYTHON_ARGCOMPLETE_OK
import argparse
from datetime import datetime
import functools
import logging
import os
import re
import sys
import time
from datetime import datetime
import argcomplete
@ -39,14 +39,14 @@ from esphome.const import (
)
from esphome.core import CORE, EsphomeError, coroutine
from esphome.helpers import indent, is_ip_address
from esphome.log import Fore, color, setup_log
from esphome.util import (
get_serial_ports,
list_yaml_files,
run_external_command,
run_external_process,
safe_print,
list_yaml_files,
get_serial_ports,
)
from esphome.log import color, setup_log, Fore
_LOGGER = logging.getLogger(__name__)
@ -116,6 +116,7 @@ def get_port_type(port):
def run_miniterm(config, port):
import serial
from esphome import platformio_api
if CONF_LOGGER not in config:
@ -596,9 +597,10 @@ def command_update_all(args):
def command_idedata(args, config):
from esphome import platformio_api
import json
from esphome import platformio_api
logging.disable(logging.INFO)
logging.disable(logging.WARNING)

View File

@ -7,10 +7,10 @@ from esphome.const import (
CONF_ELSE,
CONF_ID,
CONF_THEN,
CONF_TIME,
CONF_TIMEOUT,
CONF_TRIGGER_ID,
CONF_TYPE_ID,
CONF_TIME,
CONF_UPDATE_INTERVAL,
)
from esphome.schema_extractors import SCHEMA_EXTRACT, schema_extractor

View File

@ -8,55 +8,78 @@
# want to break suddenly due to a rename (this file will get backports for features).
# pylint: disable=unused-import
from esphome.cpp_generator import ( # noqa
from esphome.cpp_generator import ( # noqa: F401
ArrayInitializer,
Expression,
LineComment,
MockObj,
MockObjClass,
Pvariable,
RawExpression,
RawStatement,
TemplateArguments,
StructInitializer,
ArrayInitializer,
safe_exp,
Statement,
LineComment,
progmem_array,
static_const_array,
statement,
variable,
with_local_variable,
new_variable,
Pvariable,
new_Pvariable,
StructInitializer,
TemplateArguments,
add,
add_global,
add_library,
add_build_flag,
add_define,
add_global,
add_library,
add_platformio_option,
get_variable,
get_variable_with_full_id,
process_lambda,
is_template,
new_Pvariable,
new_variable,
process_lambda,
progmem_array,
safe_exp,
statement,
static_const_array,
templatable,
MockObj,
MockObjClass,
variable,
with_local_variable,
)
from esphome.cpp_helpers import ( # noqa
gpio_pin_expression,
register_component,
from esphome.cpp_helpers import ( # noqa: F401
build_registry_entry,
build_registry_list,
extract_registry_entry_config,
register_parented,
gpio_pin_expression,
past_safe_mode,
register_component,
register_parented,
)
from esphome.cpp_types import ( # noqa
global_ns,
void,
nullptr,
float_,
double,
from esphome.cpp_types import ( # noqa: F401
NAN,
App,
Application,
Component,
ComponentPtr,
Controller,
EntityBase,
EntityCategory,
ESPTime,
GPIOPin,
InternalGPIOPin,
JsonObject,
JsonObjectConst,
Parented,
PollingComponent,
arduino_json_ns,
bool_,
const_char_ptr,
double,
esphome_ns,
float_,
global_ns,
gpio_Flags,
int16,
int32,
int64,
int_,
nullptr,
optional,
size_t,
std_ns,
std_shared_ptr,
std_string,
@ -66,28 +89,5 @@ from esphome.cpp_types import ( # noqa
uint16,
uint32,
uint64,
int16,
int32,
int64,
size_t,
const_char_ptr,
NAN,
esphome_ns,
App,
EntityBase,
Component,
ComponentPtr,
PollingComponent,
Application,
optional,
arduino_json_ns,
JsonObject,
JsonObjectConst,
Controller,
GPIOPin,
InternalGPIOPin,
gpio_Flags,
EntityCategory,
Parented,
ESPTime,
void,
)

View File

@ -1,40 +1,38 @@
from __future__ import annotations
import abc
from contextlib import contextmanager
import contextvars
import functools
import heapq
import logging
import re
from typing import Union, Any
from contextlib import contextmanager
import contextvars
from typing import Any, Union
import voluptuous as vol
from esphome import core, yaml_util, loader, pins
import esphome.core.config as core_config
from esphome import core, loader, pins, yaml_util
from esphome.config_helpers import Extend, Remove
import esphome.config_validation as cv
from esphome.const import (
CONF_ESPHOME,
CONF_ID,
CONF_PLATFORM,
CONF_PACKAGES,
CONF_SUBSTITUTIONS,
CONF_EXTERNAL_COMPONENTS,
CONF_ID,
CONF_PACKAGES,
CONF_PLATFORM,
CONF_SUBSTITUTIONS,
TARGET_PLATFORMS,
)
from esphome.core import CORE, EsphomeError, DocumentRange
from esphome.helpers import indent
from esphome.util import safe_print, OrderedDict
from esphome.config_helpers import Extend, Remove
from esphome.loader import get_component, get_platform, ComponentManifest
from esphome.yaml_util import is_secret, ESPHomeDataBase, ESPForceValue
from esphome.voluptuous_schema import ExtraKeysInvalid
from esphome.log import color, Fore
from esphome.core import CORE, DocumentRange, EsphomeError
import esphome.core.config as core_config
import esphome.final_validate as fv
import esphome.config_validation as cv
from esphome.types import ConfigType, ConfigFragmentType
from esphome.helpers import indent
from esphome.loader import ComponentManifest, get_component, get_platform
from esphome.log import Fore, color
from esphome.types import ConfigFragmentType, ConfigType
from esphome.util import OrderedDict, safe_print
from esphome.voluptuous_schema import ExtraKeysInvalid
from esphome.yaml_util import ESPForceValue, ESPHomeDataBase, is_secret
_LOGGER = logging.getLogger(__name__)

View File

@ -1,13 +1,13 @@
"""Helpers for config validation using voluptuous."""
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import datetime
import logging
import os
import re
from contextlib import contextmanager
import uuid as uuid_
from datetime import datetime
from string import ascii_letters, digits
import uuid as uuid_
import voluptuous as vol
@ -17,37 +17,37 @@ from esphome.config_helpers import Extend, Remove
from esphome.const import (
ALLOWED_NAME_CHARS,
CONF_AVAILABILITY,
CONF_COMMAND_TOPIC,
CONF_COMMAND_RETAIN,
CONF_COMMAND_TOPIC,
CONF_DAY,
CONF_DISABLED_BY_DEFAULT,
CONF_DISCOVERY,
CONF_ENTITY_CATEGORY,
CONF_HOUR,
CONF_ICON,
CONF_ID,
CONF_INTERNAL,
CONF_MINUTE,
CONF_MONTH,
CONF_NAME,
CONF_PASSWORD,
CONF_PATH,
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
CONF_RETAIN,
CONF_QOS,
CONF_REF,
CONF_RETAIN,
CONF_SECOND,
CONF_SETUP_PRIORITY,
CONF_STATE_TOPIC,
CONF_TOPIC,
CONF_YEAR,
CONF_MONTH,
CONF_DAY,
CONF_HOUR,
CONF_MINUTE,
CONF_SECOND,
CONF_VALUE,
CONF_UPDATE_INTERVAL,
CONF_TYPE_ID,
CONF_TYPE,
CONF_REF,
CONF_TYPE_ID,
CONF_UPDATE_INTERVAL,
CONF_URL,
CONF_PATH,
CONF_USERNAME,
CONF_PASSWORD,
CONF_VALUE,
CONF_YEAR,
ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC,
ENTITY_CATEGORY_NONE,
@ -71,15 +71,15 @@ from esphome.core import (
TimePeriod,
TimePeriodMicroseconds,
TimePeriodMilliseconds,
TimePeriodMinutes,
TimePeriodNanoseconds,
TimePeriodSeconds,
TimePeriodMinutes,
)
from esphome.helpers import list_starts_with, add_class_to_obj
from esphome.helpers import add_class_to_obj, list_starts_with
from esphome.schema_extractors import (
SCHEMA_EXTRACT,
schema_extractor_list,
schema_extractor,
schema_extractor_list,
schema_extractor_registry,
schema_extractor_typed,
)
@ -1686,9 +1686,9 @@ class SplitDefault(Optional):
if CORE.is_esp32:
from esphome.components.esp32 import get_esp32_variant
from esphome.components.esp32.const import (
VARIANT_ESP32C3,
VARIANT_ESP32S2,
VARIANT_ESP32S3,
VARIANT_ESP32C3,
)
variant = get_esp32_variant()

View File

@ -7,26 +7,29 @@ from typing import TYPE_CHECKING, Optional, Union
from esphome.const import (
CONF_COMMENT,
CONF_ESPHOME,
CONF_USE_ADDRESS,
CONF_ETHERNET,
CONF_PORT,
CONF_USE_ADDRESS,
CONF_WEB_SERVER,
CONF_WIFI,
CONF_PORT,
KEY_CORE,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
PLATFORM_BK72XX,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_BK72XX,
PLATFORM_RTL87XX,
PLATFORM_RP2040,
PLATFORM_HOST,
PLATFORM_RP2040,
PLATFORM_RTL87XX,
)
from esphome.coroutine import FakeAwaitable as _FakeAwaitable
from esphome.coroutine import FakeEventLoop as _FakeEventLoop
# pylint: disable=unused-import
from esphome.coroutine import coroutine, coroutine_with_priority # noqa
from esphome.coroutine import ( # noqa: F401
FakeAwaitable as _FakeAwaitable,
FakeEventLoop as _FakeEventLoop,
coroutine,
coroutine_with_priority,
)
from esphome.helpers import ensure_unique_string, get_str_env, is_ha_addon
from esphome.util import OrderedDict

View File

@ -3,9 +3,9 @@ import multiprocessing
import os
import re
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import (
CONF_ARDUINO_VERSION,
CONF_AREA,
@ -16,11 +16,11 @@ from esphome.const import (
CONF_COMPILE_PROCESS_LIMIT,
CONF_ESPHOME,
CONF_FRAMEWORK,
CONF_FRIENDLY_NAME,
CONF_INCLUDES,
CONF_LIBRARIES,
CONF_MIN_VERSION,
CONF_NAME,
CONF_FRIENDLY_NAME,
CONF_ON_BOOT,
CONF_ON_LOOP,
CONF_ON_SHUTDOWN,
@ -34,8 +34,8 @@ from esphome.const import (
CONF_TYPE,
CONF_VERSION,
KEY_CORE,
TARGET_PLATFORMS,
PLATFORM_ESP8266,
TARGET_PLATFORMS,
__version__ as ESPHOME_VERSION,
)
from esphome.core import CORE, coroutine_with_priority

View File

@ -1,6 +1,5 @@
import esphome.final_validate as fv
from esphome.const import CONF_ID
import esphome.final_validate as fv
def inherit_property_from(property_to_inherit, parent_id_property, transform=None):

View File

@ -43,13 +43,13 @@ the last `yield` expression defines what is returned.
"""
import collections
from collections.abc import Awaitable, Generator, Iterator
import functools
import heapq
import inspect
import logging
import types
from typing import Any, Callable
from collections.abc import Awaitable, Generator, Iterator
_LOGGER = logging.getLogger(__name__)

View File

@ -1,8 +1,8 @@
import abc
from collections.abc import Sequence
import inspect
import math
import re
from collections.abc import Sequence
from typing import Any, Callable, Optional, Union
from esphome.core import (

View File

@ -12,15 +12,13 @@ from esphome.const import (
CONF_UPDATE_INTERVAL,
KEY_PAST_SAFE_MODE,
)
from esphome.core import coroutine, ID, CORE
from esphome.core import CORE, ID, coroutine
from esphome.coroutine import FakeAwaitable
from esphome.types import ConfigType, ConfigFragmentType
from esphome.cpp_generator import add, get_variable
from esphome.cpp_types import App
from esphome.helpers import sanitize, snake_case
from esphome.types import ConfigFragmentType, ConfigType
from esphome.util import Registry, RegistryEntry
from esphome.helpers import snake_case, sanitize
_LOGGER = logging.getLogger(__name__)

View File

@ -1,13 +1,13 @@
from __future__ import annotations
import asyncio
from collections.abc import Coroutine
import contextlib
import logging
import threading
from dataclasses import dataclass
from functools import partial
import logging
import threading
from typing import TYPE_CHECKING, Any, Callable
from collections.abc import Coroutine
from ..zeroconf import DiscoveredImport
from .dns import DNSCache

View File

@ -1,14 +1,14 @@
from __future__ import annotations
import asyncio
from asyncio import events
from concurrent.futures import ThreadPoolExecutor
import logging
import os
import socket
import threading
import traceback
from asyncio import events
from concurrent.futures import ThreadPoolExecutor
from time import monotonic
import traceback
from typing import Any
from esphome.storage_json import EsphomeStorageJSON, esphome_storage_path

View File

@ -1,9 +1,9 @@
from __future__ import annotations
import asyncio
from collections import defaultdict
import logging
import os
from collections import defaultdict
from typing import TYPE_CHECKING, Any
from esphome import const, util

View File

@ -1,7 +1,7 @@
import logging
import os
import tempfile
from pathlib import Path
import tempfile
_LOGGER = logging.getLogger(__name__)

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio
import base64
from collections.abc import Iterable
import datetime
import functools
import gzip
@ -9,13 +10,12 @@ import hashlib
import json
import logging
import os
from pathlib import Path
import secrets
import shutil
import subprocess
import threading
import time
from collections.abc import Iterable
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, TypeVar
from urllib.parse import urlparse
@ -26,13 +26,13 @@ import tornado.httpserver
import tornado.httputil
import tornado.ioloop
import tornado.iostream
from tornado.log import access_log
import tornado.netutil
import tornado.process
import tornado.queues
import tornado.web
import tornado.websocket
import yaml
from tornado.log import access_log
from yaml.nodes import Node
from esphome import const, platformio_api, yaml_util

View File

@ -1,13 +1,15 @@
from __future__ import annotations
import logging
from pathlib import Path
import os
from datetime import datetime
import logging
import os
from pathlib import Path
import requests
import esphome.config_validation as cv
from esphome.core import CORE, TimePeriodSeconds
from esphome.const import __version__
from esphome.core import CORE, TimePeriodSeconds
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@landonr"]

View File

@ -1,9 +1,9 @@
from abc import ABC, abstractmethod
from typing import Any
import contextvars
from typing import Any
from esphome.types import ConfigFragmentType, ID, ConfigPathType
import esphome.config_validation as cv
from esphome.types import ID, ConfigFragmentType, ConfigPathType
class FinalValidateConfig(ABC):

View File

@ -1,12 +1,12 @@
import hashlib
import logging
import re
import subprocess
import urllib.parse
from dataclasses import dataclass
from datetime import datetime
import hashlib
import logging
from pathlib import Path
import re
import subprocess
from typing import Callable, Optional
import urllib.parse
import esphome.config_validation as cv
from esphome.core import CORE, TimePeriodSeconds

View File

@ -1,14 +1,13 @@
import codecs
from contextlib import suppress
import logging
import os
import platform
from pathlib import Path
from typing import Union
import tempfile
from urllib.parse import urlparse
import platform
import re
import tempfile
from typing import Union
from urllib.parse import urlparse
_LOGGER = logging.getLogger(__name__)
@ -129,9 +128,10 @@ def _resolve_with_zeroconf(host):
def resolve_ip_address(host):
from esphome.core import EsphomeError
import socket
from esphome.core import EsphomeError
errs = []
if host.endswith(".local"):

View File

@ -1,10 +1,10 @@
from datetime import datetime
import hashlib
import json
import logging
import ssl
import sys
import time
import json
import paho.mqtt.client as mqtt
@ -24,9 +24,9 @@ from esphome.const import (
CONF_USERNAME,
)
from esphome.core import CORE, EsphomeError
from esphome.log import color, Fore
from esphome.helpers import get_int_env, get_str_env
from esphome.log import Fore, color
from esphome.util import safe_print
from esphome.helpers import get_str_env, get_int_env
_LOGGER = logging.getLogger(__name__)

View File

@ -1,20 +1,20 @@
import operator
from functools import reduce
import esphome.config_validation as cv
from esphome.core import CORE
import operator
import esphome.config_validation as cv
from esphome.const import (
CONF_ALLOW_OTHER_USES,
CONF_IGNORE_STRAPPING_WARNING,
CONF_INPUT,
CONF_INVERTED,
CONF_MODE,
CONF_NUMBER,
CONF_OPEN_DRAIN,
CONF_OUTPUT,
CONF_PULLDOWN,
CONF_PULLUP,
CONF_IGNORE_STRAPPING_WARNING,
CONF_ALLOW_OTHER_USES,
CONF_INVERTED,
)
from esphome.core import CORE
class PinRegistry(dict):

View File

@ -1,12 +1,11 @@
from dataclasses import dataclass
import json
from typing import Union
from pathlib import Path
import logging
import os
from pathlib import Path
import re
import subprocess
from typing import Union
from esphome.const import CONF_COMPILE_PROCESS_LIMIT, CONF_ESPHOME, KEY_CORE
from esphome.core import CORE, EsphomeError
@ -20,9 +19,10 @@ def patch_structhash():
# removed/added. This might have unintended consequences, but this improves compile
# times greatly when adding/removing components and a simple clean build solves
# all issues
from platformio.run import helpers, cli
from os.path import join, isdir, getmtime
from os import makedirs
from os.path import getmtime, isdir, join
from platformio.run import cli, helpers
def patched_clean_build_dir(build_dir, *args):
from platformio import fs

View File

@ -1,10 +1,11 @@
from __future__ import annotations
import binascii
import codecs
from datetime import datetime
import json
import logging
import os
from datetime import datetime
from esphome import const
from esphome.const import CONF_DISABLED, CONF_MDNS

View File

@ -2,7 +2,7 @@
from typing import Union
from esphome.core import ID, Lambda, EsphomeCore
from esphome.core import ID, EsphomeCore, Lambda
ConfigFragmentType = Union[
str,

View File

@ -1,13 +1,12 @@
from typing import Union
import collections
import io
import logging
import os
from pathlib import Path
import re
import subprocess
import sys
from pathlib import Path
from typing import Union
from esphome import const

View File

@ -2,6 +2,7 @@ import difflib
import itertools
import voluptuous as vol
from esphome.schema_extractors import schema_extractor_extended

View File

@ -1,13 +1,14 @@
from __future__ import annotations
from io import StringIO
import json
import os
from io import StringIO
from typing import Any
from esphome.yaml_util import parse_yaml
from esphome.config import validate_config, _format_vol_invalid, Config
from esphome.core import CORE, DocumentRange
from esphome.config import Config, _format_vol_invalid, validate_config
import esphome.config_validation as cv
from esphome.core import CORE, DocumentRange
from esphome.yaml_util import parse_yaml
def _get_invalid_range(res: Config, invalid: cv.Invalid) -> DocumentRange | None:

View File

@ -276,8 +276,8 @@ def wizard(path):
from esphome.components.bk72xx import boards as bk72xx_boards
from esphome.components.esp32 import boards as esp32_boards
from esphome.components.esp8266 import boards as esp8266_boards
from esphome.components.rtl87xx import boards as rtl87xx_boards
from esphome.components.rp2040 import boards as rp2040_boards
from esphome.components.rtl87xx import boards as rtl87xx_boards
if not path.endswith(".yaml") and not path.endswith(".yml"):
safe_print(

View File

@ -1,27 +1,27 @@
import logging
import os
import re
from pathlib import Path
import re
from typing import Union
from esphome.config import iter_components, iter_component_configs
from esphome import loader
from esphome.config import iter_component_configs, iter_components
from esphome.const import (
ENV_NOGITIGNORE,
HEADER_FILE_EXTENSIONS,
SOURCE_FILE_EXTENSIONS,
__version__,
ENV_NOGITIGNORE,
)
from esphome.core import CORE, EsphomeError
from esphome.helpers import (
mkdir_p,
read_file,
write_file_if_changed,
walk_files,
copy_file_if_changed,
get_bool_env,
mkdir_p,
read_file,
walk_files,
write_file_if_changed,
)
from esphome.storage_json import StorageJSON, storage_path
from esphome import loader
_LOGGER = logging.getLogger(__name__)

View File

@ -3,16 +3,16 @@ from __future__ import annotations
import fnmatch
import functools
import inspect
from io import TextIOWrapper
import logging
import math
import os
import uuid
from io import TextIOWrapper
from typing import Any
import uuid
import yaml
import yaml.constructor
from yaml import SafeLoader as PurePythonLoader
import yaml.constructor
try:
from yaml import CSafeLoader as FastestAvailableSafeLoader

View File

@ -1,8 +1,8 @@
from __future__ import annotations
import asyncio
import logging
from dataclasses import dataclass
import logging
from typing import Callable
from zeroconf import IPVersion, ServiceInfo, ServiceStateChange, Zeroconf