Skip to content

Commit 397b206

Browse files
authored
Merge branch 'main' into on_door_state_change
2 parents e8025a8 + 9df4ceb commit 397b206

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

components/ratgdo/dry_contact.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

2-
#include "dry_contact.h"
32
#include "ratgdo.h"
43

5-
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
4+
#ifdef PROTOCOL_DRYCONTACT
5+
6+
#include "dry_contact.h"
67
#include "esphome/core/gpio.h"
78
#include "esphome/core/log.h"
89
#include "esphome/core/scheduler.h"
@@ -128,6 +129,8 @@ namespace ratgdo {
128129
return {};
129130
}
130131

131-
} // namespace DryContact
132+
} // namespace dry_contact
132133
} // namespace ratgdo
133134
} // namespace esphome
135+
136+
#endif

components/ratgdo/dry_contact.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#pragma once
22

3+
#include "esphome/core/defines.h"
4+
5+
#ifdef PROTOCOL_DRYCONTACT
6+
37
#include "SoftwareSerial.h" // Using espsoftwareserial https://github.com/plerup/espsoftwareserial
4-
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
58
#include "esphome/core/gpio.h"
69
#include "esphome/core/optional.h"
710

@@ -72,6 +75,8 @@ namespace ratgdo {
7275
bool last_close_limit_;
7376
};
7477

75-
} // namespace secplus1
78+
} // namespace dry_contact
7679
} // namespace ratgdo
7780
} // namespace esphome
81+
82+
#endif

components/ratgdo/ratgdo.cpp

