Compare commits

...

4 Commits

Author SHA1 Message Date
Kevin P. Fleming 349752ce9f
Merge 034125d3fc into b148105423 2024-07-03 07:53:04 -04:00
Paul Wieland b148105423
Auto detect obstruction sensors (#298)
* Auto detect obstruction sensors
* Use toggle action instead of close if no sensors detected
* Toggle for close only when door is open
2024-07-03 07:44:33 -04:00
Kevin P. Fleming 034125d3fc Improve compatibility for non-dry-contact configurations.
* Don't compile any dry-contact code unless PROTOCOL_DRYCONTACT is defined.

* Use BinarySensor instead of GPIOBinarySensor to allow the user to use a
  template binary sensor (or any other type) if they wish.
2024-07-03 06:55:16 -04:00
J. Nick Koston c603f012e6
chore: add clang pre-commit (#299) 2024-07-02 19:59:37 -05:00
11 changed files with 90 additions and 73 deletions

View File

@ -7,3 +7,12 @@ repos:
- id: trailing-whitespace - id: trailing-whitespace
- id: end-of-file-fixer - id: end-of-file-fixer
- id: check-added-large-files - id: check-added-large-files
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.8
hooks:
- id: clang-format
types_or:
- "c++"
- "c"
- "cuda"
args: [-style=Webkit, -i]

View File

@ -51,7 +51,7 @@ def validate_protocol(config):
raise cv.Invalid("dry_contact_close_sensor and dry_contact_open_sensor are only valid when using protocol drycontact") raise cv.Invalid("dry_contact_close_sensor and dry_contact_open_sensor are only valid when using protocol drycontact")
# if config.get(CONF_PROTOCOL, None) == PROTOCOL_DRYCONTACT and CONF_DRY_CONTACT_OPEN_SENSOR not in config: # if config.get(CONF_PROTOCOL, None) == PROTOCOL_DRYCONTACT and CONF_DRY_CONTACT_OPEN_SENSOR not in config:
# raise cv.Invalid("dry_contact_open_sensor is required when using protocol drycontact") # raise cv.Invalid("dry_contact_open_sensor is required when using protocol drycontact")
return config return config
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.Schema( cv.Schema(
@ -144,4 +144,4 @@ async def to_code(config):
cg.add(var.set_discrete_open_pin(pin)) cg.add(var.set_discrete_open_pin(pin))
if CONF_DISCRETE_CLOSE_PIN in config and config[CONF_DISCRETE_CLOSE_PIN]: if CONF_DISCRETE_CLOSE_PIN in config and config[CONF_DISCRETE_CLOSE_PIN]:
pin = await cg.gpio_pin_expression(config[CONF_DISCRETE_CLOSE_PIN]) pin = await cg.gpio_pin_expression(config[CONF_DISCRETE_CLOSE_PIN])
cg.add(var.set_discrete_close_pin(pin)) cg.add(var.set_discrete_close_pin(pin))

View File

@ -1,4 +1,4 @@
#pragma once #pragma once
#define ESP_LOG1 ESP_LOGV #define ESP_LOG1 ESP_LOGV
#define ESP_LOG2 ESP_LOGV #define ESP_LOG2 ESP_LOGV

View File

@ -1,11 +1,12 @@
#include "dry_contact.h"
#include "ratgdo.h" #include "ratgdo.h"
#ifdef PROTOCOL_DRYCONTACT
#include "dry_contact.h"
#include "esphome/core/gpio.h" #include "esphome/core/gpio.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/scheduler.h" #include "esphome/core/scheduler.h"
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
namespace esphome { namespace esphome {
namespace ratgdo { namespace ratgdo {
@ -19,7 +20,7 @@ namespace ratgdo {
this->scheduler_ = scheduler; this->scheduler_ = scheduler;
this->tx_pin_ = tx_pin; this->tx_pin_ = tx_pin;
this->rx_pin_ = rx_pin; this->rx_pin_ = rx_pin;
this->open_limit_reached_ = 0; this->open_limit_reached_ = 0;
this->last_open_limit_ = 0; this->last_open_limit_ = 0;
this->close_limit_reached_ = 0; this->close_limit_reached_ = 0;
@ -58,18 +59,19 @@ namespace ratgdo {
this->close_limit_reached_ = state; this->close_limit_reached_ = state;
this->send_door_state(); this->send_door_state();
} }
void DryContact::send_door_state(){ void DryContact::send_door_state()
if(this->open_limit_reached_){ {
if (this->open_limit_reached_) {
this->door_state_ = DoorState::OPEN; this->door_state_ = DoorState::OPEN;
}else if(this->close_limit_reached_){ } else if (this->close_limit_reached_) {
this->door_state_ = DoorState::CLOSED; this->door_state_ = DoorState::CLOSED;
}else if(!this->close_limit_reached_ && !this->open_limit_reached_){ } else if (!this->close_limit_reached_ && !this->open_limit_reached_) {
if(this->last_close_limit_){ if (this->last_close_limit_) {
this->door_state_ = DoorState::OPENING; this->door_state_ = DoorState::OPENING;
} }
if(this->last_open_limit_){ if (this->last_open_limit_) {
this->door_state_ = DoorState::CLOSING; this->door_state_ = DoorState::CLOSING;
} }
} }
@ -102,14 +104,14 @@ namespace ratgdo {
ESP_LOG1(TAG, "Door action: %s", DoorAction_to_string(action)); ESP_LOG1(TAG, "Door action: %s", DoorAction_to_string(action));
if (action == DoorAction::OPEN){ if (action == DoorAction::OPEN) {
this->discrete_open_pin_->digital_write(1); this->discrete_open_pin_->digital_write(1);
this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] { this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] {
this->discrete_open_pin_->digital_write(0); this->discrete_open_pin_->digital_write(0);
}); });
} }
if (action == DoorAction::CLOSE){ if (action == DoorAction::CLOSE) {
this->discrete_close_pin_->digital_write(1); this->discrete_close_pin_->digital_write(1);
this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] { this->scheduler_->set_timeout(this->ratgdo_, "", 500, [=] {
this->discrete_close_pin_->digital_write(0); this->discrete_close_pin_->digital_write(0);
@ -127,6 +129,8 @@ namespace ratgdo {
return {}; return {};
} }
} // namespace DryContact } // namespace dry_contact
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome
#endif

View File

@ -1,9 +1,12 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#ifdef PROTOCOL_DRYCONTACT
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial #include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
#include "esphome/core/optional.h"
#include "esphome/core/gpio.h" #include "esphome/core/gpio.h"
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #include "esphome/core/optional.h"
#include "callbacks.h" #include "callbacks.h"
#include "observable.h" #include "observable.h"
@ -36,13 +39,15 @@ namespace ratgdo {
void set_close_limit(bool state); void set_close_limit(bool state);
void send_door_state(); void send_door_state();
void set_discrete_open_pin(InternalGPIOPin* pin) { void set_discrete_open_pin(InternalGPIOPin* pin)
{
this->discrete_open_pin_ = pin; this->discrete_open_pin_ = pin;
this->discrete_open_pin_->setup(); this->discrete_open_pin_->setup();
this->discrete_open_pin_->pin_mode(gpio::FLAG_OUTPUT); this->discrete_open_pin_->pin_mode(gpio::FLAG_OUTPUT);
} }
void set_discrete_close_pin(InternalGPIOPin* pin) { void set_discrete_close_pin(InternalGPIOPin* pin)
{
this->discrete_close_pin_ = pin; this->discrete_close_pin_ = pin;
this->discrete_close_pin_->setup(); this->discrete_close_pin_->setup();
this->discrete_close_pin_->pin_mode(gpio::FLAG_OUTPUT); this->discrete_close_pin_->pin_mode(gpio::FLAG_OUTPUT);
@ -68,9 +73,10 @@ namespace ratgdo {
bool last_open_limit_; bool last_open_limit_;
bool close_limit_reached_; bool close_limit_reached_;
bool last_close_limit_; bool last_close_limit_;
}; };
} // namespace secplus1 } // namespace dry_contact
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome
#endif

View File

@ -38,15 +38,9 @@ namespace ratgdo {
this->input_gdo_pin_->setup(); this->input_gdo_pin_->setup();
this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
if (this->input_obst_pin_ == nullptr) { this->input_obst_pin_->setup();
// Our base.yaml is always going to set this so we check for 0 this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
// as well to avoid a breaking change. this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
this->obstruction_from_status_ = true;
} else {
this->input_obst_pin_->setup();
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
}
this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_); this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_);
@ -76,9 +70,7 @@ namespace ratgdo {
void RATGDOComponent::loop() void RATGDOComponent::loop()
{ {
if (!this->obstruction_from_status_) { this->obstruction_loop();
this->obstruction_loop();
}
this->protocol_->loop(); this->protocol_->loop();
} }
@ -87,11 +79,7 @@ namespace ratgdo {
ESP_LOGCONFIG(TAG, "Setting up RATGDO..."); ESP_LOGCONFIG(TAG, "Setting up RATGDO...");
LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_); LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_);
LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_); LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_);
if (this->obstruction_from_status_) { LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
ESP_LOGCONFIG(TAG, " Input Obstruction Pin: not used, will detect from GDO status");
} else {
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
}
this->protocol_->dump_config(); this->protocol_->dump_config();
} }
@ -218,7 +206,7 @@ namespace ratgdo {
void RATGDOComponent::received(const ObstructionState obstruction_state) void RATGDOComponent::received(const ObstructionState obstruction_state)
{ {
if (this->obstruction_from_status_) { if (!this->obstruction_sensor_detected_) {
ESP_LOGD(TAG, "Obstruction: state=%s", ObstructionState_to_string(*this->obstruction_state)); ESP_LOGD(TAG, "Obstruction: state=%s", ObstructionState_to_string(*this->obstruction_state));
this->obstruction_state = obstruction_state; this->obstruction_state = obstruction_state;
@ -378,6 +366,7 @@ namespace ratgdo {
// check to see if we got more then PULSES_LOWER_LIMIT pulses // check to see if we got more then PULSES_LOWER_LIMIT pulses
if (this->isr_store_.obstruction_low_count > PULSES_LOWER_LIMIT) { if (this->isr_store_.obstruction_low_count > PULSES_LOWER_LIMIT) {
this->obstruction_state = ObstructionState::CLEAR; this->obstruction_state = ObstructionState::CLEAR;
this->obstruction_sensor_detected_ = true;
} else if (this->isr_store_.obstruction_low_count == 0) { } else if (this->isr_store_.obstruction_low_count == 0) {
// if there have been no pulses the line is steady high or low // if there have been no pulses the line is steady high or low
if (!this->input_obst_pin_->digital_read()) { if (!this->input_obst_pin_->digital_read()) {
@ -471,7 +460,12 @@ namespace ratgdo {
return; return;
} }
this->door_action(DoorAction::CLOSE); if (this->obstruction_sensor_detected_) {
this->door_action(DoorAction::CLOSE);
} else if (*this->door_state == DoorState::OPEN) {
ESP_LOGD(TAG, "No obstruction sensors detected. Close using TOGGLE.");
this->door_action(DoorAction::TOGGLE);
}
if (*this->closing_duration > 0) { if (*this->closing_duration > 0) {
// query state in case we don't get a status message // query state in case we don't get a status message
@ -686,26 +680,24 @@ namespace ratgdo {
this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); }); this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); });
} }
#ifdef PROTOCOL_DRYCONTACT
// dry contact methods // dry contact methods
void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor) void RATGDOComponent::set_dry_contact_open_sensor(esphome::binary_sensor::BinarySensor* dry_contact_open_sensor)
{ {
dry_contact_open_sensor_ = dry_contact_open_sensor; dry_contact_open_sensor_ = dry_contact_open_sensor;
dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) {
{
this->protocol_->set_open_limit(sensor_value); this->protocol_->set_open_limit(sensor_value);
} });
);
} }
void RATGDOComponent::set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor) void RATGDOComponent::set_dry_contact_close_sensor(esphome::binary_sensor::BinarySensor* dry_contact_close_sensor)
{ {
dry_contact_close_sensor_ = dry_contact_close_sensor; dry_contact_close_sensor_ = dry_contact_close_sensor;
dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) {
{
this->protocol_->set_close_limit(sensor_value); this->protocol_->set_close_limit(sensor_value);
} });
);
} }
#endif
} // namespace ratgdo } // namespace ratgdo
} // namespace esphome } // namespace esphome

