async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up NuHeat from a config entry.""" conf = entry.data username = conf[CONF_USERNAME] password = conf[CONF_PASSWORD] serial_number = conf[CONF_SERIAL_NUMBER] api = nuheat.NuHeat(username, password) try: await hass.async_add_executor_job(api.authenticate) except requests.exceptions.Timeout: raise ConfigEntryNotReady except requests.exceptions.HTTPError as ex: if (ex.response.status_code > HTTP_BAD_REQUEST and ex.response.status_code < HTTP_INTERNAL_SERVER_ERROR): _LOGGER.error("Failed to login to nuheat: %s", ex) return False raise ConfigEntryNotReady except Exception as ex: # pylint: disable=broad-except _LOGGER.error("Failed to login to nuheat: %s", ex) return False hass.data[DOMAIN][entry.entry_id] = (api, serial_number) for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component)) return True
async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. """ api = nuheat.NuHeat(data[CONF_USERNAME], data[CONF_PASSWORD]) try: await hass.async_add_executor_job(api.authenticate) except requests.exceptions.Timeout: raise CannotConnect except requests.exceptions.HTTPError as ex: if (ex.response.status_code > HTTP_BAD_REQUEST and ex.response.status_code < HTTP_INTERNAL_SERVER_ERROR): raise InvalidAuth raise CannotConnect # # The underlying module throws a generic exception on login failure # except Exception: # pylint: disable=broad-except raise InvalidAuth try: thermostat = await hass.async_add_executor_job( api.get_thermostat, data[CONF_SERIAL_NUMBER]) except requests.exceptions.HTTPError: raise InvalidThermostat return { "title": thermostat.room, "serial_number": thermostat.serial_number }
def setup(hass, config): """Set up the NuHeat thermostat component.""" conf = config[DOMAIN] username = conf.get(CONF_USERNAME) password = conf.get(CONF_PASSWORD) devices = conf.get(CONF_DEVICES) api = nuheat.NuHeat(username, password) api.authenticate() hass.data[DOMAIN] = (api, devices) discovery.load_platform(hass, "climate", DOMAIN, {}, config) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up NuHeat from a config entry.""" conf = entry.data username = conf[CONF_USERNAME] password = conf[CONF_PASSWORD] serial_number = conf[CONF_SERIAL_NUMBER] api = nuheat.NuHeat(username, password) try: thermostat = await hass.async_add_executor_job( _get_thermostat, api, serial_number ) except requests.exceptions.Timeout as ex: raise ConfigEntryNotReady from ex except requests.exceptions.HTTPError as ex: if ( ex.response.status_code > HTTPStatus.BAD_REQUEST and ex.response.status_code < HTTPStatus.INTERNAL_SERVER_ERROR ): _LOGGER.error("Failed to login to nuheat: %s", ex) return False raise ConfigEntryNotReady from ex except Exception as ex: # pylint: disable=broad-except _LOGGER.error("Failed to login to nuheat: %s", ex) return False async def _async_update_data(): """Fetch data from API endpoint.""" await hass.async_add_executor_job(thermostat.get_data) coordinator = DataUpdateCoordinator( hass, _LOGGER, name=f"nuheat {serial_number}", update_method=_async_update_data, update_interval=timedelta(minutes=5), ) hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = (thermostat, coordinator) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True