async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: """Set up Azure DevOps from a config entry.""" client = DevOpsClient() try: if entry.data[CONF_PAT] is not None: await client.authorize(entry.data[CONF_PAT], entry.data[CONF_ORG]) if not client.authorized: _LOGGER.warning( "Could not authorize with Azure DevOps. You may need to update your token" ) hass.async_create_task( hass.config_entries.flow.async_init( DOMAIN, context={"source": "reauth"}, data=entry.data, )) return False await client.get_project(entry.data[CONF_ORG], entry.data[CONF_PROJECT]) except aiohttp.ClientError as exception: _LOGGER.warning(exception) raise ConfigEntryNotReady from exception instance_key = f"{DOMAIN}_{entry.data[CONF_ORG]}_{entry.data[CONF_PROJECT]}" hass.data.setdefault(instance_key, {})[DATA_AZURE_DEVOPS_CLIENT] = client # Setup components hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, "sensor")) return True
async def handle(organization: str, project: str, pat: str = None) -> None: client = DevOpsClient() if pat is not None: await client.authorize(pat, organization) print(client.authorized) if client.authorized: print("Authorized.") else: print("Not authorized.") return print("Project:") doProject: DevOpsProject = await client.get_project(organization, project) if doProject is not None: print(doProject.id) print(doProject.name) print(doProject.description) print("Builds:") builds: List[DevOpsBuild] = await client.get_builds( organization, project, "?queryOrder=queueTimeDescending&maxBuildsPerDefinition=1", ) if builds is not None and len(builds) > 0: build: DevOpsBuild = builds[0] if build is not None: print(build.id) print(build.build_number) print(build.status) print(build.result) print(build.source_branch) print(build.source_version) print(build.definition.id) print(build.definition.name) print(build.project.id) print(build.project.name)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Azure DevOps from a config entry.""" client = DevOpsClient() if entry.data.get(CONF_PAT) is not None: await client.authorize(entry.data[CONF_PAT], entry.data[CONF_ORG]) if not client.authorized: raise ConfigEntryAuthFailed( "Could not authorize with Azure DevOps. You will need to update your token" ) project = await client.get_project( entry.data[CONF_ORG], entry.data[CONF_PROJECT], ) async def async_update_data() -> list[DevOpsBuild]: """Fetch data from Azure DevOps.""" try: return await client.get_builds( entry.data[CONF_ORG], entry.data[CONF_PROJECT], BUILDS_QUERY, ) except (aiohttp.ClientError, aiohttp.ClientError) as exception: raise UpdateFailed from exception coordinator = DataUpdateCoordinator( hass, _LOGGER, name=f"{DOMAIN}_coordinator", update_method=async_update_data, update_interval=timedelta(seconds=300), ) await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = coordinator, project await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True
async def _check_setup(self): """Check the setup of the flow.""" errors = {} client = DevOpsClient() try: if self._pat is not None: await client.authorize(self._pat, self._organization) if not client.authorized: errors["base"] = "invalid_auth" return errors project_info = await client.get_project(self._organization, self._project) if project_info is None: errors["base"] = "project_error" return errors except aiohttp.ClientError: errors["base"] = "cannot_connect" return errors return None
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Azure DevOps from a config entry.""" client = DevOpsClient() try: if entry.data[CONF_PAT] is not None: await client.authorize(entry.data[CONF_PAT], entry.data[CONF_ORG]) if not client.authorized: raise ConfigEntryAuthFailed( "Could not authorize with Azure DevOps. You may need to update your token" ) await client.get_project(entry.data[CONF_ORG], entry.data[CONF_PROJECT]) except aiohttp.ClientError as exception: _LOGGER.warning(exception) raise ConfigEntryNotReady from exception instance_key = f"{DOMAIN}_{entry.data[CONF_ORG]}_{entry.data[CONF_PROJECT]}" hass.data.setdefault(instance_key, {})[DATA_AZURE_DEVOPS_CLIENT] = client # Setup components hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True