Skip to content

Commit

Permalink
Introduce switch entities of protocol version JK02_32S (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Mar 18, 2023
1 parent 59373d0 commit f859a84
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
6 changes: 5 additions & 1 deletion components/jk_bms_ble/jk_bms_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,11 @@ void JkBmsBle::decode_jk02_settings_(const std::vector<uint8_t> &data) {
// 270 4 0x00 0x00 0x00 0x00
// 274 4 0x00 0x00 0x00 0x00
// 278 4 0x00 0x00 0x00 0x00
// 282 4 0x00 0x00 0x00 0x00
// 282 1 0x00 New controls bitmask
this->publish_state_(this->disable_temperature_sensors_switch_, (bool) check_bit_(data[282], 2));
this->publish_state_(this->display_always_on_switch_, (bool) check_bit_(data[282], 16));

// 283 3 0x00 0x00 0x00
// 286 4 0x00 0x00 0x00 0x00
// 290 4 0x00 0x00 0x00 0x00
// 294 4 0x00 0x00 0x00 0x00
Expand Down
12 changes: 12 additions & 0 deletions components/jk_bms_ble/jk_bms_ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class JkBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompon
void set_charging_switch(switch_::Switch *charging_switch) { charging_switch_ = charging_switch; }
void set_discharging_switch(switch_::Switch *discharging_switch) { discharging_switch_ = discharging_switch; }
void set_balancer_switch(switch_::Switch *balancer_switch) { balancer_switch_ = balancer_switch; }
void set_emergency_switch(switch_::Switch *emergency_switch) { emergency_switch_ = emergency_switch; }
void set_disable_temperature_sensors_switch(switch_::Switch *disable_temperature_sensors_switch) {
disable_temperature_sensors_switch_ = disable_temperature_sensors_switch;
}
void set_display_always_on_switch(switch_::Switch *display_always_on_switch) {
display_always_on_switch_ = display_always_on_switch;
}

void set_enable_fake_traffic(bool enable_fake_traffic) { enable_fake_traffic_ = enable_fake_traffic; }
void set_protocol_version(ProtocolVersion protocol_version) { protocol_version_ = protocol_version; }
Expand Down Expand Up @@ -220,6 +227,9 @@ class JkBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompon
switch_::Switch *charging_switch_;
switch_::Switch *discharging_switch_;
switch_::Switch *balancer_switch_;
switch_::Switch *emergency_switch_;
switch_::Switch *disable_temperature_sensors_switch_;
switch_::Switch *display_always_on_switch_;

text_sensor::TextSensor *errors_text_sensor_;
text_sensor::TextSensor *operation_status_text_sensor_;
Expand Down Expand Up @@ -264,6 +274,8 @@ class JkBmsBle : public esphome::ble_client::BLEClientNode, public PollingCompon
std::memcpy(&ret, &f, sizeof(float));
return ret;
}

bool check_bit_(uint8_t mask, uint8_t flag) { return (mask & flag) == flag; }
};

} // namespace jk_bms_ble
Expand Down
41 changes: 36 additions & 5 deletions components/jk_bms_ble/switch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
CONF_CHARGING = "charging"
CONF_DISCHARGING = "discharging"
CONF_BALANCER = "balancer"
CONF_EMERGENCY = "emergency"
CONF_DISABLE_TEMPERATURE_SENSORS = "disable_temperature_sensors"
CONF_DISPLAY_ALWAYS_ON = "display_always_on"

ICON_CHARGING = "mdi:battery-charging-50"
ICON_DISCHARGING = "mdi:battery-charging-50"
ICON_BALANCER = "mdi:seesaw"
ICON_EMERGENCY = "mdi:exit-run"
ICON_DISABLE_TEMPERATURE_SENSORS = "mdi:thermometer-off"
ICON_DISPLAY_ALWAYS_ON = "mdi:led-on"

SWITCHES = {
CONF_CHARGING: [0x1D, 0x00],
CONF_DISCHARGING: [0x1E, 0x00],
CONF_BALANCER: [0x1F, 0x6C],
# JK04, JK02, JK02_32S
CONF_CHARGING: [0x00, 0x1D, 0x1D],
CONF_DISCHARGING: [0x00, 0x1E, 0x1E],
CONF_BALANCER: [0x6C, 0x1F, 0x1F],
CONF_EMERGENCY: [0x00, 0x00, 0x6B],
CONF_DISABLE_TEMPERATURE_SENSORS: [0x00, 0x00, 0x28],
CONF_DISPLAY_ALWAYS_ON: [0x00, 0x00, 0x2B],
}

