mirror of
https://github.com/esphome/esphome.git
synced 2025-02-12 07:58:17 +00:00
DALY Modbus BMS: linting
This commit is contained in:
parent
eef430e932
commit
a93a48879a
@ -14,16 +14,13 @@ DalyHkmsBmsComponent = daly_hkms_bms.class_(
|
|||||||
"DalyHkmsBmsComponent", cg.PollingComponent, modbus.ModbusDevice
|
"DalyHkmsBmsComponent", cg.PollingComponent, modbus.ModbusDevice
|
||||||
)
|
)
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = cv.Schema(
|
||||||
cv.Schema(
|
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.declare_id(DalyHkmsBmsComponent),
|
cv.GenerateID(): cv.declare_id(DalyHkmsBmsComponent),
|
||||||
cv.GenerateID(modbus.CONF_MODBUS_ID): cv.use_id(modbus.Modbus),
|
cv.GenerateID(modbus.CONF_MODBUS_ID): cv.use_id(modbus.Modbus),
|
||||||
cv.Optional(CONF_ADDRESS, default=1): cv.positive_int,
|
cv.Optional(CONF_ADDRESS, default=1): cv.positive_int,
|
||||||
}
|
}
|
||||||
)
|
).extend(cv.polling_component_schema("30s"))
|
||||||
.extend(cv.polling_component_schema("30s"))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
|
@ -19,18 +19,10 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(CONF_DALY_HKMS_BMS_ID): cv.use_id(DalyHkmsBmsComponent),
|
cv.GenerateID(CONF_DALY_HKMS_BMS_ID): cv.use_id(DalyHkmsBmsComponent),
|
||||||
cv.Optional(
|
cv.Optional(CONF_CHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(),
|
||||||
CONF_CHARGING_MOS_ENABLED
|
cv.Optional(CONF_DISCHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(),
|
||||||
): binary_sensor.binary_sensor_schema(),
|
cv.Optional(CONF_PRECHARGING_MOS_ENABLED): binary_sensor.binary_sensor_schema(),
|
||||||
cv.Optional(
|
cv.Optional(CONF_BALANCING_ACTIVE): binary_sensor.binary_sensor_schema(),
|
||||||
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)
|
).extend(cv.COMPONENT_SCHEMA)
|
||||||
)
|
)
|
||||||
|
@ -47,8 +47,7 @@ void DalyHkmsBmsComponent::loop() {
|
|||||||
|
|
||||||
uint8_t start_address;
|
uint8_t start_address;
|
||||||
uint8_t register_count;
|
uint8_t register_count;
|
||||||
switch (this->read_state_)
|
switch (this->read_state_) {
|
||||||
{
|
|
||||||
case ReadState::READ_CELL_VOLTAGES:
|
case ReadState::READ_CELL_VOLTAGES:
|
||||||
start_address = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR;
|
start_address = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR;
|
||||||
// avoid reading all 48 cell voltages if we only want 16 or so
|
// avoid reading all 48 cell voltages if we only want 16 or so
|
||||||
@ -62,8 +61,8 @@ void DalyHkmsBmsComponent::loop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Sending modbus read request to %d: start register %d, register count %d",
|
ESP_LOGD(TAG, "Sending modbus read request to %d: start register %d, register count %d", this->daly_address_,
|
||||||
this->daly_address_, start_address, register_count);
|
start_address, register_count);
|
||||||
|
|
||||||
// send the request using Modbus directly instead of ModbusDevice so we can send the data with the request address
|
// 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;
|
uint8_t modbus_device_request_address = this->daly_address_ + DALY_MODBUS_REQUEST_ADDRESS_OFFSET;
|
||||||
@ -94,8 +93,7 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector<uint8_t> &data) {
|
|||||||
size_t register_count;
|
size_t register_count;
|
||||||
size_t register_offset;
|
size_t register_offset;
|
||||||
|
|
||||||
switch (this->read_state_)
|
switch (this->read_state_) {
|
||||||
{
|
|
||||||
case ReadState::READ_CELL_VOLTAGES:
|
case ReadState::READ_CELL_VOLTAGES:
|
||||||
register_offset = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR;
|
register_offset = DALY_MODBUS_READ_CELL_VOLTAGES_ADDR;
|
||||||
register_count = this->cell_voltage_sensors_max_;
|
register_count = this->cell_voltage_sensors_max_;
|
||||||
@ -118,8 +116,8 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector<uint8_t> &data) {
|
|||||||
return encode_uint16(data[(i - register_offset) * 2], data[(i - register_offset) * 2 + 1]);
|
return encode_uint16(data[(i - register_offset) * 2], data[(i - register_offset) * 2 + 1]);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto publish_sensor_state = [&](sensor::Sensor *sensor, size_t i,
|
auto publish_sensor_state = [&](sensor::Sensor *sensor, size_t i, int16_t offset, float factor,
|
||||||
int16_t offset, float factor, int32_t unavailable_value = -1) -> void {
|
int32_t unavailable_value = -1) -> void {
|
||||||
if (sensor == nullptr)
|
if (sensor == nullptr)
|
||||||
return;
|
return;
|
||||||
uint16_t register_value = get_register(i);
|
uint16_t register_value = get_register(i);
|
||||||
@ -129,8 +127,7 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector<uint8_t> &data) {
|
|||||||
|
|
||||||
if (this->read_state_ == ReadState::READ_CELL_VOLTAGES) {
|
if (this->read_state_ == ReadState::READ_CELL_VOLTAGES) {
|
||||||
#ifdef USE_SENSOR
|
#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);
|
publish_sensor_state(this->cell_voltage_sensors_[i], register_offset, DALY_MODBUS_ADDR_CELL_VOLT_1 + i, 0, 0.001);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -208,8 +205,7 @@ void DalyHkmsBmsComponent::on_modbus_data(const std::vector<uint8_t> &data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DalyHkmsBmsComponent::advance_read_state() {
|
void DalyHkmsBmsComponent::advance_read_state() {
|
||||||
switch (this->read_state_)
|
switch (this->read_state_) {
|
||||||
{
|
|
||||||
case ReadState::IDLE:
|
case ReadState::IDLE:
|
||||||
// skip reading cell voltages if there are no cell voltage sensors
|
// skip reading cell voltages if there are no cell voltage sensors
|
||||||
if (this->cell_voltage_sensors_max_ == 0) {
|
if (this->cell_voltage_sensors_max_ == 0) {
|
||||||
|
@ -103,15 +103,18 @@ CELL_VOLTAGE_SCHEMA = sensor.sensor_schema(
|
|||||||
accuracy_decimals=3,
|
accuracy_decimals=3,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_cell_voltage_key(cell):
|
def get_cell_voltage_key(cell):
|
||||||
return f"cell_{cell}_voltage"
|
return f"cell_{cell}_voltage"
|
||||||
|
|
||||||
|
|
||||||
def get_cell_voltages_schema():
|
def get_cell_voltages_schema():
|
||||||
schema_obj = {}
|
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
|
schema_obj[cv.Optional(get_cell_voltage_key(i))] = CELL_VOLTAGE_SCHEMA
|
||||||
return cv.Schema(schema_obj)
|
return cv.Schema(schema_obj)
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
@ -121,7 +124,7 @@ CONFIG_SCHEMA = (
|
|||||||
unit_of_measurement=UNIT_VOLT,
|
unit_of_measurement=UNIT_VOLT,
|
||||||
accuracy_decimals=1,
|
accuracy_decimals=1,
|
||||||
device_class=DEVICE_CLASS_VOLTAGE,
|
device_class=DEVICE_CLASS_VOLTAGE,
|
||||||
state_class=STATE_CLASS_MEASUREMENT
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
|
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
|
||||||
unit_of_measurement=UNIT_AMPERE,
|
unit_of_measurement=UNIT_AMPERE,
|
||||||
@ -226,17 +229,20 @@ CONFIG_SCHEMA = (
|
|||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def setup_conf(config, key, hub):
|
async def setup_conf(config, key, hub):
|
||||||
if sensor_config := config.get(key):
|
if sensor_config := config.get(key):
|
||||||
sens = await sensor.new_sensor(sensor_config)
|
sens = await sensor.new_sensor(sensor_config)
|
||||||
cg.add(getattr(hub, f"set_{key}_sensor")(sens))
|
cg.add(getattr(hub, f"set_{key}_sensor")(sens))
|
||||||
|
|
||||||
|
|
||||||
async def setup_cell_voltage_conf(config, cell, hub):
|
async def setup_cell_voltage_conf(config, cell, hub):
|
||||||
key = get_cell_voltage_key(cell)
|
key = get_cell_voltage_key(cell)
|
||||||
if sensor_config := config.get(key):
|
if sensor_config := config.get(key):
|
||||||
sens = await sensor.new_sensor(sensor_config)
|
sens = await sensor.new_sensor(sensor_config)
|
||||||
cg.add(hub.set_cell_voltage_sensor(cell, sens))
|
cg.add(hub.set_cell_voltage_sensor(cell, sens))
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
hub = await cg.get_variable(config[CONF_DALY_HKMS_BMS_ID])
|
hub = await cg.get_variable(config[CONF_DALY_HKMS_BMS_ID])
|
||||||
for key in TYPES:
|
for key in TYPES:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user