1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-21 12:28:17 +00:00

Add deep_sleep.allow YAML action (#3459)

This commit is contained in:
Jesse Hills 2022-05-11 12:47:50 +12:00 committed by GitHub
parent 4116caff6a
commit 40ad9f4911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 29 deletions

View File

@ -93,7 +93,14 @@ deep_sleep_ns = cg.esphome_ns.namespace("deep_sleep")
DeepSleepComponent = deep_sleep_ns.class_("DeepSleepComponent", cg.Component) DeepSleepComponent = deep_sleep_ns.class_("DeepSleepComponent", cg.Component)
EnterDeepSleepAction = deep_sleep_ns.class_("EnterDeepSleepAction", automation.Action) EnterDeepSleepAction = deep_sleep_ns.class_("EnterDeepSleepAction", automation.Action)
PreventDeepSleepAction = deep_sleep_ns.class_( PreventDeepSleepAction = deep_sleep_ns.class_(
"PreventDeepSleepAction", automation.Action "PreventDeepSleepAction",
automation.Action,
cg.Parented.template(DeepSleepComponent),
)
AllowDeepSleepAction = deep_sleep_ns.class_(
"AllowDeepSleepAction",
automation.Action,
cg.Parented.template(DeepSleepComponent),
) )
WakeupPinMode = deep_sleep_ns.enum("WakeupPinMode") WakeupPinMode = deep_sleep_ns.enum("WakeupPinMode")
@ -208,28 +215,32 @@ async def to_code(config):
cg.add_define("USE_DEEP_SLEEP") cg.add_define("USE_DEEP_SLEEP")
DEEP_SLEEP_ENTER_SCHEMA = cv.All( DEEP_SLEEP_ACTION_SCHEMA = cv.Schema(
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(DeepSleepComponent),
cv.Exclusive(CONF_SLEEP_DURATION, "time"): cv.templatable(
cv.positive_time_period_milliseconds
),
# Only on ESP32 due to how long the RTC on ESP8266 can stay asleep
cv.Exclusive(CONF_UNTIL, "time"): cv.All(cv.only_on_esp32, cv.time_of_day),
cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
}
),
cv.has_none_or_all_keys(CONF_UNTIL, CONF_TIME_ID),
)
DEEP_SLEEP_PREVENT_SCHEMA = automation.maybe_simple_id(
{ {
cv.GenerateID(): cv.use_id(DeepSleepComponent), cv.GenerateID(): cv.use_id(DeepSleepComponent),
} }
) )
DEEP_SLEEP_ENTER_SCHEMA = cv.All(
automation.maybe_simple_id(
DEEP_SLEEP_ACTION_SCHEMA.extend(
cv.Schema(
{
cv.Exclusive(CONF_SLEEP_DURATION, "time"): cv.templatable(
cv.positive_time_period_milliseconds
),
# Only on ESP32 due to how long the RTC on ESP8266 can stay asleep
cv.Exclusive(CONF_UNTIL, "time"): cv.All(
cv.only_on_esp32, cv.time_of_day
),
cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
}
)
)
),
cv.has_none_or_all_keys(CONF_UNTIL, CONF_TIME_ID),
)
@automation.register_action( @automation.register_action(
"deep_sleep.enter", EnterDeepSleepAction, DEEP_SLEEP_ENTER_SCHEMA "deep_sleep.enter", EnterDeepSleepAction, DEEP_SLEEP_ENTER_SCHEMA
@ -252,8 +263,16 @@ async def deep_sleep_enter_to_code(config, action_id, template_arg, args):
@automation.register_action( @automation.register_action(
"deep_sleep.prevent", PreventDeepSleepAction, DEEP_SLEEP_PREVENT_SCHEMA "deep_sleep.prevent",
PreventDeepSleepAction,
automation.maybe_simple_id(DEEP_SLEEP_ACTION_SCHEMA),
) )
async def deep_sleep_prevent_to_code(config, action_id, template_arg, args): @automation.register_action(
paren = await cg.get_variable(config[CONF_ID]) "deep_sleep.allow",
return cg.new_Pvariable(action_id, template_arg, paren) AllowDeepSleepAction,
automation.maybe_simple_id(DEEP_SLEEP_ACTION_SCHEMA),
)
async def deep_sleep_action_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var

View File

@ -190,14 +190,14 @@ template<typename... Ts> class EnterDeepSleepAction : public Action<Ts...> {
#endif #endif
}; };
template<typename... Ts> class PreventDeepSleepAction : public Action<Ts...> { template<typename... Ts> class PreventDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
public: public:
PreventDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {} void play(Ts... x) override { this->parent_->prevent_deep_sleep(); }
};
void play(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); } template<typename... Ts> class AllowDeepSleepAction : public Action<Ts...>, public Parented<DeepSleepComponent> {
public:
protected: void play(Ts... x) override { this->parent_->allow_deep_sleep(); }
DeepSleepComponent *deep_sleep_;
}; };
} // namespace deep_sleep } // namespace deep_sleep

View File

@ -121,6 +121,7 @@ mqtt:
- topic: livingroom/ota_mode - topic: livingroom/ota_mode
then: then:
- deep_sleep.prevent - deep_sleep.prevent
- deep_sleep.allow
- topic: livingroom/ota_mode - topic: livingroom/ota_mode
then: then:
- deep_sleep.enter: - deep_sleep.enter:
@ -2833,4 +2834,3 @@ button:
id: scd40 id: scd40
- scd4x.factory_reset: - scd4x.factory_reset:
id: scd40 id: scd40