mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Add duration option to action start deep sleep (#1526)
This commit is contained in:
@@ -84,18 +84,28 @@ def to_code(config):
|
||||
cg.add_define('USE_DEEP_SLEEP')
|
||||
|
||||
|
||||
DEEP_SLEEP_ACTION_SCHEMA = automation.maybe_simple_id({
|
||||
DEEP_SLEEP_ENTER_SCHEMA = automation.maybe_simple_id({
|
||||
cv.GenerateID(): cv.use_id(DeepSleepComponent),
|
||||
cv.Optional(CONF_SLEEP_DURATION): cv.positive_time_period_milliseconds,
|
||||
})
|
||||
|
||||
|
||||
DEEP_SLEEP_PREVENT_SCHEMA = automation.maybe_simple_id({
|
||||
cv.GenerateID(): cv.use_id(DeepSleepComponent),
|
||||
})
|
||||
|
||||
|
||||
@automation.register_action('deep_sleep.enter', EnterDeepSleepAction, DEEP_SLEEP_ACTION_SCHEMA)
|
||||
@automation.register_action('deep_sleep.enter', EnterDeepSleepAction, DEEP_SLEEP_ENTER_SCHEMA)
|
||||
def deep_sleep_enter_to_code(config, action_id, template_arg, args):
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(action_id, template_arg, paren)
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
if CONF_SLEEP_DURATION in config:
|
||||
template_ = yield cg.templatable(config[CONF_SLEEP_DURATION], args, cg.int32)
|
||||
cg.add(var.set_sleep_duration(template_))
|
||||
yield var
|
||||
|
||||
|
||||
@automation.register_action('deep_sleep.prevent', PreventDeepSleepAction, DEEP_SLEEP_ACTION_SCHEMA)
|
||||
@automation.register_action('deep_sleep.prevent', PreventDeepSleepAction, DEEP_SLEEP_PREVENT_SCHEMA)
|
||||
def deep_sleep_prevent_to_code(config, action_id, template_arg, args):
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
yield cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
@@ -84,8 +84,14 @@ extern bool global_has_deep_sleep;
|
||||
template<typename... Ts> class EnterDeepSleepAction : public Action<Ts...> {
|
||||
public:
|
||||
EnterDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {}
|
||||
TEMPLATABLE_VALUE(uint32_t, sleep_duration);
|
||||
|
||||
void play(Ts... x) override { this->deep_sleep_->begin_sleep(true); }
|
||||
void play(Ts... x) override {
|
||||
if (this->sleep_duration_.has_value()) {
|
||||
this->deep_sleep_->set_sleep_duration(this->sleep_duration_.value(x...));
|
||||
}
|
||||
this->deep_sleep_->begin_sleep(true);
|
||||
}
|
||||
|
||||
protected:
|
||||
DeepSleepComponent *deep_sleep_;
|
||||
|
||||
Reference in New Issue
Block a user