Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for WeatherFlow."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from asyncio import Future
7 from asyncio.exceptions import CancelledError
8 from typing import Any
9 
10 from pyweatherflowudp.client import EVENT_DEVICE_DISCOVERED, WeatherFlowListener
11 from pyweatherflowudp.errors import AddressInUseError, EndpointError, ListenerError
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.core import callback
15 
16 from .const import (
17  DOMAIN,
18  ERROR_MSG_ADDRESS_IN_USE,
19  ERROR_MSG_CANNOT_CONNECT,
20  ERROR_MSG_NO_DEVICE_FOUND,
21 )
22 
23 
24 async def _async_can_discover_devices() -> bool:
25  """Return if there are devices that can be discovered."""
26  future_event: Future[None] = asyncio.get_running_loop().create_future()
27 
28  @callback
29  def _async_found(_):
30  """Handle a discovered device - only need to do this once so."""
31 
32  if not future_event.done():
33  future_event.set_result(None)
34 
35  async with WeatherFlowListener() as client, asyncio.timeout(10):
36  try:
37  client.on(EVENT_DEVICE_DISCOVERED, _async_found)
38  await future_event
39  except TimeoutError:
40  return False
41 
42  return True
43 
44 
45 class WeatherFlowConfigFlow(ConfigFlow, domain=DOMAIN):
46  """Handle a config flow for WeatherFlow."""
47 
48  VERSION = 1
49 
50  async def async_step_user(
51  self, user_input: dict[str, Any] | None = None
52  ) -> ConfigFlowResult:
53  """Handle a flow initialized by the user."""
54 
55  # Only allow a single instance of integration since the listener
56  # will pick up all devices on the network and we don't want to
57  # create multiple entries.
58  if self._async_current_entries_async_current_entries():
59  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
60  found = False
61  errors = {}
62  try:
63  found = await _async_can_discover_devices()
64  except AddressInUseError:
65  errors["base"] = ERROR_MSG_ADDRESS_IN_USE
66  except (ListenerError, EndpointError, CancelledError):
67  errors["base"] = ERROR_MSG_CANNOT_CONNECT
68 
69  if not found and not errors:
70  errors["base"] = ERROR_MSG_NO_DEVICE_FOUND
71 
72  if errors:
73  return self.async_show_formasync_show_formasync_show_form(step_id="user", errors=errors)
74 
75  return self.async_create_entryasync_create_entryasync_create_entry(title="WeatherFlow", data={})
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:52
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)