[sprinkler] Use register_apply_action for the setter actions (#19471)

This commit is contained in:
J. Nick Koston
2026-09-23 22:41:47 +01:00
committed by GitHub
parent 70f8ec73c9
commit ce6a66959a
3 changed files with 24 additions and 128 deletions
+12 -60
View File
@@ -66,14 +66,9 @@ SprinklerControllerSwitch = sprinkler_ns.class_(
"SprinklerControllerSwitch", switch.Switch, cg.Component
)
SetDividerAction = sprinkler_ns.class_("SetDividerAction", automation.Action)
SetMultiplierAction = sprinkler_ns.class_("SetMultiplierAction", automation.Action)
QueueValveAction = sprinkler_ns.class_("QueueValveAction", automation.Action)
ClearQueuedValvesAction = sprinkler_ns.class_(
"ClearQueuedValvesAction", automation.Action
)
SetRepeatAction = sprinkler_ns.class_("SetRepeatAction", automation.Action)
SetRunDurationAction = sprinkler_ns.class_("SetRunDurationAction", automation.Action)
StartFromQueueAction = sprinkler_ns.class_("StartFromQueueAction", automation.Action)
StartFullCycleAction = sprinkler_ns.class_("StartFullCycleAction", automation.Action)
StartSingleValveAction = sprinkler_ns.class_(
@@ -418,80 +413,37 @@ CONFIG_SCHEMA = cv.All(
)
@automation.register_action(
_VALVE_AND_DURATION = ((CONF_VALVE_NUMBER, cg.size_t), (CONF_RUN_DURATION, cg.uint32))
automation.register_apply_action(
"sprinkler.set_divider",
SetDividerAction,
SPRINKLER_ACTION_SET_DIVIDER_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_DIVIDER, "set_divider", cg.uint32),
)
async def sprinkler_set_divider_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_DIVIDER], args, cg.uint32)
cg.add(var.set_divider(template_))
return var
@automation.register_action(
automation.register_apply_action(
"sprinkler.set_multiplier",
SetMultiplierAction,
SPRINKLER_ACTION_SET_MULTIPLIER_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_MULTIPLIER, "set_multiplier", cg.float_),
)
async def sprinkler_set_multiplier_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_MULTIPLIER], args, cg.float_)
cg.add(var.set_multiplier(template_))
return var
@automation.register_action(
automation.register_apply_action(
"sprinkler.queue_valve",
QueueValveAction,
SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA,
synchronous=True,
automation.ApplyCall("queue_valve({}, {})", _VALVE_AND_DURATION),
)
async def sprinkler_set_queued_valve_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALVE_NUMBER], args, cg.size_t)
cg.add(var.set_valve_number(template_))
template_ = await cg.templatable(config[CONF_RUN_DURATION], args, cg.uint32)
cg.add(var.set_valve_run_duration(template_))
return var
@automation.register_action(
automation.register_apply_action(
"sprinkler.set_repeat",
SetRepeatAction,
SPRINKLER_ACTION_REPEAT_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_REPEAT, "set_repeat", cg.uint32),
)
async def sprinkler_set_repeat_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_REPEAT], args, cg.uint32)
cg.add(var.set_repeat(template_))
return var
@automation.register_action(
automation.register_apply_action(
"sprinkler.set_valve_run_duration",
SetRunDurationAction,
SPRINKLER_ACTION_SET_RUN_DURATION_SCHEMA,
synchronous=True,
automation.ApplyCall("set_valve_run_duration({}, {})", _VALVE_AND_DURATION),
)
async def sprinkler_set_valve_run_duration_to_code(
config, action_id, template_arg, args
):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_VALVE_NUMBER], args, cg.size_t)
cg.add(var.set_valve_number(template_))
template_ = await cg.templatable(config[CONF_RUN_DURATION], args, cg.uint32)
cg.add(var.set_valve_run_duration(template_))
return var
@automation.register_action(
-68
View File
@@ -6,46 +6,6 @@
namespace esphome::sprinkler {
template<typename... Ts> class SetDividerAction final : public Action<Ts...> {
public:
explicit SetDividerAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
TEMPLATABLE_VALUE(uint32_t, divider)
void play(const Ts &...x) override { this->sprinkler_->set_divider(this->divider_.optional_value(x...)); }
protected:
Sprinkler *sprinkler_;
};
template<typename... Ts> class SetMultiplierAction final : public Action<Ts...> {
public:
explicit SetMultiplierAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
TEMPLATABLE_VALUE(float, multiplier)
void play(const Ts &...x) override { this->sprinkler_->set_multiplier(this->multiplier_.optional_value(x...)); }
protected:
Sprinkler *sprinkler_;
};
template<typename... Ts> class QueueValveAction final : public Action<Ts...> {
public:
explicit QueueValveAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
TEMPLATABLE_VALUE(size_t, valve_number)
TEMPLATABLE_VALUE(uint32_t, valve_run_duration)
void play(const Ts &...x) override {
this->sprinkler_->queue_valve(this->valve_number_.optional_value(x...),
this->valve_run_duration_.optional_value(x...));
}
protected:
Sprinkler *sprinkler_;
};
template<typename... Ts> class ClearQueuedValvesAction final : public Action<Ts...> {
public:
explicit ClearQueuedValvesAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
@@ -56,34 +16,6 @@ template<typename... Ts> class ClearQueuedValvesAction final : public Action<Ts.
Sprinkler *sprinkler_;
};
template<typename... Ts> class SetRepeatAction final : public Action<Ts...> {
public:
explicit SetRepeatAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
TEMPLATABLE_VALUE(uint32_t, repeat)
void play(const Ts &...x) override { this->sprinkler_->set_repeat(this->repeat_.optional_value(x...)); }
protected:
Sprinkler *sprinkler_;
};
template<typename... Ts> class SetRunDurationAction final : public Action<Ts...> {
public:
explicit SetRunDurationAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
TEMPLATABLE_VALUE(size_t, valve_number)
TEMPLATABLE_VALUE(uint32_t, valve_run_duration)
void play(const Ts &...x) override {
this->sprinkler_->set_valve_run_duration(this->valve_number_.optional_value(x...),
this->valve_run_duration_.optional_value(x...));
}
protected:
Sprinkler *sprinkler_;
};
template<typename... Ts> class StartFromQueueAction final : public Action<Ts...> {
public:
explicit StartFromQueueAction(Sprinkler *a_sprinkler) : sprinkler_(a_sprinkler) {}
+12
View File
@@ -31,6 +31,18 @@ esphome:
id: yard_sprinkler_ctrlr
valve_number: 0
run_duration: 600s
# Templated values, including both keys of the two-key calls
- sprinkler.set_multiplier:
id: yard_sprinkler_ctrlr
multiplier: !lambda return 1.5f;
- sprinkler.queue_valve:
id: yard_sprinkler_ctrlr
valve_number: !lambda return 1;
run_duration: !lambda return 300;
- sprinkler.set_valve_run_duration:
id: yard_sprinkler_ctrlr
valve_number: !lambda return 1;
run_duration: !lambda return 120;
switch:
- platform: template