mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Fix incorrect entity count due to undefined execution order with globals (#10497)
This commit is contained in:
		| @@ -423,7 +423,7 @@ async def _add_automations(config): | |||||||
| DATETIME_SUBTYPES = {"date", "time", "datetime"} | DATETIME_SUBTYPES = {"date", "time", "datetime"} | ||||||
|  |  | ||||||
|  |  | ||||||
| @coroutine_with_priority(-100.0) | @coroutine_with_priority(-1000.0) | ||||||
| async def _add_platform_defines() -> None: | async def _add_platform_defines() -> None: | ||||||
|     # Generate compile-time defines for platforms that have actual entities |     # Generate compile-time defines for platforms that have actual entities | ||||||
|     # Only add USE_* and count defines when there are entities |     # Only add USE_* and count defines when there are entities | ||||||
|   | |||||||
| @@ -8,6 +8,7 @@ import pytest | |||||||
|  |  | ||||||
| from esphome import config_validation as cv, core | from esphome import config_validation as cv, core | ||||||
| from esphome.const import CONF_AREA, CONF_AREAS, CONF_DEVICES | from esphome.const import CONF_AREA, CONF_AREAS, CONF_DEVICES | ||||||
|  | from esphome.core import config | ||||||
| from esphome.core.config import Area, validate_area_config | from esphome.core.config import Area, validate_area_config | ||||||
|  |  | ||||||
| from .common import load_config_from_fixture | from .common import load_config_from_fixture | ||||||
| @@ -223,3 +224,24 @@ def test_device_duplicate_id( | |||||||
|     # Check for the specific error message from IDPassValidationStep |     # Check for the specific error message from IDPassValidationStep | ||||||
|     captured = capsys.readouterr() |     captured = capsys.readouterr() | ||||||
|     assert "ID duplicate_device redefined!" in captured.out |     assert "ID duplicate_device redefined!" in captured.out | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def test_add_platform_defines_priority() -> None: | ||||||
|  |     """Test that _add_platform_defines runs after globals. | ||||||
|  |  | ||||||
|  |     This ensures the fix for issue #10431 where sensor counts were incorrect | ||||||
|  |     when lambdas were present. The function must run at a lower priority than | ||||||
|  |     globals (-100.0) to ensure all components (including those using globals | ||||||
|  |     in lambdas) have registered their entities before the count defines are | ||||||
|  |     generated. | ||||||
|  |  | ||||||
|  |     Regression test for https://github.com/esphome/esphome/issues/10431 | ||||||
|  |     """ | ||||||
|  |     # Import globals to check its priority | ||||||
|  |     from esphome.components.globals import to_code as globals_to_code | ||||||
|  |  | ||||||
|  |     # _add_platform_defines must run AFTER globals (lower priority number = runs later) | ||||||
|  |     assert config._add_platform_defines.priority < globals_to_code.priority, ( | ||||||
|  |         f"_add_platform_defines priority ({config._add_platform_defines.priority}) must be lower than " | ||||||
|  |         f"globals priority ({globals_to_code.priority}) to fix issue #10431 (sensor count bug with lambdas)" | ||||||
|  |     ) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user