This commit is contained in:
Daniël Koek 2024-03-25 16:17:17 +00:00
parent 9622a34ecc
commit 886f606355
6 changed files with 62 additions and 92 deletions

View file

@ -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

View file

@ -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

View file

@ -1,31 +0,0 @@
#pragma once
#include <utility>
#include <vector>
#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

View file

@ -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]))

View file

@ -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

View file

@ -0,0 +1,25 @@
#pragma once
#include <utility>
#include <vector>
#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