1 """Config flow for Samsung SyncThru."""
5 from urllib.parse
import urlparse
7 from pysyncthru
import ConnectionMode, SyncThru, SyncThruAPINotSupported
8 from url_normalize
import url_normalize
9 import voluptuous
as vol
16 from .const
import DEFAULT_MODEL, DEFAULT_NAME_TEMPLATE, DOMAIN
20 """Samsung SyncThru config flow."""
28 self, user_input: dict[str, Any] |
None =
None
29 ) -> ConfigFlowResult:
30 """Handle user initiated flow."""
31 if user_input
is None:
36 self, discovery_info: ssdp.SsdpServiceInfo
37 ) -> ConfigFlowResult:
38 """Handle SSDP initiated flow."""
42 self.
urlurl = url_normalize(
43 discovery_info.upnp.get(
44 ssdp.ATTR_UPNP_PRESENTATION_URL,
45 f
"http://{urlparse(discovery_info.ssdp_location or '').hostname}/",
49 for existing_entry
in (
53 if not existing_entry.unique_id:
54 self.hass.config_entries.async_update_entry(
55 existing_entry, unique_id=discovery_info.upnp[ssdp.ATTR_UPNP_UDN]
59 self.
namename = discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME,
"")
62 self.
namename = re.sub(
r"\s+\([\d.]+\)\s*$",
"", self.
namename)
64 self.context[
"title_placeholders"] = {CONF_NAME: self.
namename}
68 self, user_input: dict[str, str] |
None =
None
69 ) -> ConfigFlowResult:
70 """Handle discovery confirmation by user."""
71 if user_input
is not None:
76 user_input={CONF_URL: self.
urlurl, CONF_NAME: self.
namename},
81 if user_input
is None:
85 data_schema=vol.Schema(
87 vol.Required(CONF_URL, default=user_input.get(CONF_URL,
"")): str,
88 vol.Optional(CONF_NAME, default=user_input.get(CONF_NAME,
"")): str,
95 """Validate input, proceed to create."""
96 user_input[CONF_URL] = url_normalize(
97 user_input[CONF_URL], default_scheme=
"http"
99 if "://" not in user_input[CONF_URL]:
101 step_id=step_id, user_input=user_input, errors={CONF_URL:
"invalid_url"}
106 for existing_entry
in (
109 if x.data[CONF_URL] == user_input[CONF_URL]
and x.unique_id
114 session = aiohttp_client.async_get_clientsession(self.hass)
116 user_input[CONF_URL], session, connection_mode=ConnectionMode.API
120 await printer.update()
121 if not user_input.get(CONF_NAME):
122 user_input[CONF_NAME] = DEFAULT_NAME_TEMPLATE.format(
123 printer.model()
or DEFAULT_MODEL
125 except SyncThruAPINotSupported:
126 errors[CONF_URL] =
"syncthru_not_supported"
128 if printer.is_unknown_state():
129 errors[CONF_URL] =
"unknown_state"
133 step_id=step_id, user_input=user_input, errors=errors
137 title=user_input.get(CONF_NAME),
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
ConfigFlowResult async_step_confirm(self, dict[str, str]|None user_input=None)
def _async_show_form(self, step_id, user_input=None, errors=None)
def _async_check_and_create(self, step_id, user_input)
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)
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)
_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)