From b273ac854557e85723b0dfc557462a3bf10058ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Koek?= Date: Sat, 18 May 2024 15:23:59 +0100 Subject: [PATCH] More logic around the switch stuff --- esphome/components/ebyte_lora/__init__.py | 2 +- ...byte_lora.cpp => ebyte_lora_component.cpp} | 8 +++++++- .../{ebyte_lora.h => ebyte_lora_component.h} | 19 +++++++++++++------ esphome/components/ebyte_lora/switch.py | 10 +++++----- 4 files changed, 26 insertions(+), 13 deletions(-) rename esphome/components/ebyte_lora/{ebyte_lora.cpp => ebyte_lora_component.cpp} (99%) rename esphome/components/ebyte_lora/{ebyte_lora.h => ebyte_lora_component.h} (92%) diff --git a/esphome/components/ebyte_lora/__init__.py b/esphome/components/ebyte_lora/__init__.py index a00e35a8d6..6f9615468d 100644 --- a/esphome/components/ebyte_lora/__init__.py +++ b/esphome/components/ebyte_lora/__init__.py @@ -84,7 +84,7 @@ ENABLE_OPTIONS = { } -CONF_EBYTE_LORA = "ebyte_lora" +CONF_EBYTE_LORA_COMPONENT_ID = "ebyte_lora_component_id" CONF_PIN_AUX = "pin_aux" CONF_PIN_M0 = "pin_m0" CONF_PIN_M1 = "pin_m1" diff --git a/esphome/components/ebyte_lora/ebyte_lora.cpp b/esphome/components/ebyte_lora/ebyte_lora_component.cpp similarity index 99% rename from esphome/components/ebyte_lora/ebyte_lora.cpp rename to esphome/components/ebyte_lora/ebyte_lora_component.cpp index 17945ca683..14c2471287 100644 --- a/esphome/components/ebyte_lora/ebyte_lora.cpp +++ b/esphome/components/ebyte_lora/ebyte_lora_component.cpp @@ -1,4 +1,4 @@ -#include "ebyte_lora.h" +#include "ebyte_lora_component.h" namespace esphome { namespace ebyte_lora { static const uint8_t SWITCH_PUSH = 0x55; @@ -222,8 +222,10 @@ void EbyteLoraComponent::update() { ESP_LOGD(TAG, "Mode is not set right"); this->set_mode_(NORMAL); } +#ifdef USE_SWITCH if (!this->switch_info_receiver_) this->send_switch_info_(); +#endif } void EbyteLoraComponent::set_config_() { uint8_t data[11]; @@ -404,6 +406,7 @@ void EbyteLoraComponent::loop() { this->read_byte(&c); data.push_back(c); } +#ifdef USE_SWITCH // if it is only push info if (data[0] == SWITCH_PUSH) { ESP_LOGD(TAG, "GOT SWITCH PUSH"); @@ -439,6 +442,7 @@ void EbyteLoraComponent::loop() { this->rssi_sensor_->publish_state((data[data.size() - 1] / 255.0) * 100); ESP_LOGD(TAG, "RSSI: %f", (data[data.size() - 1] / 255.0) * 100); } +#endif if (data[0] == PROGRAM_CONF) { ESP_LOGD(TAG, "GOT PROGRAM_CONF"); this->setup_conf_(data); @@ -485,6 +489,7 @@ void EbyteLoraComponent::setup_conf_(std::vector data) { } } } +#ifdef USE_SWITCH void EbyteLoraComponent::send_switch_info_() { if (!this->can_send_message_()) { return; @@ -502,5 +507,6 @@ void EbyteLoraComponent::send_switch_info_() { this->write_array(data); this->setup_wait_response_(5000); } +#endif } // namespace ebyte_lora } // namespace esphome diff --git a/esphome/components/ebyte_lora/ebyte_lora.h b/esphome/components/ebyte_lora/ebyte_lora_component.h similarity index 92% rename from esphome/components/ebyte_lora/ebyte_lora.h rename to esphome/components/ebyte_lora/ebyte_lora_component.h index deb190b945..f500b9f6cc 100644 --- a/esphome/components/ebyte_lora/ebyte_lora.h +++ b/esphome/components/ebyte_lora/ebyte_lora_component.h @@ -3,11 +3,13 @@ #include #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" -#include "esphome/components/switch/switch.h" #include "esphome/core/helpers.h" #include "esphome/components/uart/uart.h" #include "esphome/core/log.h" #include "config.h" +#ifdef USE_SWITCH +#include "esphome/components/switch/switch.h" +#endif namespace esphome { namespace ebyte_lora { @@ -32,7 +34,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { void digital_write(uint8_t pin, bool value); void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; } void set_pin_aux(InternalGPIOPin *pin_aux) { pin_aux_ = pin_aux; } - void register_sensor(EbyteLoraSwitch *obj) { this->sensors_.push_back(obj); } +#ifdef USE_SWITCH + void set_switch(EbyteLoraSwitch *obj) { this->sensors_.push_back(obj); } +#endif void set_pin_m0(InternalGPIOPin *pin_m0) { pin_m0_ = pin_m0; } void set_pin_m1(InternalGPIOPin *pin_m1) { pin_m1_ = pin_m1; } void set_addh(uint8_t addh) { expected_config_.addh = addh; } @@ -51,7 +55,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { void set_switch_info_receiver(bool enable) { switch_info_receiver_ = enable; } private: +#ifdef USE_SWITCH std::vector sensors_; +#endif ModeType mode_ = MODE_INIT; // set WOR mode void set_mode_(ModeType mode); @@ -62,8 +68,10 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { bool check_config_(); void set_config_(); void get_current_config_(); +#ifdef USE_SWITCH void send_switch_push_(uint8_t pin, bool value); void send_switch_info_(); +#endif void setup_conf_(std::vector data); protected: @@ -80,10 +88,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice { InternalGPIOPin *pin_m0_{nullptr}; InternalGPIOPin *pin_m1_{nullptr}; }; -class EbyteLoraSwitch : public switch_::Switch, public Component { +#ifdef USE_SWITCH +class EbyteLoraSwitch : public switch_::Switch, public Parented { public: - void dump_config() override { LOG_SWITCH("ebyte_lora_switch", "Ebyte Lora Switch", this); } - void set_parent(EbyteLoraComponent *parent) { parent_ = parent; } void set_pin(uint8_t pin) { pin_ = pin; } uint8_t get_pin() { return pin_; } @@ -92,6 +99,6 @@ class EbyteLoraSwitch : public switch_::Switch, public Component { EbyteLoraComponent *parent_; uint8_t pin_; }; - +#endif } // namespace ebyte_lora } // namespace esphome diff --git a/esphome/components/ebyte_lora/switch.py b/esphome/components/ebyte_lora/switch.py index 9bd692bfcd..3d8b59f988 100644 --- a/esphome/components/ebyte_lora/switch.py +++ b/esphome/components/ebyte_lora/switch.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import switch -from . import EbyteLoraComponent, CONF_EBYTE_LORA, ebyte_lora_ns +from . import EbyteLoraComponent, CONF_EBYTE_LORA_COMPONENT_ID, ebyte_lora_ns DEPENDENCIES = ["ebyte_lora"] @@ -12,7 +12,7 @@ CONFIG_SCHEMA = ( switch.switch_schema(EbyteLoraSwitch) .extend( { - cv.Required(CONF_EBYTE_LORA): cv.use_id(EbyteLoraComponent), + cv.GenerateID(CONF_EBYTE_LORA_COMPONENT_ID): cv.use_id(EbyteLoraComponent), cv.Required(PIN_TO_SEND): cv.int_range(min=1, max=4), } ) @@ -21,9 +21,9 @@ CONFIG_SCHEMA = ( async def to_code(config): + ebyte_lora_component = await cg.get_variable(config[CONF_EBYTE_LORA_COMPONENT_ID]) var = await switch.new_switch(config) - parent = await cg.get_variable(config[CONF_EBYTE_LORA]) + await cg.register_parented(var, config[CONF_EBYTE_LORA_COMPONENT_ID]) 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)) + cg.add(ebyte_lora_component.set_switch(var))