From 4a459e3d0dfc28088beeb68fa5d60ff61c9cc642 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 22 Aug 2026 14:52:38 -0500 Subject: [PATCH] Lift the remaining function-local imports in test_main to top level --- tests/unit_tests/test_main.py | 36 ++++++++--------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/tests/unit_tests/test_main.py b/tests/unit_tests/test_main.py index 5eb48cb725..a47edb2ece 100644 --- a/tests/unit_tests/test_main.py +++ b/tests/unit_tests/test_main.py @@ -11,24 +11,28 @@ from pathlib import Path import re import sys import time +from types import SimpleNamespace from typing import Any, Self from unittest.mock import AsyncMock, MagicMock, Mock, patch import pytest from pytest import CaptureFixture +import serial from zeroconf import ServiceStateChange -from esphome import __main__ as main +from esphome import __main__ as main, yaml_util from esphome.__main__ import ( Purpose, _get_configured_xtal_freq, _make_crystal_freq_callback, _redact_with_legacy_fallback, _resolve_network_devices, + _should_subscribe_states, _split_network_devices, _unresolved_default_error, _validate_bootloader_binary, _validate_partition_table_binary, + _wrap_to_code, check_permissions, choose_upload_log_host, command_analyze_memory, @@ -67,19 +71,21 @@ from esphome.__main__ import ( ) from esphome.address_cache import AddressCache from esphome.bundle import BUNDLE_EXTENSION, BundleFile, BundleResult -from esphome.components import esp32, esp8266 +from esphome.components import esp32, esp8266, mqtt from esphome.components.esp32 import ( KEY_ESP32, KEY_VARIANT, VARIANT_ESP32, get_esp32_variant, ) +from esphome.config import Config from esphome.const import ( CONF_API, CONF_AUTH, CONF_BAUD_RATE, CONF_BROKER, CONF_DISABLED, + CONF_DISCOVER_IP, CONF_ESPHOME, CONF_LEVEL, CONF_LOG, @@ -565,8 +571,6 @@ def test_command_config__no_defaults_dumps_user_snapshot( ) -> None: """``--no-defaults`` dumps ``config.user_config`` instead of the validated config, so schema defaults don't leak into the output.""" - from esphome.config import Config - setup_core(tmp_path=tmp_path, config={"esphome": {"name": "test"}}) args = MockArgs() args.show_secrets = True @@ -619,8 +623,6 @@ def test_command_config__no_defaults_skips_strip_default_ids( ) -> None: """When ``--no-defaults`` is set, ``strip_default_ids`` isn't run -- the user snapshot is already free of schema-injected IDs.""" - from esphome.config import Config - setup_core(tmp_path=tmp_path, config={"esphome": {"name": "test"}}) args = MockArgs() args.show_secrets = True @@ -3438,9 +3440,6 @@ def test_get_port_type() -> None: def test_mqtt_reexports_discover_ip() -> None: """The old import path must keep working for external code.""" - from esphome.components import mqtt - from esphome.const import CONF_DISCOVER_IP - assert mqtt.CONF_DISCOVER_IP is CONF_DISCOVER_IP @@ -5907,8 +5906,6 @@ class MockSerial: chunk = self.chunks[self.chunk_index] if chunk is MOCK_SERIAL_END: # Sentinel means we're done - simulate port closed - import serial - raise serial.SerialException("Port closed") # Respect the requested size and keep any remaining bytes if size <= 0: @@ -5922,8 +5919,6 @@ class MockSerial: # Entire chunk consumed; advance to the next one self.chunk_index += 1 return data # type: ignore[return-value] - import serial - raise serial.SerialException("Port closed") @@ -6782,8 +6777,6 @@ def test_parse_args_argcomplete_only_runs_when_completing() -> None: def test_should_subscribe_states_default() -> None: """Test that states are shown by default when nothing is set.""" - from esphome.__main__ import _should_subscribe_states - args = parse_args(["esphome", "logs", "device.yaml"]) with patch.dict(os.environ, {}, clear=False): os.environ.pop("ESPHOME_LOG_STATES", None) @@ -6792,8 +6785,6 @@ def test_should_subscribe_states_default() -> None: def test_should_subscribe_states_env_suppresses() -> None: """Test that ESPHOME_LOG_STATES=false suppresses states by default.""" - from esphome.__main__ import _should_subscribe_states - args = parse_args(["esphome", "logs", "device.yaml"]) with patch.dict(os.environ, {"ESPHOME_LOG_STATES": "false"}): assert _should_subscribe_states(args) is False @@ -6801,8 +6792,6 @@ def test_should_subscribe_states_env_suppresses() -> None: def test_should_subscribe_states_env_enables() -> None: """Test that ESPHOME_LOG_STATES=true enables states by default.""" - from esphome.__main__ import _should_subscribe_states - args = parse_args(["esphome", "logs", "device.yaml"]) with patch.dict(os.environ, {"ESPHOME_LOG_STATES": "true"}): assert _should_subscribe_states(args) is True @@ -6810,8 +6799,6 @@ def test_should_subscribe_states_env_enables() -> None: def test_should_subscribe_states_flag_overrides_env() -> None: """Test that --states overrides ESPHOME_LOG_STATES=false.""" - from esphome.__main__ import _should_subscribe_states - args = parse_args(["esphome", "logs", "--states", "device.yaml"]) with patch.dict(os.environ, {"ESPHOME_LOG_STATES": "false"}): assert _should_subscribe_states(args) is True @@ -6819,8 +6806,6 @@ def test_should_subscribe_states_flag_overrides_env() -> None: def test_should_subscribe_states_no_flag_overrides_env() -> None: """Test that --no-states overrides ESPHOME_LOG_STATES=true.""" - from esphome.__main__ import _should_subscribe_states - args = parse_args(["esphome", "logs", "--no-states", "device.yaml"]) with patch.dict(os.environ, {"ESPHOME_LOG_STATES": "true"}): assert _should_subscribe_states(args) is False @@ -7214,11 +7199,6 @@ async def test_wrap_to_code_comment_is_insertion_order_independent() -> None: """The config comment dumps with sorted keys: voluptuous fills schema defaults in set-iteration order, so an unsorted dump would churn main.cpp and relink the firmware on every run.""" - from types import SimpleNamespace - - from esphome import yaml_util - from esphome.__main__ import _wrap_to_code - comments: list[str] = [] async def to_code(conf):