1 """Config flow for Squeezebox integration."""
3 from __future__
import annotations
6 from http
import HTTPStatus
8 from typing
import TYPE_CHECKING, Any
10 from pysqueezebox
import Server, async_discover
11 import voluptuous
as vol
22 from .const
import CONF_HTTPS, DEFAULT_PORT, DOMAIN
24 _LOGGER = logging.getLogger(__name__)
30 discovery_info: dict[str, Any] |
None =
None,
32 """Generate base schema."""
33 base_schema: dict[Any, Any] = {}
34 if discovery_info
and CONF_HOST
in discovery_info:
39 description={
"suggested_value": discovery_info[CONF_HOST]},
44 base_schema.update({vol.Required(CONF_HOST): str})
46 if discovery_info
and CONF_PORT
in discovery_info:
52 description={
"suggested_value": discovery_info[CONF_PORT]},
57 base_schema.update({vol.Required(CONF_PORT, default=DEFAULT_PORT): int})
61 vol.Optional(CONF_USERNAME): str,
62 vol.Optional(CONF_PASSWORD): str,
63 vol.Optional(CONF_HTTPS, default=
False): bool,
67 return vol.Schema(base_schema)
71 """Handle a config flow for Squeezebox."""
76 """Initialize an instance of the squeezebox config flow."""
80 async
def _discover(self, uuid: str |
None =
None) ->
None:
81 """Discover an unconfigured LMS server."""
83 discovery_event = asyncio.Event()
85 def _discovery_callback(server: Server) ->
None:
89 if entry.unique_id == server.uuid:
92 CONF_HOST: server.host,
93 CONF_PORT:
int(server.port),
96 _LOGGER.debug(
"Discovered server: %s", self.
discovery_infodiscovery_info)
99 discovery_task = self.hass.async_create_task(
103 await discovery_event.wait()
104 discovery_task.cancel()
110 """Validate the user input allows us to connect.
112 Retrieve unique id and abort if already configured.
118 data.get(CONF_USERNAME),
119 data.get(CONF_PASSWORD),
120 https=data[CONF_HTTPS],
124 status = await server.async_query(
"serverstatus")
126 if server.http_status == HTTPStatus.UNAUTHORIZED:
127 return "invalid_auth"
128 return "cannot_connect"
139 self, user_input: dict[str, Any] |
None =
None
140 ) -> ConfigFlowResult:
141 """Handle a flow initialized by the user."""
143 if user_input
and CONF_HOST
in user_input:
150 async
with asyncio.timeout(TIMEOUT):
154 errors[
"base"] =
"no_server_found"
159 data_schema=vol.Schema({vol.Optional(CONF_HOST): str}),
164 self, user_input: dict[str, Any] |
None =
None
165 ) -> ConfigFlowResult:
166 """Edit a discovered or manually inputted server."""
172 title=user_input[CONF_HOST], data=user_input
174 errors[
"base"] = error
177 step_id=
"edit", data_schema=self.
data_schemadata_schema, errors=errors
181 self, discovery_info: dict[str, Any]
182 ) -> ConfigFlowResult:
183 """Handle discovery of a server."""
184 _LOGGER.debug(
"Reached server discovery flow with info: %s", discovery_info)
185 if "uuid" in discovery_info:
198 self.context.
update({
"title_placeholders": {
"host": discovery_info[CONF_HOST]}})
203 self, discovery_info: dhcp.DhcpServiceInfo
204 ) -> ConfigFlowResult:
205 """Handle dhcp discovery of a Squeezebox player."""
207 "Reached dhcp discovery of a player with info: %s", discovery_info
212 _LOGGER.debug(
"Configuring dhcp player with unique id: %s", self.
unique_idunique_id)
214 registry = er.async_get(self.hass)
219 if registry.async_get_entity_id(MP_DOMAIN, DOMAIN, self.
unique_idunique_id)
is not None:
ConfigFlowResult async_step_integration_discovery(self, dict[str, Any] discovery_info)
None _discover(self, str|None uuid=None)
ConfigFlowResult async_step_edit(self, dict[str, Any]|None user_input=None)
str|None _validate_input(self, dict[str, Any] data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
None _async_handle_discovery_without_unique_id(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_step_user(self, dict[str, Any]|None user_input=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)
IssData update(pyiss.ISS iss)
None async_discover(HomeAssistant hass)
vol.Schema _base_schema(dict[str, Any]|None discovery_info=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)