1 """Config flow for LG ThinQ."""
3 from __future__
import annotations
9 from thinqconnect
import ThinQApi, ThinQAPIErrorCodes, ThinQAPIException
10 from thinqconnect.country
import Country
11 import voluptuous
as vol
21 CONF_CONNECT_CLIENT_ID,
28 SUPPORTED_COUNTRIES = [country.value
for country
in Country]
30 ThinQAPIErrorCodes.INVALID_TOKEN:
"invalid_token",
31 ThinQAPIErrorCodes.NOT_ACCEPTABLE_TERMS:
"not_acceptable_terms",
32 ThinQAPIErrorCodes.NOT_ALLOWED_API_AGAIN:
"not_allowed_api_again",
33 ThinQAPIErrorCodes.NOT_SUPPORTED_COUNTRY:
"not_supported_country",
34 ThinQAPIErrorCodes.EXCEEDED_API_CALLS:
"exceeded_api_calls",
37 _LOGGER = logging.getLogger(__name__)
41 """Handle a config flow."""
46 """Get the default country code based on config."""
47 country = self.hass.config.country
48 if country
is not None and country
in SUPPORTED_COUNTRIES:
51 return DEFAULT_COUNTRY
54 self, access_token: str, country_code: str
55 ) -> ConfigFlowResult:
56 """Create an entry for the flow."""
57 connect_client_id = f
"{CLIENT_PREFIX}-{uuid.uuid4()!s}"
62 access_token=access_token,
63 country_code=country_code,
64 client_id=connect_client_id,
65 ).async_get_device_list()
69 title=THINQ_DEFAULT_NAME,
71 CONF_ACCESS_TOKEN: access_token,
72 CONF_CONNECT_CLIENT_ID: connect_client_id,
73 CONF_COUNTRY: country_code,
78 self, user_input: dict[str, Any] |
None =
None
79 ) -> ConfigFlowResult:
80 """Handle a flow initiated by the user."""
81 errors: dict[str, str] = {}
83 if user_input
is not None:
84 access_token = user_input[CONF_ACCESS_TOKEN]
85 country_code = user_input[CONF_COUNTRY]
93 except ThinQAPIException
as exc:
94 errors[
"base"] = THINQ_ERRORS.get(exc.code,
"token_unauthorized")
95 _LOGGER.error(
"Failed to validate access_token %s", exc)
99 data_schema=vol.Schema(
101 vol.Required(CONF_ACCESS_TOKEN): cv.string,
109 description_placeholders={
"pat_url": THINQ_PAT_URL},
str _get_default_country_code(self)
ConfigFlowResult _validate_and_create_entry(self, str access_token, str country_code)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)