Catch KeyError if data is not available (fixes #18082) (#18089)

This commit is contained in:
Fabian Affolter 2018-11-01 16:32:36 +01:00 committed by GitHub
parent 83e83520e6
commit a9361482d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,7 +58,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if None in (hass.config.latitude, hass.config.longitude): if None in (hass.config.latitude, hass.config.longitude):
_LOGGER.error("Latitude or longitude not set in Home Assistant config") _LOGGER.error("Latitude or longitude not set in Home Assistant config")
return False return
SENSOR_TYPES['temperature'][1] = hass.config.units.temperature_unit SENSOR_TYPES['temperature'][1] = hass.config.units.temperature_unit
@ -72,10 +72,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if not owm: if not owm:
_LOGGER.error("Unable to connect to OpenWeatherMap") _LOGGER.error("Unable to connect to OpenWeatherMap")
return False return
data = WeatherData(owm, forecast, hass.config.latitude, data = WeatherData(
hass.config.longitude) owm, forecast, hass.config.latitude, hass.config.longitude)
dev = [] dev = []
for variable in config[CONF_MONITORED_CONDITIONS]: for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(OpenWeatherMapSensor( dev.append(OpenWeatherMapSensor(
@ -131,7 +131,7 @@ class OpenWeatherMapSensor(Entity):
try: try:
self.owa_client.update() self.owa_client.update()
except APICallError: except APICallError:
_LOGGER.error("Exception when calling OWM web API to update data") _LOGGER.error("Error when calling API to update data")
return return
data = self.owa_client.data data = self.owa_client.data
@ -140,46 +140,52 @@ class OpenWeatherMapSensor(Entity):
if data is None: if data is None:
return return
if self.type == 'weather': try:
self._state = data.get_detailed_status() if self.type == 'weather':
elif self.type == 'temperature': self._state = data.get_detailed_status()
if self.temp_unit == TEMP_CELSIUS: elif self.type == 'temperature':
self._state = round(data.get_temperature('celsius')['temp'], 1) if self.temp_unit == TEMP_CELSIUS:
elif self.temp_unit == TEMP_FAHRENHEIT: self._state = round(
self._state = round(data.get_temperature('fahrenheit')['temp'], data.get_temperature('celsius')['temp'], 1)
1) elif self.temp_unit == TEMP_FAHRENHEIT:
else: self._state = round(
self._state = round(data.get_temperature()['temp'], 1) data.get_temperature('fahrenheit')['temp'], 1)
elif self.type == 'wind_speed': else:
self._state = round(data.get_wind()['speed'], 1) self._state = round(data.get_temperature()['temp'], 1)
elif self.type == 'wind_bearing': elif self.type == 'wind_speed':
self._state = round(data.get_wind()['deg'], 1) self._state = round(data.get_wind()['speed'], 1)
elif self.type == 'humidity': elif self.type == 'wind_bearing':
self._state = round(data.get_humidity(), 1) self._state = round(data.get_wind()['deg'], 1)
elif self.type == 'pressure': elif self.type == 'humidity':
self._state = round(data.get_pressure()['press'], 0) self._state = round(data.get_humidity(), 1)
elif self.type == 'clouds': elif self.type == 'pressure':
self._state = data.get_clouds() self._state = round(data.get_pressure()['press'], 0)
elif self.type == 'rain': elif self.type == 'clouds':
if data.get_rain(): self._state = data.get_clouds()
self._state = round(data.get_rain()['3h'], 0) elif self.type == 'rain':
self._unit_of_measurement = 'mm' if data.get_rain():
else: self._state = round(data.get_rain()['3h'], 0)
self._state = 'not raining' self._unit_of_measurement = 'mm'
self._unit_of_measurement = '' else:
elif self.type == 'snow': self._state = 'not raining'
if data.get_snow(): self._unit_of_measurement = ''
self._state = round(data.get_snow(), 0) elif self.type == 'snow':
self._unit_of_measurement = 'mm' if data.get_snow():
else: self._state = round(data.get_snow(), 0)
self._state = 'not snowing' self._unit_of_measurement = 'mm'
self._unit_of_measurement = '' else:
elif self.type == 'forecast': self._state = 'not snowing'
if fc_data is None: self._unit_of_measurement = ''
return elif self.type == 'forecast':
self._state = fc_data.get_weathers()[0].get_detailed_status() if fc_data is None:
elif self.type == 'weather_code': return
self._state = data.get_weather_code() self._state = fc_data.get_weathers()[0].get_detailed_status()
elif self.type == 'weather_code':
self._state = data.get_weather_code()
except KeyError:
self._state = None
_LOGGER.warning(
"Condition is currently not available: %s", self.type)
class WeatherData: class WeatherData:
@ -202,8 +208,8 @@ class WeatherData:
try: try:
obs = self.owm.weather_at_coords(self.latitude, self.longitude) obs = self.owm.weather_at_coords(self.latitude, self.longitude)
except (APICallError, TypeError): except (APICallError, TypeError):
_LOGGER.error("Exception when calling OWM web API " _LOGGER.error(
"to get weather at coords") "Error when calling API to get weather at coordinates")
obs = None obs = None
if obs is None: if obs is None: