2019-02-14 04:35:12 +00:00
|
|
|
"""Support for NuHeat thermostats."""
|
2020-10-18 18:45:47 +00:00
|
|
|
from datetime import timedelta
|
2021-10-23 18:56:30 +00:00
|
|
|
from http import HTTPStatus
|
2017-11-11 06:22:37 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-23 15:30:38 +00:00
|
|
|
import nuheat
|
2020-03-23 05:29:45 +00:00
|
|
|
import requests
|
2017-11-11 06:22:37 +00:00
|
|
|
|
2021-01-23 05:27:32 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-23 18:56:30 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2020-03-23 05:29:45 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
2020-10-20 18:22:10 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2017-11-11 06:22:37 +00:00
|
|
|
|
2020-03-23 05:29:45 +00:00
|
|
|
from .const import CONF_SERIAL_NUMBER, DOMAIN, PLATFORMS
|
2017-11-11 06:22:37 +00:00
|
|
|
|
2020-03-23 05:29:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-01-23 05:27:32 +00:00
|
|
|
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
2017-11-11 06:22:37 +00:00
|
|
|
|
2017-11-11 22:47:12 +00:00
|
|
|
|
2020-10-18 18:45:47 +00:00
|
|
|
def _get_thermostat(api, serial_number):
|
|
|
|
"""Authenticate and create the thermostat object."""
|
|
|
|
api.authenticate()
|
|
|
|
return api.get_thermostat(serial_number)
|
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-23 05:29:45 +00:00
|
|
|
"""Set up NuHeat from a config entry."""
|
|
|
|
|
|
|
|
conf = entry.data
|
|
|
|
|
|
|
|
username = conf[CONF_USERNAME]
|
|
|
|
password = conf[CONF_PASSWORD]
|
|
|
|
serial_number = conf[CONF_SERIAL_NUMBER]
|
2017-11-14 04:40:18 +00:00
|
|
|
|
2017-11-11 06:22:37 +00:00
|
|
|
api = nuheat.NuHeat(username, password)
|
|
|
|
|
2020-03-23 05:29:45 +00:00
|
|
|
try:
|
2020-10-18 18:45:47 +00:00
|
|
|
thermostat = await hass.async_add_executor_job(
|
|
|
|
_get_thermostat, api, serial_number
|
|
|
|
)
|
2020-08-28 11:50:32 +00:00
|
|
|
except requests.exceptions.Timeout as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
2020-03-23 05:29:45 +00:00
|
|
|
except requests.exceptions.HTTPError as ex:
|
2020-04-08 21:20:03 +00:00
|
|
|
if (
|
2021-10-23 18:56:30 +00:00
|
|
|
ex.response.status_code > HTTPStatus.BAD_REQUEST
|
|
|
|
and ex.response.status_code < HTTPStatus.INTERNAL_SERVER_ERROR
|
2020-04-08 21:20:03 +00:00
|
|
|
):
|
2020-03-23 05:29:45 +00:00
|
|
|
_LOGGER.error("Failed to login to nuheat: %s", ex)
|
|
|
|
return False
|
2020-08-28 11:50:32 +00:00
|
|
|
raise ConfigEntryNotReady from ex
|
2020-03-23 05:29:45 +00:00
|
|
|
except Exception as ex: # pylint: disable=broad-except
|
|
|
|
_LOGGER.error("Failed to login to nuheat: %s", ex)
|
|
|
|
return False
|
|
|
|
|
2020-10-18 18:45:47 +00:00
|
|
|
async def _async_update_data():
|
|
|
|
"""Fetch data from API endpoint."""
|
2020-10-20 18:22:10 +00:00
|
|
|
await hass.async_add_executor_job(thermostat.get_data)
|
2020-10-18 18:45:47 +00:00
|
|
|
|
|
|
|
coordinator = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=f"nuheat {serial_number}",
|
|
|
|
update_method=_async_update_data,
|
|
|
|
update_interval=timedelta(minutes=5),
|
|
|
|
)
|
|
|
|
|
2021-04-16 16:23:27 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-10-18 18:45:47 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = (thermostat, coordinator)
|
2020-03-23 05:29:45 +00:00
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-03-23 05:29:45 +00:00
|
|
|
|
2017-11-11 06:22:37 +00:00
|
|
|
return True
|
2020-03-23 05:29:45 +00:00
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-03-23 05:29:45 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 18:42:21 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-03-23 05:29:45 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|