Skip to content

Commit

Permalink
optional config flow added
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerbranda committed Mar 18, 2024
1 parent 863dd26 commit 6146716
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
7 changes: 6 additions & 1 deletion custom_components/rbfa/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def __init__(self, hass, my_api):
self.matchdata = {'upcoming': None, 'lastmatch': None}
self.hass = hass
self.team = my_api.data['team']
self.duration = my_api.data['duration']

if 'duration' in my_api.options:
self.duration = my_api.options['duration']
else:
self.duration = my_api.data['duration']

self.collections = [];
_LOGGER.debug('duration: %r', self.duration)

Expand Down
51 changes: 50 additions & 1 deletion custom_components/rbfa/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from homeassistant.core import callback
from homeassistant import config_entries
from homeassistant.const import UnitOfTime
from .const import DOMAIN
import logging
import voluptuous as vol

from typing import Any
from homeassistant.helpers import selector
from homeassistant.data_entry_flow import FlowResult
_LOGGER = logging.getLogger(__name__)

class RbfaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Example config flow."""
Expand Down Expand Up @@ -46,3 +50,48 @@ async def async_step_user(self, user_input):
data_schema=schema,
errors={},
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return OptionsFlowHandler(config_entry)


class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
_LOGGER.debug('data? %r', config_entry.data['duration'])

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="test", data=user_input)

if 'duration' in self.config_entry.options:
duration = self.config_entry.options['duration']
else:
duration = self.config_entry.data['duration']

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required('duration', default=duration
): selector.NumberSelector(
selector.NumberSelectorConfig(
min=5,
max=120,
step=5,
mode=selector.NumberSelectorMode.BOX,
unit_of_measurement=UnitOfTime.MINUTES,
),
),
}
),
)
12 changes: 12 additions & 0 deletions custom_components/rbfa/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
"already_configured":"Team is al toegevoegd"
}
},
"options":{
"step":{
"init":{
"title":"RBFA",
"description":"Royal Belgian Football Association",
"data":{
"team":"Identiteit van het team",
"duration":"Duur van de wedstrijd inclusief rust"
}
}
}
},
"entity":{
"sensor":{
"hometeam":{
Expand Down

0 comments on commit 6146716

Please sign in to comment.