From 1ea235812a3a42eeffc75e991ef041872dc3f85c Mon Sep 17 00:00:00 2001 From: MonsterD1 <66463436+MonsterD1@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:12:53 +0100 Subject: [PATCH] Ignore cheap-window target current for non-dynamic appliances (#68) The current step rejected a pre-filled cheap-window target current when dynamic current was off, so the user had to clear the field manually. The value is only meaningful for dynamic-current appliances; it is now dropped silently in both the add and reconfigure flows. --- .../pv_excess_control/config_flow.py | 12 ++++--- .../pv_excess_control/strings.json | 1 - .../pv_excess_control/translations/en.json | 1 - tests/test_config_flow_subentry.py | 34 +++++++++++++++---- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/custom_components/pv_excess_control/config_flow.py b/custom_components/pv_excess_control/config_flow.py index b2da54aa..b512453b 100644 --- a/custom_components/pv_excess_control/config_flow.py +++ b/custom_components/pv_excess_control/config_flow.py @@ -1605,8 +1605,10 @@ async def async_step_current( if min_c >= max_c: errors[CONF_MIN_CURRENT] = "invalid_current_range" - if not dynamic and user_input.get(CONF_CHEAP_GRID_TARGET_CURRENT) is not None: - errors[CONF_CHEAP_GRID_TARGET_CURRENT] = "cheap_grid_target_current_requires_dynamic" + if not dynamic: + # Only meaningful for dynamic current; a pre-filled value must + # not block the step (#68). It is dropped from the stored data below. + user_input = {k: v for k, v in user_input.items() if k != CONF_CHEAP_GRID_TARGET_CURRENT} if not errors: self._data.update(user_input) @@ -1798,8 +1800,10 @@ async def async_step_reconfigure_current( if min_c >= max_c: errors[CONF_MIN_CURRENT] = "invalid_current_range" - if not dynamic and user_input.get(CONF_CHEAP_GRID_TARGET_CURRENT) is not None: - errors[CONF_CHEAP_GRID_TARGET_CURRENT] = "cheap_grid_target_current_requires_dynamic" + if not dynamic: + # Only meaningful for dynamic current; a pre-filled value must + # not block the step (#68). It is dropped from the stored data below. + user_input = {k: v for k, v in user_input.items() if k != CONF_CHEAP_GRID_TARGET_CURRENT} if not errors: self._data.update(user_input) diff --git a/custom_components/pv_excess_control/strings.json b/custom_components/pv_excess_control/strings.json index 1b243cce..bc7cc43c 100644 --- a/custom_components/pv_excess_control/strings.json +++ b/custom_components/pv_excess_control/strings.json @@ -404,7 +404,6 @@ "invalid_runtime_range": "Minimum daily runtime must be less than or equal to maximum daily runtime.", "missing_max_grid_power": "Maximum grid power is required when grid supplementation is enabled.", "suggest_big_consumer": "This appliance draws 3000W or more. Consider enabling 'Big Consumer' to prevent battery discharge when active. Submit again to keep current setting.", - "cheap_grid_target_current_requires_dynamic": "Cheap-window target current is only available for dynamic-current appliances. Enable dynamic_current or leave this field empty.", "circular_dependency": "Circular dependency: the selected appliance already depends on this one.", "helper_only_with_requires": "An appliance cannot be both a helper and have its own dependency. Either clear 'Requires Appliance' or uncheck 'Run Only as Helper'.", "helper_only_with_gate": "Helper-only appliances cannot have their own enable condition or start delay. Configure these on the dependent appliance.", diff --git a/custom_components/pv_excess_control/translations/en.json b/custom_components/pv_excess_control/translations/en.json index 1b243cce..bc7cc43c 100644 --- a/custom_components/pv_excess_control/translations/en.json +++ b/custom_components/pv_excess_control/translations/en.json @@ -404,7 +404,6 @@ "invalid_runtime_range": "Minimum daily runtime must be less than or equal to maximum daily runtime.", "missing_max_grid_power": "Maximum grid power is required when grid supplementation is enabled.", "suggest_big_consumer": "This appliance draws 3000W or more. Consider enabling 'Big Consumer' to prevent battery discharge when active. Submit again to keep current setting.", - "cheap_grid_target_current_requires_dynamic": "Cheap-window target current is only available for dynamic-current appliances. Enable dynamic_current or leave this field empty.", "circular_dependency": "Circular dependency: the selected appliance already depends on this one.", "helper_only_with_requires": "An appliance cannot be both a helper and have its own dependency. Either clear 'Requires Appliance' or uncheck 'Run Only as Helper'.", "helper_only_with_gate": "Helper-only appliances cannot have their own enable condition or start delay. Configure these on the dependent appliance.", diff --git a/tests/test_config_flow_subentry.py b/tests/test_config_flow_subentry.py index e524b5a9..a647b1a2 100644 --- a/tests/test_config_flow_subentry.py +++ b/tests/test_config_flow_subentry.py @@ -1118,14 +1118,14 @@ async def test_cheap_grid_target_current_accepted_for_dynamic_appliance(self): assert flow._data[CONF_CHEAP_GRID_TARGET_CURRENT] == 14.0 @pytest.mark.asyncio - async def test_cheap_grid_target_current_rejected_for_non_dynamic_appliance(self): - """Setting cheap_grid_target_current on a non-dynamic appliance fails validation.""" + async def test_cheap_grid_target_current_ignored_for_non_dynamic_appliance(self): + """A pre-filled cheap-window current must not block a non-dynamic appliance (#68).""" from custom_components.pv_excess_control.const import CONF_CHEAP_GRID_TARGET_CURRENT flow = _make_subentry_flow() flow._data = dict(VALID_BASIC_INPUT) + flow._data[CONF_CHEAP_GRID_TARGET_CURRENT] = 16.0 # stale value from an earlier dynamic config - # Submit the current step with dynamic current disabled but cheap_grid_target_current set result = await flow.async_step_current( user_input={ CONF_DYNAMIC_CURRENT: False, @@ -1133,7 +1133,29 @@ async def test_cheap_grid_target_current_rejected_for_non_dynamic_appliance(self } ) - # Should be rejected with a form error assert result["type"] == "form" - assert result["step_id"] == "current" - assert CONF_CHEAP_GRID_TARGET_CURRENT in result["errors"] + assert result["step_id"] == "constraints" + assert CONF_CHEAP_GRID_TARGET_CURRENT not in flow._data + + @pytest.mark.asyncio + async def test_cheap_grid_target_current_ignored_on_reconfigure_non_dynamic(self): + """Reconfigure: switching dynamic current off with a pre-filled value proceeds (#68).""" + from custom_components.pv_excess_control.const import CONF_CHEAP_GRID_TARGET_CURRENT + + flow = TestReconfigure._make_reconfigure_flow(None, { + CONF_APPLIANCE_NAME: "Charger", + CONF_APPLIANCE_ENTITY: "switch.charger", + CONF_APPLIANCE_PRIORITY: 500, + CONF_NOMINAL_POWER: 3680, + CONF_PHASES: 1, + CONF_DYNAMIC_CURRENT: True, + CONF_CHEAP_GRID_TARGET_CURRENT: 16.0, + }) + await flow.async_step_reconfigure(user_input=None) + result = await flow.async_step_reconfigure_current( + user_input={CONF_DYNAMIC_CURRENT: False, CONF_CHEAP_GRID_TARGET_CURRENT: 16.0} + ) + + assert result["type"] == "form" + assert result["step_id"] == "reconfigure_constraints" + assert CONF_CHEAP_GRID_TARGET_CURRENT not in flow._data