Skip to content

Commit

Permalink
Using PyCountry library in Config Flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Sep 27, 2020
1 parent f0359c1 commit c24cb44
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 54 deletions.
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/.translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Region (ISO 3166-1 alpha-2, e.g. US)",
"language": "Language (ISO-639-1, e.g. en)",
"token": "Refresh Token (only for Authentication mode v1)",
"use_api_v2": "Use Authentication mode v2"
"region": "Country Code",
"language": "Language Code"
},
"description": "Fill in your SmartThinQ access information. If you don't have a refresh token, leave it empty to start the authentication process with your credentials. Refresh token is not used with Authentication mode v2.",
"description": "Select your SmartThinQ account localization information.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/.translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "País (ISO 3166-1 alpha-2, p. ej. ES)",
"language": "Idioma (ISO-639-1, p. ej. es)",
"token": "Actualizar el Token (Sólo para autenticación modo v1)",
"use_api_v2": "Usar autenticación modo v2"
"region": "Codigo País",
"language": "Codigo Idioma"
},
"description": "Inserta tu información de acceso a SmartThinQ. Si no dispones de un Token de actualización, déjalo vacío para empezar el proceso de autenticación con tus credenciales. El Token de Actualización no es necesario con el Authentication mode v2.",
"description": "Inserta tu información de acceso a SmartThinQ.",
"title": "Sensores SmartThinQ LGE"
},
"url": {
Expand Down
10 changes: 4 additions & 6 deletions custom_components/smartthinq_sensors/.translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Paese (ISO 3166-1 alpha-2, per es. IT)",
"language": "Lingua (ISO-639-1, per es. it)",
"token": "Aggiornare il Token (solo per Authentication mode v1)",
"use_api_v2": "Usa Authentication mode v2"
"region": "Codice Paese",
"language": "Codice Lingua"
},
"description": "Inserisci le informazioni di acceso SmartThinQ. Se non si dispone di un token di aggiornamento, lasciarlo vuoto per avviare il processo di autenticazione con le proprie credenziali. Il token di aggiornamento non viene usato con Authentication mode v2.",
"description": "Seleziona le informazioni di localizzazione del tuo account SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand All @@ -27,7 +25,7 @@
"callback_url": "URL di reindirizzamento"
},
"description": "Utilizzare l'URL del primo campo per eseguire l'accesso con le proprie credenziali a SmartThinQ, poi incollare l'URL in cui si viene reindirizzati con il browser dopo l'accesso nel secondo campo.",
"title": "SmartThinQ LGE Sensors - Autentificazione"
"title": "SmartThinQ LGE Sensors - Autenticazione"
},
"token": {
"data": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/.translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Region (ISO 3166-1 alpha-2, np. PL)",
"language": "Język (ISO-639-1, np. pl)",
"token": "Wpisz token (tylko dla trybu uwierzytelniania API v1)",
"use_api_v2": "Użyj trybu API v2"
"region": "Region",
"language": "Język"
},
"description": "Podaj swoje ustawienia regionalne dla SmartThinQ. Jeśli nie posiadasz tokena, pozostaw to pole puste, rozpocznij proces uwierzytelniania. Token nie jest używany w trybie uwierzytelniania API v2.",
"description": "Podaj swoje ustawienia regionalne dla SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/.translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Región (ISO 3166-1 alpha-2, napr. SK)",
"language": "Jazyk (ISO-639-1, napr. sk)",
"token": "Obnova Token (len pre Autorizačný mód v1)",
"use_api_v2": "Použi Autorizačný mód v2"
"region": "Región",
"language": "Jazyk"
},
"description": "Vyplňte informácie o prístupe k SmartThinQ. Ak nemáte obnovovací token, nechajte ho prázdny a začnite proces autentifikácie pomocou vašich poverení. Obnovovací token sa nepoužíva v režime overovania v2.",
"description": "Vyplňte informácie o prístupe k SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down
29 changes: 28 additions & 1 deletion custom_components/smartthinq_sensors/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Config flow for TP-Link."""
import logging
import pycountry
import re

import voluptuous as vol
Expand Down Expand Up @@ -32,6 +33,23 @@
_LOGGER = logging.getLogger(__name__)


def _countries_list():
"""Returns a list of countries, suitable for use in a multiple choice field."""
countries = {}
for country in sorted(pycountry.countries, key=lambda x: x.name):
countries[country.alpha_2] = f"{country.name} - {country.alpha_2}"
return countries


def _languages_list():
"""Returns a list of languages, suitable for use in a multiple choice field."""
languages = {}
for language in sorted(pycountry.languages, key=lambda x: x.name):
if hasattr(language, "alpha_2"):
languages[language.alpha_2] = f"{language.name} - {language.alpha_2}"
return languages


class SmartThinQFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle SmartThinQ config flow."""

