1 """Config flow to configure songpal component."""
3 from __future__
import annotations
6 from typing
import TYPE_CHECKING, Any
7 from urllib.parse
import urlparse
9 from songpal
import Device, SongpalException
10 import voluptuous
as vol
16 from .const
import CONF_ENDPOINT, DOMAIN
18 _LOGGER = logging.getLogger(__name__)
22 """Device Configuration."""
24 def __init__(self, name: str, host: str |
None, endpoint: str) ->
None:
25 """Initialize Configuration."""
28 assert host
is not None
34 """Songpal configuration flow."""
41 self, user_input: dict[str, str] |
None =
None
42 ) -> ConfigFlowResult:
43 """Handle a flow initiated by the user."""
44 if user_input
is None:
47 data_schema=vol.Schema({vol.Required(CONF_ENDPOINT): str}),
51 endpoint = user_input[CONF_ENDPOINT]
52 parsed_url = urlparse(endpoint)
57 await device.get_supported_methods()
58 interface_info = await device.get_interface_information()
59 name = interface_info.modelName
60 except SongpalException
as ex:
61 _LOGGER.debug(
"Connection failed: %s", ex)
64 data_schema=vol.Schema(
67 CONF_ENDPOINT, default=user_input.get(CONF_ENDPOINT,
"")
71 errors={
"base":
"cannot_connect"},
79 self, user_input: dict[str, Any] |
None =
None
80 ) -> ConfigFlowResult:
81 """Handle a flow start."""
84 if user_input
is None:
87 description_placeholders={
88 CONF_NAME: self.
confconf.name,
89 CONF_HOST: self.
confconf.host,
97 title=self.
confconf.name,
98 data={CONF_NAME: self.
confconf.name, CONF_ENDPOINT: self.
confconf.endpoint},
102 self, discovery_info: ssdp.SsdpServiceInfo
103 ) -> ConfigFlowResult:
104 """Handle a discovered Songpal device."""
108 _LOGGER.debug(
"Discovered: %s", discovery_info)
110 friendly_name = discovery_info.upnp[ssdp.ATTR_UPNP_FRIENDLY_NAME]
111 hostname = urlparse(discovery_info.ssdp_location).hostname
112 scalarweb_info = discovery_info.upnp[
"X_ScalarWebAPI_DeviceInfo"]
113 endpoint = scalarweb_info[
"X_ScalarWebAPI_BaseURL"]
114 service_types = scalarweb_info[
"X_ScalarWebAPI_ServiceList"][
115 "X_ScalarWebAPI_ServiceType"
119 if "videoScreen" in service_types:
125 assert isinstance(hostname, str)
127 self.context[
"title_placeholders"] = {
128 CONF_NAME: friendly_name,
137 """Import a config entry."""
138 name = import_data.get(CONF_NAME)
139 endpoint = import_data[CONF_ENDPOINT]
140 parsed_url = urlparse(endpoint)
145 await device.get_supported_methods()
148 interface_info = await device.get_interface_information()
149 name = interface_info.modelName
150 except SongpalException
as ex:
151 _LOGGER.error(
"Import from yaml configuration failed: %s", ex)
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
ConfigFlowResult async_step_import(self, dict[str, str] import_data)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
None __init__(self, str name, str|None host, str endpoint)
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)