diff --git a/esphome/components/deep_sleep/__init__.py b/esphome/components/deep_sleep/__init__.py index 058358fa67..8b60b4eb5f 100644 --- a/esphome/components/deep_sleep/__init__.py +++ b/esphome/components/deep_sleep/__init__.py @@ -93,7 +93,14 @@ deep_sleep_ns = cg.esphome_ns.namespace("deep_sleep") DeepSleepComponent = deep_sleep_ns.class_("DeepSleepComponent", cg.Component) EnterDeepSleepAction = deep_sleep_ns.class_("EnterDeepSleepAction", automation.Action) 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") @@ -208,28 +215,32 @@ async def to_code(config): cg.add_define("USE_DEEP_SLEEP") -DEEP_SLEEP_ENTER_SCHEMA = cv.All( - 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( +DEEP_SLEEP_ACTION_SCHEMA = cv.Schema( { 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( "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( - "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): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) +@automation.register_action( + "deep_sleep.allow", + 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 diff --git a/esphome/components/deep_sleep/deep_sleep_component.h b/esphome/components/deep_sleep/deep_sleep_component.h index f384dee01d..8dc87cece8 100644 --- a/esphome/components/deep_sleep/deep_sleep_component.h +++ b/esphome/components/deep_sleep/deep_sleep_component.h @@ -190,14 +190,14 @@ template class EnterDeepSleepAction : public Action { #endif }; -template class PreventDeepSleepAction : public Action { +template class PreventDeepSleepAction : public Action, public Parented { 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(); } - - protected: - DeepSleepComponent *deep_sleep_; +template class AllowDeepSleepAction : public Action, public Parented { + public: + void play(Ts... x) override { this->parent_->allow_deep_sleep(); } }; } // namespace deep_sleep diff --git a/tests/test1.yaml b/tests/test1.yaml index e15b2cf6b7..7bb1fbe954 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -121,6 +121,7 @@ mqtt: - topic: livingroom/ota_mode then: - deep_sleep.prevent + - deep_sleep.allow - topic: livingroom/ota_mode then: - deep_sleep.enter: @@ -2833,4 +2834,3 @@ button: id: scd40 - scd4x.factory_reset: id: scd40 -