async def async_step_user(self, user_input: Optional[ConfigType] = None): """Handle a flow start.""" # Supporting a single account. entries = self.hass.config_entries.async_entries(DOMAIN) if entries: return self.async_abort(reason="already_setup") errors = {} if user_input is not None: username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] try: aqualink = AqualinkClient(username, password) await aqualink.login() return self.async_create_entry(title=username, data=user_input) except AqualinkLoginException: errors["base"] = "connection_failure" return self.async_show_form( step_id="user", data_schema=vol.Schema({ vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str }), errors=errors, )
async def async_step_user(self, user_input: dict[str, Any] | None = None): """Handle a flow start.""" # Supporting a single account. entries = self._async_current_entries() if entries: return self.async_abort(reason="single_instance_allowed") errors = {} if user_input is not None: username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] try: aqualink = AqualinkClient(username, password) await aqualink.login() return self.async_create_entry(title=username, data=user_input) except AqualinkLoginException: errors["base"] = "cannot_connect" return self.async_show_form( step_id="user", data_schema=vol.Schema( {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} ), errors=errors, )
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None: """Set up Aqualink from a config entry.""" username = entry.data[CONF_USERNAME] password = entry.data[CONF_PASSWORD] # These will contain the initialized devices climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = [] lights = hass.data[DOMAIN][LIGHT_DOMAIN] = [] sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = [] switches = hass.data[DOMAIN][SWITCH_DOMAIN] = [] session = async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True)) aqualink = AqualinkClient(username, password, session) try: await aqualink.login() except AqualinkLoginException as login_exception: _LOGGER.error("Exception raised while attempting to login: %s", login_exception) return False systems = await aqualink.get_systems() systems = list(systems.values()) if not systems: _LOGGER.error("No systems detected or supported") return False # Only supporting the first system for now. devices = await systems[0].get_devices() for dev in devices.values(): if isinstance(dev, AqualinkThermostat): climates += [dev] elif isinstance(dev, AqualinkLight): lights += [dev] elif isinstance(dev, AqualinkSensor): sensors += [dev] elif isinstance(dev, AqualinkToggle): switches += [dev] forward_setup = hass.config_entries.async_forward_entry_setup if climates: _LOGGER.debug("Got %s climates: %s", len(climates), climates) hass.async_create_task(forward_setup(entry, CLIMATE_DOMAIN)) if lights: _LOGGER.debug("Got %s lights: %s", len(lights), lights) hass.async_create_task(forward_setup(entry, LIGHT_DOMAIN)) if sensors: _LOGGER.debug("Got %s sensors: %s", len(sensors), sensors) hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN)) if switches: _LOGGER.debug("Got %s switches: %s", len(switches), switches) hass.async_create_task(forward_setup(entry, SWITCH_DOMAIN)) async def _async_systems_update(now): """Refresh internal state for all systems.""" await systems[0].update() async_dispatcher_send(hass, DOMAIN) async_track_time_interval(hass, _async_systems_update, UPDATE_INTERVAL) return True
async def async_step_user(self, user_input: Optional[ConfigType] = None): """Handle a flow start.""" from iaqualink import AqualinkClient errors = {} if user_input is not None: # Supporting a single account. entries = self.hass.config_entries.async_entries(DOMAIN) if entries: return self.async_abort(reason='already_setup') username = user_input[CONF_USERNAME] password = user_input[CONF_PASSWORD] try: aqualink = AqualinkClient(username, password) await aqualink.login() return await self.async_step_import(user_input) except Exception: errors['base'] = 'connection_failure' return self.async_show_form( step_id='user', data_schema=vol.Schema({ vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str, }), errors=errors, )
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: """Set up Aqualink from a config entry.""" username = entry.data[CONF_USERNAME] password = entry.data[CONF_PASSWORD] # These will contain the initialized devices binary_sensors = hass.data[DOMAIN][BINARY_SENSOR_DOMAIN] = [] climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = [] lights = hass.data[DOMAIN][LIGHT_DOMAIN] = [] sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = [] switches = hass.data[DOMAIN][SWITCH_DOMAIN] = [] session = async_get_clientsession(hass) aqualink = AqualinkClient(username, password, session) try: await aqualink.login() except AqualinkLoginException as login_exception: _LOGGER.error("Failed to login: %s", login_exception) return False except ( asyncio.TimeoutError, aiohttp.client_exceptions.ClientConnectorError, ) as aio_exception: _LOGGER.warning("Exception raised while attempting to login: %s", aio_exception) raise ConfigEntryNotReady from aio_exception systems = await aqualink.get_systems() systems = list(systems.values()) if not systems: _LOGGER.error("No systems detected or supported") return False # Only supporting the first system for now. devices = await systems[0].get_devices() for dev in devices.values(): if isinstance(dev, AqualinkThermostat): climates += [dev] elif isinstance(dev, AqualinkLight): lights += [dev] elif isinstance(dev, AqualinkBinarySensor): binary_sensors += [dev] elif isinstance(dev, AqualinkSensor): sensors += [dev] elif isinstance(dev, AqualinkToggle): switches += [dev] forward_setup = hass.config_entries.async_forward_entry_setup if binary_sensors: _LOGGER.debug("Got %s binary sensors: %s", len(binary_sensors), binary_sensors) hass.async_create_task(forward_setup(entry, BINARY_SENSOR_DOMAIN)) if climates: _LOGGER.debug("Got %s climates: %s", len(climates), climates) hass.async_create_task(forward_setup(entry, CLIMATE_DOMAIN)) if lights: _LOGGER.debug("Got %s lights: %s", len(lights), lights) hass.async_create_task(forward_setup(entry, LIGHT_DOMAIN)) if sensors: _LOGGER.debug("Got %s sensors: %s", len(sensors), sensors) hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN)) if switches: _LOGGER.debug("Got %s switches: %s", len(switches), switches) hass.async_create_task(forward_setup(entry, SWITCH_DOMAIN)) async def _async_systems_update(now): """Refresh internal state for all systems.""" prev = systems[0].last_run_success await systems[0].update() success = systems[0].last_run_success if not success and prev: _LOGGER.warning("Failed to refresh iAqualink state") elif success and not prev: _LOGGER.warning("Reconnected to iAqualink") async_dispatcher_send(hass, DOMAIN) async_track_time_interval(hass, _async_systems_update, UPDATE_INTERVAL) return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None: """Set up Aqualink from a config entry.""" from iaqualink import (AqualinkClient, AqualinkLight, AqualinkSensor, AqualinkSystem, AqualinkToggle, AqualinkThermostat) username = entry.data[CONF_USERNAME] password = entry.data[CONF_PASSWORD] if DOMAIN not in hass.data: hass.data[DOMAIN] = {} # These will contain the initialized devices climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = [] lights = hass.data[DOMAIN][LIGHT_DOMAIN] = [] sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = [] switches = hass.data[DOMAIN][SWITCH_DOMAIN] = [] session = async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True)) aqualink = AqualinkClient(username, password, session) try: await aqualink.login() except Exception as e: _LOGGER.error(f'Exception raised while attempting to login: {e}') return False systems = await aqualink.get_systems() systems = list(systems.values()) if len(systems) == 0: _LOGGER.error("No systems detected or supported.") return False # Only supporting the first system for now. devices = await systems[0].get_devices() for dev in devices.values(): if isinstance(dev, AqualinkSensor): sensors += [dev] elif isinstance(dev, AqualinkLight): lights += [dev] elif isinstance(dev, AqualinkToggle): switches += [dev] elif isinstance(dev, AqualinkThermostat): climates += [dev] forward_setup = hass.config_entries.async_forward_entry_setup if climates: _LOGGER.debug("Got %s climates: %s", len(climates), climates) hass.async_create_task(forward_setup(entry, CLIMATE_DOMAIN)) if lights: _LOGGER.debug("Got %s lights: %s", len(lights), lights) hass.async_create_task(forward_setup(entry, LIGHT_DOMAIN)) if sensors: _LOGGER.debug("Got %s sensors: %s", len(sensors), sensors) hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN)) if switches: _LOGGER.debug("Got %s switches: %s", len(switches), switches) hass.async_create_task(forward_setup(entry, SWITCH_DOMAIN)) return True