1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00
ha-core/homeassistant/components/sensibo/util.py
2022-05-28 13:26:50 -10:00

50 lines
1.5 KiB
Python

"""Utils for Sensibo integration."""
from __future__ import annotations
import async_timeout
from pysensibo import SensiboClient
from pysensibo.exceptions import AuthenticationError
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import LOGGER, SENSIBO_ERRORS, TIMEOUT
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: str = 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
class NoDevicesError(Exception):
"""No devices from Sensibo api."""
class NoUsernameError(Exception):
"""No username from Sensibo api."""