This commit is contained in:
David Woodhouse 2024-09-07 21:12:56 +01:00
parent 1a34516956
commit b5b5afffc0
3 changed files with 11 additions and 3 deletions

View file

@ -2,7 +2,7 @@ from esphome import pins
import esphome.codegen as cg import esphome.codegen as cg
from esphome.components import switch from esphome.components import switch
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_PULSE_LENGTH, CONF_WAIT_TIME from esphome.const import CONF_OPTIMISTIC, CONF_PULSE_LENGTH, CONF_WAIT_TIME
from .. import hbridge_ns from .. import hbridge_ns
@ -23,6 +23,7 @@ CONFIG_SCHEMA = (
CONF_PULSE_LENGTH, default="100ms" CONF_PULSE_LENGTH, default="100ms"
): cv.positive_time_period_milliseconds, ): cv.positive_time_period_milliseconds,
cv.Optional(CONF_WAIT_TIME): cv.positive_time_period_milliseconds, cv.Optional(CONF_WAIT_TIME): cv.positive_time_period_milliseconds,
cv.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
} }
) )
.extend(cv.COMPONENT_SCHEMA) .extend(cv.COMPONENT_SCHEMA)
@ -38,5 +39,6 @@ async def to_code(config):
off_pin = await cg.gpio_pin_expression(config[CONF_OFF_PIN]) off_pin = await cg.gpio_pin_expression(config[CONF_OFF_PIN])
cg.add(var.set_off_pin(off_pin)) cg.add(var.set_off_pin(off_pin))
cg.add(var.set_pulse_length(config[CONF_PULSE_LENGTH])) cg.add(var.set_pulse_length(config[CONF_PULSE_LENGTH]))
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
if wait_time := config.get(CONF_WAIT_TIME): if wait_time := config.get(CONF_WAIT_TIME):
cg.add(var.set_wait_time(wait_time)) cg.add(var.set_wait_time(wait_time))

View file

@ -58,19 +58,23 @@ void HBridgeSwitch::timer_fn_() {
this->relay_state_ = RELAY_STATE_SWITCHING_OFF; this->relay_state_ = RELAY_STATE_SWITCHING_OFF;
} }
next_timeout = this->pulse_length_; next_timeout = this->pulse_length_;
if (!this->optimistic_)
this->publish_state(this->desired_state_);
break; break;
case RELAY_STATE_SWITCHING_ON: case RELAY_STATE_SWITCHING_ON:
this->on_pin_->digital_write(false); this->on_pin_->digital_write(false);
this->relay_state_ = RELAY_STATE_ON; this->relay_state_ = RELAY_STATE_ON;
this->publish_state(true); if (this->optimistic_)
this->publish_state(true);
next_timeout = this->wait_time_; next_timeout = this->wait_time_;
break; break;
case RELAY_STATE_SWITCHING_OFF: case RELAY_STATE_SWITCHING_OFF:
this->off_pin_->digital_write(false); this->off_pin_->digital_write(false);
this->relay_state_ = RELAY_STATE_OFF; this->relay_state_ = RELAY_STATE_OFF;
this->publish_state(false); if (this->optimistic_)
this->publish_state(false);
next_timeout = this->wait_time_; next_timeout = this->wait_time_;
break; break;
} }

View file

@ -23,6 +23,7 @@ class HBridgeSwitch : public switch_::Switch, public Component {
void set_off_pin(GPIOPin *pin) { this->off_pin_ = pin; } void set_off_pin(GPIOPin *pin) { this->off_pin_ = pin; }
void set_pulse_length(uint32_t pulse_length) { this->pulse_length_ = pulse_length; } void set_pulse_length(uint32_t pulse_length) { this->pulse_length_ = pulse_length; }
void set_wait_time(uint32_t wait_time) { this->wait_time_ = wait_time; } void set_wait_time(uint32_t wait_time) { this->wait_time_ = wait_time; }
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
// ========== INTERNAL METHODS ========== // ========== INTERNAL METHODS ==========
// (In most use cases you won't need these) // (In most use cases you won't need these)
@ -42,6 +43,7 @@ class HBridgeSwitch : public switch_::Switch, public Component {
GPIOPin *off_pin_; GPIOPin *off_pin_;
uint32_t pulse_length_; uint32_t pulse_length_;
uint32_t wait_time_{0}; uint32_t wait_time_{0};
bool optimistic_{false};
}; };
} // namespace hbridge } // namespace hbridge