Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Starlink."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from starlink_grpc import ChannelContext, GrpcError, get_id
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_IP_ADDRESS
12 
13 from .const import DOMAIN
14 
15 CONFIG_SCHEMA = vol.Schema(
16  {vol.Required(CONF_IP_ADDRESS, default="192.168.100.1:9200"): str}
17 )
18 
19 
20 class StarlinkConfigFlow(ConfigFlow, domain=DOMAIN):
21  """The configuration flow for a Starlink system."""
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Ask the user for a server address and a name for the system."""
27  errors = {}
28  if user_input:
29  # Input validation. If everything looks good, create the entry
30  if uid := await self.get_device_id(url=user_input[CONF_IP_ADDRESS]):
31  # Make sure we're not configuring the same device
32  await self.async_set_unique_idasync_set_unique_id(uid)
33  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
34 
35  return self.async_create_entryasync_create_entryasync_create_entry(
36  title="Starlink",
37  data=user_input,
38  )
39  errors[CONF_IP_ADDRESS] = "cannot_connect"
40  return self.async_show_formasync_show_formasync_show_form(
41  step_id="user", data_schema=CONFIG_SCHEMA, errors=errors
42  )
43 
44  async def get_device_id(self, url: str) -> str | None:
45  """Get the device UID, or None if no device exists at the given URL."""
46  context = ChannelContext(target=url)
47  response: str | None
48  try:
49  response = await self.hass.async_add_executor_job(get_id, context)
50  except GrpcError:
51  response = None
52  context.close()
53  return response
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_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)
str get_device_id(ServerInfoMessage server_info, MatterEndpoint endpoint)
Definition: helpers.py:59