From a93a48879ae816b58ec606cba8b658c3c709225c Mon Sep 17 00:00:00 2001 From: patagona Date: Wed, 2 Oct 2024 02:45:50 +0200 Subject: [PATCH] DALY Modbus BMS: linting --- esphome/components/daly_hkms_bms/__init__.py | 17 ++- .../components/daly_hkms_bms/binary_sensor.py | 16 +-- .../daly_hkms_bms/daly_hkms_bms.cpp | 100 +++++++++--------- .../components/daly_hkms_bms/daly_hkms_bms.h | 6 +- esphome/components/daly_hkms_bms/sensor.py | 12 ++- 5 files changed, 71 insertions(+), 80 deletions(-) diff --git a/esphome/components/daly_hkms_bms/__init__.py b/esphome/components/daly_hkms_bms/__init__.py index d2a5eccbd9..3f9c75d2ac 100644 --- a/esphome/components/daly_hkms_bms/__init__.py +++ b/esphome/components/daly_hkms_bms/__init__.py @@ -14,16 +14,13 @@ DalyHkmsBmsComponent = daly_hkms_bms.class_( "DalyHkmsBmsComponent", cg.PollingComponent, modbus.ModbusDevice ) -CONFIG_SCHEMA = ( - cv.Schema( - { - cv.GenerateID(): cv.declare_id(DalyHkmsBmsComponent), - cv.GenerateID(modbus.CONF_MODBUS_ID): cv.use_id(modbus.Modbus), - cv.Optional(CONF_ADDRESS, default=1): cv.positive_int, - } - ) - .extend(cv.polling_component_schema("30s")) -) +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(DalyHkmsBmsComponent), + cv.GenerateID(modbus.CONF_MODBUS_ID): cv.use_id(modbus.Modbus), + cv.Optional(CONF_ADDRESS, default=1): cv.positive_int, + } +).extend(cv.polling_component_schema("30s")) async def to_code(config): diff --git a/esphome/components/daly_hkms_bms/binary_sensor.py b/esphome/components/daly_hkms_bms/binary_sensor.py index ab3afb27af..f2bcd23f0b 100644 --- a/esphome/components/daly_hkms_bms/binary_sensor.py +++ b/esphome/components/daly_hkms_bms/binary_sensor.py @@ -19,18 +19,10 @@ CONFIG_SCHEMA = cv.All( cv.Schema( { cv.GenerateID(CONF_DALY_HKMS_BMS_ID): cv.use_id(DalyHkmsBmsComponent), - cv.Optional( - CONF_CHARGING_MOS_ENABLED - ): binary_sensor.binary_sensor_schema(), - cv.Optional( - CONF_DISCHARGING_MOS_ENABLED - ): binary_sensor.binary_sensor_schema(), - cv.Optional( - CONF_PRECHARGING_MOS_ENABLED - ): binary_sensor.binary_sensor_schema(), - cv.Optional( - CONF_BALANCING_ACTIVE - ): binary_sensor.binary_sensor_schema(), + cv.Optional(CONF_CHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(), + cv.Optional(CONF_DISCHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(), + cv.Optional(CONF_PRECHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(), + cv.Optional(CONF_BALANCING_ACTIVE): binary_sensor.binary_sensor_schema(), } ).extend(cv.COMPONENT_SCHEMA) ) diff --git a/esphome/components/daly_hkms_bms/daly_hkms_bms.cpp b/esphome/components/daly_hkms_bms/daly_hkms_bms.cpp index b825d36372..495cbaddfa 100644 --- a/esphome/components/daly_hkms_bms/daly_hkms_bms.cpp +++ b/esphome/components/daly_hkms_bms/daly_hkms_bms.cpp @@ -47,23 +47,22 @@ void DalyHkmsBmsComponent::loop() { uint8_t start_address; uint8_t register_count; - switch (this->read_state_) - { - case ReadState::READ_CELL_VOLTAGES: - start_address = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR; - // avoid reading all 48 cell voltages if we only want 16 or so - register_count = this->cell_voltage_sensors_max_; - break; - case ReadState::READ_DATA: - start_address = DALY_MODBUS_READ_DATA_ADDR; - register_count = DALY_MODBUS_READ_DATA_LENGTH; - break; - default: - return; + switch (this->read_state_) { + case ReadState::READ_CELL_VOLTAGES: + start_address = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR; + // avoid reading all 48 cell voltages if we only want 16 or so + register_count = this->cell_voltage_sensors_max_; + break; + case ReadState::READ_DATA: + start_address = DALY_MODBUS_READ_DATA_ADDR; + register_count = DALY_MODBUS_READ_DATA_LENGTH; + break; + default: + return; } - ESP_LOGD(TAG, "Sending modbus read request to %d: start register %d, register count %d", - this->daly_address_, start_address, register_count); + ESP_LOGD(TAG, "Sending modbus read request to %d: start register %d, register count %d", this->daly_address_, + start_address, register_count); // send the request using Modbus directly instead of ModbusDevice so we can send the data with the request address uint8_t modbus_device_request_address = this->daly_address_ + DALY_MODBUS_REQUEST_ADDRESS_OFFSET; @@ -75,7 +74,7 @@ void DalyHkmsBmsComponent::loop() { } void DalyHkmsBmsComponent::update() { - if(this->read_state_ == ReadState::IDLE){ + if (this->read_state_ == ReadState::IDLE) { this->advance_read_state(); } } @@ -93,20 +92,19 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector &data) { size_t register_count; size_t register_offset; - - switch (this->read_state_) - { - case ReadState::READ_CELL_VOLTAGES: - register_offset = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR; - register_count = this->cell_voltage_sensors_max_; - break; - case ReadState::READ_DATA: - register_offset = DALY_MODBUS_READ_DATA_ADDR; - register_count = DALY_MODBUS_READ_DATA_LENGTH; - break; - default: - ESP_LOGE(TAG, "Invalid read state"); - return; + + switch (this->read_state_) { + case ReadState::READ_CELL_VOLTAGES: + register_offset = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR; + register_count = this->cell_voltage_sensors_max_; + break; + case ReadState::READ_DATA: + register_offset = DALY_MODBUS_READ_DATA_ADDR; + register_count = DALY_MODBUS_READ_DATA_LENGTH; + break; + default: + ESP_LOGE(TAG, "Invalid read state"); + return; } if (data.size() < register_count * 2) { @@ -118,8 +116,8 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector &data) { return encode_uint16(data[(i - register_offset) * 2], data[(i - register_offset) * 2 + 1]); }; - auto publish_sensor_state = [&](sensor::Sensor *sensor, size_t i, - int16_t offset, float factor, int32_t unavailable_value = -1) -> void { + auto publish_sensor_state = [&](sensor::Sensor *sensor, size_t i, int16_t offset, float factor, + int32_t unavailable_value = -1) -> void { if (sensor == nullptr) return; uint16_t register_value = get_register(i); @@ -129,14 +127,13 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector &data) { if (this->read_state_ == ReadState::READ_CELL_VOLTAGES) { #ifdef USE_SENSOR - for (size_t i = 0; i < this->cell_voltage_sensors_max_; i++) - { + for (size_t i = 0; i < this->cell_voltage_sensors_max_; i++) { publish_sensor_state(this->cell_voltage_sensors_[i], register_offset, DALY_MODBUS_ADDR_CELL_VOLT_1 + i, 0, 0.001); } #endif } else if (this->read_state_ == ReadState::READ_DATA) { #ifdef USE_SENSOR - publish_sensor_state(this->temperature_1_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_1 , -40, 1, 255); + publish_sensor_state(this->temperature_1_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_1, -40, 1, 255); publish_sensor_state(this->temperature_2_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_1 + 1, -40, 1, 255); publish_sensor_state(this->temperature_3_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_1 + 2, -40, 1, 255); publish_sensor_state(this->temperature_4_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_1 + 3, -40, 1, 255); @@ -154,13 +151,13 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector &data) { publish_sensor_state(this->max_cell_voltage_sensor_, DALY_MODBUS_ADDR_CELL_VOLT_MAX, 0, 0.001); publish_sensor_state(this->max_cell_voltage_number_sensor_, DALY_MODBUS_ADDR_CELL_VOLT_MAX_NUM, 0, 1); - + publish_sensor_state(this->min_cell_voltage_sensor_, DALY_MODBUS_ADDR_CELL_VOLT_MIN, 0, 0.001); publish_sensor_state(this->min_cell_voltage_number_sensor_, DALY_MODBUS_ADDR_CELL_VOLT_MIN_NUM, 0, 1); publish_sensor_state(this->max_temperature_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_MAX, -40, 1, 255); publish_sensor_state(this->max_temperature_probe_number_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_MAX_NUM, 0, 1); - + publish_sensor_state(this->min_temperature_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_MIN, -40, 1, 255); publish_sensor_state(this->min_temperature_probe_number_sensor_, DALY_MODBUS_ADDR_CELL_TEMP_MIN_NUM, 0, 1); @@ -208,22 +205,21 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector &data) { } void DalyHkmsBmsComponent::advance_read_state() { - switch (this->read_state_) - { - case ReadState::IDLE: - // skip reading cell voltages if there are no cell voltage sensors - if (this->cell_voltage_sensors_max_ == 0) { + switch (this->read_state_) { + case ReadState::IDLE: + // skip reading cell voltages if there are no cell voltage sensors + if (this->cell_voltage_sensors_max_ == 0) { + this->read_state_ = ReadState::READ_DATA; + } else { + this->read_state_ = ReadState::READ_CELL_VOLTAGES; + } + break; + case ReadState::READ_CELL_VOLTAGES: this->read_state_ = ReadState::READ_DATA; - } else { - this->read_state_ = ReadState::READ_CELL_VOLTAGES; - } - break; - case ReadState::READ_CELL_VOLTAGES: - this->read_state_ = ReadState::READ_DATA; - break; - case ReadState::READ_DATA: - this->read_state_ = ReadState::IDLE; - break; + break; + case ReadState::READ_DATA: + this->read_state_ = ReadState::IDLE; + break; } } diff --git a/esphome/components/daly_hkms_bms/daly_hkms_bms.h b/esphome/components/daly_hkms_bms/daly_hkms_bms.h index 7e0abde99e..aca121f488 100644 --- a/esphome/components/daly_hkms_bms/daly_hkms_bms.h +++ b/esphome/components/daly_hkms_bms/daly_hkms_bms.h @@ -31,9 +31,9 @@ class DalyHkmsBmsComponent : public PollingComponent, public modbus::ModbusDevic #ifdef USE_SENSOR void set_cell_voltage_sensor(size_t cell, sensor::Sensor *sensor) { - if(cell > this->cell_voltage_sensors_max_) + if (cell > this->cell_voltage_sensors_max_) this->cell_voltage_sensors_max_ = cell; - this->cell_voltage_sensors_[cell-1] = sensor; + this->cell_voltage_sensors_[cell - 1] = sensor; }; SUB_SENSOR(voltage) @@ -84,7 +84,7 @@ class DalyHkmsBmsComponent : public PollingComponent, public modbus::ModbusDevic void advance_read_state(); - enum class ReadState{ READ_CELL_VOLTAGES, READ_DATA, IDLE } read_state_{ReadState::IDLE}; + enum class ReadState { READ_CELL_VOLTAGES, READ_DATA, IDLE } read_state_{ReadState::IDLE}; }; } // namespace daly_hkms_bms diff --git a/esphome/components/daly_hkms_bms/sensor.py b/esphome/components/daly_hkms_bms/sensor.py index cf24a8be8b..0a86f7e115 100644 --- a/esphome/components/daly_hkms_bms/sensor.py +++ b/esphome/components/daly_hkms_bms/sensor.py @@ -103,15 +103,18 @@ CELL_VOLTAGE_SCHEMA = sensor.sensor_schema( accuracy_decimals=3, ) + def get_cell_voltage_key(cell): return f"cell_{cell}_voltage" + def get_cell_voltages_schema(): schema_obj = {} - for i in range(1, MAX_CELL_NUMBER+1): + for i in range(1, MAX_CELL_NUMBER + 1): schema_obj[cv.Optional(get_cell_voltage_key(i))] = CELL_VOLTAGE_SCHEMA return cv.Schema(schema_obj) + CONFIG_SCHEMA = ( cv.Schema( { @@ -121,7 +124,7 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_VOLT, accuracy_decimals=1, device_class=DEVICE_CLASS_VOLTAGE, - state_class=STATE_CLASS_MEASUREMENT + state_class=STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( unit_of_measurement=UNIT_AMPERE, @@ -226,20 +229,23 @@ CONFIG_SCHEMA = ( .extend(cv.COMPONENT_SCHEMA) ) + async def setup_conf(config, key, hub): if sensor_config := config.get(key): sens = await sensor.new_sensor(sensor_config) cg.add(getattr(hub, f"set_{key}_sensor")(sens)) + async def setup_cell_voltage_conf(config, cell, hub): key = get_cell_voltage_key(cell) if sensor_config := config.get(key): sens = await sensor.new_sensor(sensor_config) cg.add(hub.set_cell_voltage_sensor(cell, sens)) + async def to_code(config): hub = await cg.get_variable(config[CONF_DALY_HKMS_BMS_ID]) for key in TYPES: await setup_conf(config, key, hub) - for i in range(1, MAX_CELL_NUMBER+1): + for i in range(1, MAX_CELL_NUMBER + 1): await setup_cell_voltage_conf(config, i, hub)