From b5d0aede381d21ea6683575603805a56fddaf1a8 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 30 Mar 2023 14:08:31 +1300 Subject: [PATCH] Remove AUTO_LOAD from as3935 (#4630) --- esphome/components/as3935/__init__.py | 1 - esphome/components/as3935/as3935.cpp | 14 ++++++++++++-- esphome/components/as3935/as3935.h | 23 +++++++++++++++-------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/esphome/components/as3935/__init__.py b/esphome/components/as3935/__init__.py index 0951d01e68..cf0580ca62 100644 --- a/esphome/components/as3935/__init__.py +++ b/esphome/components/as3935/__init__.py @@ -12,7 +12,6 @@ from esphome.const import ( CONF_CAPACITANCE, ) -AUTO_LOAD = ["sensor", "binary_sensor"] MULTI_CONF = True CONF_AS3935_ID = "as3935_id" diff --git a/esphome/components/as3935/as3935.cpp b/esphome/components/as3935/as3935.cpp index b36856218a..c5651caee3 100644 --- a/esphome/components/as3935/as3935.cpp +++ b/esphome/components/as3935/as3935.cpp @@ -26,9 +26,13 @@ void AS3935Component::setup() { void AS3935Component::dump_config() { ESP_LOGCONFIG(TAG, "AS3935:"); LOG_PIN(" Interrupt Pin: ", this->irq_pin_); +#ifdef USE_BINARY_SENSOR LOG_BINARY_SENSOR(" ", "Thunder alert", this->thunder_alert_binary_sensor_); +#endif +#ifdef USE_SENSOR LOG_SENSOR(" ", "Distance", this->distance_sensor_); LOG_SENSOR(" ", "Lightning energy", this->energy_sensor_); +#endif } float AS3935Component::get_setup_priority() const { return setup_priority::DATA; } @@ -44,16 +48,22 @@ void AS3935Component::loop() { ESP_LOGI(TAG, "Disturber was detected - try increasing the spike rejection value!"); } else if (int_value == LIGHTNING_INT) { ESP_LOGI(TAG, "Lightning has been detected!"); - if (this->thunder_alert_binary_sensor_ != nullptr) +#ifdef USE_BINARY_SENSOR + if (this->thunder_alert_binary_sensor_ != nullptr) { this->thunder_alert_binary_sensor_->publish_state(true); + this->set_timeout(10, [this]() { this->thunder_alert_binary_sensor_->publish_state(false); }); + } +#endif +#ifdef USE_SENSOR uint8_t distance = this->get_distance_to_storm_(); if (this->distance_sensor_ != nullptr) this->distance_sensor_->publish_state(distance); + uint32_t energy = this->get_lightning_energy_(); if (this->energy_sensor_ != nullptr) this->energy_sensor_->publish_state(energy); +#endif } - this->thunder_alert_binary_sensor_->publish_state(false); } void AS3935Component::write_indoor(bool indoor) { diff --git a/esphome/components/as3935/as3935.h b/esphome/components/as3935/as3935.h index 2cba9b11a0..a8af703a59 100644 --- a/esphome/components/as3935/as3935.h +++ b/esphome/components/as3935/as3935.h @@ -1,9 +1,14 @@ #pragma once #include "esphome/core/component.h" +#include "esphome/core/defines.h" #include "esphome/core/hal.h" +#ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" +#endif namespace esphome { namespace as3935 { @@ -52,6 +57,15 @@ enum AS3935Values { }; class AS3935Component : public Component { +#ifdef USE_SENSOR + SUB_SENSOR(distance) + SUB_SENSOR(energy) +#endif + +#ifdef USE_BINARY_SENSOR + SUB_BINARY_SENSOR(thunder_alert) +#endif + public: void setup() override; void dump_config() override; @@ -59,11 +73,7 @@ class AS3935Component : public Component { void loop() override; void set_irq_pin(GPIOPin *irq_pin) { irq_pin_ = irq_pin; } - void set_distance_sensor(sensor::Sensor *distance_sensor) { distance_sensor_ = distance_sensor; } - void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; } - void set_thunder_alert_binary_sensor(binary_sensor::BinarySensor *thunder_alert_binary_sensor) { - thunder_alert_binary_sensor_ = thunder_alert_binary_sensor; - } + void set_indoor(bool indoor) { indoor_ = indoor; } void write_indoor(bool indoor); void set_noise_level(uint8_t noise_level) { noise_level_ = noise_level; } @@ -92,9 +102,6 @@ class AS3935Component : public Component { virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position) = 0; - sensor::Sensor *distance_sensor_{nullptr}; - sensor::Sensor *energy_sensor_{nullptr}; - binary_sensor::BinarySensor *thunder_alert_binary_sensor_{nullptr}; GPIOPin *irq_pin_; bool indoor_;