async def async_step_user(self, user_input=None): """Handle the initial step.""" errors = {} if user_input is not None: name = user_input[CONF_NAME] longitude = user_input[CONF_LONGITUDE] latitude = user_input[CONF_LATITUDE] session = aiohttp_client.async_get_clientsession(self.hass) api = AuroraForecast(session=session) try: await api.get_forecast_data(longitude, latitude) except ClientError: errors["base"] = "cannot_connect" except Exception: # pylint: disable=broad-except _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: await self.async_set_unique_id( f"{user_input[CONF_LONGITUDE]}_{user_input[CONF_LATITUDE]}" ) self._abort_if_unique_id_configured() return self.async_create_entry(title=f"Aurora - {name}", data=user_input) return self.async_show_form( step_id="user", data_schema=vol.Schema({ vol.Required(CONF_NAME, default=DEFAULT_NAME): str, vol.Required( CONF_LONGITUDE, default=self.hass.config.longitude, ): vol.All( vol.Coerce(float), vol.Range(min=-180, max=180), ), vol.Required( CONF_LATITUDE, default=self.hass.config.latitude, ): vol.All( vol.Coerce(float), vol.Range(min=-90, max=90), ), }), errors=errors, )
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Aurora from a config entry.""" conf = entry.data options = entry.options session = aiohttp_client.async_get_clientsession(hass) api = AuroraForecast(session) longitude = conf[CONF_LONGITUDE] latitude = conf[CONF_LATITUDE] polling_interval = DEFAULT_POLLING_INTERVAL threshold = options.get(CONF_THRESHOLD, DEFAULT_THRESHOLD) name = conf[CONF_NAME] coordinator = AuroraDataUpdateCoordinator( hass=hass, name=name, polling_interval=polling_interval, api=api, latitude=latitude, longitude=longitude, threshold=threshold, ) await coordinator.async_refresh() if not coordinator.last_update_success: raise ConfigEntryNotReady hass.data[DOMAIN][entry.entry_id] = { COORDINATOR: coordinator, AURORA_API: api, } for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component) ) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Aurora from a config entry.""" conf = entry.data options = entry.options session = aiohttp_client.async_get_clientsession(hass) api = AuroraForecast(session) longitude = conf[CONF_LONGITUDE] latitude = conf[CONF_LATITUDE] polling_interval = DEFAULT_POLLING_INTERVAL threshold = options.get(CONF_THRESHOLD, DEFAULT_THRESHOLD) name = conf[CONF_NAME] coordinator = AuroraDataUpdateCoordinator( hass=hass, name=name, polling_interval=polling_interval, api=api, latitude=latitude, longitude=longitude, threshold=threshold, ) await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = { COORDINATOR: coordinator, AURORA_API: api, } hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True