JkSwitch = jk_bms_ble_ns.class_("JkSwitch", switch.Switch, cg.Component)
Expand All @@ -45,6 +55,26 @@
cv.Optional(CONF_ICON, default=ICON_BALANCER): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
cv.Optional(CONF_EMERGENCY): switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JkSwitch),
cv.Optional(CONF_ICON, default=ICON_EMERGENCY): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
cv.Optional(CONF_DISABLE_TEMPERATURE_SENSORS): switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JkSwitch),
cv.Optional(
CONF_ICON, default=ICON_DISABLE_TEMPERATURE_SENSORS
): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
cv.Optional(CONF_DISPLAY_ALWAYS_ON): switch.SWITCH_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(JkSwitch),
cv.Optional(CONF_ICON, default=ICON_DISPLAY_ALWAYS_ON): cv.icon,
}
).extend(cv.COMPONENT_SCHEMA),
}
)

Expand All @@ -59,5 +89,6 @@ async def to_code(config):
await switch.register_switch(var, conf)
cg.add(getattr(hub, f"set_{key}_switch")(var))
cg.add(var.set_parent(hub))
cg.add(var.set_jk02_holding_register(address[0]))
cg.add(var.set_jk04_holding_register(address[1]))
cg.add(var.set_jk04_holding_register(address[0]))
cg.add(var.set_jk02_holding_register(address[1]))
cg.add(var.set_jk02_32s_holding_register(address[2]))
24 changes: 16 additions & 8 deletions components/jk_bms_ble/switch/jk_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ static const char *const TAG = "jk_bms_ble.switch";

void JkSwitch::dump_config() { LOG_SWITCH("", "JkBmsBle Switch", this); }
void JkSwitch::write_state(bool state) {
if (this->parent_->get_protocol_version() == PROTOCOL_VERSION_JK04) {
if (this->jk04_holding_register_ == 0x00) {
ESP_LOGE(TAG, "This switch isn't supported by the selected protocol version");
return;
if (this->parent_->get_protocol_version() == PROTOCOL_VERSION_JK04 && this->jk04_holding_register_) {
if (this->parent_->write_register(this->jk04_holding_register_, (state) ? 0x00000001 : 0x00000000, 0x01)) {
this->publish_state(state);
}
return;
}

if (this->parent_->write_register(this->jk04_holding_register_, (state) ? 0x00000001 : 0x00000000, 0x01)) {
if (this->parent_->get_protocol_version() == PROTOCOL_VERSION_JK02 && this->jk02_holding_register_) {
if (this->parent_->write_register(this->jk02_holding_register_, (state) ? 0x00000001 : 0x00000000, 0x04)) {
this->publish_state(state);
}
return;
}

// JK02 & JK02_32S
if (this->parent_->write_register(this->jk02_holding_register_, (state) ? 0x00000001 : 0x00000000, 0x04)) {
this->publish_state(state);
if (this->parent_->get_protocol_version() == PROTOCOL_VERSION_JK02_32S && this->jk02_32s_holding_register_) {
if (this->parent_->write_register(this->jk02_32s_holding_register_, (state) ? 0x00000001 : 0x00000000, 0x04)) {
this->publish_state(state);
}
return;
}

ESP_LOGE(TAG, "This switch isn't supported by the selected protocol version");
}

bool JkSwitch::assumed_state() { return (this->jk02_32s_holding_register_ == 0x6B); }

} // namespace jk_bms_ble
} // namespace esphome
11 changes: 8 additions & 3 deletions components/jk_bms_ble/switch/jk_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ class JkBmsBle;
class JkSwitch : public switch_::Switch, public Component {
public:
void set_parent(JkBmsBle *parent) { this->parent_ = parent; };
void set_jk04_holding_register(uint8_t jk04_holding_register) {
this->jk04_holding_register_ = jk04_holding_register;
};
void set_jk02_holding_register(uint8_t jk02_holding_register) {
this->jk02_holding_register_ = jk02_holding_register;
};
void set_jk04_holding_register(uint8_t jk04_holding_register) {
this->jk04_holding_register_ = jk04_holding_register;
void set_jk02_32s_holding_register(uint8_t jk02_32s_holding_register) {
this->jk02_32s_holding_register_ = jk02_32s_holding_register;
};
void dump_config() override;
void loop() override {}
float get_setup_priority() const override { return setup_priority::DATA; }

protected:
bool assumed_state() override;
void write_state(bool state) override;
JkBmsBle *parent_;
uint8_t jk02_holding_register_;
uint8_t jk04_holding_register_;
uint8_t jk02_holding_register_;
uint8_t jk02_32s_holding_register_;
};

} // namespace jk_bms_ble
Expand Down
6 changes: 6 additions & 0 deletions esp32-ble-b2a8s20p-v11-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ switch:
name: "${name} discharging"
balancer:
name: "${name} balancer"
emergency:
name: "${name} emergency"
disable_temperature_sensors:
name: "${name} disable temperature sensors"
display_always_on:
name: "${name} display always on"

- platform: ble_client
ble_client_id: client0
Expand Down

0 comments on commit f859a84

Please sign in to comment.