From ba7737e9f89926e80dc08cdfcbb7c9ab5c482be7 Mon Sep 17 00:00:00 2001 From: Samuel Sieb Date: Tue, 2 Aug 2022 12:48:02 -0700 Subject: [PATCH] dalybms: support changing the address (#3676) --- esphome/components/daly_bms/__init__.py | 10 ++++++++-- esphome/components/daly_bms/daly_bms.cpp | 2 +- esphome/components/daly_bms/daly_bms.h | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/esphome/components/daly_bms/__init__.py b/esphome/components/daly_bms/__init__.py index 45b8f98f0c..ce0cf5216a 100644 --- a/esphome/components/daly_bms/__init__.py +++ b/esphome/components/daly_bms/__init__.py @@ -1,7 +1,7 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import uart -from esphome.const import CONF_ID +from esphome.const import CONF_ID, CONF_ADDRESS CODEOWNERS = ["@s1lvi0"] DEPENDENCIES = ["uart"] @@ -15,7 +15,12 @@ DalyBmsComponent = daly_bms.class_( ) CONFIG_SCHEMA = ( - cv.Schema({cv.GenerateID(): cv.declare_id(DalyBmsComponent)}) + cv.Schema( + { + cv.GenerateID(): cv.declare_id(DalyBmsComponent), + cv.Optional(CONF_ADDRESS, default=0x80): cv.positive_int, + } + ) .extend(uart.UART_DEVICE_SCHEMA) .extend(cv.polling_component_schema("30s")) ) @@ -25,3 +30,4 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await uart.register_uart_device(var, config) + cg.add(var.set_address(config[CONF_ADDRESS])) diff --git a/esphome/components/daly_bms/daly_bms.cpp b/esphome/components/daly_bms/daly_bms.cpp index f2b4c0e92b..36047e02d0 100644 --- a/esphome/components/daly_bms/daly_bms.cpp +++ b/esphome/components/daly_bms/daly_bms.cpp @@ -50,7 +50,7 @@ void DalyBmsComponent::request_data_(uint8_t data_id) { uint8_t request_message[DALY_FRAME_SIZE]; request_message[0] = 0xA5; // Start Flag - request_message[1] = 0x80; // Communication Module Address + request_message[1] = addr_; // Communication Module Address request_message[2] = data_id; // Data ID request_message[3] = 0x08; // Data Length (Fixed) request_message[4] = 0x00; // Empty Data diff --git a/esphome/components/daly_bms/daly_bms.h b/esphome/components/daly_bms/daly_bms.h index 90faab77f7..44915368ee 100644 --- a/esphome/components/daly_bms/daly_bms.h +++ b/esphome/components/daly_bms/daly_bms.h @@ -69,11 +69,14 @@ class DalyBmsComponent : public PollingComponent, public uart::UARTDevice { void update() override; float get_setup_priority() const override; + void set_address(uint8_t address) { this->addr_ = address; } protected: void request_data_(uint8_t data_id); void decode_data_(std::vector data); + uint8_t addr_; + sensor::Sensor *voltage_sensor_{nullptr}; sensor::Sensor *current_sensor_{nullptr}; sensor::Sensor *battery_level_sensor_{nullptr};