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):
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
return False
return
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:
_LOGGER.error("Unable to connect to OpenWeatherMap")
return False
return
data = WeatherData(owm, forecast, hass.config.latitude,
hass.config.longitude)
data = WeatherData(
owm, forecast, hass.config.latitude, hass.config.longitude)
dev = []
for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(OpenWeatherMapSensor(
@ -131,7 +131,7 @@ class OpenWeatherMapSensor(Entity):
try:
self.owa_client.update()
except APICallError:
_LOGGER.error("Exception when calling OWM web API to update data")
_LOGGER.error("Error when calling API to update data")
return
data = self.owa_client.data
@ -140,14 +140,16 @@ class OpenWeatherMapSensor(Entity):
if data is None:
return
try:
if self.type == 'weather':
self._state = data.get_detailed_status()
elif self.type == 'temperature':
if self.temp_unit == TEMP_CELSIUS:
self._state = round(data.get_temperature('celsius')['temp'], 1)
self._state = round(
data.get_temperature('celsius')['temp'], 1)
elif self.temp_unit == TEMP_FAHRENHEIT:
self._state = round(data.get_temperature('fahrenheit')['temp'],
1)
self._state = round(
data.get_temperature('fahrenheit')['temp'], 1)
else:
self._state = round(data.get_temperature()['temp'], 1)
elif self.type == 'wind_speed':
@ -180,6 +182,10 @@ class OpenWeatherMapSensor(Entity):
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:
@ -202,8 +208,8 @@ class WeatherData:
try:
obs = self.owm.weather_at_coords(self.latitude, self.longitude)
except (APICallError, TypeError):
_LOGGER.error("Exception when calling OWM web API "
"to get weather at coords")
_LOGGER.error(
"Error when calling API to get weather at coordinates")
obs = None
if obs is None: