Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure Coolmaster."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pycoolmasternet_async import CoolMasterNet
8 import voluptuous as vol
9 
10 from homeassistant.components.climate import HVACMode
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_PORT
13 from homeassistant.core import callback
14 
15 from .const import CONF_SUPPORTED_MODES, CONF_SWING_SUPPORT, DEFAULT_PORT, DOMAIN
16 
17 AVAILABLE_MODES = [
18  HVACMode.OFF.value,
19  HVACMode.HEAT.value,
20  HVACMode.COOL.value,
21  HVACMode.DRY.value,
22  HVACMode.HEAT_COOL.value,
23  HVACMode.FAN_ONLY.value,
24 ]
25 
26 MODES_SCHEMA = {vol.Required(mode, default=True): bool for mode in AVAILABLE_MODES}
27 
28 DATA_SCHEMA = vol.Schema(
29  {
30  vol.Required(CONF_HOST): str,
31  **MODES_SCHEMA,
32  vol.Required(CONF_SWING_SUPPORT, default=False): bool,
33  }
34 )
35 
36 
37 async def _validate_connection(host: str) -> bool:
38  cool = CoolMasterNet(host, DEFAULT_PORT)
39  units = await cool.status()
40  return bool(units)
41 
42 
43 class CoolmasterConfigFlow(ConfigFlow, domain=DOMAIN):
44  """Handle a Coolmaster config flow."""
45 
46  VERSION = 1
47 
48  @callback
49  def _async_get_entry(self, data: dict[str, Any]) -> ConfigFlowResult:
50  supported_modes = [
51  key for (key, value) in data.items() if key in AVAILABLE_MODES and value
52  ]
53  return self.async_create_entryasync_create_entryasync_create_entry(
54  title=data[CONF_HOST],
55  data={
56  CONF_HOST: data[CONF_HOST],
57  CONF_PORT: DEFAULT_PORT,
58  CONF_SUPPORTED_MODES: supported_modes,
59  CONF_SWING_SUPPORT: data[CONF_SWING_SUPPORT],
60  },
61  )
62 
63  async def async_step_user(
64  self, user_input: dict[str, Any] | None = None
65  ) -> ConfigFlowResult:
66  """Handle a flow initialized by the user."""
67  if user_input is None:
68  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=DATA_SCHEMA)
69 
70  errors = {}
71 
72  host = user_input[CONF_HOST]
73 
74  try:
75  result = await _validate_connection(host)
76  if not result:
77  errors["base"] = "no_units"
78  except OSError:
79  errors["base"] = "cannot_connect"
80 
81  if errors:
82  return self.async_show_formasync_show_formasync_show_form(
83  step_id="user", data_schema=DATA_SCHEMA, errors=errors
84  )
85 
86  return self._async_get_entry_async_get_entry(user_input)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:65
ConfigFlowResult _async_get_entry(self, dict[str, Any] data)
Definition: config_flow.py:49
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)