1 """Config flow to configure webostv component."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
7 from typing
import Any, Self
8 from urllib.parse
import urlparse
10 from aiowebostv
import WebOsTvPairError
11 import voluptuous
as vol
25 from .
import async_control_connect, update_client_key
26 from .const
import CONF_SOURCES, DEFAULT_NAME, DOMAIN, WEBOSTV_EXCEPTIONS
27 from .helpers
import async_get_sources
29 DATA_SCHEMA = vol.Schema(
31 vol.Required(CONF_HOST): cv.string,
32 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
34 extra=vol.ALLOW_EXTRA,
37 _LOGGER = logging.getLogger(__name__)
41 """WebosTV configuration flow."""
46 """Initialize workflow."""
47 self.
_host_host: str =
""
48 self.
_name_name: str =
""
49 self.
_uuid_uuid: str |
None =
None
54 """Get the options flow for this handler."""
58 self, user_input: dict[str, Any] |
None =
None
59 ) -> ConfigFlowResult:
60 """Handle a flow initialized by the user."""
61 errors: dict[str, str] = {}
62 if user_input
is not None:
63 self.
_host_host = user_input[CONF_HOST]
64 self.
_name_name = user_input[CONF_NAME]
68 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
73 """Check if entry is configured, update unique_id if needed."""
75 if entry.data[CONF_HOST] != self.
_host_host:
78 if self.
_uuid_uuid
and not entry.unique_id:
80 "Updating unique_id for host %s, unique_id: %s",
84 self.hass.config_entries.async_update_entry(entry, unique_id=self.
_uuid_uuid)
89 self, user_input: dict[str, Any] |
None =
None
90 ) -> ConfigFlowResult:
91 """Display pairing form."""
94 self.context[
"title_placeholders"] = {
"name": self.
_name_name}
97 if user_input
is not None:
100 except WebOsTvPairError:
102 except WEBOSTV_EXCEPTIONS:
103 errors[
"base"] =
"cannot_connect"
106 client.hello_info[
"deviceUUID"], raise_on_progress=
False
109 data = {CONF_HOST: self.
_host_host, CONF_CLIENT_SECRET: client.client_key}
115 self, discovery_info: ssdp.SsdpServiceInfo
116 ) -> ConfigFlowResult:
117 """Handle a flow initialized by discovery."""
118 assert discovery_info.ssdp_location
119 host = urlparse(discovery_info.ssdp_location).hostname
121 self.
_host_host = host
122 self.
_name_name = discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, DEFAULT_NAME)
124 uuid = discovery_info.upnp[ssdp.ATTR_UPNP_UDN]
126 if uuid.startswith(
"uuid:"):
131 if self.hass.config_entries.flow.async_has_matching_flow(self):
138 """Return True if other_flow is matching this flow."""
139 return other_flow._host == self.
_host_host
142 self, entry_data: Mapping[str, Any]
143 ) -> ConfigFlowResult:
144 """Perform reauth upon an WebOsTvPairError."""
145 self.
_host_host = entry_data[CONF_HOST]
149 self, user_input: dict[str, Any] |
None =
None
150 ) -> ConfigFlowResult:
151 """Dialog that informs the user that reauth is required."""
152 if user_input
is not None:
155 except WebOsTvPairError:
157 except WEBOSTV_EXCEPTIONS:
162 await self.hass.config_entries.async_reload(reauth_entry.entry_id)
169 """Handle options."""
171 def __init__(self, config_entry: ConfigEntry) ->
None:
172 """Initialize options flow."""
173 self.
hosthost = config_entry.data[CONF_HOST]
174 self.
keykey = config_entry.data[CONF_CLIENT_SECRET]
177 self, user_input: dict[str, Any] |
None =
None
178 ) -> ConfigFlowResult:
179 """Manage the options."""
181 if user_input
is not None:
182 options_input = {CONF_SOURCES: user_input[CONF_SOURCES]}
187 errors[
"base"] =
"cannot_retrieve"
190 sources = [s
for s
in option_sources
if s
in sources_list]
192 sources = sources_list
194 options_schema = vol.Schema(
198 description={
"suggested_value": sources},
199 ): cv.multi_select({source: source
for source
in sources_list}),
204 step_id=
"init", data_schema=options_schema, errors=errors
ConfigFlowResult async_step_pairing(self, dict[str, Any]|None user_input=None)
None _async_check_configured_entry(self)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
bool is_matching(self, Self other_flow)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
None __init__(self, ConfigEntry config_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)
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)
list[str] async_get_sources(str host, str key)
WebOsClient async_control_connect(str host, str|None key)
None update_client_key(HomeAssistant hass, ConfigEntry entry, WebOsClient client)