From 771162bfb113ab105ceb5edb545848e530a1fd81 Mon Sep 17 00:00:00 2001 From: Niorix Date: Mon, 21 Feb 2022 06:52:14 +0700 Subject: [PATCH] light: add RESTORE_AND_OFF/RESTORE_AND_ON LightRestoreMode (#3238) --- esphome/components/light/__init__.py | 2 ++ esphome/components/light/light_state.cpp | 6 ++++++ esphome/components/light/light_state.h | 2 ++ 3 files changed, 10 insertions(+) diff --git a/esphome/components/light/__init__.py b/esphome/components/light/__init__.py index fe8a90b8db..c397910ec4 100644 --- a/esphome/components/light/__init__.py +++ b/esphome/components/light/__init__.py @@ -52,6 +52,8 @@ RESTORE_MODES = { "ALWAYS_ON": LightRestoreMode.LIGHT_ALWAYS_ON, "RESTORE_INVERTED_DEFAULT_OFF": LightRestoreMode.LIGHT_RESTORE_INVERTED_DEFAULT_OFF, "RESTORE_INVERTED_DEFAULT_ON": LightRestoreMode.LIGHT_RESTORE_INVERTED_DEFAULT_ON, + "RESTORE_AND_OFF": LightRestoreMode.LIGHT_RESTORE_AND_OFF, + "RESTORE_AND_ON": LightRestoreMode.LIGHT_RESTORE_AND_ON, } LIGHT_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend( diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index 151bc58a1c..dadad38235 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -68,6 +68,12 @@ void LightState::setup() { recovered.state = !recovered.state; } break; + case LIGHT_RESTORE_AND_OFF: + case LIGHT_RESTORE_AND_ON: + this->rtc_ = global_preferences->make_preference(this->get_object_id_hash()); + this->rtc_.load(&recovered); + recovered.state = (this->restore_mode_ == LIGHT_RESTORE_AND_ON); + break; case LIGHT_ALWAYS_OFF: recovered.state = false; break; diff --git a/esphome/components/light/light_state.h b/esphome/components/light/light_state.h index ae3711234d..2e523eda5c 100644 --- a/esphome/components/light/light_state.h +++ b/esphome/components/light/light_state.h @@ -22,6 +22,8 @@ enum LightRestoreMode { LIGHT_ALWAYS_ON, LIGHT_RESTORE_INVERTED_DEFAULT_OFF, LIGHT_RESTORE_INVERTED_DEFAULT_ON, + LIGHT_RESTORE_AND_OFF, + LIGHT_RESTORE_AND_ON, }; /** This class represents the communication layer between the front-end MQTT layer and the