Skip to content

Commit

Permalink
[BREAKING] Convert switch entities into binary sensors (#51)
Browse files Browse the repository at this point in the history
It looks like the serial interface is read-only. The switch entity doesn't fit here anymore.

Please update your configuration from

```
switch:
  - platform: jk_bms
    charging:
      name: "${name} charging"
    discharging:
      name: "${name} discharging"
    balancing:
      name: "${name} balancing"
    dedicated_charger:
      name: "${name} dedicated charger"
```

to

```
binary_sensor:
  - platform: jk_bms
    charging:
      name: "${name} charging"
    discharging:
      name: "${name} discharging"
    balancing:
      name: "${name} balancing"
    dedicated_charger:
      name: "${name} dedicated charger"
```
  • Loading branch information
syssi authored Mar 2, 2022
1 parent b3d75ea commit f455055
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 163 deletions.
2 changes: 1 addition & 1 deletion components/jk_bms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from esphome.components import jk_modbus
from esphome.const import CONF_ID

AUTO_LOAD = ["jk_modbus", "sensor", "switch", "text_sensor"]
AUTO_LOAD = ["jk_modbus", "binary_sensor", "sensor", "text_sensor"]
CODEOWNERS = ["@syssi"]
MULTI_CONF = True

Expand Down
67 changes: 67 additions & 0 deletions components/jk_bms/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_ICON, CONF_ID

from . import CONF_JK_BMS_ID, JkBms

DEPENDENCIES = ["jk_bms"]

CODEOWNERS = ["@syssi"]

CONF_CHARGING = "charging"
CONF_DISCHARGING = "discharging"
CONF_BALANCING = "balancing"
CONF_DEDICATED_CHARGER = "dedicated_charger"

ICON_CHARGING = "mdi:battery-charging"
ICON_DISCHARGING = "mdi:power-plug"
ICON_BALANCING = "mdi:battery-heart-variant"
ICON_DEDICATED_CHARGER = "mdi:battery-charging"

BINARY_SENSORS = [
CONF_CHARGING,
CONF_DISCHARGING,
CONF_BALANCING,
CONF_DEDICATED_CHARGER,
]

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_JK_BMS_ID): cv.use_id(JkBms),
cv.Optional(CONF_CHARGING): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
cv.Optional(CONF_ICON, default=ICON_CHARGING): cv.icon,
}
),
cv.Optional(CONF_DISCHARGING): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
cv.Optional(CONF_ICON, default=ICON_DISCHARGING): cv.icon,
}
),
cv.Optional(CONF_BALANCING): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
cv.Optional(CONF_ICON, default=ICON_BALANCING): cv.icon,
}
),
cv.Optional(CONF_DEDICATED_CHARGER): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
cv.Optional(CONF_ICON, default=ICON_DEDICATED_CHARGER): cv.icon,
}
),
}
)


def to_code(config):
hub = yield cg.get_variable(config[CONF_JK_BMS_ID])
for key in BINARY_SENSORS:
if key in config:
conf = config[key]
sens = cg.new_Pvariable(conf[CONF_ID])
yield binary_sensor.register_binary_sensor(sens, conf)
cg.add(getattr(hub, f"set_{key}_binary_sensor")(sens))
26 changes: 14 additions & 12 deletions components/jk_bms/jk_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {
(float) jk_get_16bit(offset + 6 + 3 * 24) * 0.001f);

// 0x9D 0x01: Active balance switch 1 (on) Bool 0 (off), 1 (on)
this->publish_state_(this->balancing_switch_, (bool) data[offset + 6 + 3 * 25]);
this->publish_state_(this->balancing_binary_sensor_, (bool) data[offset + 6 + 3 * 25]);

// 0x9E 0x00 0x5A: Power tube temperature protection value 90°C 1.0 °C 0-100°C
this->publish_state_(this->power_tube_temperature_protection_sensor_, (float) jk_get_16bit(offset + 8 + 3 * 25));
Expand Down Expand Up @@ -286,10 +286,10 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {
(float) (raw_total_battery_capacity_setting * (raw_battery_remaining_capacity * 0.01f)));

