mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 01:34:18 +01:00
Add optimistic mode from https://github.com/szupi-ipuzs/esphome-external-mrbridge
This commit is contained in:
parent
1a34516956
commit
b5b5afffc0
3 changed files with 11 additions and 3 deletions
|
@ -2,7 +2,7 @@ from esphome import pins
|
|||
import esphome.codegen as cg
|
||||
from esphome.components import switch
|
||||
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
|
||||
|
||||
|
@ -23,6 +23,7 @@ CONFIG_SCHEMA = (
|
|||
CONF_PULSE_LENGTH, default="100ms"
|
||||
): 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)
|
||||
|
@ -38,5 +39,6 @@ async def to_code(config):
|
|||
off_pin = await cg.gpio_pin_expression(config[CONF_OFF_PIN])
|
||||
cg.add(var.set_off_pin(off_pin))
|
||||
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):
|
||||
cg.add(var.set_wait_time(wait_time))
|
||||
|
|
|
@ -58,19 +58,23 @@ void HBridgeSwitch::timer_fn_() {
|
|||
this->relay_state_ = RELAY_STATE_SWITCHING_OFF;
|
||||
}
|
||||
next_timeout = this->pulse_length_;
|
||||
if (!this->optimistic_)
|
||||
this->publish_state(this->desired_state_);
|
||||
break;
|
||||
|
||||
case RELAY_STATE_SWITCHING_ON:
|
||||
this->on_pin_->digital_write(false);
|
||||
this->relay_state_ = RELAY_STATE_ON;
|
||||
this->publish_state(true);
|
||||
if (this->optimistic_)
|
||||
this->publish_state(true);
|
||||
next_timeout = this->wait_time_;
|
||||
break;
|
||||
|
||||
case RELAY_STATE_SWITCHING_OFF:
|
||||
this->off_pin_->digital_write(false);
|
||||
this->relay_state_ = RELAY_STATE_OFF;
|
||||
this->publish_state(false);
|
||||
if (this->optimistic_)
|
||||
this->publish_state(false);
|
||||
next_timeout = this->wait_time_;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ class HBridgeSwitch : public switch_::Switch, public Component {
|
|||
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_wait_time(uint32_t wait_time) { this->wait_time_ = wait_time; }
|
||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
|
@ -42,6 +43,7 @@ class HBridgeSwitch : public switch_::Switch, public Component {
|
|||
GPIOPin *off_pin_;
|
||||
uint32_t pulse_length_;
|
||||
uint32_t wait_time_{0};
|
||||
bool optimistic_{false};
|
||||
};
|
||||
|
||||
} // namespace hbridge
|
||||
|
|
Loading…
Reference in a new issue