8000 Catch KeyError if data is not available (fixes #18082) by fabaff · Pull Request #18089 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 53 additions & 47 deletions homeassistant/components/sensor/openweathermap.py
10272
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -131,7 +131,7 @@ def update(self):
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
Expand All @@ -140,46 +140,52 @@ def update(self):
if data is None:
return

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)
elif self.temp_unit == TEMP_FAHRENHEIT:
self._state = round(data.get_temperature('fahrenheit')['temp'],
1)
else:
self._state = round(data.get_temperature()['temp'], 1)
elif self.type == 'wind_speed':
self._state = round(data.get_wind()['speed'], 1)
elif self.type == 'wind_bearing':
self._state = round(data.get_wind()['deg'], 1)
elif self.type == 'humidity':
self._state = round(data.get_humidity(), 1)
elif self.type == 'pressure':
self._state = round(data.get_pressure()['press'], 0)
elif self.type == 'clouds':
self._state = data.get_clouds()
elif self.type == 'rain':
if data.get_rain():
self._state = round(data.get_rain()['3h'], 0)
self._unit_of_measurement = 'mm'
else:
self._state = 'not raining'
self._unit_of_measurement = ''
elif self.type == 'snow':
if data.get_snow():
self._state = round(data.get_snow(), 0)
self._unit_of_measurement = 'mm'
else:
self._state = 'not snowing'
self._unit_of_measurement = ''
elif self.type == 'forecast':
if fc_data is None:
return
self._state = fc_data.get_weathers()[0].get_detailed_status()
elif self.type == 'weather_code':
self._state = data.get_weather_code()
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)
elif self.temp_unit == TEMP_FAHRENHEIT:
self._state = round(
data.get_temperature('fahrenheit')['temp'], 1)
else:
self._state = round(data.get_temperature()['temp'], 1)
elif self.type == 'wind_speed':
self._state = round(data.get_wind()['speed'], 1)
elif self.type == 'wind_bearing':
self._state = round(data.get_wind()['deg'], 1)
elif self.type == 'humidity':
self._state = round(data.get_humidity(), 1)
elif self.type == 'pressure':
self._state = round(data.get_pressure()['press'], 0)
elif self.type == 'clouds':
self._state = data.get_clouds()
elif self.type == 'rain':
if data.get_rain():
self._state = round(data.get_rain()['3h'], 0)
self._unit_of_measurement = 'mm'
else:
self._state = 'not raining'
self._unit_of_measurement = ''
elif self.type == 'snow':
if data.get_snow():
self._state = round(data.get_snow(), 0)
self._unit_of_measurement = 'mm'
else:
self._state = 'not snowing'
self._unit_of_measurement = ''
elif self.type == 'forecast':
if fc_data is None:
return
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:
Expand All @@ -202,8 +208,8 @@ def update(self):
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:
Expand Down
0