View File

@ -14,9 +14,12 @@
#pragma once #pragma once
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/core/preferences.h" #include "esphome/core/preferences.h"
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h" #ifdef PROTOCOL_DRYCONTACT
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#include "callbacks.h" #include "callbacks.h"
#include "macros.h" #include "macros.h"
@ -92,11 +95,13 @@ namespace ratgdo {
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; } void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; } void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }
#ifdef PROTOCOL_DRYCONTACT
// dry contact methods // dry contact methods
void set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_); void set_dry_contact_open_sensor(esphome::binary_sensor::BinarySensor* dry_contact_open_sensor_);
void set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_); void set_dry_contact_close_sensor(esphome::binary_sensor::BinarySensor* dry_contact_close_sensor_);
void set_discrete_open_pin(InternalGPIOPin* pin){ this->protocol_->set_discrete_open_pin(pin); } void set_discrete_open_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_open_pin(pin); }
void set_discrete_close_pin(InternalGPIOPin* pin){ this->protocol_->set_discrete_close_pin(pin); } void set_discrete_close_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_close_pin(pin); }
#endif
Result call_protocol(Args args); Result call_protocol(Args args);
@ -176,13 +181,15 @@ namespace ratgdo {
protected: protected:
RATGDOStore isr_store_ {}; RATGDOStore isr_store_ {};
protocol::Protocol* protocol_; protocol::Protocol* protocol_;
bool obstruction_from_status_ { false }; bool obstruction_sensor_detected_ { false };
InternalGPIOPin* output_gdo_pin_; InternalGPIOPin* output_gdo_pin_;
InternalGPIOPin* input_gdo_pin_; InternalGPIOPin* input_gdo_pin_;
InternalGPIOPin* input_obst_pin_; InternalGPIOPin* input_obst_pin_;
esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_; #ifdef PROTOCOL_DRYCONTACT
esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_; esphome::binary_sensor::BinarySensor* dry_contact_open_sensor_;
esphome::binary_sensor::BinarySensor* dry_contact_close_sensor_;
#endif
}; // RATGDOComponent }; // RATGDOComponent
} // namespace ratgdo } // namespace ratgdo

