diff --git a/esphome/components/lora/gpio/__init__.py b/esphome/components/lora/gpio/__init__.py deleted file mode 100644 index 7bb038e103..0000000000 --- a/esphome/components/lora/gpio/__init__.py +++ /dev/null @@ -1,41 +0,0 @@ -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 - -CONF_LORA_GPIO = "lora_gpio" -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_GPIO, 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/gpio_lora.cpp b/esphome/components/lora/gpio/gpio_lora.cpp deleted file mode 100644 index 8b22f33a45..0000000000 --- a/esphome/components/lora/gpio/gpio_lora.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "gpio_lora.h" - -namespace esphome { -namespace lora { -static const char *const TAG_PIN = "lora.pin"; -void LoraGPIOPin::setup() { pin_mode(flags_); } -void LoraGPIOPin::pin_mode(gpio::Flags flags) { - if (flags != gpio::FLAG_OUTPUT) { - ESP_LOGD(TAG_PIN, "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/gpio_lora.h b/esphome/components/lora/gpio/gpio_lora.h deleted file mode 100644 index b412ad09ad..0000000000 --- a/esphome/components/lora/gpio/gpio_lora.h +++ /dev/null @@ -1,31 +0,0 @@ -#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 { -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/switch/__init__.py b/esphome/components/lora/switch/__init__.py new file mode 100644 index 0000000000..f778651f61 --- /dev/null +++ b/esphome/components/lora/switch/__init__.py @@ -0,0 +1,27 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import switch +from esphome.const import * +from .. import CONF_LORA, Lora, lora_ns + +LoraSwitch = lora_ns.class_("LoraSwitch", switch.Switch, cg.Component) + + +CONFIG_SCHEMA = ( + switch.switch_schema(LoraSwitch, block_inverted=True) + .extend( + { + cv.Required(CONF_LORA): cv.use_id(Lora), + cv.Required(CONF_PIN): cv.int_range(min=1, max=256), + } + ) + .extend(cv.COMPONENT_SCHEMA) +) + + +async def to_code(config): + var = await switch.new_switch(config) + parent = await cg.get_variable(config[CONF_LORA]) + await cg.register_component(var, config) + cg.add(var.set_parent(parent)) + cg.add(var.set_pin(config[CONF_PIN])) diff --git a/esphome/components/lora/switch/switch_lora.cpp b/esphome/components/lora/switch/switch_lora.cpp new file mode 100644 index 0000000000..617b87148a --- /dev/null +++ b/esphome/components/lora/switch/switch_lora.cpp @@ -0,0 +1,10 @@ +#include "switch_lora.h" + +namespace esphome { +namespace lora { +static const char *const TAG_SWITCH = "lora.switch"; + +void LoraSwitch::write_state(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); } +void LoraSwitch::dump_config() { LOG_SWITCH(TAG_SWITCH, "Lora Switch", this); } +} // namespace lora +} // namespace esphome diff --git a/esphome/components/lora/switch/switch_lora.h b/esphome/components/lora/switch/switch_lora.h new file mode 100644 index 0000000000..2fa0af9673 --- /dev/null +++ b/esphome/components/lora/switch/switch_lora.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include +#include "esphome/core/component.h" +#include "esphome/components/uart/uart.h" +#include "esphome/components/switch/switch.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" +#include "../lora.h" + +namespace esphome { +namespace lora { +class LoraSwitch : public switch_::Switch, public Component { + public: + void dump_config() override; + void set_parent(Lora *parent) { parent_ = parent; } + void set_pin(uint8_t pin) { pin_ = pin; } + + protected: + void write_state(bool state) override; + Lora *parent_; + uint8_t pin_; +}; +} // namespace lora +} // namespace esphome