mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 02:28:42 +00:00
[switch] Use Switch::control() instead of hand written turn_on/turn_off branches (#19364)
This commit is contained in:
@@ -720,12 +720,7 @@ uint16_t APIConnection::try_send_switch_info(EntityBase *entity, APIConnection *
|
||||
}
|
||||
void APIConnection::on_switch_command_request(const SwitchCommandRequest &msg) {
|
||||
ENTITY_COMMAND_GET(switch_::Switch, a_switch, switch)
|
||||
|
||||
if (msg.state) {
|
||||
a_switch->turn_on();
|
||||
} else {
|
||||
a_switch->turn_off();
|
||||
}
|
||||
a_switch->control(msg.state);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -13,12 +13,6 @@ void CopySwitch::setup() {
|
||||
|
||||
void CopySwitch::dump_config() { LOG_SWITCH("", "Copy Switch", this); }
|
||||
|
||||
void CopySwitch::write_state(bool state) {
|
||||
if (state) {
|
||||
source_->turn_on();
|
||||
} else {
|
||||
source_->turn_off();
|
||||
}
|
||||
}
|
||||
void CopySwitch::write_state(bool state) { this->source_->control(state); }
|
||||
|
||||
} // namespace esphome::copy
|
||||
|
||||
@@ -13,18 +13,10 @@ void GPIOSwitch::setup() {
|
||||
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
|
||||
|
||||
// write state before setup
|
||||
if (initial_state) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
}
|
||||
this->control(initial_state);
|
||||
this->pin_->setup();
|
||||
// write after setup again for other IOs
|
||||
if (initial_state) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
}
|
||||
this->control(initial_state);
|
||||
}
|
||||
void GPIOSwitch::dump_config() {
|
||||
LOG_SWITCH("", "GPIO Switch", this);
|
||||
|
||||
@@ -301,14 +301,10 @@ void LD6002BComponent::setup() {
|
||||
target_display_controlled = true;
|
||||
// Nothing reports this switch back, so its restored state is the only state
|
||||
// there is. Restoring through the switch keeps its inversion in the path:
|
||||
// the restored value is logical, and turn_on()/turn_off() are what turn it
|
||||
// the restored value is logical, and driving the switch is what turns it
|
||||
// into the raw command, the published state and the stream flag.
|
||||
const bool state = this->target_display_switch_->get_initial_state_with_restore_mode().value_or(true);
|
||||
if (state) {
|
||||
this->target_display_switch_->turn_on();
|
||||
} else {
|
||||
this->target_display_switch_->turn_off();
|
||||
}
|
||||
this->target_display_switch_->control(state);
|
||||
}
|
||||
#endif
|
||||
if (!target_display_controlled) {
|
||||
@@ -328,11 +324,7 @@ void LD6002BComponent::setup() {
|
||||
// The switch owns the stream, so it is also what applies the restored state:
|
||||
// driving it rather than the module keeps the entity's inversion in the path.
|
||||
const bool state = this->point_cloud_switch_->get_initial_state_with_restore_mode().value_or(false);
|
||||
if (state) {
|
||||
this->point_cloud_switch_->turn_on();
|
||||
} else {
|
||||
this->point_cloud_switch_->turn_off();
|
||||
}
|
||||
this->point_cloud_switch_->control(state);
|
||||
}
|
||||
#endif
|
||||
if (!point_cloud_controlled) {
|
||||
@@ -375,11 +367,7 @@ void LD6002BComponent::setup() {
|
||||
// Driving the switch applies its inversion; it also marks the restored value
|
||||
// as reported, so the work mode fallback runs on that until the query lands.
|
||||
const bool state = this->low_power_switch_->get_initial_state_with_restore_mode().value_or(false);
|
||||
if (state) {
|
||||
this->low_power_switch_->turn_on();
|
||||
} else {
|
||||
this->low_power_switch_->turn_off();
|
||||
}
|
||||
this->low_power_switch_->control(state);
|
||||
}
|
||||
#else
|
||||
bool want_low_power = false;
|
||||
|
||||
@@ -16,11 +16,7 @@ void ModbusSwitch::setup() {
|
||||
optional<bool> initial_state = Switch::get_initial_state_with_restore_mode();
|
||||
if (initial_state.has_value()) {
|
||||
// if it has a value, restore_mode is not "DISABLED", therefore act on the switch:
|
||||
if (initial_state.value()) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
}
|
||||
this->control(initial_state.value());
|
||||
}
|
||||
}
|
||||
void ModbusSwitch::dump_config() { LOG_SWITCH(TAG, "Modbus Controller Switch", this); }
|
||||
|
||||
@@ -6,15 +6,7 @@ namespace esphome::output {
|
||||
static const char *const TAG = "output.switch";
|
||||
|
||||
void OutputSwitch::dump_config() { LOG_SWITCH("", "Output Switch", this); }
|
||||
void OutputSwitch::setup() {
|
||||
bool initial_state = this->get_initial_state_with_restore_mode().value_or(false);
|
||||
|
||||
if (initial_state) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
}
|
||||
}
|
||||
void OutputSwitch::setup() { this->control(this->get_initial_state_with_restore_mode().value_or(false)); }
|
||||
void OutputSwitch::write_state(bool state) {
|
||||
if (state) {
|
||||
this->output_->turn_on();
|
||||
|
||||
@@ -546,11 +546,7 @@ void Sprinkler::set_auto_advance(const bool auto_advance) {
|
||||
if (this->auto_adv_sw_->state == auto_advance) {
|
||||
return;
|
||||
}
|
||||
if (auto_advance) {
|
||||
this->auto_adv_sw_->turn_on();
|
||||
} else {
|
||||
this->auto_adv_sw_->turn_off();
|
||||
}
|
||||
this->auto_adv_sw_->control(auto_advance);
|
||||
}
|
||||
|
||||
void Sprinkler::set_repeat(optional<uint32_t> repeat) {
|
||||
@@ -573,11 +569,7 @@ void Sprinkler::set_queue_enable(bool queue_enable) {
|
||||
if (this->queue_enable_sw_->state == queue_enable) {
|
||||
return;
|
||||
}
|
||||
if (queue_enable) {
|
||||
this->queue_enable_sw_->turn_on();
|
||||
} else {
|
||||
this->queue_enable_sw_->turn_off();
|
||||
}
|
||||
this->queue_enable_sw_->control(queue_enable);
|
||||
}
|
||||
|
||||
void Sprinkler::set_reverse(const bool reverse) {
|
||||
@@ -587,11 +579,7 @@ void Sprinkler::set_reverse(const bool reverse) {
|
||||
if (this->reverse_sw_->state == reverse) {
|
||||
return;
|
||||
}
|
||||
if (reverse) {
|
||||
this->reverse_sw_->turn_on();
|
||||
} else {
|
||||
this->reverse_sw_->turn_off();
|
||||
}
|
||||
this->reverse_sw_->control(reverse);
|
||||
}
|
||||
|
||||
void Sprinkler::set_standby(const bool standby) {
|
||||
@@ -601,11 +589,7 @@ void Sprinkler::set_standby(const bool standby) {
|
||||
if (this->standby_sw_->state == standby) {
|
||||
return;
|
||||
}
|
||||
if (standby) {
|
||||
this->standby_sw_->turn_on();
|
||||
} else {
|
||||
this->standby_sw_->turn_off();
|
||||
}
|
||||
this->standby_sw_->control(standby);
|
||||
}
|
||||
|
||||
uint32_t Sprinkler::valve_run_duration(const size_t valve_number) {
|
||||
|
||||
@@ -10,7 +10,6 @@ static const char *const TAG = "switch";
|
||||
Switch::Switch() : state(false) {}
|
||||
|
||||
void Switch::control(bool target_state) {
|
||||
ESP_LOGV(TAG, "'%s' Control: %s", this->get_name().c_str(), ONOFF(target_state));
|
||||
if (target_state) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
|
||||
@@ -42,11 +42,7 @@ void TemplateSwitch::setup() {
|
||||
if (initial_state.has_value()) {
|
||||
ESP_LOGD(TAG, " Restored state %s", ONOFF(initial_state.value()));
|
||||
// if it has a value, restore_mode is not "DISABLED", therefore act on the switch:
|
||||
if (initial_state.value()) {
|
||||
this->turn_on();
|
||||
} else {
|
||||
this->turn_off();
|
||||
}
|
||||
this->control(initial_state.value());
|
||||
}
|
||||
}
|
||||
void TemplateSwitch::dump_config() {
|
||||
|
||||
Reference in New Issue
Block a user