[core] Let register_apply_action read the parent id from another schema key (#19548)

This commit is contained in:
J. Nick Koston
2026-09-24 09:12:04 -04:00
committed by GitHub
parent fed5dd6a9d
commit e92b1e27ca
2 changed files with 41 additions and 14 deletions
+29 -6
View File
@@ -610,12 +610,13 @@ async def _run_entry(
config: dict[str, object],
args: list[tuple[object, str]] | None,
platform: str,
id_key: str = CONF_ID,
) -> RegistryEntry:
"""Run a registered builder with the given config, trigger args and platform."""
CORE.data[KEY_CORE] = {KEY_TARGET_PLATFORM: platform}
args = args or []
template_arg = cg.TemplateArguments(*(t for t, _ in args))
await entry.fun({CONF_ID: PARENT_ID, **config}, ID("obj_1"), template_arg, args)
await entry.fun({id_key: PARENT_ID, **config}, ID("obj_1"), template_arg, args)
return entry
@@ -626,11 +627,12 @@ async def _run_apply_action(
args: list[tuple[object, str]] | None = None,
call: str | None = None,
platform: str = "esp32",
id_key: str = CONF_ID,
) -> RegistryEntry:
"""Register an apply action and run its builder with the given config."""
actions, _ = registries
register_apply_action("my.apply", None, *fields, call=call)
return await _run_entry(actions["my.apply"], config, args, platform)
register_apply_action("my.apply", None, *fields, call=call, id_key=id_key)
return await _run_entry(actions["my.apply"], config, args, platform, id_key)
async def _run_apply_condition(
@@ -639,11 +641,12 @@ async def _run_apply_condition(
config: dict[str, object],
args: list[tuple[object, str]] | None = None,
platform: str = "esp32",
id_key: str = CONF_ID,
) -> RegistryEntry:
"""Register an apply condition and run its builder with the given config."""
_, conditions = registries
register_apply_condition("my.check", None, check)
return await _run_entry(conditions["my.check"], config, args, platform)
register_apply_condition("my.check", None, check, id_key=id_key)
return await _run_entry(conditions["my.check"], config, args, platform, id_key)
def _apply_lambda(mock_cg: MockCodegen) -> str:
@@ -663,6 +666,17 @@ async def test_register_apply_action_entry(
assert str(template_arg) == "<int32_t>"
@pytest.mark.asyncio
async def test_apply_custom_id_key(
registries: tuple[Registry, Registry], mock_cg: MockCodegen
) -> None:
await _run_apply_action(registries, (), {}, id_key="transmitter_id")
mock_cg.get_variable.assert_awaited_once_with(PARENT_ID)
mock_cg.get_variable.reset_mock()
await _run_apply_condition(registries, "is_on()", {}, id_key="transmitter_id")
mock_cg.get_variable.assert_awaited_once_with(PARENT_ID)
@pytest.mark.asyncio
async def test_apply_constants(
registries: tuple[Registry, Registry], mock_cg: MockCodegen
@@ -806,6 +820,10 @@ def test_apply_registration_checks(registries: tuple[Registry, Registry]) -> Non
register_apply_condition(
"my.bad_is", schema, ApplyCall("kd == {}", (("kd", cg.float_),))
)
with pytest.raises(ValueError, match="'parent_id' is not in the schema"):
register_apply_action("my.bad_id", schema, id_key="parent_id")
with pytest.raises(ValueError, match="'parent_id' is not in the schema"):
register_apply_condition("my.bad_is_id", schema, "is_on()", id_key="parent_id")
either = cv.Any(schema, cv.Schema({cv.Optional("kd"): cv.float_}))
register_apply_action("my.any", either, ApplyField("kd", "set_kd", cg.float_))
for wrapped in (
@@ -818,7 +836,12 @@ def test_apply_registration_checks(registries: tuple[Registry, Registry]) -> Non
register_apply_action(
"my.bad", wrapped, ApplyField("kd", "set_kd", cg.float_)
)
nested = cv.Schema({cv.Optional("v"): cv.Schema({cv.Optional("dir"): cv.int_})})
nested = cv.Schema(
{
cv.Required(CONF_ID): cv.string,
cv.Optional("v"): cv.Schema({cv.Optional("dir"): cv.int_}),
}
)
register_apply_action(
"my.nested", nested, ApplyField(("v", "dir"), "set_dir", cg.int_)
)