Skip to content

Commit b9319c6

Browse files
authored
Persist extra battery config on initial setup and migrate old entries (#255)
Extra batteries were detected on the first setup, but they weren't written into the config due to oversight - only reconfigure menu contains schema for extra batteries. This means that initial configuration did not have extra battery keys and was relying on runtime detection which could fail if the message does not arrive before this check. User had to explicitly reconfigure it for it to persist. This PR fixes this and adds migration for old entries to contain extra batteries key. Also it removes second extra battery slot from River 3 Plus as it only supports a single battery.
1 parent ad2819d commit b9319c6

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

custom_components/ef_ble/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
CONF_BLUEZ_START_NOTIFY,
2323
CONF_COLLECT_PACKETS_AMOUNT,
2424
CONF_CONNECTION_TIMEOUT,
25+
CONF_EXTRA_BATTERY,
2526
CONF_PACKET_VERSION,
2627
CONF_UPDATE_PERIOD,
2728
CONF_USER_ID,
@@ -188,8 +189,8 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
188189
config_entry.minor_version,
189190
)
190191

191-
match config_entry.version, config_entry.minor_version:
192-
case 1, 0:
192+
if config_entry.version < 2:
193+
if config_entry.minor_version < 1:
193194
address = config_entry.data.get(CONF_ADDRESS)
194195
device_reg = dr.async_get(hass)
195196
device_entry = device_reg.async_get_device(identifiers={(DOMAIN, address)})
@@ -212,6 +213,13 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
212213

213214
hass.config_entries.async_update_entry(config_entry, minor_version=1)
214215

216+
if config_entry.minor_version < 2:
217+
data = {**config_entry.data}
218+
data.setdefault(CONF_EXTRA_BATTERY, [])
219+
hass.config_entries.async_update_entry(
220+
config_entry, data=data, minor_version=2
221+
)
222+
215223
return True
216224

217225

custom_components/ef_ble/config_flow.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import base64
77
import enum
88
import logging
9-
from collections.abc import Mapping
9+
from collections.abc import Iterable, Mapping
1010
from functools import cached_property
1111
from typing import Any, ClassVar, cast
1212

@@ -88,7 +88,7 @@ class EFBLEConfigFlow(ConfigFlow, domain=DOMAIN):
8888
"""EcoFlow BLE ConfigFlow"""
8989

9090
VERSION = 1
91-
MINOR_VERSION = 1
91+
MINOR_VERSION = 2
9292

9393
CONNECTION_CLASS = CONN_CLASS_LOCAL_PUSH
9494

@@ -385,9 +385,14 @@ def _create_entry(self, user_input: dict[str, Any], device: eflib.DeviceBase):
385385
entry_data["local_name"] = self._local_names.get(device.address, None)
386386
entry_data.pop("login", None)
387387

388+
if CONF_EXTRA_BATTERY not in entry_data:
389+
entry_data[CONF_EXTRA_BATTERY] = _find_enabled_batteries(
390+
device, range(1, 6)
391+
)
392+
388393
return self.async_create_entry(title=device.name, data=entry_data)
389394

390-
def _check_user_id(self, user_id: str):
395+
def _check_user_id(self, user_id: str) -> dict[str, str] | None:
391396
try:
392397
int(user_id.strip())
393398
except ValueError:
@@ -758,11 +763,7 @@ def extra_battery(
758763
extra_batteries_default = (
759764
extra_battery_conf
760765
if extra_battery_conf is not None
761-
else [
762-
str(i)
763-
for i in available_battery_slots
764-
if getattr(device, f"battery_{i}_enabled", False)
765-
]
766+
else _find_enabled_batteries(device, available_battery_slots)
766767
)
767768

768769
extra_battery_labels = {
@@ -789,3 +790,7 @@ def extra_battery(
789790

790791
def schema_builder():
791792
return _SchemaBuilder()
793+
794+
795+
def _find_enabled_batteries(device: eflib.DeviceBase, slots: Iterable[int]):
796+
return [str(i) for i in slots if getattr(device, f"battery_{i}_enabled", False)]

custom_components/ef_ble/eflib/devices/river3_plus.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ class Device(river3.Device):
2727
battery_1_cell_temperature = pb_field(pb.plug_in_info_dcp_resv, resv_temperature)
2828
battery_1_sn = pb_field(pb.plug_in_info_dcp_sn)
2929

30-
battery_2_enabled = pb_field(pb.plug_in_info_dcp2_in_flag)
31-
battery_2_battery_level = pb_field(pb.plug_in_info_dcp2_resv, resv_soc)
32-
battery_2_cell_temperature = pb_field(pb.plug_in_info_dcp2_resv, resv_temperature)
33-
battery_2_sn = pb_field(pb.plug_in_info_dcp2_sn)
34-
3530
led_mode = pb_field(river3.pb.led_mode, LedMode.from_value)
3631

3732
@controls.select(led_mode, options=LedMode)

0 commit comments

Comments
 (0)