Skip to content

Commit

Permalink
[BREAKING] Rename balancing binary sensor to balancing enabled (syssi#60
Browse files Browse the repository at this point in the history
)

...and introduce a new binary_sensor.balancing indicating the BMS is balancing at the moment
  • Loading branch information
syssi authored Mar 27, 2022
1 parent 1ea3744 commit 2e60bef
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ repos:
rev: v1.3.5
hooks:
- id: clang-format
- id: clang-tidy
9 changes: 9 additions & 0 deletions components/jk_bms/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
CONF_CHARGING = "charging"
CONF_DISCHARGING = "discharging"
CONF_BALANCING = "balancing"
CONF_BALANCING_ENABLED = "balancing_enabled"
CONF_DEDICATED_CHARGER = "dedicated_charger"

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

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

Expand All @@ -47,6 +50,12 @@
cv.Optional(CONF_ICON, default=ICON_BALANCING): cv.icon,
}
),
cv.Optional(CONF_BALANCING_ENABLED): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
cv.Optional(CONF_ICON, default=ICON_BALANCING_ENABLED): cv.icon,
}
),
cv.Optional(CONF_DEDICATED_CHARGER): binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(binary_sensor.BinarySensor),
Expand Down
4 changes: 3 additions & 1 deletion components/jk_bms/jk_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void JkBms::on_status_data_(const std::vector<uint8_t> &data) {
uint16_t raw_modes_bitmask = jk_get_16bit(offset + 6 + 3 * 9);
this->publish_state_(this->operation_mode_bitmask_sensor_, (float) raw_modes_bitmask);
this->publish_state_(this->operation_mode_text_sensor_, this->mode_bits_to_string_(raw_modes_bitmask));
this->publish_state_(this->balancing_binary_sensor_, (4 & raw_modes_bitmask) == 4);

// 0x8E 0x16 0x26: Total voltage overvoltage protection 5670 * 0.01 = 56.70V 0.01 V
this->publish_state_(this->total_voltage_overvoltage_protection_sensor_,
Expand Down Expand Up @@ -234,7 +235,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_binary_sensor_, (bool) data[offset + 6 + 3 * 25]);
this->publish_state_(this->balancing_enabled_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 @@ -547,6 +548,7 @@ void JkBms::dump_config() { // NOLINT(google-readability-function-size,readabil
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("", "Balancing Enabled", this->balancing_enabled_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_);
Expand Down
5 changes: 4 additions & 1 deletion components/jk_bms/jk_bms.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
void set_balancing_binary_sensor(binary_sensor::BinarySensor *balancing_binary_sensor) {
balancing_binary_sensor_ = balancing_binary_sensor;
}
void set_balancing_enabled_binary_sensor(binary_sensor::BinarySensor *balancing_enabled_binary_sensor) {
balancing_enabled_binary_sensor_ = balancing_enabled_binary_sensor;
}
void set_charging_binary_sensor(binary_sensor::BinarySensor *charging_binary_sensor) {
charging_binary_sensor_ = charging_binary_sensor;
}
Expand Down Expand Up @@ -247,7 +250,6 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
sensor::Sensor *charging_overcurrent_delay_sensor_;
sensor::Sensor *balance_starting_voltage_sensor_;
sensor::Sensor *balance_opening_pressure_difference_sensor_;
sensor::Sensor *balancing_sensor_;
sensor::Sensor *power_tube_temperature_protection_sensor_;
sensor::Sensor *power_tube_temperature_recovery_sensor_;
sensor::Sensor *temperature_sensor_temperature_protection_sensor_;
Expand All @@ -274,6 +276,7 @@ class JkBms : public PollingComponent, public jk_modbus::JkModbusDevice {
sensor::Sensor *protocol_version_sensor_;

binary_sensor::BinarySensor *balancing_binary_sensor_;
binary_sensor::BinarySensor *balancing_enabled_binary_sensor_;
binary_sensor::BinarySensor *charging_binary_sensor_;
binary_sensor::BinarySensor *discharging_binary_sensor_;
binary_sensor::BinarySensor *dedicated_charger_binary_sensor_;
Expand Down
2 changes: 2 additions & 0 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ binary_sensor:
name: "${name} discharging"
balancing:
name: "${name} balancing"
balancing_enabled:
name: "${name} balancing enabled"
dedicated_charger:
name: "${name} dedicated charger"

Expand Down
2 changes: 2 additions & 0 deletions esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ binary_sensor:
name: "${name} discharging"
balancing:
name: "${name} balancing"
balancing_enabled:
name: "${name} balancing enabled"
dedicated_charger:
name: "${name} dedicated charger"

Expand Down

0 comments on commit 2e60bef

Please sign in to comment.