View File

@ -97,11 +97,10 @@ namespace ratgdo {
const Traits& traits() const { return this->traits_; } const Traits& traits() const { return this->traits_; }
// methods not used by secplus1 // methods not used by secplus1
void set_open_limit(bool state){} void set_open_limit(bool state) { }
void set_close_limit(bool state){} void set_close_limit(bool state) { }
void set_discrete_open_pin(InternalGPIOPin* pin){} void set_discrete_open_pin(InternalGPIOPin* pin) { }
void set_discrete_close_pin(InternalGPIOPin* pin){} void set_discrete_close_pin(InternalGPIOPin* pin) { }
protected: protected:
void wall_panel_emulation(size_t index = 0); void wall_panel_emulation(size_t index = 0);

View File

@ -102,10 +102,10 @@ namespace ratgdo {
const Traits& traits() const { return this->traits_; } const Traits& traits() const { return this->traits_; }
// methods not used by secplus2 // methods not used by secplus2
void set_open_limit(bool state){} void set_open_limit(bool state) { }
void set_close_limit(bool state){} void set_close_limit(bool state) { }
void set_discrete_open_pin(InternalGPIOPin* pin){} void set_discrete_open_pin(InternalGPIOPin* pin) { }
void set_discrete_close_pin(InternalGPIOPin* pin){} void set_discrete_close_pin(InternalGPIOPin* pin) { }
protected: protected:
void increment_rolling_code_counter(int delta = 1); void increment_rolling_code_counter(int delta = 1);

View File

@ -289,25 +289,25 @@
const button = document.querySelector("esp-web-install-button"); const button = document.querySelector("esp-web-install-button");
var protocol = document.querySelector('input[name="protocol"]:checked').value; var protocol = document.querySelector('input[name="protocol"]:checked').value;
var hardware = document.querySelector('input[name="hardware"]:checked').value; var hardware = document.querySelector('input[name="hardware"]:checked').value;
if(protocol === "drycontact"){ if(protocol === "drycontact"){
document.querySelector("#wiring_diagram").src = "wiring_diagrams/dry_contact_diagram.png"; document.querySelector("#wiring_diagram").src = "wiring_diagrams/dry_contact_diagram.png";
}else{ }else{
document.querySelector("#wiring_diagram").src = "wiring_diagrams/secplus_diagram.png"; document.querySelector("#wiring_diagram").src = "wiring_diagrams/secplus_diagram.png";
} }
if(protocol !== "secplusv2" && (hardware === "v2board_esp8266_d1_mini" || hardware === "v2board_esp32_d1_mini")){ if(protocol !== "secplusv2" && (hardware === "v2board_esp8266_d1_mini" || hardware === "v2board_esp32_d1_mini")){
alert("ratgdo version 2.0 only works with Security + 2.0"); alert("ratgdo version 2.0 only works with Security + 2.0");
document.querySelector('input[name="protocol"][value="secplusv2"]').checked = true; document.querySelector('input[name="protocol"][value="secplusv2"]').checked = true;
return; return;
} }
if(protocol === "secplusv2"){ if(protocol === "secplusv2"){
protocol = ""; protocol = "";
}else{ }else{
protocol = `_${protocol}`; protocol = `_${protocol}`;
} }
button.manifest = `${hardware}${protocol}-manifest.json`; button.manifest = `${hardware}${protocol}-manifest.json`;
}) })
); );

View File

@ -48,4 +48,4 @@ wifi:
ap: ap:
logger: logger:
level: DEBUG level: DEBUG