1 """Config flow for Steam integration."""
3 from __future__
import annotations
5 from collections.abc
import Iterator, Mapping
9 import voluptuous
as vol
21 from .
import SteamConfigEntry
22 from .const
import CONF_ACCOUNT, CONF_ACCOUNTS, DOMAIN, LOGGER, PLACEHOLDERS
25 MAX_IDS_TO_REQUEST = 275
29 """Handle common flow input validation."""
30 steam.api.key.set(user_input[CONF_API_KEY])
31 interface = steam.api.interface(
"ISteamUser")
32 names = interface.GetPlayerSummaries(steamids=user_input[CONF_ACCOUNT])
33 return names[
"response"][
"players"][
"player"][0]
37 """Handle a config flow for Steam."""
42 config_entry: SteamConfigEntry,
43 ) -> SteamOptionsFlowHandler:
44 """Get the options flow for this handler."""
48 self, user_input: dict[str, Any] |
None =
None
49 ) -> ConfigFlowResult:
50 """Handle a flow initiated by the user."""
53 user_input = {CONF_ACCOUNT: self.
_get_reauth_entry_get_reauth_entry().data[CONF_ACCOUNT]}
54 elif user_input
is not None:
56 res = await self.hass.async_add_executor_job(validate_input, user_input)
58 name =
str(res[
"personaname"])
60 errors[
"base"] =
"invalid_account"
61 except (steam.api.HTTPError, steam.api.HTTPTimeoutError)
as ex:
62 errors[
"base"] =
"cannot_connect"
64 errors[
"base"] =
"invalid_auth"
65 except Exception
as ex:
66 LOGGER.exception(
"Unknown exception: %s", ex)
67 errors[
"base"] =
"unknown"
71 self.hass.config_entries.async_update_entry(entry, data=user_input)
72 await self.hass.config_entries.async_reload(entry.entry_id)
78 options={CONF_ACCOUNTS: {user_input[CONF_ACCOUNT]: name}},
80 user_input = user_input
or {}
83 data_schema=vol.Schema(
86 CONF_API_KEY, default=user_input.get(CONF_API_KEY)
or ""
89 CONF_ACCOUNT, default=user_input.get(CONF_ACCOUNT)
or ""
94 description_placeholders=PLACEHOLDERS,
98 self, entry_data: Mapping[str, Any]
99 ) -> ConfigFlowResult:
100 """Handle a reauthorization flow request."""
104 self, user_input: dict[str, str] |
None =
None
105 ) -> ConfigFlowResult:
106 """Confirm reauth dialog."""
107 if user_input
is not None:
112 step_id=
"reauth_confirm", description_placeholders=PLACEHOLDERS
117 for i
in range(0, len(ids), MAX_IDS_TO_REQUEST):
118 yield ids[i : i + MAX_IDS_TO_REQUEST]
122 """Handle Steam client options."""
124 def __init__(self, entry: SteamConfigEntry) ->
None:
125 """Initialize options flow."""
129 self, user_input: dict[str, dict[str, str]] |
None =
None
130 ) -> ConfigFlowResult:
131 """Manage Steam options."""
132 if user_input
is not None:
134 for _id
in self.
optionsoptions[CONF_ACCOUNTS]:
135 if _id
not in user_input[CONF_ACCOUNTS]
and (
136 entity_id := er.async_get(self.hass).async_get_entity_id(
137 Platform.SENSOR, DOMAIN, f
"sensor.steam_{_id}"
144 for _id, name
in self.
optionsoptions[CONF_ACCOUNTS].items()
145 if _id
in user_input[CONF_ACCOUNTS]
153 name[
"steamid"]: name[
"personaname"]
154 for name
in await self.hass.async_add_executor_job(self.
get_accountsget_accounts)
157 error = {
"base":
"unauthorized"}
159 except steam.api.HTTPTimeoutError:
160 users = self.
optionsoptions[CONF_ACCOUNTS]
165 default=set(self.
optionsoptions[CONF_ACCOUNTS]),
166 ): cv.multi_select(users | self.
optionsoptions[CONF_ACCOUNTS]),
168 self.
optionsoptions[CONF_ACCOUNTS] = users | self.
optionsoptions[CONF_ACCOUNTS]
171 step_id=
"init", data_schema=vol.Schema(options), errors=error
176 interface = steam.api.interface(
"ISteamUser")
178 friends = interface.GetFriendList(
181 _users_str = [user[
"steamid"]
for user
in friends[
"friendslist"][
"friends"]]
182 except steam.api.HTTPError:
187 interface.GetPlayerSummaries(steamids=id_batch)[
"response"][
"players"][
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
SteamOptionsFlowHandler async_get_options_flow(SteamConfigEntry config_entry)
list[dict[str, str|int]] get_accounts(self)
ConfigFlowResult async_step_init(self, dict[str, dict[str, str]]|None user_input=None)
None __init__(self, SteamConfigEntry entry)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
None _set_confirm_only(self)
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_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, str|int] validate_input(dict[str, str] user_input)
Iterator[list[str]] _batch_ids(list[str] ids)