More logic around the switch stuff

This commit is contained in:
Daniël Koek 2024-05-18 15:23:59 +01:00
parent ce7c9cec7c
commit b273ac8545
4 changed files with 26 additions and 13 deletions

View file

@ -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_AUX = "pin_aux"
CONF_PIN_M0 = "pin_m0" CONF_PIN_M0 = "pin_m0"
CONF_PIN_M1 = "pin_m1" CONF_PIN_M1 = "pin_m1"

View file

@ -1,4 +1,4 @@
#include "ebyte_lora.h" #include "ebyte_lora_component.h"
namespace esphome { namespace esphome {
namespace ebyte_lora { namespace ebyte_lora {
static const uint8_t SWITCH_PUSH = 0x55; static const uint8_t SWITCH_PUSH = 0x55;
@ -222,8 +222,10 @@ void EbyteLoraComponent::update() {
ESP_LOGD(TAG, "Mode is not set right"); ESP_LOGD(TAG, "Mode is not set right");
this->set_mode_(NORMAL); this->set_mode_(NORMAL);
} }
#ifdef USE_SWITCH
if (!this->switch_info_receiver_) if (!this->switch_info_receiver_)
this->send_switch_info_(); this->send_switch_info_();
#endif
} }
void EbyteLoraComponent::set_config_() { void EbyteLoraComponent::set_config_() {
uint8_t data[11]; uint8_t data[11];
@ -404,6 +406,7 @@ void EbyteLoraComponent::loop() {
this->read_byte(&c); this->read_byte(&c);
data.push_back(c); data.push_back(c);
} }
#ifdef USE_SWITCH
// if it is only push info // if it is only push info
if (data[0] == SWITCH_PUSH) { if (data[0] == SWITCH_PUSH) {
ESP_LOGD(TAG, "GOT 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); this->rssi_sensor_->publish_state((data[data.size() - 1] / 255.0) * 100);
ESP_LOGD(TAG, "RSSI: %f", (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) { if (data[0] == PROGRAM_CONF) {
ESP_LOGD(TAG, "GOT PROGRAM_CONF"); ESP_LOGD(TAG, "GOT PROGRAM_CONF");
this->setup_conf_(data); this->setup_conf_(data);
@ -485,6 +489,7 @@ void EbyteLoraComponent::setup_conf_(std::vector<uint8_t> data) {
} }
} }
} }
#ifdef USE_SWITCH
void EbyteLoraComponent::send_switch_info_() { void EbyteLoraComponent::send_switch_info_() {
if (!this->can_send_message_()) { if (!this->can_send_message_()) {
return; return;
@ -502,5 +507,6 @@ void EbyteLoraComponent::send_switch_info_() {
this->write_array(data); this->write_array(data);
this->setup_wait_response_(5000); this->setup_wait_response_(5000);
} }
#endif
} // namespace ebyte_lora } // namespace ebyte_lora
} // namespace esphome } // namespace esphome

View file

@ -3,11 +3,13 @@
#include <vector> #include <vector>
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#include "esphome/components/switch/switch.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/components/uart/uart.h" #include "esphome/components/uart/uart.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "config.h" #include "config.h"
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
namespace esphome { namespace esphome {
namespace ebyte_lora { namespace ebyte_lora {
@ -32,7 +34,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
void digital_write(uint8_t pin, bool value); void digital_write(uint8_t pin, bool value);
void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; } void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; }
void set_pin_aux(InternalGPIOPin *pin_aux) { pin_aux_ = pin_aux; } 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_m0(InternalGPIOPin *pin_m0) { pin_m0_ = pin_m0; }
void set_pin_m1(InternalGPIOPin *pin_m1) { pin_m1_ = pin_m1; } void set_pin_m1(InternalGPIOPin *pin_m1) { pin_m1_ = pin_m1; }
void set_addh(uint8_t addh) { expected_config_.addh = addh; } 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; } void set_switch_info_receiver(bool enable) { switch_info_receiver_ = enable; }
private: private:
#ifdef USE_SWITCH
std::vector<EbyteLoraSwitch *> sensors_; std::vector<EbyteLoraSwitch *> sensors_;
#endif
ModeType mode_ = MODE_INIT; ModeType mode_ = MODE_INIT;
// set WOR mode // set WOR mode
void set_mode_(ModeType mode); void set_mode_(ModeType mode);
@ -62,8 +68,10 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
bool check_config_(); bool check_config_();
void set_config_(); void set_config_();
void get_current_config_(); void get_current_config_();
#ifdef USE_SWITCH
void send_switch_push_(uint8_t pin, bool value); void send_switch_push_(uint8_t pin, bool value);
void send_switch_info_(); void send_switch_info_();
#endif
void setup_conf_(std::vector<uint8_t> data); void setup_conf_(std::vector<uint8_t> data);
protected: protected:
@ -80,10 +88,9 @@ class EbyteLoraComponent : public PollingComponent, public uart::UARTDevice {
InternalGPIOPin *pin_m0_{nullptr}; InternalGPIOPin *pin_m0_{nullptr};
InternalGPIOPin *pin_m1_{nullptr}; InternalGPIOPin *pin_m1_{nullptr};
}; };
class EbyteLoraSwitch : public switch_::Switch, public Component { #ifdef USE_SWITCH
class EbyteLoraSwitch : public switch_::Switch, public Parented<EbyteLoraComponent> {
public: 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; } void set_pin(uint8_t pin) { pin_ = pin; }
uint8_t get_pin() { return pin_; } uint8_t get_pin() { return pin_; }
@ -92,6 +99,6 @@ class EbyteLoraSwitch : public switch_::Switch, public Component {
EbyteLoraComponent *parent_; EbyteLoraComponent *parent_;
uint8_t pin_; uint8_t pin_;
}; };
#endif
} // namespace ebyte_lora } // namespace ebyte_lora
} // namespace esphome } // namespace esphome

View file

@ -1,7 +1,7 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import switch 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"] DEPENDENCIES = ["ebyte_lora"]
@ -12,7 +12,7 @@ CONFIG_SCHEMA = (
switch.switch_schema(EbyteLoraSwitch) switch.switch_schema(EbyteLoraSwitch)
.extend( .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), cv.Required(PIN_TO_SEND): cv.int_range(min=1, max=4),
} }
) )
@ -21,9 +21,9 @@ CONFIG_SCHEMA = (
async def to_code(config): async def to_code(config):
ebyte_lora_component = await cg.get_variable(config[CONF_EBYTE_LORA_COMPONENT_ID])
var = await switch.new_switch(config) 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) await cg.register_component(var, config)
cg.add(var.set_parent(parent))
cg.add(var.set_pin(config[PIN_TO_SEND])) cg.add(var.set_pin(config[PIN_TO_SEND]))
cg.add(parent.register_sensor(var)) cg.add(ebyte_lora_component.set_switch(var))