Merge branch 'main' into on_door_state_change

This commit is contained in:
J. Nick Koston 2024-07-03 07:02:02 -07:00 committed by GitHub
commit 397b206299
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 32 deletions

View File

@ -1,8 +1,9 @@
#include "dry_contact.h"
#include "ratgdo.h"
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
#ifdef PROTOCOL_DRYCONTACT
#include "dry_contact.h"
#include "esphome/core/gpio.h"
#include "esphome/core/log.h"
#include "esphome/core/scheduler.h"
@ -128,6 +129,8 @@ namespace ratgdo {
return {};
}
} // namespace DryContact
} // namespace dry_contact
} // namespace ratgdo
} // namespace esphome
#endif

View File

@ -1,7 +1,10 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef PROTOCOL_DRYCONTACT
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
#include "esphome/core/gpio.h"
#include "esphome/core/optional.h"
@ -72,6 +75,8 @@ namespace ratgdo {
bool last_close_limit_;
};
} // namespace secplus1
} // namespace dry_contact
} // namespace ratgdo
} // namespace esphome
#endif

View File

@ -38,15 +38,9 @@ namespace ratgdo {
this->input_gdo_pin_->setup();
this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
if (this->input_obst_pin_ == nullptr) {
// Our base.yaml is always going to set this so we check for 0
// as well to avoid a breaking change.
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->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_);
@ -76,9 +70,7 @@ namespace ratgdo {
void RATGDOComponent::loop()
{
if (!this->obstruction_from_status_) {
this->obstruction_loop();
}
this->obstruction_loop();
this->protocol_->loop();
}
@ -87,11 +79,7 @@ namespace ratgdo {
ESP_LOGCONFIG(TAG, "Setting up RATGDO...");
LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_);
LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_);
if (this->obstruction_from_status_) {
ESP_LOGCONFIG(TAG, " Input Obstruction Pin: not used, will detect from GDO status");
} else {
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
}
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
this->protocol_->dump_config();
}
@ -218,7 +206,7 @@ namespace ratgdo {
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));
this->obstruction_state = obstruction_state;
@ -378,6 +366,7 @@ namespace ratgdo {
// check to see if we got more then PULSES_LOWER_LIMIT pulses
if (this->isr_store_.obstruction_low_count > PULSES_LOWER_LIMIT) {
this->obstruction_state = ObstructionState::CLEAR;
this->obstruction_sensor_detected_ = true;
} else if (this->isr_store_.obstruction_low_count == 0) {
// if there have been no pulses the line is steady high or low
if (!this->input_obst_pin_->digital_read()) {
@ -471,7 +460,12 @@ namespace ratgdo {
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) {
// query state in case we don't get a status message
@ -686,8 +680,9 @@ namespace ratgdo {
this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); });
}
#ifdef PROTOCOL_DRYCONTACT
// 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_->add_on_state_callback([this](bool sensor_value) {
@ -695,13 +690,14 @@ namespace ratgdo {
});
}
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_->add_on_state_callback([this](bool sensor_value) {
this->protocol_->set_close_limit(sensor_value);
});
}
#endif
} // namespace ratgdo
} // namespace esphome

View File

@ -13,10 +13,13 @@
#pragma once
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#include "esphome/core/preferences.h"
#ifdef PROTOCOL_DRYCONTACT
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#include "callbacks.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_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }
#ifdef PROTOCOL_DRYCONTACT
// dry contact methods
void set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_);
void set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_);
void set_dry_contact_open_sensor(esphome::binary_sensor::BinarySensor* dry_contact_open_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_close_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_close_pin(pin); }
#endif
Result call_protocol(Args args);
@ -176,13 +181,15 @@ namespace ratgdo {
protected:
RATGDOStore isr_store_ {};
protocol::Protocol* protocol_;
bool obstruction_from_status_ { false };
bool obstruction_sensor_detected_ { false };
InternalGPIOPin* output_gdo_pin_;
InternalGPIOPin* input_gdo_pin_;
InternalGPIOPin* input_obst_pin_;
esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_;
esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_;
#ifdef PROTOCOL_DRYCONTACT
esphome::binary_sensor::BinarySensor* dry_contact_open_sensor_;
esphome::binary_sensor::BinarySensor* dry_contact_close_sensor_;
#endif
}; // RATGDOComponent
} // namespace ratgdo