Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for DirecTV."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any, cast
7 from urllib.parse import urlparse
8 
9 from directv import DIRECTV, DIRECTVError
10 import voluptuous as vol
11 
12 from homeassistant.components import ssdp
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_HOST, CONF_NAME
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import CONF_RECEIVER_ID, DOMAIN
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 ERROR_CANNOT_CONNECT = "cannot_connect"
23 ERROR_UNKNOWN = "unknown"
24 
25 
26 async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
27  """Validate the user input allows us to connect.
28 
29  Data has the keys from DATA_SCHEMA with values provided by the user.
30  """
31  session = async_get_clientsession(hass)
32  directv = DIRECTV(data[CONF_HOST], session=session)
33  device = await directv.update()
34 
35  return {CONF_RECEIVER_ID: device.info.receiver_id}
36 
37 
38 class DirecTVConfigFlow(ConfigFlow, domain=DOMAIN):
39  """Handle a config flow for DirecTV."""
40 
41  VERSION = 1
42 
43  def __init__(self) -> None:
44  """Set up the instance."""
45  self.discovery_info: dict[str, Any] = {}
46 
47  async def async_step_user(
48  self, user_input: dict[str, Any] | None = None
49  ) -> ConfigFlowResult:
50  """Handle a flow initiated by the user."""
51  if user_input is None:
52  return self._show_setup_form_show_setup_form()
53 
54  try:
55  info = await validate_input(self.hass, user_input)
56  except DIRECTVError:
57  return self._show_setup_form_show_setup_form({"base": ERROR_CANNOT_CONNECT})
58  except Exception:
59  _LOGGER.exception("Unexpected exception")
60  return self.async_abortasync_abortasync_abort(reason=ERROR_UNKNOWN)
61 
62  user_input[CONF_RECEIVER_ID] = info[CONF_RECEIVER_ID]
63 
64  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_RECEIVER_ID])
65  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]})
66 
67  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_HOST], data=user_input)
68 
69  async def async_step_ssdp(
70  self, discovery_info: ssdp.SsdpServiceInfo
71  ) -> ConfigFlowResult:
72  """Handle SSDP discovery."""
73  # We can cast the hostname to str because the ssdp_location is not bytes and
74  # not a relative url
75  host = cast(str, urlparse(discovery_info.ssdp_location).hostname)
76  receiver_id = None
77 
78  if discovery_info.upnp.get(ssdp.ATTR_UPNP_SERIAL):
79  receiver_id = discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL][
80  4:
81  ] # strips off RID-
82 
83  self.context.update({"title_placeholders": {"name": host}})
84 
85  self.discovery_info.update(
86  {CONF_HOST: host, CONF_NAME: host, CONF_RECEIVER_ID: receiver_id}
87  )
88 
89  try:
90  info = await validate_input(self.hass, self.discovery_info)
91  except DIRECTVError:
92  return self.async_abortasync_abortasync_abort(reason=ERROR_CANNOT_CONNECT)
93  except Exception:
94  _LOGGER.exception("Unexpected exception")
95  return self.async_abortasync_abortasync_abort(reason=ERROR_UNKNOWN)
96 
97  self.discovery_info[CONF_RECEIVER_ID] = info[CONF_RECEIVER_ID]
98 
99  await self.async_set_unique_idasync_set_unique_id(self.discovery_info[CONF_RECEIVER_ID])
100  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
101  updates={CONF_HOST: self.discovery_info[CONF_HOST]}
102  )
103 
104  return await self.async_step_ssdp_confirmasync_step_ssdp_confirm()
105 
107  self, user_input: dict[str, Any] | None = None
108  ) -> ConfigFlowResult:
109  """Handle a confirmation flow initiated by SSDP."""
110  if user_input is None:
111  return self.async_show_formasync_show_formasync_show_form(
112  step_id="ssdp_confirm",
113  description_placeholders={"name": self.discovery_info[CONF_NAME]},
114  errors={},
115  )
116 
117  return self.async_create_entryasync_create_entryasync_create_entry(
118  title=self.discovery_info[CONF_NAME],
119  data=self.discovery_info,
120  )
121 
122  def _show_setup_form(self, errors: dict | None = None) -> ConfigFlowResult:
123  """Show the setup form to the user."""
124  return self.async_show_formasync_show_formasync_show_form(
125  step_id="user",
126  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
127  errors=errors or {},
128  )
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
Definition: config_flow.py:71
ConfigFlowResult _show_setup_form(self, dict|None errors=None)
Definition: config_flow.py:122
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:49
ConfigFlowResult async_step_ssdp_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:108
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)
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)
dict[str, Any] validate_input(HomeAssistant hass, dict data)
Definition: config_flow.py:26
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
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)