Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The mill component."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 
7 from mill import Mill
8 from mill_local import Mill as MillLocal
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
17 from .coordinator import MillDataUpdateCoordinator
18 
19 PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Set up the Mill heater."""
24  hass.data.setdefault(DOMAIN, {LOCAL: {}, CLOUD: {}})
25 
26  if entry.data.get(CONNECTION_TYPE) == LOCAL:
27  mill_data_connection = MillLocal(
28  entry.data[CONF_IP_ADDRESS],
29  websession=async_get_clientsession(hass),
30  )
31  update_interval = timedelta(seconds=15)
32  key = entry.data[CONF_IP_ADDRESS]
33  conn_type = LOCAL
34  else:
35  mill_data_connection = Mill(
36  entry.data[CONF_USERNAME],
37  entry.data[CONF_PASSWORD],
38  websession=async_get_clientsession(hass),
39  )
40  update_interval = timedelta(seconds=30)
41  key = entry.data[CONF_USERNAME]
42  conn_type = CLOUD
43 
44  try:
45  if not await mill_data_connection.connect():
46  raise ConfigEntryNotReady
47  except TimeoutError as error:
48  raise ConfigEntryNotReady from error
49  data_coordinator = MillDataUpdateCoordinator(
50  hass,
51  mill_data_connection=mill_data_connection,
52  update_interval=update_interval,
53  )
54 
55  await data_coordinator.async_config_entry_first_refresh()
56  hass.data[DOMAIN][conn_type][key] = data_coordinator
57 
58  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
59  return True
60 
61 
62 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
63  """Unload a config entry."""
64  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:62
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)