Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move fake traffic from code to YAML (Closes: #82) #85

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions components/jbd_bms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
MULTI_CONF = True

CONF_JBD_BMS_ID = "jbd_bms_id"
CONF_ENABLE_FAKE_TRAFFIC = "enable_fake_traffic"
CONF_RX_TIMEOUT = "rx_timeout"

jbd_bms_ns = cg.esphome_ns.namespace("jbd_bms")
Expand All @@ -20,7 +19,6 @@
cv.Schema(
{
cv.GenerateID(): cv.declare_id(JbdBms),
cv.Optional(CONF_ENABLE_FAKE_TRAFFIC, default=False): cv.boolean,
cv.Optional(
CONF_RX_TIMEOUT, default="150ms"
): cv.positive_time_period_milliseconds,
Expand All @@ -36,5 +34,4 @@ async def to_code(config):
await cg.register_component(var, config)
await uart.register_uart_device(var, config)

cg.add(var.set_enable_fake_traffic(config[CONF_ENABLE_FAKE_TRAFFIC]))
cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT]))
6 changes: 1 addition & 5 deletions components/jbd_bms/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
DEVICE_CLASS_CONNECTIVITY,
ENTITY_CATEGORY_DIAGNOSTIC,
)
from esphome.const import CONF_ID, DEVICE_CLASS_CONNECTIVITY, ENTITY_CATEGORY_DIAGNOSTIC

from . import CONF_JBD_BMS_ID, JbdBms
from .const import CONF_CHARGING, CONF_DISCHARGING
Expand Down
22 changes: 2 additions & 20 deletions components/jbd_bms/jbd_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,6 @@ void JbdBms::loop() {
void JbdBms::update() {
this->track_online_status_();
this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO);

if (this->enable_fake_traffic_) {
// Start: 0xDD 0x03 0x00 0x1D
this->on_jbd_bms_data_(JBD_CMD_HWINFO,
{0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C, 0x7C, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x64, 0x03, 0x04, 0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84});
// End: 0xFA 0x8D 0x77

// Start: 0xDD 0x04 0x00 0x08
this->on_jbd_bms_data_(JBD_CMD_CELLINFO, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D});
// End: 0xFE 0xC6 0x77

// Start: 0xDD 0x05 0x00 0x19
this->on_jbd_bms_data_(JBD_CMD_HWVER, {0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D,
0x4C, 0x34, 0x53, 0x2D, 0x32, 0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55});
// End: 0xFA, 0x08, 0x77
}
}

