Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Coolmaster integration."""
2 
3 from pycoolmasternet_async import CoolMasterNet
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import CONF_HOST, CONF_PORT, Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import ConfigEntryNotReady
9 
10 from .const import CONF_SWING_SUPPORT, DATA_COORDINATOR, DATA_INFO, DOMAIN
11 from .coordinator import CoolmasterDataUpdateCoordinator
12 
13 PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR]
14 
15 
16 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
17  """Set up Coolmaster from a config entry."""
18  host = entry.data[CONF_HOST]
19  port = entry.data[CONF_PORT]
20  if not entry.data.get(CONF_SWING_SUPPORT):
21  coolmaster = CoolMasterNet(
22  host,
23  port,
24  )
25  else:
26  # Swing support adds an additional request per unit. The requests are
27  # done in parallel, which can cause delays on the server. Therefore,
28  # we increase the request timeout to 5 seconds instead of 1.
29  coolmaster = CoolMasterNet(
30  host,
31  port,
32  read_timeout=5,
33  swing_support=True,
34  )
35  try:
36  info = await coolmaster.info()
37  if not info:
38  raise ConfigEntryNotReady
39  except OSError as error:
40  raise ConfigEntryNotReady from error
41  coordinator = CoolmasterDataUpdateCoordinator(hass, coolmaster)
42  hass.data.setdefault(DOMAIN, {})
43  await coordinator.async_config_entry_first_refresh()
44  hass.data[DOMAIN][entry.entry_id] = {
45  DATA_INFO: info,
46  DATA_COORDINATOR: coordinator,
47  }
48  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
49 
50  return True
51 
52 
53 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
54  """Unload a Coolmaster config entry."""
55  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
56  if unload_ok:
57  hass.data[DOMAIN].pop(entry.entry_id)
58  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:16
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:53