def setup(hass, config) -> bool: """Set up the SmartHab platform.""" sh_conf = config.get(DOMAIN) # Assign configuration variables username = sh_conf[CONF_EMAIL] password = sh_conf[CONF_PASSWORD] # Setup connection with SmartHab API hub = pysmarthab.SmartHab() try: hub.login(username, password) except pysmarthab.RequestFailedException as ex: _LOGGER.error("Error while trying to reach SmartHab API.") _LOGGER.debug(ex, exc_info=True) return False # Verify that passed in configuration works if not hub.is_logged_in(): _LOGGER.error("Could not authenticate with SmartHab API") return False # Pass hub object to child platforms hass.data[DOMAIN] = {DATA_HUB: hub} load_platform(hass, "light", DOMAIN, None, config) load_platform(hass, "cover", DOMAIN, None, config) return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Set up config entry for SmartHab integration.""" # Assign configuration variables username = entry.data[CONF_EMAIL] password = entry.data[CONF_PASSWORD] # Setup connection with SmartHab API hub = pysmarthab.SmartHab() try: await hub.async_login(username, password) except pysmarthab.RequestFailedException as err: _LOGGER.exception("Error while trying to reach SmartHab API") raise ConfigEntryNotReady from err # Pass hub object to child platforms hass.data[DOMAIN][entry.entry_id] = {DATA_HUB: hub} for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform) ) return True
async def async_setup_entry(opp: OpenPeerPower, entry: ConfigEntry): """Set up config entry for SmartHab integration.""" # Assign configuration variables username = entry.data[CONF_EMAIL] password = entry.data[CONF_PASSWORD] # Setup connection with SmartHab API hub = pysmarthab.SmartHab() try: await hub.async_login(username, password) except pysmarthab.RequestFailedException as err: _LOGGER.exception("Error while trying to reach SmartHab API") raise ConfigEntryNotReady from err # Pass hub object to child platforms opp.data[DOMAIN][entry.entry_id] = {DATA_HUB: hub} opp.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def async_step_user(self, user_input=None): """Handle a flow initiated by the user.""" errors = {} if user_input is None: return self._show_setup_form(user_input, None) username = user_input[CONF_EMAIL] password = user_input[CONF_PASSWORD] # Check if already configured if self.unique_id is None: await self.async_set_unique_id(username) self._abort_if_unique_id_configured() # Setup connection with SmartHab API hub = pysmarthab.SmartHab() try: await hub.async_login(username, password) # Verify that passed in configuration works if hub.is_logged_in(): return self.async_create_entry(title=username, data={ CONF_EMAIL: username, CONF_PASSWORD: password }) errors["base"] = "wrong_login" except pysmarthab.RequestFailedException: _LOGGER.exception("Error while trying to reach SmartHab API") errors["base"] = "service" except Exception: # pylint: disable=broad-except _LOGGER.exception("Unexpected error during login") errors["base"] = "unknown_error" return self._show_setup_form(user_input, errors)