Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Minecraft Server integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_TYPE
12 
13 from .api import MinecraftServer, MinecraftServerAddressError, MinecraftServerType
14 from .const import DEFAULT_NAME, DOMAIN
15 
16 DEFAULT_ADDRESS = "localhost:25565"
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class MinecraftServerConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Handle a config flow for Minecraft Server."""
23 
24  VERSION = 3
25 
26  async def async_step_user(
27  self, user_input: dict[str, Any] | None = None
28  ) -> ConfigFlowResult:
29  """Handle the initial step."""
30  errors: dict[str, str] = {}
31 
32  if user_input:
33  address = user_input[CONF_ADDRESS]
34 
35  # Abort config flow if service is already configured.
36  self._async_abort_entries_match_async_abort_entries_match({CONF_ADDRESS: address})
37 
38  # Prepare config entry data.
39  config_data = {
40  CONF_NAME: user_input[CONF_NAME],
41  CONF_ADDRESS: address,
42  }
43 
44  # Some Bedrock Edition servers mimic a Java Edition server, therefore check for a Bedrock Edition server first.
45  for server_type in MinecraftServerType:
46  api = MinecraftServer(self.hass, server_type, address)
47 
48  try:
49  await api.async_initialize()
50  except MinecraftServerAddressError as error:
51  _LOGGER.debug(
52  "Initialization of %s server failed: %s",
53  server_type,
54  error,
55  )
56  else:
57  if await api.async_is_online():
58  config_data[CONF_TYPE] = server_type
59  return self.async_create_entryasync_create_entryasync_create_entry(title=address, data=config_data)
60 
61  # Host or port invalid or server not reachable.
62  errors["base"] = "cannot_connect"
63 
64  # Show configuration form (default form in case of no user_input,
65  # form filled with user_input and eventually with errors otherwise).
66  return self._show_config_form_show_config_form(user_input, errors)
67 
69  self,
70  user_input: dict[str, Any] | None = None,
71  errors: dict[str, str] | None = None,
72  ) -> ConfigFlowResult:
73  """Show the setup form to the user."""
74  if user_input is None:
75  user_input = {}
76 
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id="user",
79  data_schema=vol.Schema(
80  {
81  vol.Required(
82  CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
83  ): str,
84  vol.Required(
85  CONF_ADDRESS,
86  default=user_input.get(CONF_ADDRESS, DEFAULT_ADDRESS),
87  ): vol.All(str, vol.Lower),
88  }
89  ),
90  errors=errors,
91  )
ConfigFlowResult _show_config_form(self, dict[str, Any]|None user_input=None, dict[str, str]|None errors=None)
Definition: config_flow.py:72
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:28
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)