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"
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_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_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 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)