Skip to content

Commit

Permalink
Refactor config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Feb 9, 2021
1 parent 76082d2 commit 04598fc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
23 changes: 19 additions & 4 deletions custom_components/erse/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# TODO pick one of the available connection classes in homeassistant/config_entries.py
CONNECTION_CLASS = config_entries.CONN_CLASS_UNKNOWN


async def async_step_user(self, user_input=None):
"""Handle choice of tarifario."""
if user_input is None:
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_OPERATOR): vol.In(Operators[COUNTRY].keys()),
}
),
)

self.operator = user_input[CONF_OPERATOR]
return await self.async_step_finish()

async def async_step_finish(self, user_input=None):
"""Handle the initial step."""
errors = {}

if user_input is not None:
try:
user_input[CONF_OPERATOR] = self.operator
info = await validate_input(self.hass, user_input)

return self.async_create_entry(
Expand All @@ -62,14 +79,12 @@ async def async_step_user(self, user_input=None):

DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_OPERATOR): vol.In(Operators[COUNTRY].keys()),
vol.Required(CONF_PLAN): vol.In(
list(
set(
[
str(p)
for plans in Operators[COUNTRY].values()
for p in plans.tariff_periods()
for p in Operators[COUNTRY][self.operator].tariff_periods()
]
)
)
Expand All @@ -85,7 +100,7 @@ async def async_step_user(self, user_input=None):
)

return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
step_id="finish", data_schema=DATA_SCHEMA, errors=errors
)


Expand Down
4 changes: 2 additions & 2 deletions custom_components/erse/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"version": "1.0.2",
"version": "1.0.3",
"domain": "erse",
"name": "Entidade Reguladora dos Servi\u00e7os Energ\u00e9ticos",
"config_flow": true,
"documentation": "https://github.com/dgomes/ha_erse",
"issue_tracker": "https://github.com/dgomes/ha_erse/issues",
"requirements": [
"python-electricity==0.0.6"
"python-electricity==0.0.7"
],
"after_dependencies": [],
"ssdp": [],
Expand Down
12 changes: 8 additions & 4 deletions custom_components/erse/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
"title": "Entidade Reguladora dos Servi\u00e7os Energ\u00e9ticos",
"step": {
"user": {
"title": "Setup your Operator and plan",
"title": "Which is your Operator",
"data": {
"operator": "Operator"
}
},
"finish": {
"title": "Choose your plan and Utility Meter",
"data": {
"operator": "Operator",
"plan": "Plan",
"source": "Energy Sensor"
"utility_meter": "Utility Meter"
}
}
},
"error": {
"invalid_plan": "The operator you chose, does not support that plan.",
"unknown": "Unexpected error"
}
}
Expand Down
13 changes: 9 additions & 4 deletions custom_components/erse/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"config": {
"error": {
"invalid_plan": "We cannot find such plan.",
"unknown": "Unexpected error"
},
"step": {
"finish": {
"data": {
"plan": "Plan",
"utility_meter": "Utility Meter"
},
"title": "Choose your plan and Utility Meter"
},
"user": {
"data": {
"operator": "Operator",
"plan": "Plan"
"operator": "Operator"
},
"title": "Setup your Operator and plan"
"title": "Which is your Operator"
}
},
"title": "Entidade Reguladora dos Servi\u00e7os Energ\u00e9ticos"
Expand Down
11 changes: 8 additions & 3 deletions custom_components/erse/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
"unknown": "Unexpected error"
},
"step": {
"user": {
"finish": {
"data": {
"operator": "Operador",
"plan": "Plano",
"utility_meter": "Utility Meter"
},
"title": "Seleccione o Operador e Plano"
"title": "Escolha o seu plano e Utility Meter"
},
"user": {
"data": {
"operator": "Operador"
},
"title": "Qual é o seu Operador"
}
},
"title": "Entidade Reguladora dos Servi\u00e7os Energ\u00e9ticos"
Expand Down

0 comments on commit 04598fc

Please sign in to comment.