async def test_auth_failure() -> Any:
    """Test that we can raise on bad auth."""
    async with aiohttp.ClientSession() as session:
        with pytest.raises(AwairError):
            Awair(session=session)

        with VCR.use_cassette("bad_auth.yaml"):
            awair = Awair(session=session, access_token="bad")
            with pytest.raises(AuthError):
                await awair.user()
Example #2
0
    def __init__(self, hass, config_entry, session) -> None:
        """Set up the AwairDataUpdateCoordinator class."""
        access_token = config_entry.data[CONF_ACCESS_TOKEN]
        self._awair = Awair(access_token=access_token, session=session)
        self._config_entry = config_entry

        super().__init__(hass, LOGGER, name=DOMAIN, update_interval=UPDATE_INTERVAL)
Example #3
0
    def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry,
                 session: ClientSession) -> None:
        """Set up the AwairCloudDataUpdateCoordinator class."""
        access_token = config_entry.data[CONF_ACCESS_TOKEN]
        self._awair = Awair(access_token=access_token, session=session)

        super().__init__(hass, config_entry, UPDATE_INTERVAL_CLOUD)
async def test_air_data_handles_datetime_limits() -> Any:
    """Test that we handle date limits."""
    async with aiohttp.ClientSession() as session:
        awair = Awair(session=session, access_token=ACCESS_TOKEN)
        device = mock_awair_device(client=awair.client)

        now = datetime.now()

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(from_date=(now + timedelta(hours=1)))

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(from_date=False)

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(from_date=(now - timedelta(hours=2)))

        with pytest.raises(vol.Invalid):
            await device.air_data_five_minute(from_date=(now -
                                                         timedelta(hours=25)))

        with pytest.raises(vol.Invalid):
            await device.air_data_fifteen_minute(from_date=(now -
                                                            timedelta(days=8)))

        with pytest.raises(vol.Invalid):
            await device.air_data_fifteen_minute(
                from_date=(now - timedelta(hours=1)),
                to_date=(now - timedelta(hours=3)))
async def test_sensor_creation_gen1() -> Any:
    """Test that an Awair gen 1 creates expected sensors."""
    target = datetime(2020, 4, 10, 10, 38, 30)
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("latest.yaml"), time_travel(target):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            device = mock_awair_device(client=awair.client)
            resp = await device.air_data_latest()

    assert hasattr(resp, "timestamp")
    assert hasattr(resp, "score")
    assert hasattr(resp, "sensors")
    assert hasattr(resp, "indices")
    expected_sensors = [
        "humidity",
        "temperature",
        "carbon_dioxide",
        "volatile_organic_compounds",
        "dust",
    ]
    assert resp is not None
    assert len(resp.sensors) == len(expected_sensors)
    assert len(resp.indices) == len(expected_sensors)
    for sensor in expected_sensors:
        assert hasattr(resp.sensors, sensor)
        assert hasattr(resp.indices, sensor)

    assert device.model == "Awair"
Example #6
0
async def fetch_data():
    """Fetch remote data."""
    async with aiohttp.ClientSession() as session:
        # Instantiate a client with your access token, and an asyncio session:
        token = os.environ.get("AWAIR_TOKEN")
        client = Awair(access_token=token, session=session)

        # Retrieve a user object:
        user = await client.user()

        # List that user's devices:
        devices = await user.devices()

        # Get some air quality data for a user's device:
        data = await devices[0].air_data_latest()

        # Print things out!
        print(f"Device: {devices[0]}")

        # You can access sensors as dict items:
        for sensor, value in data.sensors.items():
            print(f"  {sensor}: {round(value, 2)}")

        # Or, as attributes:
        print(f"  temperature again: {round(data.sensors.temperature, 2)}")
async def test_custom_auth() -> Any:
    """Test that we can use the API with a custom auth class."""
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("custom_auth.yaml"):
            auth = SillyAuth(access_token=ACCESS_TOKEN)
            awair = Awair(session=session, authenticator=auth)
            user = await awair.user()

    assert user.user_id == "32406"
async def test_not_found() -> Any:
    """Test that we can raise on 404."""
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("not_found.yaml"):
            with patch("python_awair.const.DEVICE_URL",
                       f"{const.USER_URL}/devicesxyz"):
                with pytest.raises(NotFoundError):
                    awair = Awair(session=session, access_token=ACCESS_TOKEN)
                    user = mock_awair_user(client=awair.client)
                    await user.devices()
async def test_air_data_handles_boolean_attributes() -> Any:
    """Test that we handle boolean query attributes."""
    async with aiohttp.ClientSession() as session:
        awair = Awair(session=session, access_token=ACCESS_TOKEN)
        device = mock_awair_device(client=awair.client)

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(desc=None)

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(fahrenheit=1)
async def test_get_devices() -> Any:
    """Test that we can get a list of devices."""
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("devices.yaml"):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            user = mock_awair_user(client=awair.client)
            devices = await user.devices()

    assert devices[0].device_id == AWAIR_GEN1_ID
    assert devices[0].device_type == "awair"
    assert devices[0].uuid == f"awair_{AWAIR_GEN1_ID}"
    assert "<AwairDevice" in str(devices[0])