Expand Down Expand Up @@ -160,7 +178,16 @@ def _show_form(self, errors=None, step_id="user"):
"""Show the form to the user."""
schema = INIT_SCHEMA

if step_id == "url":
if step_id == "user":
schema = vol.Schema(
{
# vol.Required(CONF_REGION): str,
vol.Required(CONF_REGION): vol.In(_countries_list()),
# vol.Required(CONF_LANGUAGE): str,
vol.Required(CONF_LANGUAGE): vol.In(_languages_list()),
}
)
elif step_id == "url":
schema = vol.Schema(
{
vol.Required(CONF_LOGIN, default=self._loginurl): str,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartthinq_sensors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"documentation": "https://github.com/ollo69/ha-smartthinq-sensors",
"dependencies": [],
"codeowners": ["@ollo69"],
"requirements": [],
"requirements": ["pycountry>=20.7.3"],
"config_flow": true
}
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Region (ISO 3166-1 alpha-2, e.g. US)",
"language": "Language (ISO-639-1, e.g. en)",
"token": "Refresh Token (only for Authentication mode v1)",
"use_api_v2": "Use Authentication mode v2"
"region": "Country Code",
"language": "Language Code"
},
"description": "Fill in your SmartThinQ access information. If you don't have a refresh token, leave it empty to start the authentication process with your credentials. Refresh token is not used with Authentication mode v2.",
"description": "Select your SmartThinQ account localization information.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "País (ISO 3166-1 alpha-2, p. ej. ES)",
"language": "Idioma (ISO-639-1, p. ej. es)",
"token": "Actualizar el Token (Sólo para autenticación modo v1)",
"use_api_v2": "Usar autenticación modo v2"
"region": "Codigo País",
"language": "Codigo Idioma"
},
"description": "Inserta tu información de acceso a SmartThinQ. Si no dispones de un Token de actualización, déjalo vacío para empezar el proceso de autenticación con tus credenciales. El Token de Actualización no es necesario con el Authentication mode v2.",
"description": "Inserta tu información de acceso a SmartThinQ.",
"title": "Sensores SmartThinQ LGE"
},
"url": {
Expand Down
10 changes: 4 additions & 6 deletions custom_components/smartthinq_sensors/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Paese (ISO 3166-1 alpha-2, per es. IT)",
"language": "Lingua (ISO-639-1, per es. it)",
"token": "Aggiornare il Token (solo per Authentication mode v1)",
"use_api_v2": "Usa Authentication mode v2"
"region": "Codice Paese",
"language": "Codice Lingua"
},
"description": "Inserisci le informazioni di acceso SmartThinQ. Se non si dispone di un token di aggiornamento, lasciarlo vuoto per avviare il processo di autenticazione con le proprie credenziali. Il token di aggiornamento non viene usato con Authentication mode v2.",
"description": "Seleziona le informazioni di localizzazione del tuo account SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand All @@ -27,7 +25,7 @@
"callback_url": "URL di reindirizzamento"
},
"description": "Utilizzare l'URL del primo campo per eseguire l'accesso con le proprie credenziali a SmartThinQ, poi incollare l'URL in cui si viene reindirizzati con il browser dopo l'accesso nel secondo campo.",
"title": "SmartThinQ LGE Sensors - Autentificazione"
"title": "SmartThinQ LGE Sensors - Autenticazione"
},
"token": {
"data": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Region (ISO 3166-1 alpha-2, np. PL)",
"language": "Język (ISO-639-1, np. pl)",
"token": "Wpisz token (tylko dla trybu uwierzytelniania API v1)",
"use_api_v2": "Użyj trybu API v2"
"region": "Region",
"language": "Język"
},
"description": "Podaj swoje ustawienia regionalne dla SmartThinQ. Jeśli nie posiadasz tokena, pozostaw to pole puste, rozpocznij proces uwierzytelniania. Token nie jest używany w trybie uwierzytelniania API v2.",
"description": "Podaj swoje ustawienia regionalne dla SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down
8 changes: 3 additions & 5 deletions custom_components/smartthinq_sensors/translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"step": {
"user": {
"data": {
"region": "Región (ISO 3166-1 alpha-2, napr. SK)",
"language": "Jazyk (ISO-639-1, napr. sk)",
"token": "Obnova Token (len pre Autorizačný mód v1)",
"use_api_v2": "Použi Autorizačný mód v2"
"region": "Región",
"language": "Jazyk"
},
"description": "Vyplňte informácie o prístupe k SmartThinQ. Ak nemáte obnovovací token, nechajte ho prázdny a začnite proces autentifikácie pomocou vašich poverení. Obnovovací token sa nepoužíva v režime overovania v2.",
"description": "Vyplňte informácie o prístupe k SmartThinQ.",
"title": "SmartThinQ LGE Sensors"
},
"url": {
Expand Down

0 comments on commit c24cb44

Please sign in to comment.