1 """Config flow for Mastodon."""
3 from __future__
import annotations
7 from mastodon.Mastodon
import MastodonNetworkError, MastodonUnauthorizedError
8 import voluptuous
as vol
25 from .const
import CONF_BASE_URL, DEFAULT_URL, DOMAIN, LOGGER
26 from .utils
import construct_mastodon_username, create_mastodon_client
28 STEP_USER_DATA_SCHEMA = vol.Schema(
47 """Return the base url from a url."""
48 return str(
URL(url).origin())
52 """Handle a config flow."""
56 config_entry: ConfigEntry
65 dict[str, str] |
None,
66 dict[str, str] |
None,
69 """Check connection to the Mastodon instance."""
77 instance = client.instance()
78 account = client.account_verify_credentials()
80 except MastodonNetworkError:
81 return None,
None, {
"base":
"network_error"}
82 except MastodonUnauthorizedError:
83 return None,
None, {
"base":
"unauthorized_error"}
85 LOGGER.exception(
"Unexpected error")
86 return None,
None, {
"base":
"unknown"}
87 return instance, account, {}
91 user_input: dict[str, Any] |
None =
None,
92 errors: dict[str, str] |
None =
None,
93 description_placeholders: dict[str, str] |
None =
None,
94 step_id: str =
"user",
95 ) -> ConfigFlowResult:
96 """Show the user form."""
97 if user_input
is None:
102 STEP_USER_DATA_SCHEMA, user_input
104 description_placeholders=description_placeholders,
109 self, user_input: dict[str, Any] |
None =
None
110 ) -> ConfigFlowResult:
111 """Handle a flow initialized by the user."""
112 errors: dict[str, str] |
None =
None
116 instance, account, errors = await self.hass.async_add_executor_job(
118 user_input[CONF_BASE_URL],
119 user_input[CONF_CLIENT_ID],
120 user_input[CONF_CLIENT_SECRET],
121 user_input[CONF_ACCESS_TOKEN],
136 """Import a config entry from configuration.yaml."""
137 errors: dict[str, str] |
None =
None
139 LOGGER.debug(
"Importing Mastodon from configuration.yaml")
142 client_id =
str(import_data.get(CONF_CLIENT_ID))
143 client_secret =
str(import_data.get(CONF_CLIENT_SECRET))
144 access_token =
str(import_data.get(CONF_ACCESS_TOKEN))
145 name = import_data.get(CONF_NAME)
147 instance, account, errors = await self.hass.async_add_executor_job(
166 CONF_BASE_URL: base_url,
167 CONF_CLIENT_ID: client_id,
168 CONF_CLIENT_SECRET: client_secret,
169 CONF_ACCESS_TOKEN: access_token,
173 reason = next(iter(errors.items()))[1]
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult show_user_form(self, dict[str, Any]|None user_input=None, dict[str, str]|None errors=None, dict[str, str]|None description_placeholders=None, str step_id="user")
tuple[ dict[str, str]|None, dict[str, str]|None, dict[str, str],] check_connection(self, str base_url, str client_id, str client_secret, str access_token)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
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_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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
str base_url_from_url(str url)
Mastodon create_mastodon_client(str base_url, str client_id, str client_secret, str access_token)
str construct_mastodon_username(dict[str, str]|None instance, dict[str, str]|None account)