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.
This commit is contained in:
Kevin P. Fleming 2024-04-27 10:10:35 -04:00
parent f9590d94ad
commit 709d7a5eae
4 changed files with 26 additions and 11 deletions

View File

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

View File

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

View File

@ -686,8 +686,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)
@ -697,7 +698,7 @@ 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)
@ -706,6 +707,7 @@ namespace ratgdo {
}
);
}
#endif
} // namespace ratgdo
} // namespace esphome

View File

@ -13,10 +13,13 @@
#pragma once
#include "esphome/core/defines.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.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 "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);
@ -181,8 +186,10 @@ namespace ratgdo {
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