Skip to content

Commit

Permalink
Merge pull request #76 from drc38/mult-fix
Browse files Browse the repository at this point in the history
Test measurand selection
  • Loading branch information
drc38 authored Jan 28, 2025
2 parents c41b2cf + 7b5583b commit 1eb6ca1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
31 changes: 19 additions & 12 deletions custom_components/ocpp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ class ConfigFlow(ConfigFlow, domain=DOMAIN):

def __init__(self):
"""Initialize."""
self._data = {}
self._data: dict[str, Any] = {}
self._cp_id: str
self._entry: ConfigEntry
self._measurands: str
self._measurands: str = ""

async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle user central system initiated configuration."""
Expand Down Expand Up @@ -142,6 +142,8 @@ async def async_step_integration_discovery(
self._data = {**self._entry.data}

await self.async_set_unique_id(self._cp_id)
# Abort the flow if a config entry with the same unique ID exists
self._abort_if_unique_id_configured()
return await self.async_step_cp_user()

async def async_step_cp_user(
Expand Down Expand Up @@ -175,17 +177,22 @@ async def async_step_measurands(self, user_input=None):
errors: dict[str, str] = {}
if user_input is not None:
selected_measurands = [m for m, value in user_input.items() if value]
if set(selected_measurands).issubset(set(MEASURANDS)):
self._measurands = ",".join(selected_measurands)
if not set(selected_measurands).issubset(set(MEASURANDS)):
errors["base"] = "no_measurands_selected"
return self.async_show_form(

Check warning on line 182 in custom_components/ocpp/config_flow.py

View check run for this annotation

Codecov / codecov/patch

custom_components/ocpp/config_flow.py#L181-L182

Added lines #L181 - L182 were not covered by tests
step_id="measurands",
data_schema=STEP_USER_MEASURANDS_SCHEMA,
errors=errors,
)
else:
errors["base"] = "measurand"
self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = (
self._measurands
)
return self.async_update_reload_and_abort(
self._entry,
data_updates={**self._data},
)
self._measurands = ",".join(selected_measurands)
self._data[CONF_CPIDS][-1][self._cp_id][CONF_MONITORED_VARIABLES] = (
self._measurands
)
return self.async_update_reload_and_abort(
self._entry,
data_updates={**self._data},
)

return self.async_show_form(
step_id="measurands",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ocpp/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"error": {
"auth": "Username/Password is wrong.",
"measurand": "Unknown measurand"
"no_measurands_selected": "No measurand selected: please select at least one"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed."
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ocpp/translations/i-default.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"error": {
"auth": "Username/Password is wrong.",
"measurand": "Unknown measurand"
"no_measurands_selected": "No measurand selected: please select at least one"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed."
Expand Down
39 changes: 29 additions & 10 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
DOMAIN,
)

from .const import MOCK_CONFIG_CS, MOCK_CONFIG_CP, MOCK_CONFIG_FLOW, CONF_CPIDS
from .const import (
MOCK_CONFIG_CS,
MOCK_CONFIG_CP,
MOCK_CONFIG_FLOW,
CONF_CPIDS,
CONF_MONITORED_VARIABLES_AUTOCONFIG,
DEFAULT_MONITORED_VARIABLES,
)


# This fixture bypasses the actual setup of the integration
Expand Down Expand Up @@ -80,27 +87,39 @@ async def test_successful_discovery_flow(hass, bypass_get_data):
await hass.async_block_till_done()
entry = hass.config_entries._entries.get_entries_for_domain(DOMAIN)[0]
info = {"cp_id": "test_cp_id", "entry": entry}
# Need to find out how to pass in discovery_info
result = await hass.config_entries.flow.async_init(
# data here is discovery_info not user_input
result_disc = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
data=info,
)

# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "cp_user"
result["discovery_info"] = info
assert result_disc["type"] == data_entry_flow.FlowResultType.FORM
assert result_disc["step_id"] == "cp_user"
result_disc["discovery_info"] = info

# Switch to manual measurand selection to test full flow
cp_input = MOCK_CONFIG_CP.copy()
cp_input[CONF_MONITORED_VARIABLES_AUTOCONFIG] = False
result_cp = await hass.config_entries.flow.async_configure(
result_disc["flow_id"], user_input=cp_input
)

result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=MOCK_CONFIG_CP
measurand_input = {value: True for value in DEFAULT_MONITORED_VARIABLES.split(",")}
result_meas = await hass.config_entries.flow.async_configure(
result_cp["flow_id"], user_input=measurand_input
)

# Check that the config flow is complete and a new entry is created with
# the input data
assert result2["type"] == data_entry_flow.FlowResultType.ABORT
flow_output = MOCK_CONFIG_FLOW.copy()
flow_output[CONF_CPIDS][-1]["test_cp_id"][CONF_MONITORED_VARIABLES_AUTOCONFIG] = (
False
)
assert result_meas["type"] == data_entry_flow.FlowResultType.ABORT
entry = hass.config_entries._entries.get_entries_for_domain(DOMAIN)[0]
assert entry.data == MOCK_CONFIG_FLOW
assert entry.data == flow_output


# In this case, we want to simulate a failure during the config flow.
Expand Down

0 comments on commit 1eb6ca1

Please sign in to comment.