async def _async_update_data(self): """Update data via library.""" data = {} try: obs = await self.airnow.observations.latLong( self.latitude, self.longitude, distance=self.distance, ) except (AirNowError, ClientConnectorError) as error: raise UpdateFailed(error) from error if not obs: raise UpdateFailed("No data was returned from AirNow") max_aqi = 0 max_aqi_level = 0 max_aqi_desc = "" max_aqi_poll = "" for obv in obs: # Convert AQIs to Concentration pollutant = obv[ATTR_API_AQI_PARAM] concentration = aqi_to_concentration(obv[ATTR_API_AQI], pollutant) data[obv[ATTR_API_AQI_PARAM]] = concentration # Overall AQI is the max of all pollutant AQIs if obv[ATTR_API_AQI] > max_aqi: max_aqi = obv[ATTR_API_AQI] max_aqi_level = obv[ATTR_API_CATEGORY][ATTR_API_CAT_LEVEL] max_aqi_desc = obv[ATTR_API_CATEGORY][ATTR_API_CAT_DESCRIPTION] max_aqi_poll = pollutant # Copy other data from PM2.5 Value if obv[ATTR_API_AQI_PARAM] == ATTR_API_PM25: # Copy Report Details data[ATTR_API_REPORT_DATE] = obv[ATTR_API_REPORT_DATE] data[ATTR_API_REPORT_HOUR] = obv[ATTR_API_REPORT_HOUR] # Copy Station Details data[ATTR_API_STATE] = obv[ATTR_API_STATE] data[ATTR_API_STATION] = obv[ATTR_API_STATION] data[ATTR_API_STATION_LATITUDE] = obv[ ATTR_API_STATION_LATITUDE] data[ATTR_API_STATION_LONGITUDE] = obv[ ATTR_API_STATION_LONGITUDE] # Store Overall AQI data[ATTR_API_AQI] = max_aqi data[ATTR_API_AQI_LEVEL] = max_aqi_level data[ATTR_API_AQI_DESCRIPTION] = max_aqi_desc data[ATTR_API_POLLUTANT] = max_aqi_poll return data
def test_convert_pm25(): assert aqi_to_concentration(0, 'PM2.5') == 0 assert aqi_to_concentration(25, 'PM2.5') == 6 assert aqi_to_concentration(50, 'PM2.5') == 12 assert aqi_to_concentration(51, 'PM2.5') == 12.1 assert aqi_to_concentration(86, 'PM2.5') == 28.7 assert aqi_to_concentration(144, 'PM2.5') == 53.0 assert concentration_to_aqi(0, 'PM2.5') == 0 assert concentration_to_aqi(12.0, 'PM2.5') == 50 assert concentration_to_aqi(12.1, 'PM2.5') == 51 assert concentration_to_aqi(28.66, 'PM2.5') == 86 assert concentration_to_aqi(53.0, 'PM2.5') == 144
def test_convert_invalid_overrange_aqi(): with pytest.raises(ValueError): aqi_to_concentration(501, 'PM2.5')
def test_convert_invalid_negative_aqi(): with pytest.raises(ValueError): aqi_to_concentration(-1, 'PM2.5')