// 0xAB 0x01: Charging MOS tube switch 1 (on) Bool 0 (off), 1 (on)
this->publish_state_(this->charging_switch_, (bool) data[offset + 15 + 3 * 36]);
this->publish_state_(this->charging_binary_sensor_, (bool) data[offset + 15 + 3 * 36]);

// 0xAC 0x01: Discharge MOS tube switch 1 (on) Bool 0 (off), 1 (on)
this->publish_state_(this->discharging_switch_, (bool) data[offset + 17 + 3 * 36]);
this->publish_state_(this->discharging_binary_sensor_, (bool) data[offset + 17 + 3 * 36]);

// 0xAD 0x04 0x11: Current calibration 1041mA * 0.001 = 1.041A 0.001 A 0.1-2.0A
this->publish_state_(this->current_calibration_sensor_, (float) jk_get_16bit(offset + 19 + 3 * 36) * 0.001f);
Expand Down Expand Up @@ -317,7 +317,7 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {
std::string(data.begin() + offset + 25 + 3 * 38, data.begin() + offset + 35 + 3 * 38));

// 0xB3 0x00: Dedicated charger switch 1 (on) Bool 0 (off), 1 (on)
this->publish_state_(this->dedicated_charger_switch_, (bool) data[offset + 36 + 3 * 38]);
this->publish_state_(this->dedicated_charger_binary_sensor_, (bool) data[offset + 36 + 3 * 38]);

