Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Android IP Webcam integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pydroid_ipcam import PyDroidIPCam
8 from pydroid_ipcam.exceptions import PyDroidIPCamException, Unauthorized
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers import config_validation as cv
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DEFAULT_PORT, DOMAIN
18 
19 STEP_USER_DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_HOST): str,
22  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
23  vol.Inclusive(CONF_USERNAME, "authentication"): str,
24  vol.Inclusive(CONF_PASSWORD, "authentication"): str,
25  }
26 )
27 
28 
29 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
30  """Validate the user input allows us to connect."""
31 
32  websession = async_get_clientsession(hass)
33  cam = PyDroidIPCam(
34  websession,
35  data[CONF_HOST],
36  data[CONF_PORT],
37  username=data.get(CONF_USERNAME),
38  password=data.get(CONF_PASSWORD),
39  ssl=False,
40  )
41  errors = {}
42  try:
43  await cam.update()
44  except Unauthorized:
45  errors[CONF_USERNAME] = "invalid_auth"
46  errors[CONF_PASSWORD] = "invalid_auth"
47  except PyDroidIPCamException:
48  errors["base"] = "cannot_connect"
49 
50  return errors
51 
52 
53 class AndroidIPWebcamConfigFlow(ConfigFlow, domain=DOMAIN):
54  """Handle a config flow for Android IP Webcam."""
55 
56  VERSION = 1
57 
58  async def async_step_user(
59  self, user_input: dict[str, Any] | None = None
60  ) -> ConfigFlowResult:
61  """Handle the initial step."""
62  if user_input is None:
63  return self.async_show_formasync_show_formasync_show_form(
64  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
65  )
66 
67  self._async_abort_entries_match_async_abort_entries_match(
68  {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
69  )
70  if not (errors := await validate_input(self.hass, user_input)):
71  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_HOST], data=user_input)
72 
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="user",
75  data_schema=STEP_USER_DATA_SCHEMA,
76  errors=errors,
77  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:60
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)
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)
dict[str, str] validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:29
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)