Fixes AirVisual bug regarding incorrect location data (#10054)

* Fixes AirVisual bug regarding incorrect location data

* Owner-requested changes
This commit is contained in:
Aaron Bach 2017-11-17 13:11:05 -07:00 committed by Paulus Schoutsen
parent b3d66e5881
commit 0202e966ea

View file

@ -126,7 +126,7 @@ class AirVisualBaseSensor(Entity):
def __init__(self, data, name, icon, locale): def __init__(self, data, name, icon, locale):
"""Initialize the sensor.""" """Initialize the sensor."""
self._data = data self.data = data
self._icon = icon self._icon = icon
self._locale = locale self._locale = locale
self._name = name self._name = name
@ -136,20 +136,17 @@ class AirVisualBaseSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the device state attributes.""" """Return the device state attributes."""
attrs = { attrs = merge_two_dicts({
ATTR_ATTRIBUTION: CONF_ATTRIBUTION, ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_CITY: self._data.city, ATTR_TIMESTAMP: self.data.pollution_info.get('ts')
ATTR_COUNTRY: self._data.country, }, self.data.attrs)
ATTR_REGION: self._data.state,
ATTR_TIMESTAMP: self._data.pollution_info.get('ts')
}
if self._data.show_on_map: if self.data.show_on_map:
attrs[ATTR_LATITUDE] = self._data.latitude attrs[ATTR_LATITUDE] = self.data.latitude
attrs[ATTR_LONGITUDE] = self._data.longitude attrs[ATTR_LONGITUDE] = self.data.longitude
else: else:
attrs['lati'] = self._data.latitude attrs['lati'] = self.data.latitude
attrs['long'] = self._data.longitude attrs['long'] = self.data.longitude
return attrs return attrs
@ -174,9 +171,9 @@ class AirPollutionLevelSensor(AirVisualBaseSensor):
def update(self): def update(self):
"""Update the status of the sensor.""" """Update the status of the sensor."""
self._data.update() self.data.update()
aqi = self._data.pollution_info.get('aqi{0}'.format(self._locale)) aqi = self.data.pollution_info.get('aqi{0}'.format(self._locale))
try: try:
[level] = [ [level] = [
i for i in POLLUTANT_LEVEL_MAPPING i for i in POLLUTANT_LEVEL_MAPPING
@ -199,9 +196,9 @@ class AirQualityIndexSensor(AirVisualBaseSensor):
def update(self): def update(self):
"""Update the status of the sensor.""" """Update the status of the sensor."""
self._data.update() self.data.update()
self._state = self._data.pollution_info.get( self._state = self.data.pollution_info.get(
'aqi{0}'.format(self._locale)) 'aqi{0}'.format(self._locale))
@ -224,9 +221,9 @@ class MainPollutantSensor(AirVisualBaseSensor):
def update(self): def update(self):
"""Update the status of the sensor.""" """Update the status of the sensor."""
self._data.update() self.data.update()
symbol = self._data.pollution_info.get('main{0}'.format(self._locale)) symbol = self.data.pollution_info.get('main{0}'.format(self._locale))
pollution_info = POLLUTANT_MAPPING.get(symbol, {}) pollution_info = POLLUTANT_MAPPING.get(symbol, {})
self._state = pollution_info.get('label') self._state = pollution_info.get('label')
self._unit = pollution_info.get('unit') self._unit = pollution_info.get('unit')
@ -239,6 +236,7 @@ class AirVisualData(object):
def __init__(self, client, **kwargs): def __init__(self, client, **kwargs):
"""Initialize the AirVisual data element.""" """Initialize the AirVisual data element."""
self._client = client self._client = client
self.attrs = {}
self.pollution_info = None self.pollution_info = None
self.city = kwargs.get(CONF_CITY) self.city = kwargs.get(CONF_CITY)
@ -260,17 +258,20 @@ class AirVisualData(object):
if self.city and self.state and self.country: if self.city and self.state and self.country:
resp = self._client.city( resp = self._client.city(
self.city, self.state, self.country).get('data') self.city, self.state, self.country).get('data')
self.longitude, self.latitude = resp.get('location').get(
'coordinates')
else: else:
resp = self._client.nearest_city( resp = self._client.nearest_city(
self.latitude, self.longitude, self._radius).get('data') self.latitude, self.longitude, self._radius).get('data')
_LOGGER.debug("New data retrieved: %s", resp) _LOGGER.debug("New data retrieved: %s", resp)
self.city = resp.get('city')
self.state = resp.get('state')
self.country = resp.get('country')
self.longitude, self.latitude = resp.get('location').get(
'coordinates')
self.pollution_info = resp.get('current', {}).get('pollution', {}) self.pollution_info = resp.get('current', {}).get('pollution', {})
self.attrs = {
ATTR_CITY: resp.get('city'),
ATTR_REGION: resp.get('state'),
ATTR_COUNTRY: resp.get('country')
}
except exceptions.HTTPError as exc_info: except exceptions.HTTPError as exc_info:
_LOGGER.error("Unable to retrieve data on this location: %s", _LOGGER.error("Unable to retrieve data on this location: %s",
self.__dict__) self.__dict__)