diff --git a/esphome/components/captive_portal/__init__.py b/esphome/components/captive_portal/__init__.py index 39cafc7cb4..9f2af0a230 100644 --- a/esphome/components/captive_portal/__init__.py +++ b/esphome/components/captive_portal/__init__.py @@ -10,7 +10,8 @@ from esphome.const import ( PLATFORM_LN882X, PLATFORM_RTL87XX, ) -from esphome.core import CORE, CoroPriority, coroutine_with_priority +from esphome.core import CORE, coroutine_with_priority +from esphome.coroutine import CoroPriority AUTO_LOAD = ["web_server_base", "ota.web_server"] DEPENDENCIES = ["wifi"] @@ -40,7 +41,7 @@ CONFIG_SCHEMA = cv.All( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.CAPTIVE_PORTAL) async def to_code(config): paren = await cg.get_variable(config[CONF_WEB_SERVER_BASE_ID]) diff --git a/esphome/components/esphome/ota/__init__.py b/esphome/components/esphome/ota/__init__.py index 7b579501ed..93216f9425 100644 --- a/esphome/components/esphome/ota/__init__.py +++ b/esphome/components/esphome/ota/__init__.py @@ -16,7 +16,8 @@ from esphome.const import ( CONF_SAFE_MODE, CONF_VERSION, ) -from esphome.core import CoroPriority, coroutine_with_priority +from esphome.core import coroutine_with_priority +from esphome.coroutine import CoroPriority import esphome.final_validate as fv _LOGGER = logging.getLogger(__name__) @@ -121,7 +122,7 @@ CONFIG_SCHEMA = ( FINAL_VALIDATE_SCHEMA = ota_esphome_final_validate -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.OTA_UPDATES) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_port(config[CONF_PORT])) diff --git a/esphome/components/http_request/ota/__init__.py b/esphome/components/http_request/ota/__init__.py index fd542e594a..d2c574d8c6 100644 --- a/esphome/components/http_request/ota/__init__.py +++ b/esphome/components/http_request/ota/__init__.py @@ -3,7 +3,8 @@ import esphome.codegen as cg from esphome.components.ota import BASE_OTA_SCHEMA, OTAComponent, ota_to_code import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_PASSWORD, CONF_URL, CONF_USERNAME -from esphome.core import CoroPriority, coroutine_with_priority +from esphome.core import coroutine_with_priority +from esphome.coroutine import CoroPriority from .. import CONF_HTTP_REQUEST_ID, HttpRequestComponent, http_request_ns @@ -40,7 +41,7 @@ CONFIG_SCHEMA = cv.All( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.OTA_UPDATES) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await ota_to_code(var, config) diff --git a/esphome/components/mdns/__init__.py b/esphome/components/mdns/__init__.py index a21ef9d97b..a84fe5a249 100644 --- a/esphome/components/mdns/__init__.py +++ b/esphome/components/mdns/__init__.py @@ -11,7 +11,8 @@ from esphome.const import ( CONF_SERVICES, PlatformFramework, ) -from esphome.core import CORE, CoroPriority, coroutine_with_priority +from esphome.core import CORE, coroutine_with_priority +from esphome.coroutine import CoroPriority CODEOWNERS = ["@esphome/core"] DEPENDENCIES = ["network"] @@ -72,7 +73,7 @@ def mdns_service( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.NETWORK_SERVICES) async def to_code(config): if config[CONF_DISABLED] is True: return diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index cf814fb1ee..eec39668db 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -10,7 +10,8 @@ from esphome.const import ( CONF_TRIGGER_ID, PlatformFramework, ) -from esphome.core import CORE, CoroPriority, coroutine_with_priority +from esphome.core import CORE, coroutine_with_priority +from esphome.coroutine import CoroPriority CODEOWNERS = ["@esphome/core"] AUTO_LOAD = ["md5", "safe_mode"] @@ -82,7 +83,7 @@ BASE_OTA_SCHEMA = cv.Schema( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.OTA_UPDATES) async def to_code(config): cg.add_define("USE_OTA") diff --git a/esphome/components/web_server/ota/__init__.py b/esphome/components/web_server/ota/__init__.py index 3ec7e65e1d..22e56639e1 100644 --- a/esphome/components/web_server/ota/__init__.py +++ b/esphome/components/web_server/ota/__init__.py @@ -3,7 +3,8 @@ from esphome.components.esp32 import add_idf_component from esphome.components.ota import BASE_OTA_SCHEMA, OTAComponent, ota_to_code import esphome.config_validation as cv from esphome.const import CONF_ID -from esphome.core import CORE, CoroPriority, coroutine_with_priority +from esphome.core import CORE, coroutine_with_priority +from esphome.coroutine import CoroPriority CODEOWNERS = ["@esphome/core"] DEPENDENCIES = ["network", "web_server_base"] @@ -22,7 +23,7 @@ CONFIG_SCHEMA = ( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.WEB_SERVER_OTA) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await ota_to_code(var, config) diff --git a/esphome/components/web_server_base/__init__.py b/esphome/components/web_server_base/__init__.py index 8add2f051f..2aff405036 100644 --- a/esphome/components/web_server_base/__init__.py +++ b/esphome/components/web_server_base/__init__.py @@ -1,7 +1,8 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import CONF_ID -from esphome.core import CORE, CoroPriority, coroutine_with_priority +from esphome.core import CORE, coroutine_with_priority +from esphome.coroutine import CoroPriority CODEOWNERS = ["@esphome/core"] DEPENDENCIES = ["network"] @@ -26,7 +27,7 @@ CONFIG_SCHEMA = cv.Schema( ) -@coroutine_with_priority(CoroPriority.COMMUNICATION) +@coroutine_with_priority(CoroPriority.WEB_SERVER_BASE) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) diff --git a/esphome/coroutine.py b/esphome/coroutine.py index 741a0c7c0c..0331c602c5 100644 --- a/esphome/coroutine.py +++ b/esphome/coroutine.py @@ -90,11 +90,30 @@ class CoroPriority(enum.IntEnum): # Examples: status_led (80) STATUS = 80 + # Web server infrastructure + # Examples: web_server_base (65) + WEB_SERVER_BASE = 65 + + # Network portal services + # Examples: captive_portal (64) + CAPTIVE_PORTAL = 64 + # Communication protocols and services - # Examples: web_server_base (65), captive_portal (64), wifi (60), ethernet (60), - # mdns (55), ota_updates (54), web_server_ota (52) + # Examples: wifi (60), ethernet (60) COMMUNICATION = 60 + # Network discovery and management services + # Examples: mdns (55) + NETWORK_SERVICES = 55 + + # OTA update services + # Examples: ota_updates (54) + OTA_UPDATES = 54 + + # Web-based OTA services + # Examples: web_server_ota (52) + WEB_SERVER_OTA = 52 + # Application-level services # Examples: safe_mode (50) APPLICATION = 50 diff --git a/tests/components/mdns/test-comprehensive.esp8266-ard.yaml b/tests/components/mdns/test-comprehensive.esp8266-ard.yaml new file mode 100644 index 0000000000..02767833a3 --- /dev/null +++ b/tests/components/mdns/test-comprehensive.esp8266-ard.yaml @@ -0,0 +1,42 @@ +# Comprehensive ESP8266 test for mdns with multiple network components +# Tests the complete priority chain: +# wifi (60) -> mdns (55) -> ota (54) -> web_server_ota (52) + +esphome: + name: mdns-comprehensive-test + +esp8266: + board: esp01_1m + +logger: + level: DEBUG + +wifi: + ssid: MySSID + password: password1 + +# web_server_base should run at priority 65 (before wifi) +web_server: + port: 80 + +# mdns should run at priority 55 (after wifi at 60) +mdns: + services: + - service: _http + protocol: _tcp + port: 80 + +# OTA should run at priority 54 (after mdns) +ota: + - platform: esphome + password: "otapassword" + +# Test status LED at priority 80 +status_led: + pin: + number: GPIO2 + inverted: true + +# Include API at priority 40 +api: + password: "apipassword" diff --git a/tests/unit_tests/test_coroutine.py b/tests/unit_tests/test_coroutine.py index 138b08edb5..e12c273294 100644 --- a/tests/unit_tests/test_coroutine.py +++ b/tests/unit_tests/test_coroutine.py @@ -13,7 +13,12 @@ def test_coro_priority_enum_values() -> None: assert CoroPriority.CORE == 100 assert CoroPriority.DIAGNOSTICS == 90 assert CoroPriority.STATUS == 80 + assert CoroPriority.WEB_SERVER_BASE == 65 + assert CoroPriority.CAPTIVE_PORTAL == 64 assert CoroPriority.COMMUNICATION == 60 + assert CoroPriority.NETWORK_SERVICES == 55 + assert CoroPriority.OTA_UPDATES == 54 + assert CoroPriority.WEB_SERVER_OTA == 52 assert CoroPriority.APPLICATION == 50 assert CoroPriority.WEB == 40 assert CoroPriority.AUTOMATION == 30 @@ -70,7 +75,12 @@ def test_float_and_enum_are_interchangeable() -> None: (CoroPriority.CORE, 100.0), (CoroPriority.DIAGNOSTICS, 90.0), (CoroPriority.STATUS, 80.0), + (CoroPriority.WEB_SERVER_BASE, 65.0), + (CoroPriority.CAPTIVE_PORTAL, 64.0), (CoroPriority.COMMUNICATION, 60.0), + (CoroPriority.NETWORK_SERVICES, 55.0), + (CoroPriority.OTA_UPDATES, 54.0), + (CoroPriority.WEB_SERVER_OTA, 52.0), (CoroPriority.APPLICATION, 50.0), (CoroPriority.WEB, 40.0), (CoroPriority.AUTOMATION, 30.0), @@ -164,8 +174,13 @@ def test_enum_priority_comparison() -> None: assert CoroPriority.NETWORK_TRANSPORT > CoroPriority.CORE assert CoroPriority.CORE > CoroPriority.DIAGNOSTICS assert CoroPriority.DIAGNOSTICS > CoroPriority.STATUS - assert CoroPriority.STATUS > CoroPriority.COMMUNICATION - assert CoroPriority.COMMUNICATION > CoroPriority.APPLICATION + assert CoroPriority.STATUS > CoroPriority.WEB_SERVER_BASE + assert CoroPriority.WEB_SERVER_BASE > CoroPriority.CAPTIVE_PORTAL + assert CoroPriority.CAPTIVE_PORTAL > CoroPriority.COMMUNICATION + assert CoroPriority.COMMUNICATION > CoroPriority.NETWORK_SERVICES + assert CoroPriority.NETWORK_SERVICES > CoroPriority.OTA_UPDATES + assert CoroPriority.OTA_UPDATES > CoroPriority.WEB_SERVER_OTA + assert CoroPriority.WEB_SERVER_OTA > CoroPriority.APPLICATION assert CoroPriority.APPLICATION > CoroPriority.WEB assert CoroPriority.WEB > CoroPriority.AUTOMATION assert CoroPriority.AUTOMATION > CoroPriority.BUS