2020-03-20 02:03:51 +00:00
|
|
|
"""Support for Nexia / Trane XL Thermostats."""
|
2022-05-18 23:08:02 +00:00
|
|
|
import asyncio
|
2020-03-20 02:03:51 +00:00
|
|
|
import logging
|
|
|
|
|
2022-05-18 23:08:02 +00:00
|
|
|
import aiohttp
|
2021-05-15 14:24:36 +00:00
|
|
|
from nexia.const import BRAND_NEXIA
|
2020-03-20 02:03:51 +00:00
|
|
|
from nexia.home import NexiaHome
|
2022-06-25 16:02:38 +00:00
|
|
|
from nexia.thermostat import NexiaThermostat
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2021-01-21 18:45:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-04 14:54:27 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2020-03-20 02:03:51 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2022-06-25 16:02:38 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2022-05-18 23:08:02 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2020-03-20 02:03:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2021-10-15 03:52:26 +00:00
|
|
|
from .const import CONF_BRAND, DOMAIN, PLATFORMS
|
|
|
|
from .coordinator import NexiaDataUpdateCoordinator
|
2020-09-04 14:54:27 +00:00
|
|
|
from .util import is_invalid_auth_code
|
2020-03-20 02:03:51 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-12-21 11:46:10 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2020-03-20 02:03:51 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-20 02:03:51 +00:00
|
|
|
"""Configure the base Nexia device for Home Assistant."""
|
|
|
|
|
|
|
|
conf = entry.data
|
|
|
|
username = conf[CONF_USERNAME]
|
|
|
|
password = conf[CONF_PASSWORD]
|
2021-05-15 14:24:36 +00:00
|
|
|
brand = conf.get(CONF_BRAND, BRAND_NEXIA)
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2020-04-17 15:55:04 +00:00
|
|
|
state_file = hass.config.path(f"nexia_config_{username}.conf")
|
2022-05-18 23:08:02 +00:00
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
nexia_home = NexiaHome(
|
|
|
|
session,
|
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
device_name=hass.config.location_name,
|
|
|
|
state_file=state_file,
|
|
|
|
brand=brand,
|
|
|
|
)
|
2020-04-17 15:55:04 +00:00
|
|
|
|
2020-03-20 02:03:51 +00:00
|
|
|
try:
|
2022-05-18 23:08:02 +00:00
|
|
|
await nexia_home.login()
|
|
|
|
except asyncio.TimeoutError as ex:
|
|
|
|
raise ConfigEntryNotReady(
|
|
|
|
f"Timed out trying to connect to Nexia service: {ex}"
|
|
|
|
) from ex
|
|
|
|
except aiohttp.ClientResponseError as http_ex:
|
|
|
|
if is_invalid_auth_code(http_ex.status):
|
2020-03-20 02:03:51 +00:00
|
|
|
_LOGGER.error(
|
2020-04-08 21:20:03 +00:00
|
|
|
"Access error from Nexia service, please check credentials: %s", http_ex
|
2020-03-20 02:03:51 +00:00
|
|
|
)
|
|
|
|
return False
|
2022-05-18 23:08:02 +00:00
|
|
|
raise ConfigEntryNotReady(f"Error from Nexia service: {http_ex}") from http_ex
|
Fix not retrying on connection reset during nexia config entry setup (#93576)
* Fix not retrying on connection reset during nexia config entry setup
fixes
```
2023-05-26 00:15:39.129 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Alexander for nexia
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 558, in _request
resp = await req.send(conn)
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 670, in send
await writer.write_headers(status_line, self.headers)
File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 130, in write_headers
self._write(buf)
File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 75, in _write
raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/config_entries.py", line 387, in async_setup
result = await component.async_setup_entry(hass, self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/nexia/__init__.py", line 47, in async_setup_entry
await nexia_home.login()
File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 385, in login
request = await self.post_url(self.API_MOBILE_ACCOUNTS_SIGN_IN_URL, payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 157, in post_url
response: aiohttp.ClientResponse = await self.session.post(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 572, in _request
raise ClientOSError(*exc.args) from exc
aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport
```
* coverage
2023-05-26 01:52:44 +00:00
|
|
|
except aiohttp.ClientOSError as os_error:
|
|
|
|
raise ConfigEntryNotReady(
|
|
|
|
f"Error connecting to Nexia service: {os_error}"
|
|
|
|
) from os_error
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2021-10-15 03:52:26 +00:00
|
|
|
coordinator = NexiaDataUpdateCoordinator(hass, nexia_home)
|
2022-05-18 23:08:02 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2021-10-15 03:52:26 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
2020-03-20 02:03:51 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2020-03-20 02:03:51 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-20 02:03:51 +00:00
|
|
|
"""Unload a config entry."""
|
2022-06-25 16:02:38 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2020-03-20 02:03:51 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
return unload_ok
|
2022-06-25 16:02:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_remove_config_entry_device(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: dr.DeviceEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Remove a nexia config entry from a device."""
|
|
|
|
coordinator: NexiaDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
nexia_home: NexiaHome = coordinator.nexia_home
|
|
|
|
dev_ids = {dev_id[1] for dev_id in device_entry.identifiers if dev_id[0] == DOMAIN}
|
|
|
|
for thermostat_id in nexia_home.get_thermostat_ids():
|
|
|
|
if thermostat_id in dev_ids:
|
|
|
|
return False
|
|
|
|
thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id)
|
|
|
|
for zone_id in thermostat.get_zone_ids():
|
|
|
|
if zone_id in dev_ids:
|
|
|
|
return False
|
|
|
|
return True
|