try this!

This commit is contained in:
Daniël Koek 2024-03-27 18:06:33 +00:00
parent 19b0c77999
commit 35665cc0b9
6 changed files with 17 additions and 18 deletions

View file

@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import sensor, text_sensor, uart, pcf8574
from esphome.components import sensor, text_sensor, uart
from esphome.const import (
DEVICE_CLASS_SIGNAL_STRENGTH,
@ -23,7 +23,6 @@ CONF_EBYTE_LORA = "ebyte_lora"
CONF_PIN_AUX = "pin_aux"
CONF_PIN_M0 = "pin_m0"
CONF_PIN_M1 = "pin_m1"
CONF_PCF8574 = "pcf8574"
CONF_LORA_MESSAGE = "lora_message"
CONF_LORA_RSSI = "lora_rssi"
CONFIG_SCHEMA = (
@ -40,7 +39,6 @@ CONFIG_SCHEMA = (
cv.Optional(CONF_LORA_MESSAGE): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_NONE,
),
cv.Optional(CONF_PCF8574): cv.use_id(pcf8574.PCF8574Component),
# if you want to see the rssi
cv.Optional(CONF_LORA_RSSI): sensor.sensor_schema(
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
@ -71,11 +69,6 @@ async def to_code(config):
if CONF_LORA_MESSAGE in config:
sens = await text_sensor.new_text_sensor(config[CONF_LORA_MESSAGE])
cg.add(var.set_message_sensor(sens))
if CONF_PCF8574 in config:
comp = await cg.get_variable(config[CONF_PCF8574])
cg.add(var.set_pcf8574(comp))
if CONF_LORA_RSSI in config:
sens = await sensor.new_sensor(config[CONF_LORA_RSSI])
cg.add(var.set_rssi_sensor(sens))

View file

@ -166,10 +166,12 @@ void EbyteLoraComponent::loop() {
this->rssi_sensor_->publish_state(data[3]);
if (this->message_text_sensor_ != nullptr)
this->message_text_sensor_->publish_state("Got something");
if (this->pcf8574_ != nullptr) {
ESP_LOGD(TAG, "pcf8574 PIN: %u ", data[1]);
ESP_LOGD(TAG, "pcf8574 VALUE: %u ", !data[2]);
this->pcf8574_->digital_write(data[1], !data[2]);
for (auto *sensor : this->sensors_) {
if (sensor->get_pin() == data[1]) {
ESP_LOGD(TAG, "Updating switch");
sensor->got_state_message(data[2]);
}
}
} else {
ESP_LOGD(TAG, "WEIRD");

View file

@ -8,6 +8,7 @@
#include "esphome/core/helpers.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/log.h"
#include "switch/ebyte_lora_switch.h"
namespace esphome {
namespace ebyte_lora {
@ -39,11 +40,13 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
void set_message_sensor(text_sensor::TextSensor *message_text_sensor) { message_text_sensor_ = message_text_sensor; }
void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; }
void set_pin_aux(GPIOPin *pin_aux) { pin_aux_ = pin_aux; }
void register_sensor(EbyteLoraSwitch *obj) { this->sensors_.push_back(obj); }
void set_pin_m0(GPIOPin *pin_m0) { pin_m0_ = pin_m0; }
void set_pin_m1(GPIOPin *pin_m1) { pin_m1_ = pin_m1; }
void set_pcf8574(pcf8574::PCF8574Component *pcf8574) { pcf8574_ = pcf8574; }
private:
std::vector<EbyteLoraSwitch *> sensors_;
ModeType mode_ = MODE_INIT;
// set WOR mode
void set_mode_(ModeType mode);

View file

@ -24,3 +24,4 @@ async def to_code(config):
await cg.register_component(var, config)
cg.add(var.set_parent(parent))
cg.add(var.set_pin(config[PIN_TO_SEND]))
cg.add(parent.register_sensor(var))

View file

@ -3,12 +3,7 @@
namespace esphome {
namespace ebyte_lora {
static const char *const TAG_SWITCH = "ebyte_lora_switch";
void EbyteLoraSwitch::write_state(bool state) {
this->parent_->digital_write(this->pin_, state);
// finally set it
this->publish_state(state);
}
void EbyteLoraSwitch::write_state(bool state) { this->parent_->digital_write(this->pin_, state); }
void EbyteLoraSwitch::dump_config() { LOG_SWITCH(TAG_SWITCH, "Ebyte Lora Switch", this); }
} // namespace ebyte_lora
} // namespace esphome

View file

@ -15,6 +15,11 @@ class EbyteLoraSwitch : public switch_::Switch, public Component {
void dump_config() override;
void set_parent(EbyteLoraComponent *parent) { parent_ = parent; }
void set_pin(uint8_t pin) { pin_ = pin; }
uint8_t get_pin() { return pin_; }
void got_state_message(bool state) {
ESP_LOGD(TAG, "Got an update");
this->publish_state(state);
};
protected:
void write_state(bool state) override;