+17-21
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,9 @@ namespace ratgdo {
3838
this->input_gdo_pin_->setup();
3939
this->input_gdo_pin_->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
4040

41-
if (this->input_obst_pin_ == nullptr) {
42-
// Our base.yaml is always going to set this so we check for 0
43-
// as well to avoid a breaking change.
44-
this->obstruction_from_status_ = true;
45-
} else {
46-
this->input_obst_pin_->setup();
47-
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
48-
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
49-
}
41+
this->input_obst_pin_->setup();
42+
this->input_obst_pin_->pin_mode(gpio::FLAG_INPUT);
43+
this->input_obst_pin_->attach_interrupt(RATGDOStore::isr_obstruction, &this->isr_store_, gpio::INTERRUPT_FALLING_EDGE);
5044

5145
this->protocol_->setup(this, &App.scheduler, this->input_gdo_pin_, this->output_gdo_pin_);
5246

@@ -76,9 +70,7 @@ namespace ratgdo {
7670

7771
void RATGDOComponent::loop()
7872
{
79-
if (!this->obstruction_from_status_) {
80-
this->obstruction_loop();
81-
}
73+
this->obstruction_loop();
8274
this->protocol_->loop();
8375
}
8476

@@ -87,11 +79,7 @@ namespace ratgdo {
8779
ESP_LOGCONFIG(TAG, "Setting up RATGDO...");
8880
LOG_PIN(" Output GDO Pin: ", this->output_gdo_pin_);
8981
LOG_PIN(" Input GDO Pin: ", this->input_gdo_pin_);
90-
if (this->obstruction_from_status_) {
91-
ESP_LOGCONFIG(TAG, " Input Obstruction Pin: not used, will detect from GDO status");
92-
} else {
93-
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
94-
}
82+
LOG_PIN(" Input Obstruction Pin: ", this->input_obst_pin_);
9583
this->protocol_->dump_config();
9684
}
9785

@@ -218,7 +206,7 @@ namespace ratgdo {
218206

219207
void RATGDOComponent::received(const ObstructionState obstruction_state)
220208
{
221-
if (this->obstruction_from_status_) {
209+
if (!this->obstruction_sensor_detected_) {
222210
ESP_LOGD(TAG, "Obstruction: state=%s", ObstructionState_to_string(*this->obstruction_state));
223211

224212
this->obstruction_state = obstruction_state;
@@ -378,6 +366,7 @@ namespace ratgdo {
378366
// check to see if we got more then PULSES_LOWER_LIMIT pulses
379367
if (this->isr_store_.obstruction_low_count > PULSES_LOWER_LIMIT) {
380368
this->obstruction_state = ObstructionState::CLEAR;
369+
this->obstruction_sensor_detected_ = true;
381370
} else if (this->isr_store_.obstruction_low_count == 0) {
382371
// if there have been no pulses the line is steady high or low
383372
if (!this->input_obst_pin_->digital_read()) {
@@ -471,7 +460,12 @@ namespace ratgdo {
471460
return;
472461
}
473462

474-
this->door_action(DoorAction::CLOSE);
463+
if (this->obstruction_sensor_detected_) {
464+
this->door_action(DoorAction::CLOSE);
465+
} else if (*this->door_state == DoorState::OPEN) {
466+
ESP_LOGD(TAG, "No obstruction sensors detected. Close using TOGGLE.");
467+
this->door_action(DoorAction::TOGGLE);
468+
}
475469

476470
if (*this->closing_duration > 0) {
477471
// query state in case we don't get a status message
@@ -686,22 +680,24 @@ namespace ratgdo {
686680
this->learn_state.subscribe([=](LearnState state) { defer("learn_state", [=] { f(state); }); });
687681
}
688682

683+
#ifdef PROTOCOL_DRYCONTACT
689684
// dry contact methods
690-
void RATGDOComponent::set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor)
685+
void RATGDOComponent::set_dry_contact_open_sensor(esphome::binary_sensor::BinarySensor* dry_contact_open_sensor)
691686
{
692687
dry_contact_open_sensor_ = dry_contact_open_sensor;
693688
dry_contact_open_sensor_->add_on_state_callback([this](bool sensor_value) {
694689
this->protocol_->set_open_limit(sensor_value);
695690
});
696691
}
697692

698-
void RATGDOComponent::set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor)
693+
void RATGDOComponent::set_dry_contact_close_sensor(esphome::binary_sensor::BinarySensor* dry_contact_close_sensor)
699694
{
700695
dry_contact_close_sensor_ = dry_contact_close_sensor;
701696
dry_contact_close_sensor_->add_on_state_callback([this](bool sensor_value) {
702697
this->protocol_->set_close_limit(sensor_value);
703698
});
704699
}
700+
#endif
705701

706702
} // namespace ratgdo
707703
} // namespace esphome

components/ratgdo/ratgdo.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
#pragma once
1515

16-
#include "esphome/components/gpio/binary_sensor/gpio_binary_sensor.h"
1716
#include "esphome/core/component.h"
17+
#include "esphome/core/defines.h"
1818
#include "esphome/core/hal.h"
1919
#include "esphome/core/preferences.h"
20+
#ifdef PROTOCOL_DRYCONTACT
21+
#include "esphome/components/binary_sensor/binary_sensor.h"
22+
#endif
2023

2124
#include "callbacks.h"
2225
#include "macros.h"
@@ -92,11 +95,13 @@ namespace ratgdo {
9295
void set_input_gdo_pin(InternalGPIOPin* pin) { this->input_gdo_pin_ = pin; }
9396
void set_input_obst_pin(InternalGPIOPin* pin) { this->input_obst_pin_ = pin; }
9497

98+
#ifdef PROTOCOL_DRYCONTACT
9599
// dry contact methods
96-
void set_dry_contact_open_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_);
97-
void set_dry_contact_close_sensor(esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_);
100+
void set_dry_contact_open_sensor(esphome::binary_sensor::BinarySensor* dry_contact_open_sensor_);
101+
void set_dry_contact_close_sensor(esphome::binary_sensor::BinarySensor* dry_contact_close_sensor_);
98102
void set_discrete_open_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_open_pin(pin); }
99103
void set_discrete_close_pin(InternalGPIOPin* pin) { this->protocol_->set_discrete_close_pin(pin); }
104+
#endif
100105

101106
Result call_protocol(Args args);
102107

@@ -176,13 +181,15 @@ namespace ratgdo {
176181
protected:
177182
RATGDOStore isr_store_ {};
178183
protocol::Protocol* protocol_;
179-
bool obstruction_from_status_ { false };
184+
bool obstruction_sensor_detected_ { false };
180185

181186
InternalGPIOPin* output_gdo_pin_;
182187
InternalGPIOPin* input_gdo_pin_;
183188
InternalGPIOPin* input_obst_pin_;
184-
esphome::gpio::GPIOBinarySensor* dry_contact_open_sensor_;
185-
esphome::gpio::GPIOBinarySensor* dry_contact_close_sensor_;
189+
#ifdef PROTOCOL_DRYCONTACT
190+
esphome::binary_sensor::BinarySensor* dry_contact_open_sensor_;
191+
esphome::binary_sensor::BinarySensor* dry_contact_close_sensor_;
192+
#endif
186193
}; // RATGDOComponent
187194

188195
} // namespace ratgdo

0 commit comments

Comments
 (0)