From 64c09811df91be434f827da16bd30318ca1aa4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Koek?= Date: Mon, 25 Mar 2024 12:12:25 +0000 Subject: [PATCH] cleanup and new build --- esphome/components/lora/__init__.py | 49 ++++++--------------------- esphome/components/lora/gpio.py | 40 ++++++++++++++++++++++ esphome/components/lora/gpio_lora.cpp | 20 +++++++++++ esphome/components/lora/gpio_lora.h | 36 ++++++++++++++++++++ esphome/components/lora/lora.cpp | 20 ++--------- esphome/components/lora/lora.h | 21 ------------ 6 files changed, 109 insertions(+), 77 deletions(-) create mode 100644 esphome/components/lora/gpio.py create mode 100644 esphome/components/lora/gpio_lora.cpp create mode 100644 esphome/components/lora/gpio_lora.h diff --git a/esphome/components/lora/__init__.py b/esphome/components/lora/__init__.py index 52bd254252..bc9f195807 100644 --- a/esphome/components/lora/__init__.py +++ b/esphome/components/lora/__init__.py @@ -2,15 +2,19 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import sensor, text_sensor, uart -from esphome.const import * +from esphome.const import ( + DEVICE_CLASS_SIGNAL_STRENGTH, + UNIT_DECIBEL_MILLIWATT, + STATE_CLASS_MEASUREMENT, + CONF_ID, + ENTITY_CATEGORY_NONE, +) DEPENDENCIES = ["uart"] AUTO_LOAD = ["uart", "sensor", "text_sensor"] -MULTI_CONF = True lora_ns = cg.esphome_ns.namespace("lora") -Lora = lora_ns.class_("Lora", cg.Component, uart.UARTDevice) -LoraGPIOPin = lora_ns.class_("LoraGPIOPin", cg.GPIOPin) +Lora = lora_ns.class_("Lora", cg.PollingComponent, uart.UARTDevice) CONF_PIN_AUX = "pin_aux" CONF_PIN_M0 = "pin_m0" CONF_PIN_M1 = "pin_m1" @@ -40,8 +44,8 @@ CONFIG_SCHEMA = ( ), } ) - # check values every 20s - .extend(cv.polling_component_schema("20s")).extend(uart.UART_DEVICE_SCHEMA) + .extend(cv.polling_component_schema("20s")) + .extend(uart.UART_DEVICE_SCHEMA) ) @@ -65,36 +69,3 @@ async def to_code(config): if CONF_LORA_RSSI in config: sens = await sensor.new_sensor(config[CONF_LORA_RSSI]) cg.add(var.set_rssi_sensor(sens)) - - -def validate_mode(value): - if not (value[CONF_OUTPUT]): - raise cv.Invalid("Mode must be output") - return value - - -Lora_PIN_SCHEMA = pins.gpio_base_schema( - LoraGPIOPin, - cv.int_range(min=0, max=17), - modes=[CONF_OUTPUT], - mode_validator=validate_mode, - invertable=True, -).extend( - { - cv.Required(CONF_LORA): cv.use_id(Lora), - } -) - - -@pins.PIN_SCHEMA_REGISTRY.register(CONF_LORA, Lora_PIN_SCHEMA) -async def lora_pin_to_code(config): - var = cg.new_Pvariable(config[CONF_ID]) - parent = await cg.get_variable(config[CONF_LORA]) - - cg.add(var.set_parent(parent)) - - num = config[CONF_NUMBER] - cg.add(var.set_pin(num)) - cg.add(var.set_inverted(config[CONF_INVERTED])) - cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE]))) - return var diff --git a/esphome/components/lora/gpio.py b/esphome/components/lora/gpio.py new file mode 100644 index 0000000000..4c0d6478a4 --- /dev/null +++ b/esphome/components/lora/gpio.py @@ -0,0 +1,40 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome import pins +from esphome.const import CONF_ID, CONF_OUTPUT, CONF_NUMBER, CONF_INVERTED, CONF_MODE +from . import CONF_LORA, Lora, lora_ns + +LoraGPIOPin = lora_ns.class_("LoraGPIOPin", cg.GPIOPin) + + +def validate_mode(value): + if not (value[CONF_OUTPUT]): + raise cv.Invalid("Mode must be output") + return value + + +Lora_PIN_SCHEMA = pins.gpio_base_schema( + LoraGPIOPin, + cv.int_range(min=0, max=17), + modes=[CONF_OUTPUT], + mode_validator=validate_mode, + invertable=True, +).extend( + { + cv.Required(CONF_LORA): cv.use_id(Lora), + } +) + + +@pins.PIN_SCHEMA_REGISTRY.register(CONF_LORA, Lora_PIN_SCHEMA) +async def lora_pin_to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + parent = await cg.get_variable(config[CONF_LORA]) + + cg.add(var.set_parent(parent)) + + num = config[CONF_NUMBER] + cg.add(var.set_pin(num)) + cg.add(var.set_inverted(config[CONF_INVERTED])) + cg.add(var.set_flags(pins.gpio_flags_expr(config[CONF_MODE]))) + return var diff --git a/esphome/components/lora/gpio_lora.cpp b/esphome/components/lora/gpio_lora.cpp new file mode 100644 index 0000000000..06d8cbadd1 --- /dev/null +++ b/esphome/components/lora/gpio_lora.cpp @@ -0,0 +1,20 @@ +#include "gpio_lora.h" + +namespace esphome { +namespace lora { +static const char *const TAGPin = "lora.pin"; +void LoraGPIOPin::setup() { pin_mode(flags_); } +void LoraGPIOPin::pin_mode(gpio::Flags flags) { + if (flags != gpio::FLAG_OUTPUT) { + ESP_LOGD(TAGPin, "Output only supported"); + } +} +bool LoraGPIOPin::digital_read() { return false; } +void LoraGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } +std::string LoraGPIOPin::dump_summary() const { + char buffer[32]; + snprintf(buffer, sizeof(buffer), "%u via Lora", pin_); + return buffer; +} +} // namespace lora +} // namespace esphome diff --git a/esphome/components/lora/gpio_lora.h b/esphome/components/lora/gpio_lora.h new file mode 100644 index 0000000000..2f94e54ac0 --- /dev/null +++ b/esphome/components/lora/gpio_lora.h @@ -0,0 +1,36 @@ +#pragma once +#include +#include +#include "esphome/core/component.h" +#include "esphome/components/sensor/sensor.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" +#include "lora.h" + +namespace esphome { +namespace lora { + +static const char *const TAG = "lora"; +// pin stuff + +class LoraGPIOPin : public GPIOPin { + public: + void setup() override; + void pin_mode(gpio::Flags flags) override; + bool digital_read() override; + void digital_write(bool value) override; + std::string dump_summary() const override; + + void set_parent(Lora *parent) { parent_ = parent; } + void set_pin(uint8_t pin) { pin_ = pin; } + void set_inverted(bool inverted) { inverted_ = inverted; } + void set_flags(gpio::Flags flags) { flags_ = flags; } + + protected: + Lora *parent_; + uint8_t pin_; + bool inverted_; + gpio::Flags flags_; +}; +} // namespace lora +} // namespace esphome diff --git a/esphome/components/lora/lora.cpp b/esphome/components/lora/lora.cpp index 525524464c..2354914f98 100644 --- a/esphome/components/lora/lora.cpp +++ b/esphome/components/lora/lora.cpp @@ -142,6 +142,8 @@ void Lora::loop() { std::vector data; bool pin_data_found = false; ESP_LOGD(TAG, "Starting to check for messages"); + if (!this->available()) + return; while (this->available()) { uint8_t c; if (this->read_byte(&c)) { @@ -162,25 +164,9 @@ void Lora::loop() { } // set the rssi rssi_ = atoi(buffer.substr(buffer.length() - 1, 1).c_str()); + ESP_LOGD(TAG, "RSSI: %u ", rssi_); // set the raw message raw_message_ = buffer.substr(0, buffer.length() - 1); } - -// pin stuff - -static const char *const TAGPin = "lora.pin"; -void LoraGPIOPin::setup() { pin_mode(flags_); } -void LoraGPIOPin::pin_mode(gpio::Flags flags) { - if (flags != gpio::FLAG_OUTPUT) { - ESP_LOGD(TAGPin, "Output only supported"); - } -} -bool LoraGPIOPin::digital_read() { return false; } -void LoraGPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } -std::string LoraGPIOPin::dump_summary() const { - char buffer[32]; - snprintf(buffer, sizeof(buffer), "%u via Lora", pin_); - return buffer; -} } // namespace lora } // namespace esphome diff --git a/esphome/components/lora/lora.h b/esphome/components/lora/lora.h index 396552d20b..74b389ac55 100644 --- a/esphome/components/lora/lora.h +++ b/esphome/components/lora/lora.h @@ -65,26 +65,5 @@ class Lora : public PollingComponent, public uart::UARTDevice { GPIOPin *pin_m1{nullptr}; }; -// pin stuff - -class LoraGPIOPin : public GPIOPin { - public: - void setup() override; - void pin_mode(gpio::Flags flags) override; - bool digital_read() override; - void digital_write(bool value) override; - std::string dump_summary() const override; - - void set_parent(Lora *parent) { parent_ = parent; } - void set_pin(uint8_t pin) { pin_ = pin; } - void set_inverted(bool inverted) { inverted_ = inverted; } - void set_flags(gpio::Flags flags) { flags_ = flags; } - - protected: - Lora *parent_; - uint8_t pin_; - bool inverted_; - gpio::Flags flags_; -}; } // namespace lora } // namespace esphome