async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Nightscout from a config entry.""" server_url = entry.data[CONF_URL] session = async_get_clientsession(hass) api = NightscoutAPI(server_url, session=session) try: status = await api.get_server_status() except (ClientError, AsyncIOTimeoutError, OSError) as error: raise ConfigEntryNotReady from error hass.data[DOMAIN][entry.entry_id] = api device_registry = await dr.async_get_registry(hass) device_registry.async_get_or_create( config_entry_id=entry.entry_id, identifiers={(DOMAIN, server_url)}, manufacturer="Nightscout Foundation", name=status.name, sw_version=status.version, entry_type="service", ) 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 Nightscout from a config entry.""" server_url = entry.data[CONF_URL] api_key = entry.data.get(CONF_API_KEY) session = async_get_clientsession(hass) api = NightscoutAPI(server_url, session=session, api_secret=api_key) try: status = await api.get_server_status() except (ClientError, AsyncIOTimeoutError, OSError) as error: raise ConfigEntryNotReady from error hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = api device_registry = dr.async_get(hass) device_registry.async_get_or_create( config_entry_id=entry.entry_id, identifiers={(DOMAIN, server_url)}, manufacturer="Nightscout Foundation", name=status.name, sw_version=status.version, entry_type=dr.DeviceEntryType.SERVICE, ) hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def _validate_input(data): """Validate the user input allows us to connect.""" url = data[CONF_URL] try: api = NightscoutAPI(url) status = await api.get_server_status() except (ClientError, AsyncIOTimeoutError, OSError) as error: raise InputValidationError("cannot_connect") from error # Return info to be stored in the config entry. return {"title": status.name}
async def _validate_input(data): """Validate the user input allows us to connect.""" url = data[CONF_URL] api_key = data.get(CONF_API_KEY) try: api = NightscoutAPI(url, api_secret=api_key) status = await api.get_server_status() if status.settings.get("authDefaultRoles") == "status-only": await api.get_sgvs() except ClientResponseError as error: raise InputValidationError("invalid_auth") from error except (ClientError, AsyncIOTimeoutError, OSError) as error: raise InputValidationError("cannot_connect") from error # Return info to be stored in the config entry. return {"title": status.name}