1 """Config flow for PrusaLink integration."""
3 from __future__
import annotations
9 from awesomeversion
import AwesomeVersion, AwesomeVersionException
10 from httpx
import HTTPError, InvalidURL
11 from pyprusalink
import PrusaLink
12 from pyprusalink.types
import InvalidAuth, VersionInfo
13 import voluptuous
as vol
21 from .const
import DOMAIN
23 _LOGGER = logging.getLogger(__name__)
26 STEP_USER_DATA_SCHEMA = vol.Schema(
28 vol.Required(CONF_HOST): str,
31 vol.Required(CONF_USERNAME, default=
"maker"): str,
32 vol.Required(CONF_PASSWORD): str,
38 """Raise NotSupported exception if the printer is not supported."""
41 if AwesomeVersion(
"2.0.0") <= AwesomeVersion(version[
"api"]):
46 if version.get(
"original",
"").startswith(
47 (
"PrusaLink I3MK3",
"PrusaLink I3MK2")
48 )
and AwesomeVersion(
"0.7.2") <= AwesomeVersion(version[
"server"]):
51 except AwesomeVersionException
as err:
52 raise NotSupported
from err
57 async
def validate_input(hass: HomeAssistant, data: dict[str, str]) -> dict[str, str]:
58 """Validate the user input allows us to connect.
60 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
70 async
with asyncio.timeout(5):
71 version = await api.get_version()
73 except (TimeoutError, HTTPError, InvalidURL)
as err:
74 _LOGGER.error(
"Could not connect to PrusaLink: %s", err)
75 raise CannotConnect
from err
79 return {
"title": version[
"hostname"]
or version[
"text"]}
83 """Handle a config flow for PrusaLink."""
89 self, user_input: dict[str, Any] |
None =
None
90 ) -> ConfigFlowResult:
91 """Handle the initial step."""
92 if user_input
is None:
94 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA
97 host = user_input[CONF_HOST].rstrip(
"/")
98 if not host.startswith((
"http://",
"https://")):
99 host = f
"http://{host}"
103 CONF_USERNAME: user_input[CONF_USERNAME],
104 CONF_PASSWORD: user_input[CONF_PASSWORD],
110 except CannotConnect:
111 errors[
"base"] =
"cannot_connect"
113 errors[
"base"] =
"not_supported"
115 errors[
"base"] =
"invalid_auth"
117 _LOGGER.exception(
"Unexpected exception")
118 errors[
"base"] =
"unknown"
123 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
128 """Error to indicate we cannot connect."""
132 """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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_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)
None ensure_printer_is_supported(VersionInfo version)
dict[str, str] validate_input(HomeAssistant hass, dict[str, str] data)
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)