Wunderground: retry getting nearest station ID if not set (#619)
This commit is contained in:
parent
86e05c0f9e
commit
86343d04ca
@ -125,29 +125,7 @@ class Wunderground(WeatherBackend):
|
|||||||
# it below and perform a lookup.
|
# it below and perform a lookup.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
extra_opts = '/pws:0' if not self.use_pws else ''
|
self.get_station_id()
|
||||||
api_url = GEOLOOKUP_URL % (self.api_key,
|
|
||||||
extra_opts,
|
|
||||||
self.location_code)
|
|
||||||
response = self.api_request(api_url)
|
|
||||||
station_type = 'pws' if self.use_pws else 'airport'
|
|
||||||
try:
|
|
||||||
stations = response['location']['nearby_weather_stations']
|
|
||||||
nearest = stations[station_type]['station'][0]
|
|
||||||
except (KeyError, IndexError):
|
|
||||||
raise Exception(
|
|
||||||
'No locations matched location_code %s' % self.location_code)
|
|
||||||
|
|
||||||
if self.use_pws:
|
|
||||||
nearest_pws = nearest.get('id', '')
|
|
||||||
if not nearest_pws:
|
|
||||||
raise Exception('No id entry for nearest PWS')
|
|
||||||
self.station_id = 'pws:%s' % nearest_pws
|
|
||||||
else:
|
|
||||||
nearest_airport = nearest.get('icao', '')
|
|
||||||
if not nearest_airport:
|
|
||||||
raise Exception('No icao entry for nearest airport')
|
|
||||||
self.station_id = 'icao:%s' % nearest_airport
|
|
||||||
|
|
||||||
@require(internet)
|
@require(internet)
|
||||||
def get_forecast(self):
|
def get_forecast(self):
|
||||||
@ -177,6 +155,36 @@ class Wunderground(WeatherBackend):
|
|||||||
else:
|
else:
|
||||||
return no_data
|
return no_data
|
||||||
|
|
||||||
|
@require(internet)
|
||||||
|
def get_station_id(self):
|
||||||
|
'''
|
||||||
|
Use geolocation to get the station ID
|
||||||
|
'''
|
||||||
|
extra_opts = '/pws:0' if not self.use_pws else ''
|
||||||
|
api_url = GEOLOOKUP_URL % (self.api_key,
|
||||||
|
extra_opts,
|
||||||
|
self.location_code)
|
||||||
|
response = self.api_request(api_url)
|
||||||
|
station_type = 'pws' if self.use_pws else 'airport'
|
||||||
|
try:
|
||||||
|
stations = response['location']['nearby_weather_stations']
|
||||||
|
nearest = stations[station_type]['station'][0]
|
||||||
|
except (KeyError, IndexError):
|
||||||
|
raise Exception(
|
||||||
|
'No locations matched location_code %s' % self.location_code)
|
||||||
|
|
||||||
|
self.logger.error('nearest = %s', nearest)
|
||||||
|
if self.use_pws:
|
||||||
|
nearest_pws = nearest.get('id', '')
|
||||||
|
if not nearest_pws:
|
||||||
|
raise Exception('No id entry for nearest PWS')
|
||||||
|
self.station_id = 'pws:%s' % nearest_pws
|
||||||
|
else:
|
||||||
|
nearest_airport = nearest.get('icao', '')
|
||||||
|
if not nearest_airport:
|
||||||
|
raise Exception('No icao entry for nearest airport')
|
||||||
|
self.station_id = 'icao:%s' % nearest_airport
|
||||||
|
|
||||||
def check_response(self, response):
|
def check_response(self, response):
|
||||||
try:
|
try:
|
||||||
return response['response']['error']['description']
|
return response['response']['error']['description']
|
||||||
@ -189,6 +197,11 @@ class Wunderground(WeatherBackend):
|
|||||||
'''
|
'''
|
||||||
Query the configured/queried station and return the weather data
|
Query the configured/queried station and return the weather data
|
||||||
'''
|
'''
|
||||||
|
if self.station_id is None:
|
||||||
|
# Failed to get the nearest station ID when first launched, so
|
||||||
|
# retry it.
|
||||||
|
self.get_station_id()
|
||||||
|
|
||||||
self.data['update_error'] = ''
|
self.data['update_error'] = ''
|
||||||
try:
|
try:
|
||||||
query_url = STATION_QUERY_URL % (self.api_key,
|
query_url = STATION_QUERY_URL % (self.api_key,
|
||||||
|
Loading…
Reference in New Issue
Block a user