Skip to content

Commit

Permalink
Merge pull request #165 from gjohansson-ST/config-flow-mods
Browse files Browse the repository at this point in the history
Config flow mods
  • Loading branch information
gjohansson-ST authored Nov 4, 2023
2 parents ff2240a + da61d91 commit cee2ead
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
3 changes: 0 additions & 3 deletions custom_components/sector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"""Sector Alarm integration for Home Assistant."""
from __future__ import annotations

from datetime import timedelta

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CODE, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import (
CONF_CODE_FORMAT,
Expand Down
52 changes: 38 additions & 14 deletions custom_components/sector/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,40 @@
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.selector import (
BooleanSelector,
NumberSelector,
NumberSelectorConfig,
NumberSelectorMode,
TextSelector,
TextSelectorConfig,
TextSelectorType,
)

from .const import API_URL, CONF_CODE_FORMAT, CONF_TEMP, DOMAIN, LOGGER

DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_CODE_FORMAT, default=6): cv.positive_int,
vol.Optional(CONF_TEMP, default=False): cv.boolean,
vol.Required(CONF_USERNAME): TextSelector(
TextSelectorConfig(type=TextSelectorType.EMAIL)
),
vol.Required(CONF_PASSWORD): TextSelector(
TextSelectorConfig(type=TextSelectorType.PASSWORD)
),
vol.Optional(CONF_CODE_FORMAT, default=6): NumberSelector(
NumberSelectorConfig(min=0, max=6, step=1, mode=NumberSelectorMode.BOX)
),
vol.Optional(CONF_TEMP, default=False): BooleanSelector(),
}
)
DATA_SCHEMA_AUTH = vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): TextSelector(
TextSelectorConfig(type=TextSelectorType.EMAIL)
),
vol.Required(CONF_PASSWORD): TextSelector(
TextSelectorConfig(type=TextSelectorType.PASSWORD)
),
}
)

Expand Down Expand Up @@ -108,7 +126,7 @@ def async_get_options_flow(
"""Get the options flow for this handler."""
return SectorOptionFlow(config_entry)

async def async_step_reauth(self, data: Mapping[str, Any]) -> FlowResult:
async def async_step_reauth(self, user_input: Mapping[str, Any]) -> FlowResult:
"""Handle re-authentication."""

self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
Expand Down Expand Up @@ -150,14 +168,15 @@ async def async_step_reauth_confirm(
)

async def async_step_user(
self, user_input: dict[str, str] | None = None
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors = {}
errors: dict[str, str] = {}

if user_input is not None:
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
code_format = user_input[CONF_CODE_FORMAT]
try:
await validate_input(self.hass, username, password)
except CannotConnect:
Expand All @@ -177,7 +196,7 @@ async def async_step_user(
CONF_TEMP: user_input[CONF_TEMP],
},
options={
CONF_CODE_FORMAT: user_input[CONF_CODE_FORMAT],
CONF_CODE_FORMAT: int(code_format),
},
)

Expand All @@ -192,11 +211,12 @@ class SectorOptionFlow(config_entries.OptionsFlowWithConfigEntry):
"""Handle a options config flow for Sector integration."""

async def async_step_init(
self, user_input: dict[str, str] | None = None
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the Sector options."""
if user_input is not None:
return self.async_create_entry(data=user_input)
data = {**user_input, CONF_CODE_FORMAT: int(user_input[CONF_CODE_FORMAT])}
return self.async_create_entry(data=data)

return self.async_show_form(
step_id="init",
Expand All @@ -209,7 +229,11 @@ async def async_step_init(
CONF_CODE_FORMAT, 6
)
},
): cv.positive_int,
): NumberSelector(
NumberSelectorConfig(
min=0, max=6, step=1, mode=NumberSelectorMode.BOX
)
),
}
),
)
Expand Down
15 changes: 8 additions & 7 deletions custom_components/sector/strings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_account%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
},
"error": {
"auth_error": "[%key:common::config_flow::error::invalid_auth%]",
Expand All @@ -25,12 +26,12 @@
}
},
"options": {
"step": {
"init": {
"data": {
"code_format": "Code length for alarm and locks"
}
}
"step": {
"init": {
"data": {
"code_format": "Code length for alarm and locks"
}
}
}
}
}
3 changes: 2 additions & 1 deletion custom_components/sector/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"abort": {
"already_configured": "Account is already configured"
"already_configured": "Account is already configured",
"reauth_successful": "Reauthentication successful"
},
"error": {
"auth_error": "Username and/or password is incorrect",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/sector/translations/no.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"abort": {
"already_configured": "Kontoen er allerede konfigurert"
"already_configured": "Kontoen er allerede konfigurert",
"reauth_successful": "Reautentisering vellykket"
},
"error": {
"auth_error": "Brukernavn og / eller passord er feil",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/sector/translations/se.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"config": {
"abort": {
"already_configured": "Kontot existerar redan"
"already_configured": "Kontot existerar redan",
"reauth_successful": "Återautentisering lyckades"
},
"error": {
"auth_error": "Användarnamn eller lösenord är inte korrekt",
Expand Down

0 comments on commit cee2ead

Please sign in to comment.