[key_collector] Use register_apply_action for key_collector.enable and key_collector.disable (#19646)

This commit is contained in:
J. Nick Koston
2026-09-25 19:11:05 +01:00
committed by GitHub
parent 262ade0598
commit ad6996a63c
2 changed files with 12 additions and 28 deletions
+12 -20
View File
@@ -35,8 +35,6 @@ CONF_ON_RESULT = "on_result"
key_collector_ns = cg.esphome_ns.namespace("key_collector")
KeyCollector = key_collector_ns.class_("KeyCollector", cg.Component)
EnableAction = key_collector_ns.class_("EnableAction", automation.Action)
DisableAction = key_collector_ns.class_("DisableAction", automation.Action)
X_TYPE = cg.std_string_ref.operator("const")
@@ -134,25 +132,19 @@ async def to_code(config: ConfigType) -> None:
cg.add(var.set_enabled(config[CONF_ENABLE_ON_BOOT]))
automation.register_parented_action(
KEY_COLLECTOR_ACTION_SCHEMA = automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(KeyCollector),
}
)
automation.register_apply_action(
"key_collector.enable",
EnableAction,
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(KeyCollector),
}
),
synchronous=True,
KEY_COLLECTOR_ACTION_SCHEMA,
automation.ApplyCall("set_enabled(true)"),
)
automation.register_parented_action(
automation.register_apply_action(
"key_collector.disable",
DisableAction,
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(KeyCollector),
}
),
synchronous=True,
KEY_COLLECTOR_ACTION_SCHEMA,
automation.ApplyCall("set_enabled(false)"),
)
@@ -54,12 +54,4 @@ class KeyCollector final : public Component {
bool enabled_{};
};
template<typename... Ts> class EnableAction final : public Action<Ts...>, public Parented<KeyCollector> {
void play(const Ts &...x) override { this->parent_->set_enabled(true); }
};
template<typename... Ts> class DisableAction final : public Action<Ts...>, public Parented<KeyCollector> {
void play(const Ts &...x) override { this->parent_->set_enabled(false); }
};
} // namespace esphome::key_collector