Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions custom_components/pv_excess_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion custom_components/pv_excess_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
1 change: 0 additions & 1 deletion custom_components/pv_excess_control/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
34 changes: 28 additions & 6 deletions tests/test_config_flow_subentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,22 +1118,44 @@ 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,
CONF_CHEAP_GRID_TARGET_CURRENT: 14.0,
}
)

# 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