Skip to content

Commit

Permalink
Add JK balancer support
Browse files Browse the repository at this point in the history
Closes: #16
  • Loading branch information
syssi committed Jan 31, 2025
1 parent ab7c51b commit 8e2d0e8
Show file tree
Hide file tree
Showing 19 changed files with 2,493 additions and 48 deletions.
51 changes: 3 additions & 48 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ jobs:
- name: Dont move consts
run: |
sed -i 's#if len(uses) < 3:#if len(uses) < 5:#' script/ci-custom.py
sed -i 's#if len(uses) < 3:#if len(uses) < 6:#' script/ci-custom.py
git update-index --assume-unchanged script/ci-custom.py
working-directory: ${{ env.esphome_directory }}

Expand Down Expand Up @@ -203,24 +203,6 @@ jobs:
esphome -s external_components_source components config $YAML >/dev/null
done
- name: Write yaml-snippets/secrets.yaml
shell: bash
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > yaml-snippets/secrets.yaml'
- name: Validate YAML snippets
run: |
for YAML in yaml-snippets/esp*.yaml; do
esphome -s external_components_source ../components config $YAML >/dev/null
done
- name: Write tests/secrets.yaml
shell: bash
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > tests/secrets.yaml'
- name: Validate test configurations
run: |
for YAML in tests/esp*.yaml; do
esphome -s external_components_source ../components config $YAML >/dev/null
done
esphome-compile:
runs-on: ubuntu-latest
needs: [esphome-config]
Expand Down Expand Up @@ -256,32 +238,5 @@ jobs:
- name: Write secrets.yaml
shell: bash
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > secrets.yaml'
- name: Compile esp32 (heltec_balancer_ble) example configurations
run: |
esphome -s external_components_source components compile esp32-heltec-balancer-ble-example-faker.yaml
# esphome -s external_components_source components compile esp32-heltec-balancer-ble-example-multiple-devices.yaml
- name: Compile esp8266 (jk_bms) example configurations
run: |
esphome -s external_components_source components compile esp8266-example-faker.yaml
- name: Compile esp32 (jk_bms) example configurations
run: |
esphome -s external_components_source components compile esp32-example-faker.yaml
- name: Compile esp32 (jk_bms_ble) example configurations
run: |
esphome -s external_components_source components -s name jk-bms-jk04 compile esp32-ble-jk04-example-faker.yaml
esphome -s external_components_source components -s name jk-bms-jk02-24s compile esp32-ble-example-faker.yaml
esphome -s external_components_source components -s name jk-bms-jk02-32s compile esp32-ble-v14-example-faker.yaml
- name: Compile esp32 (modbus) example configurations
run: |
esphome -s name jk-bms-pb compile esp32-jk-pb-modbus-example.yaml
- name: Compile display port example configurations
run: |
esphome -s external_components_source components compile esp32-display-example.yaml
# esphome -s external_components_source components compile esp8266-display-example.yaml
- name: Write tests/secrets.yaml
shell: bash
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > tests/secrets.yaml'
- name: Compile test configurations
run: |
esphome -s external_components_source ../components compile tests/esp32c6-compatibility-test.yaml
- run: |
esphome -s external_components_source components compile esp32-active-balancer-example.yaml
37 changes: 37 additions & 0 deletions components/jk_balancer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import esphome.codegen as cg
from esphome.components import jk_balancer_modbus
import esphome.config_validation as cv
from esphome.const import CONF_ID

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

CONF_JK_BALANCER_ID = "jk_balancer_id"

jk_balancer_ns = cg.esphome_ns.namespace("jk_balancer")
JkBalancer = jk_balancer_ns.class_(
"JkBalancer", cg.PollingComponent, jk_balancer_modbus.JkBalancerModbusDevice
)

JK_BALANCER_COMPONENT_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_JK_BALANCER_ID): cv.use_id(JkBalancer),
}
)

CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(JkBalancer),
}
)
.extend(cv.polling_component_schema("5s"))
.extend(jk_balancer_modbus.jk_balancer_modbus_device_schema(0x01))
)


async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await jk_balancer_modbus.register_jk_balancer_modbus_device(var, config)
81 changes: 81 additions & 0 deletions components/jk_balancer/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, DEVICE_CLASS_CONNECTIVITY, ENTITY_CATEGORY_DIAGNOSTIC

from . import CONF_JK_BALANCER_ID, JK_BALANCER_COMPONENT_SCHEMA
from .const import CONF_CHARGING, CONF_DISCHARGING

DEPENDENCIES = ["jk_balancer"]

CODEOWNERS = ["@syssi"]

CONF_CHARGING_SWITCH = (
"charging_switch" # @DEPRECATED and superseded by switch.charging
)
CONF_DISCHARGING_SWITCH = (
"discharging_switch" # @DEPRECATED and superseded by switch.discharging
)
CONF_BALANCING = "balancing"
CONF_BALANCING_SWITCH = "balancing_switch"
CONF_DEDICATED_CHARGER_SWITCH = "dedicated_charger_switch"
CONF_ONLINE_STATUS = "online_status"

ICON_CHARGING = "mdi:battery-charging"
ICON_CHARGING_SWITCH = "mdi:battery-charging"
ICON_DISCHARGING = "mdi:power-plug"
ICON_DISCHARGING_SWITCH = "mdi:power-plug"
ICON_BALANCING = "mdi:battery-heart-variant"
ICON_BALANCING_SWITCH = "mdi:battery-heart-variant"
ICON_DEDICATED_CHARGER_SWITCH = "mdi:battery-charging"

BINARY_SENSORS = [
CONF_CHARGING,
CONF_CHARGING_SWITCH,
CONF_DISCHARGING,
CONF_DISCHARGING_SWITCH,
CONF_BALANCING,
CONF_BALANCING_SWITCH,
CONF_DEDICATED_CHARGER_SWITCH,
CONF_ONLINE_STATUS,
]

CONFIG_SCHEMA = JK_BALANCER_COMPONENT_SCHEMA.extend(
{
cv.Optional(CONF_CHARGING): binary_sensor.binary_sensor_schema(
icon=ICON_CHARGING
),
cv.Optional(CONF_CHARGING_SWITCH): binary_sensor.binary_sensor_schema(
icon=ICON_CHARGING_SWITCH
),
cv.Optional(CONF_DISCHARGING): binary_sensor.binary_sensor_schema(
icon=ICON_DISCHARGING
),
cv.Optional(CONF_DISCHARGING_SWITCH): binary_sensor.binary_sensor_schema(
icon=ICON_DISCHARGING_SWITCH
),
cv.Optional(CONF_BALANCING): binary_sensor.binary_sensor_schema(
icon=ICON_BALANCING
),
cv.Optional(CONF_BALANCING_SWITCH): binary_sensor.binary_sensor_schema(
icon=ICON_BALANCING_SWITCH
),
cv.Optional(CONF_DEDICATED_CHARGER_SWITCH): binary_sensor.binary_sensor_schema(
icon=ICON_DEDICATED_CHARGER_SWITCH
),
cv.Optional(CONF_ONLINE_STATUS): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)


async def to_code(config):
hub = await cg.get_variable(config[CONF_JK_BALANCER_ID])
for key in BINARY_SENSORS:
if key in config:
conf = config[key]
sens = cg.new_Pvariable(conf[CONF_ID])
await binary_sensor.register_binary_sensor(sens, conf)
cg.add(getattr(hub, f"set_{key}_binary_sensor")(sens))
5 changes: 5 additions & 0 deletions components/jk_balancer/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Constants for the jk bms component."""

CONF_BALANCER = "balancer"
CONF_CHARGING = "charging"
CONF_DISCHARGING = "discharging"
Loading

0 comments on commit 8e2d0e8

Please sign in to comment.