Skip to content

Commit 24bbe48

Browse files
committed
Populate operation status text sensor
1 parent 5a273ab commit 24bbe48

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

components/jbd_bms/jbd_bms.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ static const char *const ERRORS[ERRORS_SIZE] = {
4949
"Unknown (0x0F)", // 0x0F
5050
};
5151

52+
static const uint8_t OPERATION_STATUS_SIZE = 8;
53+
static const char *const OPERATION_STATUS[OPERATION_STATUS_SIZE] = {
54+
"Charging", // 0x01
55+
"Discharging", // 0x02
56+
"Unknown (0x04)", // 0x04
57+
"Unknown (0x08)", // 0x08
58+
"Unknown (0x10)", // 0x10
59+
"Unknown (0x20)", // 0x20
60+
"Unknown (0x40)", // 0x40
61+
"Unknown (0x80)", // 0x80
62+
};
63+
5264
void JbdBms::setup() { this->send_command(JBD_CMD_READ, JBD_CMD_HWINFO); }
5365

5466
void JbdBms::loop() {
@@ -303,7 +315,7 @@ void JbdBms::on_hardware_info_data_(const std::vector<uint8_t> &data) {
303315
// 16 2 0x00 0x00 Protection Status
304316
uint16_t errors_bitmask = jbd_get_16bit(16);
305317
this->publish_state_(this->errors_bitmask_sensor_, (float) errors_bitmask);
306-
this->publish_state_(this->errors_text_sensor_, this->error_bits_to_string_(errors_bitmask));
318+
this->publish_state_(this->errors_text_sensor_, this->bitmask_to_string_(ERRORS, ERRORS_SIZE, errors_bitmask));
307319

308320
// 18 1 0x80 Version 0x10 = 1.0, 0x80 = 8.0
309321
this->publish_state_(this->software_version_sensor_, (data[18] >> 4) + ((data[18] & 0x0f) * 0.1f));
@@ -315,6 +327,8 @@ void JbdBms::on_hardware_info_data_(const std::vector<uint8_t> &data) {
315327
uint8_t operation_status = data[20];
316328
this->mosfet_status_ = operation_status;
317329
this->publish_state_(this->operation_status_bitmask_sensor_, operation_status);
330+
this->publish_state_(this->operation_status_text_sensor_,
331+
this->bitmask_to_string_(OPERATION_STATUS, OPERATION_STATUS_SIZE, operation_status));
318332
this->publish_state_(this->charging_binary_sensor_, operation_status & JBD_MOS_CHARGE);
319333
this->publish_state_(this->charging_switch_, operation_status & JBD_MOS_CHARGE);
320334
this->publish_state_(this->discharging_binary_sensor_, operation_status & JBD_MOS_DISCHARGE);
@@ -574,12 +588,13 @@ void JbdBms::send_command(uint8_t action, uint8_t function) {
574588
this->flush();
575589
}
576590

577-
std::string JbdBms::error_bits_to_string_(const uint16_t mask) {
591+
std::string JbdBms::bitmask_to_string_(const char *const messages[], const uint8_t &messages_size,
592+
const uint16_t &mask) {
578593
std::string values = "";
579594
if (mask) {
580-
for (int i = 0; i < ERRORS_SIZE; i++) {
595+
for (int i = 0; i < messages_size; i++) {
581596
if (mask & (1 << i)) {
582-
values.append(ERRORS[i]);
597+
values.append(messages[i]);
583598
values.append(";");
584599
}
585600
}

components/jbd_bms/jbd_bms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
235235
void publish_device_unavailable_();
236236
void reset_online_status_tracker_();
237237
void track_online_status_();
238-
std::string error_bits_to_string_(uint16_t bitmask);
238+
std::string bitmask_to_string_(const char *const messages[], const uint8_t &messages_size, const uint16_t &mask);
239239

240240
uint16_t chksum_(const uint8_t data[], const uint16_t len) {
241241
uint16_t checksum = 0x00;

components/jbd_bms_ble/jbd_bms_ble.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ static const char *const ERRORS[ERRORS_SIZE] = {
6565
"Unknown (0x0F)", // 0x0F
6666
};
6767

68+
static const uint8_t OPERATION_STATUS_SIZE = 8;
69+
static const char *const OPERATION_STATUS[OPERATION_STATUS_SIZE] = {
70+
"Charging", // 0x01
71+
"Discharging", // 0x02
72+
"Unknown (0x04)", // 0x04
73+
"Unknown (0x08)", // 0x08
74+
"Unknown (0x10)", // 0x10
75+
"Unknown (0x20)", // 0x20
76+
"Unknown (0x40)", // 0x40
77+
"Unknown (0x80)", // 0x80
78+
};
79+
6880
static const uint8_t ROOT_PASSWORD[] = {0x4a, 0x42, 0x44, 0x62, 0x74, 0x70, 0x77, 0x64,
6981
0x21, 0x40, 0x23, 0x32, 0x30, 0x32, 0x33};
7082
static const size_t ROOT_PASSWORD_LENGTH = sizeof(ROOT_PASSWORD);
@@ -534,7 +546,7 @@ void JbdBmsBle::on_hardware_info_data_(const std::vector<uint8_t> &data) {
534546
// 16 2 0x00 0x00 Protection Status
535547
uint16_t errors_bitmask = jbd_get_16bit(16);
536548
this->publish_state_(this->errors_bitmask_sensor_, (float) errors_bitmask);
537-
this->publish_state_(this->errors_text_sensor_, this->error_bits_to_string_(errors_bitmask));
549+
this->publish_state_(this->errors_text_sensor_, this->bitmask_to_string_(ERRORS, ERRORS_SIZE, errors_bitmask));
538550

539551
// 18 1 0x80 Version 0x10 = 1.0, 0x80 = 8.0
540552
this->publish_state_(this->software_version_sensor_, (data[18] >> 4) + ((data[18] & 0x0f) * 0.1f));
@@ -546,6 +558,8 @@ void JbdBmsBle::on_hardware_info_data_(const std::vector<uint8_t> &data) {
546558
uint8_t operation_status = data[20];
547559
this->mosfet_status_ = operation_status;
548560
this->publish_state_(this->operation_status_bitmask_sensor_, operation_status);
561+
this->publish_state_(this->operation_status_text_sensor_,
562+
this->bitmask_to_string_(OPERATION_STATUS, OPERATION_STATUS_SIZE, operation_status));
549563
this->publish_state_(this->charging_binary_sensor_, operation_status & JBD_MOS_CHARGE);
550564
this->publish_state_(this->charging_switch_, operation_status & JBD_MOS_CHARGE);
551565
this->publish_state_(this->discharging_binary_sensor_, operation_status & JBD_MOS_DISCHARGE);
@@ -839,12 +853,13 @@ bool JbdBmsBle::send_command(uint8_t action, uint8_t function) {
839853
return (status == 0);
840854
}
841855

842-
std::string JbdBmsBle::error_bits_to_string_(const uint16_t mask) {
856+
std::string JbdBmsBle::bitmask_to_string_(const char *const messages[], const uint8_t &messages_size,
857+
const uint16_t &mask) {
843858
std::string values = "";
844859
if (mask) {
845-
for (int i = 0; i < ERRORS_SIZE; i++) {
860+
for (int i = 0; i < messages_size; i++) {
846861
if (mask & (1 << i)) {
847-
values.append(ERRORS[i]);
862+
values.append(messages[i]);
848863
values.append(";");
849864
}
850865
}

components/jbd_bms_ble/jbd_bms_ble.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class JbdBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompo
247247
void publish_device_unavailable_();
248248
void reset_online_status_tracker_();
249249
void track_online_status_();
250-
std::string error_bits_to_string_(uint16_t bitmask);
250+
std::string bitmask_to_string_(const char *const messages[], const uint8_t &messages_size, const uint16_t &mask);
251251

252252
uint16_t chksum_(const uint8_t data[], const uint16_t len) {
253253
uint16_t checksum = 0x00;

0 commit comments

Comments
 (0)