Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Goal Zero Yeti integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from goalzero import Yeti, exceptions
9 import voluptuous as vol
10 
11 from homeassistant.components import dhcp
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_NAME
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 from homeassistant.helpers.device_registry import format_mac
16 
17 from .const import DEFAULT_NAME, DOMAIN, MANUFACTURER
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 class GoalZeroFlowHandler(ConfigFlow, domain=DOMAIN):
23  """Handle a config flow for Goal Zero Yeti."""
24 
25  VERSION = 1
26 
27  _discovered_ip: str
28 
29  async def async_step_dhcp(
30  self, discovery_info: dhcp.DhcpServiceInfo
31  ) -> ConfigFlowResult:
32  """Handle dhcp discovery."""
33 
34  await self.async_set_unique_idasync_set_unique_id(format_mac(discovery_info.macaddress))
35  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})
36  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: discovery_info.ip})
37 
38  _, error = await self._async_try_connect_async_try_connect(discovery_info.ip)
39  if error is None:
40  self._discovered_ip_discovered_ip = discovery_info.ip
41  return await self.async_step_confirm_discoveryasync_step_confirm_discovery()
42  return self.async_abortasync_abortasync_abort(reason=error)
43 
45  self, user_input: dict[str, Any] | None = None
46  ) -> ConfigFlowResult:
47  """Allow the user to confirm adding the device."""
48  if user_input is not None:
49  return self.async_create_entryasync_create_entryasync_create_entry(
50  title=MANUFACTURER,
51  data={
52  CONF_HOST: self._discovered_ip_discovered_ip,
53  CONF_NAME: DEFAULT_NAME,
54  },
55  )
56 
57  self._set_confirm_only_set_confirm_only()
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="confirm_discovery",
60  description_placeholders={
61  CONF_HOST: self._discovered_ip_discovered_ip,
62  CONF_NAME: DEFAULT_NAME,
63  },
64  )
65 
66  async def async_step_user(
67  self, user_input: dict[str, Any] | None = None
68  ) -> ConfigFlowResult:
69  """Handle a flow initiated by the user."""
70  errors = {}
71  if user_input is not None:
72  host = user_input[CONF_HOST]
73  name = user_input[CONF_NAME]
74 
75  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: host})
76 
77  mac_address, error = await self._async_try_connect_async_try_connect(host)
78  if error is None:
79  await self.async_set_unique_idasync_set_unique_id(format_mac(str(mac_address)))
80  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: host})
81  return self.async_create_entryasync_create_entryasync_create_entry(
82  title=name,
83  data={CONF_HOST: host, CONF_NAME: name},
84  )
85  errors["base"] = error
86 
87  user_input = user_input or {}
88  return self.async_show_formasync_show_formasync_show_form(
89  step_id="user",
90  data_schema=vol.Schema(
91  {
92  vol.Required(
93  CONF_HOST, default=user_input.get(CONF_HOST) or ""
94  ): str,
95  vol.Optional(
96  CONF_NAME, default=user_input.get(CONF_NAME) or DEFAULT_NAME
97  ): str,
98  }
99  ),
100  errors=errors,
101  )
102 
103  async def _async_try_connect(self, host: str) -> tuple[str | None, str | None]:
104  """Try connecting to Goal Zero Yeti."""
105  try:
106  api = Yeti(host, async_get_clientsession(self.hass))
107  await api.sysinfo()
108  except exceptions.ConnectError:
109  return None, "cannot_connect"
110  except exceptions.InvalidHost:
111  return None, "invalid_host"
112  except Exception:
113  _LOGGER.exception("Unexpected exception")
114  return None, "unknown"
115  return str(api.sysdata["macAddress"]), None
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
Definition: config_flow.py:31
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:46
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:68
tuple[str|None, str|None] _async_try_connect(self, str host)
Definition: config_flow.py:103
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)
str
_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)
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)