// 0xB4 0x49 0x6E 0x70 0x75 0x74 0x20 0x55 0x73: Device ID code
this->publish_state_(this->device_type_text_sensor_,
Expand Down Expand Up @@ -347,8 +347,6 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {
// 00 00 00 00 68 00 00 54 D1: End of frame
}

void JkBms::write_register(uint8_t address, uint8_t value) { this->send(WRITE_REGISTER, address, value); }

void JkBms::update() {
this->read_registers(FUNCTION_READ_ALL, ADDRESS_READ_ALL);

Expand Down Expand Up @@ -401,18 +399,18 @@ void JkBms::update() {
}
}

void JkBms::publish_state_(sensor::Sensor *sensor, float value) {
if (sensor == nullptr)
void JkBms::publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state) {
if (binary_sensor == nullptr)
return;

sensor->publish_state(value);
binary_sensor->publish_state(state);
}

void JkBms::publish_state_(switch_::Switch *obj, const bool &state) {
if (obj == nullptr)
void JkBms::publish_state_(sensor::Sensor *sensor, float value) {
if (sensor == nullptr)
return;

obj->publish_state(state);
sensor->publish_state(value);
}

void JkBms::publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state) {
Expand Down Expand Up @@ -544,6 +542,10 @@ void JkBms::dump_config() { // NOLINT(google-readability-function-size,readabil
LOG_SENSOR("", "Start Current Calibration", this->start_current_calibration_sensor_);
LOG_TEXT_SENSOR("", "Manufacturer", this->manufacturer_text_sensor_);
LOG_SENSOR("", "Protocol Version", this->protocol_version_sensor_);
LOG_BINARY_SENSOR("", "Balancing", this->balancing_binary_sensor_);
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
LOG_BINARY_SENSOR("", "Discharging", this->discharging_binary_sensor_);
LOG_BINARY_SENSOR("", "Dedicated Charger", this->dedicated_charger_binary_sensor_);
}

} // namespace jk_bms
Expand Down
36 changes: 20 additions & 16 deletions components/jk_bms/jk_bms.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/switch/switch.h"
#include "esphome/components/text_sensor/text_sensor.h"
#include "esphome/components/jk_modbus/jk_modbus.h"

Expand All @@ -11,6 +11,19 @@ namespace jk_bms {

class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
public:
void set_balancing_binary_sensor(binary_sensor::BinarySensor *balancing_binary_sensor) {
balancing_binary_sensor_ = balancing_binary_sensor;
}
void set_charging_binary_sensor(binary_sensor::BinarySensor *charging_binary_sensor) {
charging_binary_sensor_ = charging_binary_sensor;
}
void set_discharging_binary_sensor(binary_sensor::BinarySensor *discharging_binary_sensor) {
discharging_binary_sensor_ = discharging_binary_sensor;
}
void set_dedicated_charger_binary_sensor(binary_sensor::BinarySensor *dedicated_charger_binary_sensor) {
dedicated_charger_binary_sensor_ = dedicated_charger_binary_sensor;
}

void set_min_cell_voltage_sensor(sensor::Sensor *min_cell_voltage_sensor) {
min_cell_voltage_sensor_ = min_cell_voltage_sensor;
}
Expand Down Expand Up @@ -171,13 +184,6 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
protocol_version_sensor_ = protocol_version_sensor;
}

void set_balancing_switch(switch_::Switch *balancing_switch) { balancing_switch_ = balancing_switch; }
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_dedicated_charger_switch(switch_::Switch *dedicated_charger_switch) {
dedicated_charger_switch_ = dedicated_charger_switch;
}

void set_errors_text_sensor(text_sensor::TextSensor *errors_text_sensor) { errors_text_sensor_ = errors_text_sensor; }
void set_operation_mode_text_sensor(text_sensor::TextSensor *operation_mode_text_sensor) {
operation_mode_text_sensor_ = operation_mode_text_sensor;
Expand Down Expand Up @@ -206,8 +212,6 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {

void update() override;

void write_register(uint8_t address, uint8_t value);

protected:
sensor::Sensor *min_cell_voltage_sensor_;
sensor::Sensor *max_cell_voltage_sensor_;
Expand Down Expand Up @@ -266,10 +270,10 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
sensor::Sensor *actual_battery_capacity_sensor_;
sensor::Sensor *protocol_version_sensor_;

switch_::Switch *balancing_switch_;
switch_::Switch *charging_switch_;
switch_::Switch *discharging_switch_;
switch_::Switch *dedicated_charger_switch_;
binary_sensor::BinarySensor *balancing_binary_sensor_;
binary_sensor::BinarySensor *charging_binary_sensor_;
binary_sensor::BinarySensor *discharging_binary_sensor_;
binary_sensor::BinarySensor *dedicated_charger_binary_sensor_;

text_sensor::TextSensor *errors_text_sensor_;
text_sensor::TextSensor *operation_mode_text_sensor_;
Expand All @@ -287,10 +291,10 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
bool enable_fake_traffic_;

void on_status_data_(const std::vector<uint8_t> &data);
void publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state);
void publish_state_(sensor::Sensor *sensor, float value);
void publish_state_(switch_::Switch *obj, const bool &state);
void publish_state_(text_sensor::TextSensor *text_sensor, const std::string &state);
// void publish_state_(binary_sensor::BinarySensor *binary_sensor, const bool &state);

std::string error_bits_to_string_(uint16_t bitmask);
std::string mode_bits_to_string_(uint16_t bitmask);
float get_temperature_(const uint16_t value) {
Expand Down
72 changes: 0 additions & 72 deletions components/jk_bms/switch/__init__.py

This file was deleted.

14 changes: 0 additions & 14 deletions components/jk_bms/switch/jk_switch.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions components/jk_bms/switch/jk_switch.h

This file was deleted.

2 changes: 2 additions & 0 deletions components/jk_modbus/jk_modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ float JkModbus::get_setup_priority() const {
// After UART bus
return setup_priority::BUS - 1.0f;
}

// The manufacturer states that no write operations are possible via the serial interface.
void JkModbus::send(uint8_t function, uint8_t address, uint8_t value) {
uint8_t frame[22];
frame[0] = 0x4E; // start sequence
Expand Down
22 changes: 11 additions & 11 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ jk_bms:
id: bms0
jk_modbus_id: modbus0

binary_sensor:
- platform: jk_bms
charging:
name: "${name} charging"
discharging:
name: "${name} discharging"
balancing:
name: "${name} balancing"
dedicated_charger:
name: "${name} dedicated charger"

sensor:
- platform: jk_bms
min_cell_voltage:
Expand Down Expand Up @@ -199,17 +210,6 @@ sensor:
# protocol_version:
# name: "${name} protocol version"

switch:
- platform: jk_bms
charging:
name: "${name} charging"
discharging:
name: "${name} discharging"
balancing:
name: "${name} balancing"
dedicated_charger:
name: "${name} dedicated charger"

text_sensor:
- platform: jk_bms
errors:
Expand Down
Loading

0 comments on commit f455055

Please sign in to comment.