Skip to content

Commit

Permalink
Introduce retrieve hardware version button (Closes: #86) (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jul 28, 2024
1 parent 2bec764 commit 0b47085
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 16 deletions.
6 changes: 6 additions & 0 deletions components/jbd_bms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
jbd_bms_ns = cg.esphome_ns.namespace("jbd_bms")
JbdBms = jbd_bms_ns.class_("JbdBms", cg.PollingComponent, uart.UARTDevice)

JBD_BMS_COMPONENT_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_JBD_BMS_ID): cv.use_id(JbdBms),
}
)

CONFIG_SCHEMA = (
cv.Schema(
{
Expand Down
45 changes: 45 additions & 0 deletions components/jbd_bms/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import esphome.codegen as cg
from esphome.components import button
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID

from .. import CONF_JBD_BMS_ID, JBD_BMS_COMPONENT_SCHEMA, jbd_bms_ns

DEPENDENCIES = ["jbd_bms"]

CODEOWNERS = ["@syssi"]

CONF_RETRIEVE_HARDWARE_VERSION = "retrieve_hardware_version"

ICON_CONF_RETRIEVE_HARDWARE_VERSION = "mdi:numeric"

BUTTONS = {
CONF_RETRIEVE_HARDWARE_VERSION: 0x05,
}

JbdButton = jbd_bms_ns.class_("JbdButton", button.Button, cg.Component)

CONFIG_SCHEMA = JBD_BMS_COMPONENT_SCHEMA.extend(
{
cv.Optional(CONF_RETRIEVE_HARDWARE_VERSION): button.BUTTON_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JbdButton),
cv.Optional(
CONF_ICON, default=ICON_CONF_RETRIEVE_HARDWARE_VERSION
): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
}
)


async def to_code(config):
hub = await cg.get_variable(config[CONF_JBD_BMS_ID])
for key, address in BUTTONS.items():
if key in config:
conf = config[key]
var = cg.new_Pvariable(conf[CONF_ID])
await cg.register_component(var, conf)
await button.register_button(var, conf)
cg.add(var.set_parent(hub))
cg.add(var.set_holding_register(address))
16 changes: 16 additions & 0 deletions components/jbd_bms/button/jbd_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jbd_button.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"

namespace esphome {
namespace jbd_bms {

static const char *const TAG = "jbd_bms.button";

static const uint8_t JBD_CMD_READ = 0xA5;

void JbdButton::dump_config() { LOG_BUTTON("", "JbdBms Button", this); }
void JbdButton::press_action() { this->parent_->send_command(JBD_CMD_READ, this->holding_register_); }

} // namespace jbd_bms
} // namespace esphome
26 changes: 26 additions & 0 deletions components/jbd_bms/button/jbd_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "../jbd_bms.h"
#include "esphome/core/component.h"
#include "esphome/components/button/button.h"

namespace esphome {
namespace jbd_bms {

class JbdBms;
class JbdButton : public button::Button, public Component {
public:
void set_parent(JbdBms *parent) { this->parent_ = parent; };
void set_holding_register(uint8_t holding_register) { this->holding_register_ = holding_register; };
void dump_config() override;
void loop() override {}
float get_setup_priority() const override { return setup_priority::DATA; }

protected:
void press_action() override;
JbdBms *parent_;
uint8_t holding_register_;
};

} // namespace jbd_bms
} // namespace esphome
8 changes: 4 additions & 4 deletions components/jbd_bms/jbd_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static const char *const ERRORS[ERRORS_SIZE] = {
"Unknown (0x0F)", // 0x0F
};

void JbdBms::setup() { this->send_command_(JBD_CMD_READ, JBD_CMD_HWVER); }
void JbdBms::setup() { this->send_command(JBD_CMD_READ, JBD_CMD_HWVER); }

void JbdBms::loop() {
const uint32_t now = millis();
Expand All @@ -74,7 +74,7 @@ void JbdBms::loop() {

void JbdBms::update() {
this->track_online_status_();
this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO);
this->send_command(JBD_CMD_READ, JBD_CMD_HWINFO);
}

bool JbdBms::parse_jbd_bms_byte_(uint8_t byte) {
Expand Down Expand Up @@ -300,7 +300,7 @@ void JbdBms::on_hardware_info_data_(const std::vector<uint8_t> &data) {
(float) (jbd_get_16bit(23 + (i * 2)) - 2731) * 0.1f);
}

this->send_command_(JBD_CMD_READ, JBD_CMD_CELLINFO);
this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
}

void JbdBms::on_hardware_version_data_(const std::vector<uint8_t> &data) {
Expand Down Expand Up @@ -513,7 +513,7 @@ bool JbdBms::write_register(uint8_t address, uint16_t value) {
return true;
}

void JbdBms::send_command_(uint8_t action, uint8_t function) {
void JbdBms::send_command(uint8_t action, uint8_t function) {
uint8_t frame[7];
uint8_t data_len = 0;

Expand Down
2 changes: 1 addition & 1 deletion components/jbd_bms/jbd_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
device_model_text_sensor_ = device_model_text_sensor;
}
void set_rx_timeout(uint16_t rx_timeout) { rx_timeout_ = rx_timeout; }
void send_command(uint8_t action, uint8_t function);
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);
Expand Down Expand Up @@ -180,7 +181,6 @@ class JbdBms : public uart::UARTDevice, public PollingComponent {
void publish_device_unavailable_();
void reset_online_status_tracker_();
void track_online_status_();
void send_command_(uint8_t action, uint8_t function);
std::string error_bits_to_string_(uint16_t bitmask);

uint16_t chksum_(const uint8_t data[], const uint16_t len) {
Expand Down
5 changes: 2 additions & 3 deletions components/jbd_bms/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID

from .. import CONF_JBD_BMS_ID, JbdBms, jbd_bms_ns
from .. import CONF_JBD_BMS_ID, JBD_BMS_COMPONENT_SCHEMA, jbd_bms_ns
from ..const import CONF_CHARGING, CONF_DISCHARGING

DEPENDENCIES = ["jbd_bms"]
Expand All @@ -28,9 +28,8 @@

JbdSwitch = jbd_bms_ns.class_("JbdSwitch", switch.Switch, cg.Component)

CONFIG_SCHEMA = cv.Schema(
CONFIG_SCHEMA = JBD_BMS_COMPONENT_SCHEMA.extend(
{
cv.GenerateID(CONF_JBD_BMS_ID): cv.use_id(JbdBms),
cv.Optional(CONF_DISCHARGING): switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JbdSwitch),
Expand Down
6 changes: 6 additions & 0 deletions components/jbd_bms_ble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"JbdBmsBle", ble_client.BLEClientNode, cg.PollingComponent
)

JBD_BMS_BLE_COMPONENT_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_JBD_BMS_BLE_ID): cv.use_id(JbdBmsBle),
}
)

CONFIG_SCHEMA = (
cv.Schema(
{
Expand Down
45 changes: 45 additions & 0 deletions components/jbd_bms_ble/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import esphome.codegen as cg
from esphome.components import button
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID

from .. import CONF_JBD_BMS_BLE_ID, JBD_BMS_BLE_COMPONENT_SCHEMA, jbd_bms_ble_ns

DEPENDENCIES = ["jbd_bms_ble"]

CODEOWNERS = ["@syssi"]

CONF_RETRIEVE_HARDWARE_VERSION = "retrieve_hardware_version"

ICON_CONF_RETRIEVE_HARDWARE_VERSION = "mdi:numeric"

BUTTONS = {
CONF_RETRIEVE_HARDWARE_VERSION: 0x05,
}

JbdButton = jbd_bms_ble_ns.class_("JbdButton", button.Button, cg.Component)

CONFIG_SCHEMA = JBD_BMS_BLE_COMPONENT_SCHEMA.extend(
{
cv.Optional(CONF_RETRIEVE_HARDWARE_VERSION): button.BUTTON_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JbdButton),
cv.Optional(
CONF_ICON, default=ICON_CONF_RETRIEVE_HARDWARE_VERSION
): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
}
)


async def to_code(config):
hub = await cg.get_variable(config[CONF_JBD_BMS_BLE_ID])
for key, address in BUTTONS.items():
if key in config:
conf = config[key]
var = cg.new_Pvariable(conf[CONF_ID])
await cg.register_component(var, conf)
await button.register_button(var, conf)
cg.add(var.set_parent(hub))
cg.add(var.set_holding_register(address))
16 changes: 16 additions & 0 deletions components/jbd_bms_ble/button/jbd_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "jbd_button.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"

namespace esphome {
namespace jbd_bms_ble {

static const char *const TAG = "jbd_bms_ble.button";

static const uint8_t JBD_CMD_READ = 0xA5;

void JbdButton::dump_config() { LOG_BUTTON("", "JbdBmsBle Button", this); }
void JbdButton::press_action() { this->parent_->send_command(JBD_CMD_READ, this->holding_register_); }

} // namespace jbd_bms_ble
} // namespace esphome
26 changes: 26 additions & 0 deletions components/jbd_bms_ble/button/jbd_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "../jbd_bms_ble.h"
#include "esphome/core/component.h"
#include "esphome/components/button/button.h"

namespace esphome {
namespace jbd_bms_ble {

class JbdBmsBle;
class JbdButton : public button::Button, public Component {
public:
void set_parent(JbdBmsBle *parent) { this->parent_ = parent; };
void set_holding_register(uint8_t holding_register) { this->holding_register_ = holding_register; };
void dump_config() override;
void loop() override {}
float get_setup_priority() const override { return setup_priority::DATA; }

protected:
void press_action() override;
JbdBmsBle *parent_;
uint8_t holding_register_;
};

} // namespace jbd_bms_ble
} // namespace esphome
8 changes: 4 additions & 4 deletions components/jbd_bms_ble/jbd_bms_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void JbdBmsBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
this->node_state = espbt::ClientState::ESTABLISHED;

ESP_LOGI(TAG, "Request device info");
this->send_command_(JBD_CMD_READ, JBD_CMD_HWVER);
this->send_command(JBD_CMD_READ, JBD_CMD_HWVER);

break;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ void JbdBmsBle::update() {
return;
}

this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO);
this->send_command(JBD_CMD_READ, JBD_CMD_HWINFO);
}

void JbdBmsBle::on_jbd_bms_data(const uint8_t &function, const std::vector<uint8_t> &data) {
Expand Down Expand Up @@ -310,7 +310,7 @@ void JbdBmsBle::on_hardware_info_data_(const std::vector<uint8_t> &data) {
(float) (jbd_get_16bit(23 + (i * 2)) - 2731) * 0.1f);
}

this->send_command_(JBD_CMD_READ, JBD_CMD_CELLINFO);
this->send_command(JBD_CMD_READ, JBD_CMD_CELLINFO);
}

void JbdBmsBle::on_hardware_version_data_(const std::vector<uint8_t> &data) {
Expand Down Expand Up @@ -520,7 +520,7 @@ bool JbdBmsBle::write_register(uint8_t address, uint16_t value) {
return (status == 0);
}

bool JbdBmsBle::send_command_(uint8_t action, uint8_t function) {
bool JbdBmsBle::send_command(uint8_t action, uint8_t function) {
uint8_t frame[7];
uint8_t data_len = 0;

Expand Down
3 changes: 2 additions & 1 deletion components/jbd_bms_ble/jbd_bms_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ 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;
}

bool send_command(uint8_t action, uint8_t function);
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);
Expand Down Expand Up @@ -186,7 +188,6 @@ class JbdBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompo
void publish_device_unavailable_();
void reset_online_status_tracker_();
void track_online_status_();
bool send_command_(uint8_t action, uint8_t function);
std::string error_bits_to_string_(uint16_t bitmask);

uint16_t chksum_(const uint8_t data[], const uint16_t len) {
Expand Down
5 changes: 2 additions & 3 deletions components/jbd_bms_ble/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import esphome.config_validation as cv
from esphome.const import CONF_ICON, CONF_ID

from .. import CONF_JBD_BMS_BLE_ID, JbdBmsBle, jbd_bms_ble_ns
from .. import CONF_JBD_BMS_BLE_ID, JBD_BMS_BLE_COMPONENT_SCHEMA, jbd_bms_ble_ns
from ..const import CONF_CHARGING, CONF_DISCHARGING

DEPENDENCIES = ["jbd_bms_ble"]
Expand All @@ -26,9 +26,8 @@

JbdSwitch = jbd_bms_ble_ns.class_("JbdSwitch", switch.Switch, cg.Component)

CONFIG_SCHEMA = cv.Schema(
CONFIG_SCHEMA = JBD_BMS_BLE_COMPONENT_SCHEMA.extend(
{
cv.GenerateID(CONF_JBD_BMS_BLE_ID): cv.use_id(JbdBmsBle),
cv.Optional(CONF_DISCHARGING): switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JbdSwitch),
Expand Down
6 changes: 6 additions & 0 deletions esp32-ble-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ jbd_bms_ble:
ble_client_id: client0
update_interval: 2s

button:
- platform: jbd_bms_ble
jbd_bms_ble_id: bms0
retrieve_hardware_version:
name: "${name} retrieve hardware version"

binary_sensor:
- platform: jbd_bms_ble
jbd_bms_ble_id: bms0
Expand Down
6 changes: 6 additions & 0 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ jbd_bms:
update_interval: 2s
rx_timeout: ${rx_timeout}

button:
- platform: jbd_bms
jbd_bms_id: bms0
retrieve_hardware_version:
name: "${name} retrieve hardware version"

binary_sensor:
- platform: jbd_bms
jbd_bms_id: bms0
Expand Down
6 changes: 6 additions & 0 deletions esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ jbd_bms:
update_interval: 2s
rx_timeout: ${rx_timeout}

button:
- platform: jbd_bms
jbd_bms_id: bms0
retrieve_hardware_version:
name: "${name} retrieve hardware version"

binary_sensor:
- platform: jbd_bms
jbd_bms_id: bms0
Expand Down

0 comments on commit 0b47085

Please sign in to comment.