From c877160833315f00834b51229009bc6b009c2b3b Mon Sep 17 00:00:00 2001 From: Simone Rossetto Date: Sat, 24 Jun 2023 15:58:19 +0200 Subject: [PATCH] Exclude wireguard sensors from build if not loaded --- esphome/components/wireguard/wireguard.cpp | 10 ++++++++++ esphome/components/wireguard/wireguard.h | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/esphome/components/wireguard/wireguard.cpp b/esphome/components/wireguard/wireguard.cpp index de431087820d..b3a2874bbb54 100644 --- a/esphome/components/wireguard/wireguard.cpp +++ b/esphome/components/wireguard/wireguard.cpp @@ -89,16 +89,20 @@ void Wireguard::update() { } } +#ifdef USE_BINARY_SENSOR if (this->status_sensor_ != nullptr) { this->status_sensor_->publish_state(peer_up); } +#endif +#ifdef USE_SENSOR if (this->handshake_sensor_ != nullptr) { if (lhs > this->latest_saved_handshake_) { this->latest_saved_handshake_ = lhs; this->handshake_sensor_->publish_state((float) lhs); } } +#endif } void Wireguard::dump_config() { @@ -164,8 +168,14 @@ void Wireguard::add_allowed_ip(const std::string &ip, const std::string &netmask void Wireguard::set_keepalive(const uint16_t seconds) { this->keepalive_ = seconds; } void Wireguard::set_reboot_timeout(const uint32_t seconds) { this->reboot_timeout_ = seconds; } void Wireguard::set_srctime(time::RealTimeClock *srctime) { this->srctime_ = srctime; } + +#ifdef USE_BINARY_SENSOR void Wireguard::set_status_sensor(binary_sensor::BinarySensor *sensor) { this->status_sensor_ = sensor; } +#endif + +#ifdef USE_SENSOR void Wireguard::set_handshake_sensor(sensor::Sensor *sensor) { this->handshake_sensor_ = sensor; } +#endif void Wireguard::disable_auto_proceed() { this->proceed_allowed_ = false; } diff --git a/esphome/components/wireguard/wireguard.h b/esphome/components/wireguard/wireguard.h index 0124a559c183..262c7b1cfd73 100644 --- a/esphome/components/wireguard/wireguard.h +++ b/esphome/components/wireguard/wireguard.h @@ -8,8 +8,14 @@ #include "esphome/core/component.h" #include "esphome/components/time/real_time_clock.h" + +#ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" +#endif + +#ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" +#endif #include @@ -39,8 +45,14 @@ class Wireguard : public PollingComponent { void set_keepalive(uint16_t seconds); void set_reboot_timeout(uint32_t seconds); void set_srctime(time::RealTimeClock *srctime); + +#ifdef USE_BINARY_SENSOR void set_status_sensor(binary_sensor::BinarySensor *sensor); +#endif + +#ifdef USE_SENSOR void set_handshake_sensor(sensor::Sensor *sensor); +#endif /// Block the setup step until peer is connected. void disable_auto_proceed(); @@ -63,8 +75,14 @@ class Wireguard : public PollingComponent { uint32_t reboot_timeout_; time::RealTimeClock *srctime_; + +#ifdef USE_BINARY_SENSOR binary_sensor::BinarySensor *status_sensor_ = nullptr; +#endif + +#ifdef USE_SENSOR sensor::Sensor *handshake_sensor_ = nullptr; +#endif /// Set to false to block the setup step until peer is connected. bool proceed_allowed_ = true;