bool JbdBms::parse_jbd_bms_byte_(uint8_t byte) {
Expand Down Expand Up @@ -162,13 +145,13 @@ bool JbdBms::parse_jbd_bms_byte_(uint8_t byte) {

std::vector<uint8_t> data(this->rx_buffer_.begin() + 4, this->rx_buffer_.begin() + frame_len - 3);

this->on_jbd_bms_data_(function, data);
this->on_jbd_bms_data(function, data);

// return false to reset buffer
return false;
}

void JbdBms::on_jbd_bms_data_(const uint8_t &function, const std::vector<uint8_t> &data) {
void JbdBms::on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t> &data) {
this->reset_online_status_tracker_();

switch (function) {
Expand Down Expand Up @@ -387,7 +370,6 @@ void JbdBms::publish_device_unavailable_() {
void JbdBms::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
ESP_LOGCONFIG(TAG, "JbdBms:");
ESP_LOGCONFIG(TAG, " RX timeout: %d ms", this->rx_timeout_);
ESP_LOGCONFIG(TAG, " Fake traffic enabled: %s", YESNO(this->enable_fake_traffic_));

LOG_BINARY_SENSOR("", "Balancing", this->balancing_binary_sensor_);
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
Expand Down
4 changes: 1 addition & 3 deletions components/jbd_bms/jbd_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
void set_device_model_text_sensor(text_sensor::TextSensor *device_model_text_sensor) {
device_model_text_sensor_ = device_model_text_sensor;
}
void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
void set_rx_timeout(uint16_t rx_timeout) { rx_timeout_ = rx_timeout; }
bool write_register(uint8_t address, uint16_t value);
bool change_mosfet_status(uint8_t address, uint8_t bitmask, bool state);
void on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t> &data);

protected:
binary_sensor::BinarySensor *balancing_binary_sensor_;
Expand Down Expand Up @@ -168,9 +168,7 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
uint16_t rx_timeout_{150};
uint8_t no_response_count_{0};
uint8_t mosfet_status_{255};
bool enable_fake_traffic_;

void on_jbd_bms_data_(const uint8_t &function, const std::vector<uint8_t> &data);
void on_cell_info_data_(const std::vector<uint8_t> &data);
void on_hardware_info_data_(const std::vector<uint8_t> &data);
void on_hardware_version_data_(const std::vector<uint8_t> &data);
Expand Down
4 changes: 0 additions & 4 deletions components/jbd_bms_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
MULTI_CONF = True

CONF_JBD_BMS_BLE_ID = "jbd_bms_ble_id"
CONF_ENABLE_FAKE_TRAFFIC = "enable_fake_traffic"

jbd_bms_ble_ns = cg.esphome_ns.namespace("jbd_bms_ble")
JbdBmsBle = jbd_bms_ble_ns.class_(
Expand All @@ -20,7 +19,6 @@
cv.Schema(
{
cv.GenerateID(): cv.declare_id(JbdBmsBle),
cv.Optional(CONF_ENABLE_FAKE_TRAFFIC, default=False): cv.boolean,
}
)
.extend(ble_client.BLE_CLIENT_SCHEMA)
Expand All @@ -32,5 +30,3 @@ async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await ble_client.register_ble_node(var, config)

cg.add(var.set_enable_fake_traffic(config[CONF_ENABLE_FAKE_TRAFFIC]))
6 changes: 1 addition & 5 deletions components/jbd_bms_ble/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
DEVICE_CLASS_CONNECTIVITY,
ENTITY_CATEGORY_DIAGNOSTIC,
)
from esphome.const import CONF_ID, DEVICE_CLASS_CONNECTIVITY, ENTITY_CATEGORY_DIAGNOSTIC

from . import CONF_JBD_BMS_BLE_ID, JbdBmsBle
from .const import CONF_CHARGING, CONF_DISCHARGING
Expand Down
23 changes: 2 additions & 21 deletions components/jbd_bms_ble/jbd_bms_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,12 @@ void JbdBmsBle::assemble_(const uint8_t *data, uint16_t length) {

std::vector<uint8_t> data(this->frame_buffer_.begin() + 4, this->frame_buffer_.end() - 3);

this->on_jbd_bms_ble_data_(function, data);
this->on_jbd_bms_data(function, data);
this->frame_buffer_.clear();
}
}

void JbdBmsBle::update() {
if (this->enable_fake_traffic_) {
// Start: 0xDD 0x03 0x00 0x1D
this->on_jbd_bms_ble_data_(
JBD_CMD_HWINFO, {0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C, 0x7C, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x64, 0x03, 0x04, 0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84});
// End: 0xFA 0x8D 0x77

// Start: 0xDD 0x04 0x00 0x08
this->on_jbd_bms_ble_data_(JBD_CMD_CELLINFO, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D});
// End: 0xFE 0xC6 0x77

// Start: 0xDD 0x05 0x00 0x19
this->on_jbd_bms_ble_data_(JBD_CMD_HWVER,
{0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D,
0x4C, 0x34, 0x53, 0x2D, 0x32, 0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55});
// End: 0xFA, 0x08, 0x77
}

this->track_online_status_();
if (this->node_state != espbt::ClientState::ESTABLISHED) {
ESP_LOGW(TAG, "[%s] Not connected", this->parent_->address_str().c_str());
Expand All @@ -179,7 +161,7 @@ void JbdBmsBle::update() {
this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO);
}

void JbdBmsBle::on_jbd_bms_ble_data_(const uint8_t &function, const std::vector<uint8_t> &data) {
void JbdBmsBle::on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t> &data) {
this->reset_online_status_tracker_();

switch (function) {
Expand Down Expand Up @@ -397,7 +379,6 @@ void JbdBmsBle::publish_device_unavailable_() {

void JbdBmsBle::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
ESP_LOGCONFIG(TAG, "JbdBmsBle:");
ESP_LOGCONFIG(TAG, " Fake traffic enabled: %s", YESNO(this->enable_fake_traffic_));

LOG_BINARY_SENSOR("", "Balancing", this->balancing_binary_sensor_);
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
Expand Down
4 changes: 1 addition & 3 deletions components/jbd_bms_ble/jbd_bms_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ class JbdBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompo
void set_device_model_text_sensor(text_sensor::TextSensor *device_model_text_sensor) {
device_model_text_sensor_ = device_model_text_sensor;
}
void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
bool write_register(uint8_t address, uint16_t value);
bool change_mosfet_status(uint8_t address, uint8_t bitmask, bool state);
void on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t> &data);

protected:
binary_sensor::BinarySensor *balancing_binary_sensor_;
Expand Down Expand Up @@ -174,10 +174,8 @@ class JbdBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompo
uint16_t char_command_handle_;
uint8_t no_response_count_{0};
uint8_t mosfet_status_{255};
bool enable_fake_traffic_;

void assemble_(const uint8_t *data, uint16_t length);
void on_jbd_bms_ble_data_(const uint8_t &function, const std::vector<uint8_t> &data);
void on_cell_info_data_(const std::vector<uint8_t> &data);
void on_hardware_info_data_(const std::vector<uint8_t> &data);
void on_hardware_version_data_(const std::vector<uint8_t> &data);
Expand Down
27 changes: 26 additions & 1 deletion esp32-ble-example-faker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,29 @@ jbd_bms_ble:
- id: bms0
ble_client_id: client0
update_interval: 1s
enable_fake_traffic: true

interval:
- interval: 5s
then:
# Start: 0xDD 0x03 0x00 0x1D
- lambda: |-
id(bms0).on_jbd_bms_data(0x03, {
0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x64, 0x03, 0x04, 0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84
});
# End: 0xFA 0x8D 0x77

- delay: 1s
# Start: 0xDD 0x04 0x00 0x08
- lambda: |-
id(bms0).on_jbd_bms_data(0x04, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D});
# End: 0xFE 0xC6 0x77

- delay: 1s
# Start: 0xDD 0x05 0x00 0x19
- lambda: |-
id(bms0).on_jbd_bms_data(0x05, {
0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D, 0x4C, 0x34, 0x53, 0x2D, 0x32,
0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55
});
# End: 0xFA, 0x08, 0x77
27 changes: 26 additions & 1 deletion esp32-example-faker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,29 @@ jbd_bms:
uart_id: uart_0
update_interval: 2s
rx_timeout: ${rx_timeout}
enable_fake_traffic: true

interval:
- interval: 5s
then:
# Start: 0xDD 0x03 0x00 0x1D
- lambda: |-
id(bms0).on_jbd_bms_data(0x03, {
0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x64, 0x03, 0x04, 0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84
});
# End: 0xFA 0x8D 0x77

- delay: 1s
# Start: 0xDD 0x04 0x00 0x08
- lambda: |-
id(bms0).on_jbd_bms_data(0x04, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D});
# End: 0xFE 0xC6 0x77

- delay: 1s
# Start: 0xDD 0x05 0x00 0x19
- lambda: |-
id(bms0).on_jbd_bms_data(0x05, {
0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D, 0x4C, 0x34, 0x53, 0x2D, 0x32,
0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55
});
# End: 0xFA, 0x08, 0x77
27 changes: 26 additions & 1 deletion esp8266-example-faker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,29 @@ jbd_bms:
uart_id: uart_0
update_interval: 2s
rx_timeout: ${rx_timeout}
enable_fake_traffic: true

interval:
- interval: 5s
then:
# Start: 0xDD 0x03 0x00 0x1D
- lambda: |-
id(bms0).on_jbd_bms_data(0x03, {
0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x64, 0x03, 0x04, 0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84
});
# End: 0xFA 0x8D 0x77

- delay: 1s
# Start: 0xDD 0x04 0x00 0x08
- lambda: |-
id(bms0).on_jbd_bms_data(0x04, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D});
# End: 0xFE 0xC6 0x77

- delay: 1s
# Start: 0xDD 0x05 0x00 0x19
- lambda: |-
id(bms0).on_jbd_bms_data(0x05, {
0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D, 0x4C, 0x34, 0x53, 0x2D, 0x32,
0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55
});
# End: 0xFA, 0x08, 0x77
Loading