some cleanup

This commit is contained in:
Guido Schreuder 2024-02-21 08:55:19 +01:00
parent 35c6d00044
commit 454eaa9109
9 changed files with 79 additions and 91 deletions

View file

@ -16,6 +16,9 @@ from esphome.const import (
# TODO: send identification response when requested # TODO: send identification response when requested
# TODO: add debug mode that logs all messages on the bus # TODO: add debug mode that logs all messages on the bus
# TODO: investigate using UART component, but that does not seem to expose the UART NUM # TODO: investigate using UART component, but that does not seem to expose the UART NUM
# TODO: telegrams are always send to slave right now, primary->primary communication
# is possible according to the spec, but haven't found any need for it yet
CODEOWNERS = ["@guidoschreuder"] CODEOWNERS = ["@guidoschreuder"]

View file

@ -35,5 +35,4 @@ async def to_code(config):
sensor_base_config(sens, config) sensor_base_config(sens, config)
cg.add(sens.set_response_read_mask(config[CONF_TELEGRAM][CONF_DECODE][CONF_MASK])) cg.add(sens.set_response_read_mask(config[CONF_TELEGRAM][CONF_DECODE][CONF_MASK]))
cg.add(ebus.add_receiver(sens)) cg.add(ebus.add_sensor(sens))
cg.add(ebus.add_sender(sens))

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "../ebus_sensor_base.h" #include "../ebus_component.h"
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
namespace esphome { namespace esphome {

View file

@ -170,6 +170,51 @@ void EbusComponent::update() {
} }
} }
void EbusSensorBase::dump_config() {
ESP_LOGCONFIG(TAG, "EbusSensor");
ESP_LOGCONFIG(TAG, " message:");
ESP_LOGCONFIG(TAG, " send_poll: %s", this->send_poll_ ? "true" : "false");
if (this->address_ == SYN) {
ESP_LOGCONFIG(TAG, " address: N/A");
} else {
ESP_LOGCONFIG(TAG, " address: 0x%02x", this->address_);
}
ESP_LOGCONFIG(TAG, " command: 0x%04x", this->command_);
};
optional<SendCommand> EbusSensorBase::prepare_command() {
optional<SendCommand> command;
if (this->send_poll_) {
command = SendCommand( //
this->primary_address_, this->address_, this->command_, this->payload_.size(), &this->payload_[0]);
}
return command;
}
uint32_t EbusSensorBase::get_response_bytes(Telegram &telegram, uint8_t start, uint8_t length) {
uint32_t result = 0;
for (uint8_t i = 0; i < 4 && i < length; i++) {
result = result | (telegram.get_response_byte(start + i) << (i * 8));
}
return result;
}
bool EbusSensorBase::is_mine(Telegram &telegram) {
if (this->address_ != SYN && this->address_ != telegram.get_zz()) {
return false;
}
if (telegram.get_command() != this->command_) {
return false;
}
for (int i = 0; i < this->payload_.size(); i++) {
if (this->payload_[i] != telegram.get_request_byte(i)) {
return false;
}
}
return true;
}
} // namespace ebus } // namespace ebus
} // namespace esphome } // namespace esphome

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <vector>
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
@ -35,6 +36,28 @@ class EbusSender {
uint8_t address_ = SYN; uint8_t address_ = SYN;
}; };
class EbusSensorBase : public EbusReceiver, public EbusSender, public Component {
public:
void dump_config() override;
void set_send_poll(bool send_poll) { this->send_poll_ = send_poll; }
void set_command(uint16_t command) { this->command_ = command; }
void set_payload(const std::vector<uint8_t> &payload) { this->payload_ = payload; }
void set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; }
optional<SendCommand> prepare_command() override;
// TODO: refactor these
uint32_t get_response_bytes(Telegram &telegram, uint8_t start, uint8_t length);
bool is_mine(Telegram &telegram);
protected:
bool send_poll_;
uint16_t command_;
std::vector<uint8_t> payload_{};
uint8_t response_position_;
};
class EbusComponent : public PollingComponent { class EbusComponent : public PollingComponent {
public: public:
EbusComponent() {} EbusComponent() {}
@ -53,6 +76,10 @@ class EbusComponent : public PollingComponent {
void add_sender(EbusSender * /*sender*/); void add_sender(EbusSender * /*sender*/);
void add_receiver(EbusReceiver * /*receiver*/); void add_receiver(EbusReceiver * /*receiver*/);
void add_sensor(EbusSensorBase *sensor) {
this->add_sender(sensor);
this->add_receiver(sensor);
};
void update() override; void update() override;

View file

@ -1,53 +0,0 @@
#include "ebus_sensor_base.h"
namespace esphome {
namespace ebus {
void EbusSensorBase::dump_config() {
ESP_LOGCONFIG(TAG, "EbusSensor");
ESP_LOGCONFIG(TAG, " message:");
ESP_LOGCONFIG(TAG, " send_poll: %s", this->send_poll_ ? "true" : "false");
if (this->address_ == SYN) {
ESP_LOGCONFIG(TAG, " address: N/A");
} else {
ESP_LOGCONFIG(TAG, " address: 0x%02x", this->address_);
}
ESP_LOGCONFIG(TAG, " command: 0x%04x", this->command_);
};
optional<SendCommand> EbusSensorBase::prepare_command() {
optional<SendCommand> command;
if (this->send_poll_) {
command = SendCommand( //
this->primary_address_, this->address_, this->command_, this->payload_.size(), &this->payload_[0]);
}
return command;
}
uint32_t EbusSensorBase::get_response_bytes(Telegram &telegram, uint8_t start, uint8_t length) {
uint32_t result = 0;
for (uint8_t i = 0; i < 4 && i < length; i++) {
result = result | (telegram.get_response_byte(start + i) << (i * 8));
}
return result;
}
bool EbusSensorBase::is_mine(Telegram &telegram) {
if (this->address_ != SYN && this->address_ != telegram.get_zz()) {
return false;
}
if (telegram.get_command() != this->command_) {
return false;
}
for (int i = 0; i < this->payload_.size(); i++) {
if (this->payload_[i] != telegram.get_request_byte(i)) {
return false;
}
}
return true;
}
} // namespace ebus
} // namespace esphome

View file

@ -1,32 +0,0 @@
#pragma once
#include "ebus_component.h"
#include <vector>
namespace esphome {
namespace ebus {
class EbusSensorBase : public EbusReceiver, public EbusSender, public Component {
public:
void dump_config() override;
void set_send_poll(bool send_poll) { this->send_poll_ = send_poll; }
void set_command(uint16_t command) { this->command_ = command; }
void set_payload(const std::vector<uint8_t> &payload) { this->payload_ = payload; }
void set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; }
optional<SendCommand> prepare_command() override;
// TODO: refactor these
uint32_t get_response_bytes(Telegram &telegram, uint8_t start, uint8_t length);
bool is_mine(Telegram &telegram);
protected:
bool send_poll_;
uint16_t command_;
std::vector<uint8_t> payload_{};
uint8_t response_position_;
};
} // namespace ebus
} // namespace esphome

View file

@ -43,5 +43,4 @@ async def to_code(config):
cg.add( cg.add(
sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER]) sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER])
) )
cg.add(ebus.add_receiver(sens)) cg.add(ebus.add_sensor(sens))
cg.add(ebus.add_sender(sens))

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "../ebus_sensor_base.h" #include "../ebus_component.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
namespace esphome { namespace esphome {