1 """Config flow for LastFm."""
3 from __future__
import annotations
7 from pylast
import LastFMNetwork, PyLastError, User, WSError
8 import voluptuous
as vol
24 from .const
import CONF_MAIN_USER, CONF_USERS, DOMAIN
26 PLACEHOLDERS = {
"api_account_url":
"https://www.last.fm/api/account/create"}
28 CONFIG_SCHEMA: vol.Schema = vol.Schema(
30 vol.Required(CONF_API_KEY): str,
31 vol.Required(CONF_MAIN_USER): str,
37 """Get and validate lastFM User."""
38 user = LastFMNetwork(api_key=api_key).get_user(username)
42 except WSError
as error:
43 if error.details ==
"User not found":
44 errors[
"base"] =
"invalid_account"
47 ==
"Invalid API key - You must be granted a valid key by last.fm"
49 errors[
"base"] =
"invalid_auth"
51 errors[
"base"] =
"unknown"
53 errors[
"base"] =
"unknown"
58 api_key: str, usernames: list[str]
59 ) -> tuple[list[str], dict[str, str]]:
60 """Validate list of users. Return tuple of valid users and errors."""
63 for username
in usernames:
66 errors = lastfm_errors
68 valid_users.append(username)
69 return valid_users, errors
73 """Config flow handler for LastFm."""
75 data: dict[str, Any] = {}
80 config_entry: ConfigEntry,
81 ) -> LastFmOptionsFlowHandler:
82 """Get the options flow for this handler."""
86 self, user_input: dict[str, Any] |
None =
None
87 ) -> ConfigFlowResult:
88 """Initialize user input."""
89 errors: dict[str, str] = {}
90 if user_input
is not None:
91 self.
datadata = user_input.copy()
93 self.
datadata[CONF_API_KEY], self.
datadata[CONF_MAIN_USER]
100 description_placeholders=PLACEHOLDERS,
105 self, user_input: dict[str, Any] |
None =
None
106 ) -> ConfigFlowResult:
107 """Form to select other users and friends."""
108 errors: dict[str, str] = {}
109 if user_input
is not None:
111 self.
datadata[CONF_API_KEY], user_input[CONF_USERS]
113 user_input[CONF_USERS] = users
119 CONF_API_KEY: self.
datadata[CONF_API_KEY],
120 CONF_MAIN_USER: self.
datadata[CONF_MAIN_USER],
122 self.
datadata[CONF_MAIN_USER],
123 *user_input[CONF_USERS],
129 self.
datadata[CONF_API_KEY], self.
datadata[CONF_MAIN_USER]
131 friends_response = await self.hass.async_add_executor_job(
132 main_user.get_friends
136 for friend
in friends_response
148 options=friends, custom_value=
True, multiple=
True
153 user_input
or {CONF_USERS: []},
159 """LastFm Options flow handler."""
162 self, user_input: dict[str, Any] |
None =
None
163 ) -> ConfigFlowResult:
164 """Initialize form."""
165 errors: dict[str, str] = {}
167 if user_input
is not None:
169 options[CONF_API_KEY], user_input[CONF_USERS]
171 user_input[CONF_USERS] = users
177 CONF_USERS: user_input[CONF_USERS],
180 if options[CONF_MAIN_USER]:
183 options[CONF_API_KEY],
184 options[CONF_MAIN_USER],
186 friends_response = await self.hass.async_add_executor_job(
187 main_user.get_friends
191 for friend
in friends_response
205 options=friends, custom_value=
True, multiple=
True
210 user_input
or options,
LastFmOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_friends(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
tuple[list[str], dict[str, str]] validate_lastfm_users(str api_key, list[str] usernames)
tuple[User, dict[str, str]] get_lastfm_user(str api_key, str username)