Example #1
0
async def async_setup_platform(hass,
                               config,
                               async_add_devices,
                               discovery_info=None):
    """Set up the devices associated with the account."""
    from foobot_async import FoobotClient

    token = config.get(CONF_TOKEN)
    username = config.get(CONF_USERNAME)

    client = FoobotClient(token,
                          username,
                          async_get_clientsession(hass),
                          timeout=TIMEOUT)
    dev = []
    try:
        devices = await client.get_devices()
        _LOGGER.debug("The following devices were found: %s", devices)
        for device in devices:
            foobot_data = FoobotData(client, device['uuid'])
            for sensor_type in SENSOR_TYPES:
                if sensor_type == 'time':
                    continue
                foobot_sensor = FoobotSensor(foobot_data, device, sensor_type)
                dev.append(foobot_sensor)
    except (aiohttp.client_exceptions.ClientConnectorError,
            asyncio.TimeoutError, FoobotClient.TooManyRequests,
            FoobotClient.InternalError):
        _LOGGER.exception('Failed to connect to foobot servers.')
        raise PlatformNotReady
    except FoobotClient.ClientError:
        _LOGGER.error('Failed to fetch data from foobot servers.')
        return
    async_add_devices(dev, True)
Example #2
0
def test_get_devices_with_session_request():
    session = aiohttp.ClientSession()
    client = FoobotClient('token', '*****@*****.**', session)

    with aioresponses() as mocked:
        mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
                   status=200, body='''[{"uuid": "1234127987696AB",
                                       "userId": 2353,
                                       "mac": "013843C3C20A",
                                       "name": "FooBot"}]''')
        resp = loop.run_until_complete(client.get_devices())

        assert [dict(uuid="1234127987696AB",
                     userId=2353,
                     mac="013843C3C20A",
                     name="FooBot")] == resp
Example #3
0
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Set up the devices associated with the account."""
    token = config.get(CONF_TOKEN)
    username = config.get(CONF_USERNAME)

    client = FoobotClient(
        token, username, async_get_clientsession(hass), timeout=TIMEOUT
    )
    entities = []
    try:
        devices = await client.get_devices()
        _LOGGER.debug("The following devices were found: %s", devices)
        for device in devices:
            foobot_data = FoobotData(client, device["uuid"])
            entities.extend(
                [
                    FoobotSensor(foobot_data, device, description)
                    for description in SENSOR_TYPES
                    if description.key != "time"
                ]
            )
    except (
        aiohttp.client_exceptions.ClientConnectorError,
        asyncio.TimeoutError,
        FoobotClient.TooManyRequests,
        FoobotClient.InternalError,
    ) as err:
        _LOGGER.exception("Failed to connect to foobot servers")
        raise PlatformNotReady from err
    except FoobotClient.ClientError:
        _LOGGER.error("Failed to fetch data from foobot servers")
        return
    async_add_entities(entities, True)
Example #4
0
async def async_setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the devices associated with the account."""
    token: str = config[CONF_TOKEN]
    username: str = config[CONF_USERNAME]

    client = FoobotClient(token,
                          username,
                          async_get_clientsession(hass),
                          timeout=TIMEOUT)
    entities: list[FoobotSensor] = []
    try:
        devices: list[dict[str, Any]] = await client.get_devices()
        _LOGGER.debug("The following devices were found: %s", devices)
        for device in devices:
            foobot_data = FoobotData(client, device["uuid"])
            entities.extend([
                FoobotSensor(foobot_data, device, description)
                for description in SENSOR_TYPES
            ])
    except (
            aiohttp.client_exceptions.ClientConnectorError,
            asyncio.TimeoutError,
            FoobotClient.TooManyRequests,
            FoobotClient.InternalError,
    ) as err:
        _LOGGER.exception("Failed to connect to foobot servers")
        raise PlatformNotReady from err
    except FoobotClient.ClientError:
        _LOGGER.error("Failed to fetch data from foobot servers")
        return
    async_add_entities(entities, True)
import aiohttp
import asyncio
import pytest
from aioresponses import aioresponses
from datetime import datetime
from foobot_async import FoobotClient

client = FoobotClient('token', '*****@*****.**')
loop = asyncio.get_event_loop()


def test_get_devices_request():

    with aioresponses() as mocked:
        mocked.get(
            'https://api.foobot.io/v2/owner/[email protected]/device/',
            status=200,
            body='''[{"uuid": "1234127987696AB",
                                       "userId": 2353,
                                       "mac": "013843C3C20A",
                                       "name": "FooBot"}]''')
        resp = loop.run_until_complete(client.get_devices())

        assert [
            dict(uuid="1234127987696AB",
                 userId=2353,
                 mac="013843C3C20A",
                 name="FooBot")
        ] == resp