Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for GPSD integration."""
2 
3 from __future__ import annotations
4 
5 import socket
6 from typing import Any
7 
8 from gps3.agps3threaded import GPSD_PORT as DEFAULT_PORT, HOST as DEFAULT_HOST
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
13 from homeassistant.helpers import config_validation as cv
14 
15 from .const import DOMAIN
16 
17 STEP_USER_DATA_SCHEMA = vol.Schema(
18  {
19  vol.Optional(CONF_HOST, default=DEFAULT_HOST): str,
20  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
21  }
22 )
23 
24 
25 class GPSDConfigFlow(ConfigFlow, domain=DOMAIN):
26  """Handle a config flow for GPSD."""
27 
28  VERSION = 1
29 
30  @staticmethod
31  def test_connection(host: str, port: int) -> bool:
32  """Test socket connection."""
33  try:
34  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
35  sock.connect((host, port))
36  sock.shutdown(2)
37  except OSError:
38  return False
39  else:
40  return True
41 
42  async def async_step_user(
43  self, user_input: dict[str, Any] | None = None
44  ) -> ConfigFlowResult:
45  """Handle the initial step."""
46  if user_input is not None:
47  self._async_abort_entries_match_async_abort_entries_match(user_input)
48 
49  connected = await self.hass.async_add_executor_job(
50  self.test_connectiontest_connection, user_input[CONF_HOST], user_input[CONF_PORT]
51  )
52 
53  if not connected:
54  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
55 
56  port = ""
57  if user_input[CONF_PORT] != DEFAULT_PORT:
58  port = f":{user_input[CONF_PORT]}"
59 
60  return self.async_create_entryasync_create_entryasync_create_entry(
61  title=user_input.get(CONF_NAME, f"GPS {user_input[CONF_HOST]}{port}"),
62  data=user_input,
63  )
64 
65  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=STEP_USER_DATA_SCHEMA)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:44
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)
_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)