Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Nexia / Trane XL Thermostats."""
2 
3 import logging
4 
5 import aiohttp
6 from nexia.const import BRAND_NEXIA
7 from nexia.home import NexiaHome
8 from nexia.thermostat import NexiaThermostat
9 
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers import device_registry as dr
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import CONF_BRAND, DOMAIN, PLATFORMS
17 from .coordinator import NexiaDataUpdateCoordinator
18 from .types import NexiaConfigEntry
19 from .util import is_invalid_auth_code
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: NexiaConfigEntry) -> bool:
25  """Configure the base Nexia device for Home Assistant."""
26 
27  conf = entry.data
28  username = conf[CONF_USERNAME]
29  password = conf[CONF_PASSWORD]
30  brand = conf.get(CONF_BRAND, BRAND_NEXIA)
31 
32  state_file = hass.config.path(f"nexia_config_{username}.conf")
33  session = async_get_clientsession(hass)
34  nexia_home = NexiaHome(
35  session,
36  username=username,
37  password=password,
38  device_name=hass.config.location_name,
39  state_file=state_file,
40  brand=brand,
41  )
42 
43  try:
44  await nexia_home.login()
45  except TimeoutError as ex:
46  raise ConfigEntryNotReady(
47  f"Timed out trying to connect to Nexia service: {ex}"
48  ) from ex
49  except aiohttp.ClientResponseError as http_ex:
50  if is_invalid_auth_code(http_ex.status):
51  _LOGGER.error(
52  "Access error from Nexia service, please check credentials: %s", http_ex
53  )
54  return False
55  raise ConfigEntryNotReady(f"Error from Nexia service: {http_ex}") from http_ex
56  except aiohttp.ClientOSError as os_error:
57  raise ConfigEntryNotReady(
58  f"Error connecting to Nexia service: {os_error}"
59  ) from os_error
60 
61  coordinator = NexiaDataUpdateCoordinator(hass, nexia_home)
62  await coordinator.async_config_entry_first_refresh()
63  entry.runtime_data = coordinator
64  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
65 
66  return True
67 
68 
69 async def async_unload_entry(hass: HomeAssistant, entry: NexiaConfigEntry) -> bool:
70  """Unload a config entry."""
71  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
72 
73 
75  hass: HomeAssistant, entry: NexiaConfigEntry, device_entry: dr.DeviceEntry
76 ) -> bool:
77  """Remove a nexia config entry from a device."""
78  coordinator = entry.runtime_data
79  nexia_home = coordinator.nexia_home
80  dev_ids = {dev_id[1] for dev_id in device_entry.identifiers if dev_id[0] == DOMAIN}
81  for thermostat_id in nexia_home.get_thermostat_ids():
82  if thermostat_id in dev_ids:
83  return False
84  thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id)
85  for zone_id in thermostat.get_zone_ids():
86  if zone_id in dev_ids:
87  return False
88  return True
89 
90 
91 async def async_migrate_entry(hass: HomeAssistant, entry: NexiaConfigEntry) -> bool:
92  """Migrate entry."""
93 
94  _LOGGER.debug("Migrating from version %s", entry.version)
95 
96  if entry.version == 1:
97  # 1 -> 2: Unique ID from integer to string
98  if entry.minor_version == 1:
99  minor_version = 2
100  hass.config_entries.async_update_entry(
101  entry, unique_id=str(entry.unique_id), minor_version=minor_version
102  )
103 
104  _LOGGER.debug("Migration successful")
105 
106  return True
def is_invalid_auth_code(http_status_code)
Definition: util.py:6
bool async_remove_config_entry_device(HomeAssistant hass, NexiaConfigEntry entry, dr.DeviceEntry device_entry)
Definition: __init__.py:76
bool async_unload_entry(HomeAssistant hass, NexiaConfigEntry entry)
Definition: __init__.py:69
bool async_migrate_entry(HomeAssistant hass, NexiaConfigEntry entry)
Definition: __init__.py:91
bool async_setup_entry(HomeAssistant hass, NexiaConfigEntry entry)
Definition: __init__.py:24
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)