diff --git a/esphome/components/ebus/__init__.py b/esphome/components/ebus/__init__.py index 4591daaa5b..052410e4f8 100644 --- a/esphome/components/ebus/__init__.py +++ b/esphome/components/ebus/__init__.py @@ -16,6 +16,9 @@ from esphome.const import ( # TODO: send identification response when requested # 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: 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"] diff --git a/esphome/components/ebus/binary_sensor/__init__.py b/esphome/components/ebus/binary_sensor/__init__.py index 2beac87439..8172c2b86d 100644 --- a/esphome/components/ebus/binary_sensor/__init__.py +++ b/esphome/components/ebus/binary_sensor/__init__.py @@ -35,5 +35,4 @@ async def to_code(config): sensor_base_config(sens, config) cg.add(sens.set_response_read_mask(config[CONF_TELEGRAM][CONF_DECODE][CONF_MASK])) - cg.add(ebus.add_receiver(sens)) - cg.add(ebus.add_sender(sens)) + cg.add(ebus.add_sensor(sens)) diff --git a/esphome/components/ebus/binary_sensor/ebus_binary_sensor.h b/esphome/components/ebus/binary_sensor/ebus_binary_sensor.h index a1bef3f8ae..8aad16e217 100644 --- a/esphome/components/ebus/binary_sensor/ebus_binary_sensor.h +++ b/esphome/components/ebus/binary_sensor/ebus_binary_sensor.h @@ -1,6 +1,6 @@ #pragma once -#include "../ebus_sensor_base.h" +#include "../ebus_component.h" #include "esphome/components/binary_sensor/binary_sensor.h" namespace esphome { diff --git a/esphome/components/ebus/ebus_component.cpp b/esphome/components/ebus/ebus_component.cpp index b3c9a6ebe4..9e6afdf93e 100644 --- a/esphome/components/ebus/ebus_component.cpp +++ b/esphome/components/ebus/ebus_component.cpp @@ -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 EbusSensorBase::prepare_command() { + optional 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 diff --git a/esphome/components/ebus/ebus_component.h b/esphome/components/ebus/ebus_component.h index 6fc6aafa7d..20ce8e73b1 100644 --- a/esphome/components/ebus/ebus_component.h +++ b/esphome/components/ebus/ebus_component.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "esphome/core/log.h" #include "esphome/core/component.h" @@ -35,6 +36,28 @@ class EbusSender { 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 &payload) { this->payload_ = payload; } + void set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; } + + optional 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 payload_{}; + uint8_t response_position_; +}; + class EbusComponent : public PollingComponent { public: EbusComponent() {} @@ -53,6 +76,10 @@ class EbusComponent : public PollingComponent { void add_sender(EbusSender * /*sender*/); void add_receiver(EbusReceiver * /*receiver*/); + void add_sensor(EbusSensorBase *sensor) { + this->add_sender(sensor); + this->add_receiver(sensor); + }; void update() override; diff --git a/esphome/components/ebus/ebus_sensor_base.cpp b/esphome/components/ebus/ebus_sensor_base.cpp deleted file mode 100644 index ea0062b965..0000000000 --- a/esphome/components/ebus/ebus_sensor_base.cpp +++ /dev/null @@ -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 EbusSensorBase::prepare_command() { - optional 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 diff --git a/esphome/components/ebus/ebus_sensor_base.h b/esphome/components/ebus/ebus_sensor_base.h deleted file mode 100644 index 1c4feb2e72..0000000000 --- a/esphome/components/ebus/ebus_sensor_base.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include "ebus_component.h" -#include - -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 &payload) { this->payload_ = payload; } - void set_response_read_position(uint8_t response_position) { this->response_position_ = response_position; } - - optional 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 payload_{}; - uint8_t response_position_; -}; - -} // namespace ebus -} // namespace esphome diff --git a/esphome/components/ebus/sensor/__init__.py b/esphome/components/ebus/sensor/__init__.py index 57281f7ca7..19b3e46f82 100644 --- a/esphome/components/ebus/sensor/__init__.py +++ b/esphome/components/ebus/sensor/__init__.py @@ -43,5 +43,4 @@ async def to_code(config): cg.add( sens.set_response_read_divider(config[CONF_TELEGRAM][CONF_DECODE][CONF_DIVIDER]) ) - cg.add(ebus.add_receiver(sens)) - cg.add(ebus.add_sender(sens)) + cg.add(ebus.add_sensor(sens)) diff --git a/esphome/components/ebus/sensor/ebus_sensor.h b/esphome/components/ebus/sensor/ebus_sensor.h index 4ee0ec0b11..14f1fa2fe9 100644 --- a/esphome/components/ebus/sensor/ebus_sensor.h +++ b/esphome/components/ebus/sensor/ebus_sensor.h @@ -1,6 +1,6 @@ #pragma once -#include "../ebus_sensor_base.h" +#include "../ebus_component.h" #include "esphome/components/sensor/sensor.h" namespace esphome {