From e661edcd24148a9e19c86802ca13c0de8e1fc238 Mon Sep 17 00:00:00 2001 From: rhammen <75572839+rhammen@users.noreply.github.com> Date: Thu, 3 Sep 2026 20:30:04 +0200 Subject: [PATCH 1/2] Enforce a 6A minimum on charger_min_current The API accepts ChargerMinCurrent = 0, but the Zaptec portal then rejects any later settings change for that charger, see #289. Raise the entity's minimum to the IEC 61851 floor of 6A, and reject setting charger_max_current below charger_min_current and vice versa. available_current and three_to_one_phase_switch_current keep a 0 minimum. 0 is not a charging current for either: it forces 3-phase charging on the latter, and is a regularly used way to hold the charger off on the former. Co-Authored-By: Claude Opus 5 --- custom_components/zaptec/number.py | 27 ++++++++++++++----- tests/test_number.py | 42 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 tests/test_number.py diff --git a/custom_components/zaptec/number.py b/custom_components/zaptec/number.py index 92b751ad..35d6738c 100644 --- a/custom_components/zaptec/number.py +++ b/custom_components/zaptec/number.py @@ -22,6 +22,8 @@ _LOGGER = logging.getLogger(__name__) +MIN_CHARGE_CURRENT = 6 # IEC 61851 minimum charging current (A) + class ZaptecNumber[ZaptecObjectT: ZaptecBase](ZaptecBaseEntity[ZaptecObjectT], NumberEntity): """Base class for Zaptec number entities.""" @@ -104,14 +106,27 @@ def _post_init(self) -> None: async def async_set_native_value(self, value: float) -> None: """Update to Zaptec.""" self._log_number(value) - if not self.entity_description.setting: + setting = self.entity_description.setting + if not setting: raise HomeAssistantError(f"No setting for {self.__class__.__qualname__}.{self.key}") + + if setting == "minChargeCurrent": + max_current = self.zaptec_obj.get("charger_max_current") + if isinstance(max_current, (int, float)) and value > max_current: + raise HomeAssistantError( + f"Min current {value} cannot be higher than max current {max_current}" + ) + elif setting == "maxChargeCurrent": + min_current = self.zaptec_obj.get("charger_min_current") + if isinstance(min_current, (int, float)) and value < min_current: + raise HomeAssistantError( + f"Max current {value} cannot be lower than min current {min_current}" + ) + try: - await self.zaptec_obj.set_settings({self.entity_description.setting: value}) + await self.zaptec_obj.set_settings({setting: value}) except Exception as exc: - raise HomeAssistantError( - f"Setting {self.entity_description.setting} to {value} failed" - ) from exc + raise HomeAssistantError(f"Setting {setting} to {value} failed") from exc await self.trigger_poll() @@ -176,7 +191,7 @@ class ZapNumberEntityDescription(ZaptecEntityDescription, NumberEntityDescriptio translation_key="charger_min_current", device_class=NumberDeviceClass.CURRENT, entity_category=EntityCategory.CONFIG, - native_min_value=0, + native_min_value=MIN_CHARGE_CURRENT, native_max_value=32, icon="mdi:current-ac", native_unit_of_measurement=const.UnitOfElectricCurrent.AMPERE, diff --git a/tests/test_number.py b/tests/test_number.py new file mode 100644 index 00000000..0cec1ec0 --- /dev/null +++ b/tests/test_number.py @@ -0,0 +1,42 @@ +"""Tests for number.py.""" + +import pytest + +from custom_components.zaptec.manager import ZaptecEntityDescription +from custom_components.zaptec.number import ( + CHARGER_ENTITIES, + INSTALLATION_ENTITIES, + MIN_CHARGE_CURRENT, + ZapNumberEntityDescription, +) + + +def _find(descriptions: list[ZaptecEntityDescription], key: str) -> ZapNumberEntityDescription: + """Return the entity description with the given key.""" + return next(d for d in descriptions if d.key == key) + + +@pytest.mark.parametrize( + ("descriptions", "key", "expected_min"), + [ + pytest.param( + CHARGER_ENTITIES, "charger_min_current", MIN_CHARGE_CURRENT, id="charger_min_current" + ), + # 0 holds the charger off rather than requesting a charging current. + pytest.param(INSTALLATION_ENTITIES, "available_current", 0, id="available_current"), + # Zaptec documents 0 as the way to force 3-phase charging. + pytest.param( + INSTALLATION_ENTITIES, + "three_to_one_phase_switch_current", + 0, + id="phase_switch_current", + ), + # Constrained relationally against charger_min_current, not by a static floor. + pytest.param(CHARGER_ENTITIES, "charger_max_current", 0, id="charger_max_current"), + ], +) +def test_native_min_value( + descriptions: list[ZaptecEntityDescription], key: str, expected_min: float +) -> None: + """Only charger_min_current declares the 6A floor.""" + assert _find(descriptions, key).native_min_value == expected_min From 48bf4d4c2e91404eead1b3dc74f9708bf9e48446 Mon Sep 17 00:00:00 2001 From: rhammen <75572839+rhammen@users.noreply.github.com> Date: Mon, 5 Oct 2026 22:54:04 +0200 Subject: [PATCH 2/2] Use explicit None checks for min/max current guard Co-Authored-By: Claude Opus 5.5 --- custom_components/zaptec/number.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/custom_components/zaptec/number.py b/custom_components/zaptec/number.py index 35d6738c..cc91ffbe 100644 --- a/custom_components/zaptec/number.py +++ b/custom_components/zaptec/number.py @@ -111,14 +111,14 @@ async def async_set_native_value(self, value: float) -> None: raise HomeAssistantError(f"No setting for {self.__class__.__qualname__}.{self.key}") if setting == "minChargeCurrent": - max_current = self.zaptec_obj.get("charger_max_current") - if isinstance(max_current, (int, float)) and value > max_current: + max_current: float | None = self.zaptec_obj.get("charger_max_current") + if max_current is not None and value > max_current: raise HomeAssistantError( f"Min current {value} cannot be higher than max current {max_current}" ) elif setting == "maxChargeCurrent": - min_current = self.zaptec_obj.get("charger_min_current") - if isinstance(min_current, (int, float)) and value < min_current: + min_current: float | None = self.zaptec_obj.get("charger_min_current") + if min_current is not None and value < min_current: raise HomeAssistantError( f"Max current {value} cannot be lower than min current {min_current}" )