mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 11:47:30 +01:00
Add restore_mode
to output switch (#3016)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
e4555f6997
commit
15fe049a99
@ -1,15 +1,28 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import output, switch
|
||||
from esphome.const import CONF_ID, CONF_OUTPUT
|
||||
from esphome.const import CONF_ID, CONF_OUTPUT, CONF_RESTORE_MODE
|
||||
from .. import output_ns
|
||||
|
||||
OutputSwitch = output_ns.class_("OutputSwitch", switch.Switch, cg.Component)
|
||||
|
||||
OutputSwitchRestoreMode = output_ns.enum("OutputSwitchRestoreMode")
|
||||
RESTORE_MODES = {
|
||||
"RESTORE_DEFAULT_OFF": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_DEFAULT_OFF,
|
||||
"RESTORE_DEFAULT_ON": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_DEFAULT_ON,
|
||||
"ALWAYS_OFF": OutputSwitchRestoreMode.OUTPUT_SWITCH_ALWAYS_OFF,
|
||||
"ALWAYS_ON": OutputSwitchRestoreMode.OUTPUT_SWITCH_ALWAYS_ON,
|
||||
"RESTORE_INVERTED_DEFAULT_OFF": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_OFF,
|
||||
"RESTORE_INVERTED_DEFAULT_ON": OutputSwitchRestoreMode.OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_ON,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(OutputSwitch),
|
||||
cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput),
|
||||
cv.Optional(CONF_RESTORE_MODE, default="RESTORE_DEFAULT_OFF"): cv.enum(
|
||||
RESTORE_MODES, upper=True, space="_"
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@ -21,3 +34,5 @@ async def to_code(config):
|
||||
|
||||
output_ = await cg.get_variable(config[CONF_OUTPUT])
|
||||
cg.add(var.set_output(output_))
|
||||
|
||||
cg.add(var.set_restore_mode(config[CONF_RESTORE_MODE]))
|
||||
|
@ -8,15 +8,32 @@ static const char *const TAG = "output.switch";
|
||||
|
||||
void OutputSwitch::dump_config() { LOG_SWITCH("", "Output Switch", this); }
|
||||
void OutputSwitch::setup() {
|
||||
auto restored = this->get_initial_state();
|
||||
if (!restored.has_value())
|
||||
return;
|
||||
|
||||
if (*restored) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
bool initial_state = false;
|
||||
switch (this->restore_mode_) {
|
||||
case OUTPUT_SWITCH_RESTORE_DEFAULT_OFF:
|
||||
initial_state = this->get_initial_state().value_or(false);
|
||||
break;
|
||||
case OUTPUT_SWITCH_RESTORE_DEFAULT_ON:
|
||||
initial_state = this->get_initial_state().value_or(true);
|
||||
break;
|
||||
case OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_OFF:
|
||||
initial_state = !this->get_initial_state().value_or(true);
|
||||
break;
|
||||
case OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_ON:
|
||||
initial_state = !this->get_initial_state().value_or(false);
|
||||
break;
|
||||
case OUTPUT_SWITCH_ALWAYS_OFF:
|
||||
initial_state = false;
|
||||
break;
|
||||
case OUTPUT_SWITCH_ALWAYS_ON:
|
||||
initial_state = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (initial_state)
|
||||
this->turn_on();
|
||||
else
|
||||
this->turn_off();
|
||||
}
|
||||
void OutputSwitch::write_state(bool state) {
|
||||
if (state) {
|
||||
|
@ -7,10 +7,21 @@
|
||||
namespace esphome {
|
||||
namespace output {
|
||||
|
||||
enum OutputSwitchRestoreMode {
|
||||
OUTPUT_SWITCH_RESTORE_DEFAULT_OFF,
|
||||
OUTPUT_SWITCH_RESTORE_DEFAULT_ON,
|
||||
OUTPUT_SWITCH_ALWAYS_OFF,
|
||||
OUTPUT_SWITCH_ALWAYS_ON,
|
||||
OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_OFF,
|
||||
OUTPUT_SWITCH_RESTORE_INVERTED_DEFAULT_ON,
|
||||
};
|
||||
|
||||
class OutputSwitch : public switch_::Switch, public Component {
|
||||
public:
|
||||
void set_output(BinaryOutput *output) { output_ = output; }
|
||||
|
||||
void set_restore_mode(OutputSwitchRestoreMode restore_mode) { restore_mode_ = restore_mode; }
|
||||
|
||||
void setup() override;
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE - 1.0f; }
|
||||
void dump_config() override;
|
||||
@ -19,6 +30,7 @@ class OutputSwitch : public switch_::Switch, public Component {
|
||||
void write_state(bool state) override;
|
||||
|
||||
output::BinaryOutput *output_;
|
||||
OutputSwitchRestoreMode restore_mode_;
|
||||
};
|
||||
|
||||
} // namespace output
|
||||
|
Loading…
Reference in New Issue
Block a user