示例#1
0
async def async_validate_api(hass: HomeAssistant, api_key: str) -> str:
    """Get data from API."""
    client = SensiboClient(
        api_key,
        session=async_get_clientsession(hass),
        timeout=TIMEOUT,
    )

    try:
        async with async_timeout.timeout(TIMEOUT):
            device_query = await client.async_get_devices()
            user_query = await client.async_get_me()
    except AuthenticationError as err:
        LOGGER.error("Could not authenticate on Sensibo servers %s", err)
        raise AuthenticationError from err
    except SENSIBO_ERRORS as err:
        LOGGER.error("Failed to get information from Sensibo servers %s", err)
        raise ConnectionError from err

    devices = device_query["result"]
    user = user_query["result"].get("username")
    if not devices:
        LOGGER.error("Could not retrieve any devices from Sensibo servers")
        raise NoDevicesError
    if not user:
        LOGGER.error("Could not retrieve username from Sensibo servers")
        raise NoUsernameError
    return user
示例#2
0
文件: conftest.py 项目: jcgoette/core
async def get_data_from_library(hass: HomeAssistant,
                                aioclient_mock: AiohttpClientMocker,
                                load_json: dict[str, Any]) -> SensiboData:
    """Retrieve data from upstream Sensibo library."""

    client = SensiboClient("123467890",
                           aioclient_mock.create_session(hass.loop))
    with patch("pysensibo.SensiboClient.async_get_devices",
               return_value=load_json):
        output = await client.async_get_devices_data()
    await client._session.close()  # pylint: disable=protected-access
    return output
示例#3
0
 def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
     """Initialize the Sensibo coordinator."""
     self.client = SensiboClient(
         entry.data[CONF_API_KEY],
         session=async_get_clientsession(hass),
         timeout=TIMEOUT,
     )
     super().__init__(
         hass,
         LOGGER,
         name=DOMAIN,
         update_interval=timedelta(seconds=DEFAULT_SCAN_INTERVAL),
     )
示例#4
0
async def async_validate_api(hass: HomeAssistant, api_key: str) -> bool:
    """Get data from API."""
    client = SensiboClient(
        api_key,
        session=async_get_clientsession(hass),
        timeout=TIMEOUT,
    )

    try:
        async with async_timeout.timeout(TIMEOUT):
            if await client.async_get_devices(_INITIAL_FETCH_FIELDS):
                return True
    except (
            aiohttp.ClientConnectionError,
            asyncio.TimeoutError,
            SensiboError,
    ) as err:
        _LOGGER.error("Failed to get devices from Sensibo servers %s", err)
    return False