Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Honeywell (US) Total Connect Comfort climate systems."""
2 
3 from dataclasses import dataclass
4 
5 from aiohttp.client_exceptions import ClientConnectionError
6 import aiosomecomfort
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13  async_create_clientsession,
14  async_get_clientsession,
15 )
16 
17 from .const import (
18  _LOGGER,
19  CONF_COOL_AWAY_TEMPERATURE,
20  CONF_HEAT_AWAY_TEMPERATURE,
21  DOMAIN,
22 )
23 
24 UPDATE_LOOP_SLEEP_TIME = 5
25 PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH]
26 
27 MIGRATE_OPTIONS_KEYS = {CONF_COOL_AWAY_TEMPERATURE, CONF_HEAT_AWAY_TEMPERATURE}
28 
29 
30 @callback
32  hass: HomeAssistant, config_entry: ConfigEntry
33 ) -> None:
34  if not MIGRATE_OPTIONS_KEYS.intersection(config_entry.data):
35  return
36  hass.config_entries.async_update_entry(
37  config_entry,
38  data={
39  k: v for k, v in config_entry.data.items() if k not in MIGRATE_OPTIONS_KEYS
40  },
41  options={
42  **config_entry.options,
43  **{k: config_entry.data.get(k) for k in MIGRATE_OPTIONS_KEYS},
44  },
45  )
46 
47 
48 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
49  """Set up the Honeywell thermostat."""
50  _async_migrate_data_to_options(hass, config_entry)
51 
52  username = config_entry.data[CONF_USERNAME]
53  password = config_entry.data[CONF_PASSWORD]
54 
55  if len(hass.config_entries.async_entries(DOMAIN)) > 1:
56  session = async_create_clientsession(hass)
57  else:
58  session = async_get_clientsession(hass)
59 
60  client = aiosomecomfort.AIOSomeComfort(username, password, session=session)
61  try:
62  await client.login()
63  await client.discover()
64 
65  except aiosomecomfort.device.AuthError as ex:
66  raise ConfigEntryAuthFailed("Incorrect Password") from ex
67 
68  except (
69  aiosomecomfort.device.ConnectionError,
70  aiosomecomfort.device.ConnectionTimeout,
71  aiosomecomfort.device.SomeComfortError,
72  ClientConnectionError,
73  TimeoutError,
74  ) as ex:
75  raise ConfigEntryNotReady(
76  "Failed to initialize the Honeywell client: Connection error"
77  ) from ex
78 
79  devices = {}
80  for location in client.locations_by_id.values():
81  for device in location.devices_by_id.values():
82  devices[device.deviceid] = device
83 
84  if len(devices) == 0:
85  _LOGGER.debug("No devices found")
86  return False
87  data = HoneywellData(config_entry.entry_id, client, devices)
88  hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data
89  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
90 
91  config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
92 
93  return True
94 
95 
96 async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
97  """Update listener."""
98  await hass.config_entries.async_reload(config_entry.entry_id)
99 
100 
101 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
102  """Unload the config and platforms."""
103  unload_ok = await hass.config_entries.async_unload_platforms(
104  config_entry, PLATFORMS
105  )
106  if unload_ok:
107  hass.data[DOMAIN].pop(config_entry.entry_id)
108  return unload_ok
109 
110 
111 @dataclass
113  """Shared data for Honeywell."""
114 
115  entry_id: str
116  client: aiosomecomfort.AIOSomeComfort
117  devices: dict[str, aiosomecomfort.device.Device]
None _async_migrate_data_to_options(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:33
None update_listener(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:96
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:101
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:48
aiohttp.ClientSession async_create_clientsession()
Definition: coordinator.py:51
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)