From 35665cc0b9c2b27d92636667f3976017664dfa02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Koek?= Date: Wed, 27 Mar 2024 18:06:33 +0000 Subject: [PATCH] try this! --- esphome/components/ebyte_lora/__init__.py | 9 +-------- esphome/components/ebyte_lora/ebyte_lora.cpp | 10 ++++++---- esphome/components/ebyte_lora/ebyte_lora.h | 3 +++ esphome/components/ebyte_lora/switch/__init__.py | 1 + .../components/ebyte_lora/switch/ebyte_lora_switch.cpp | 7 +------ .../components/ebyte_lora/switch/ebyte_lora_switch.h | 5 +++++ 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/esphome/components/ebyte_lora/__init__.py b/esphome/components/ebyte_lora/__init__.py index a823fd24b7..502c6b6e98 100644 --- a/esphome/components/ebyte_lora/__init__.py +++ b/esphome/components/ebyte_lora/__init__.py @@ -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)) diff --git a/esphome/components/ebyte_lora/ebyte_lora.cpp b/esphome/components/ebyte_lora/ebyte_lora.cpp index 8e5970cfb2..319dec8ad1 100644 --- a/esphome/components/ebyte_lora/ebyte_lora.cpp +++ b/esphome/components/ebyte_lora/ebyte_lora.cpp @@ -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"); diff --git a/esphome/components/ebyte_lora/ebyte_lora.h b/esphome/components/ebyte_lora/ebyte_lora.h index fd70625350..376f9199c6 100644 --- a/esphome/components/ebyte_lora/ebyte_lora.h +++ b/esphome/components/ebyte_lora/ebyte_lora.h @@ -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 sensors_; ModeType mode_ = MODE_INIT; // set WOR mode void set_mode_(ModeType mode); diff --git a/esphome/components/ebyte_lora/switch/__init__.py b/esphome/components/ebyte_lora/switch/__init__.py index b0dc10c5c0..b387a0cb0f 100644 --- a/esphome/components/ebyte_lora/switch/__init__.py +++ b/esphome/components/ebyte_lora/switch/__init__.py @@ -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)) diff --git a/esphome/components/ebyte_lora/switch/ebyte_lora_switch.cpp b/esphome/components/ebyte_lora/switch/ebyte_lora_switch.cpp index 78b442ff03..ca7da6d642 100644 --- a/esphome/components/ebyte_lora/switch/ebyte_lora_switch.cpp +++ b/esphome/components/ebyte_lora/switch/ebyte_lora_switch.cpp @@ -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 diff --git a/esphome/components/ebyte_lora/switch/ebyte_lora_switch.h b/esphome/components/ebyte_lora/switch/ebyte_lora_switch.h index 66d07a9221..44e2e59721 100644 --- a/esphome/components/ebyte_lora/switch/ebyte_lora_switch.h +++ b/esphome/components/ebyte_lora/switch/ebyte_lora_switch.h @@ -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;