async def test_bad_query() -> Any:
    """Test that we can raise on bad query."""
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("bad_params.yaml"):
            with patch(
                    "python_awair.devices.AwairDevice._format_args",
                    return_value="?fahrenheit=451",
            ):
                with pytest.raises(QueryError):
                    awair = Awair(session=session, access_token=ACCESS_TOKEN)
                    device = mock_awair_device(client=awair.client)
                    await device.air_data_latest()
async def test_get_raw() -> Any:
    """Test that we can get the raw air data."""
    target = datetime(2020, 4, 10, 10, 38, 31, 720296)
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("raw.yaml"), time_travel(target):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            device = mock_awair_device(client=awair.client)
            resp = await device.air_data_raw(from_date=(target -
                                                        timedelta(minutes=30)))

    assert resp[0].timestamp == datetime(2020, 4, 10, 15, 38, 24, 111000)
    assert resp[0].score == 88.0
    assert resp[0].sensors["temperature"] == 21.770000457763672
    assert resp[0].indices["temperature"] == -1.0
async def test_get_fifteen_minute() -> Any:
    """Test that we can get the fifteen-minute avg air data."""
    target = datetime(2020, 4, 10, 10, 38, 31, 252873)
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("fifteen_minute.yaml"), time_travel(target):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            device = mock_awair_device(client=awair.client)
            resp = await device.air_data_fifteen_minute(
                from_date=(target - timedelta(minutes=30)))

    assert resp[0].timestamp == datetime(2020, 4, 10, 15, 30)
    assert resp[0].score == 88.0
    assert resp[0].sensors["temperature"] == 21.791961108936984
    assert resp[0].indices["temperature"] == -1.0
async def test_get_latest() -> Any:
    """Test that we can get the latest air data."""
    target = datetime(2020, 4, 10, 10, 38, 30)
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("latest.yaml"), time_travel(target):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            device = mock_awair_device(client=awair.client)
            resp = await device.air_data_latest()

    assert resp is not None
    assert resp.timestamp == datetime(2020, 4, 10, 15, 38, 24, 111000)
    assert resp.score == 88.0
    assert resp.sensors["temperature"] == 21.770000457763672
    assert resp.indices["temperature"] == -1.0
    assert "<AirData@2020-04-10" in str(resp)
async def test_get_user() -> Any:
    """Test that we can get a user response."""
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("user.yaml"):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            user = await awair.user()

    assert user.user_id == "32406"
    assert user.email == "*****@*****.**"
    assert user.first_name == "Andrew"
    assert user.dob == date(year=2020, month=4, day=8)
    assert user.tier == "Large_developer"
    assert user.permissions["FIFTEEN_MIN"] == 30000
    assert user.usages["USER_INFO"] == 80

    assert "<AwairUser" in str(user)
async def test_air_data_handles_numeric_limits() -> Any:
    """Test that we handle numeric query attributes."""
    async with aiohttp.ClientSession() as session:
        awair = Awair(session=session, access_token=ACCESS_TOKEN)
        device = mock_awair_device(client=awair.client)

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(limit=-1)

        with pytest.raises(vol.Invalid):
            await device.air_data_raw(limit=361)

        with pytest.raises(vol.Invalid):
            await device.air_data_five_minute(limit=289)

        with pytest.raises(vol.Invalid):
            await device.air_data_fifteen_minute(limit=673)
    async def _check_connection(self, access_token: str):
        """Check the access token is valid."""
        session = async_get_clientsession(self.hass)
        awair = Awair(access_token=access_token, session=session)

        try:
            user = await awair.user()
            devices = await user.devices()
            if not devices:
                return (None, "no_devices_found")

            return (user, None)

        except AuthError:
            return (None, "invalid_access_token")
        except AwairError as err:
            LOGGER.error("Unexpected API error: %s", err)
            return (None, "unknown")
async def test_sensor_creation_omni() -> Any:
    """Test that an Awair omni creates expected sensors."""
    target = datetime(2020, 4, 10, 10, 38, 30)
    async with aiohttp.ClientSession() as session:
        with VCR.use_cassette("omni.yaml"), time_travel(target):
            awair = Awair(session=session, access_token=ACCESS_TOKEN)
            device = mock_awair_device(client=awair.client,
                                       device=MOCK_OMNI_DEVICE_ATTRS)
            resp = await device.air_data_latest()

    assert hasattr(resp, "timestamp")
    assert hasattr(resp, "score")
    assert hasattr(resp, "sensors")
    assert hasattr(resp, "indices")

    expected_sensors = [
        "humidity",
        "temperature",
        "carbon_dioxide",
        "volatile_organic_compounds",
        "particulate_matter_2_5",
        "illuminance",
        "sound_pressure_level",
    ]
    assert resp is not None
    assert len(resp.sensors) == len(expected_sensors)
    for sensor in expected_sensors:
        assert hasattr(resp.sensors, sensor)

    expected_indices = [
        "humidity",
        "temperature",
        "carbon_dioxide",
        "volatile_organic_compounds",
        "particulate_matter_2_5",
    ]
    assert len(resp.indices) == len(expected_indices)
    for sensor in expected_indices:
        assert hasattr(resp.indices, sensor)