Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for FiveM integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from fivem import FiveM, FiveMServerOfflineError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_PORT
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 DEFAULT_PORT = 30120
20 
21 STEP_USER_DATA_SCHEMA = vol.Schema(
22  {
23  vol.Required(CONF_HOST): cv.string,
24  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
25  }
26 )
27 
28 
29 async def validate_input(data: dict[str, Any]) -> None:
30  """Validate the user input allows us to connect."""
31 
32  fivem = FiveM(data[CONF_HOST], data[CONF_PORT])
33  info = await fivem.get_info_raw()
34 
35  game_name = info.get("vars")["gamename"]
36  if game_name is None or game_name != "gta5":
37  raise InvalidGameNameError
38 
39 
40 class FiveMConfigFlow(ConfigFlow, domain=DOMAIN):
41  """Handle a config flow for FiveM."""
42 
43  VERSION = 1
44 
45  async def async_step_user(
46  self, user_input: dict[str, Any] | None = None
47  ) -> ConfigFlowResult:
48  """Handle the initial step."""
49  if user_input is None:
50  return self.async_show_formasync_show_formasync_show_form(
51  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
52  )
53 
54  errors = {}
55 
56  try:
57  await validate_input(user_input)
58  except FiveMServerOfflineError:
59  errors["base"] = "cannot_connect"
60  except InvalidGameNameError:
61  errors["base"] = "invalid_game_name"
62  except Exception:
63  _LOGGER.exception("Unexpected exception")
64  errors["base"] = "unknown"
65  else:
66  self._async_abort_entries_match_async_abort_entries_match(user_input)
67  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_HOST], data=user_input)
68 
69  return self.async_show_formasync_show_formasync_show_form(
70  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
71  )
72 
73 
74 class InvalidGameNameError(Exception):
75  """Handle errors in the game name from the api."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:47
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)
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)
None validate_input(dict[str, Any] data)
Definition: config_flow.py:29