Fix issues from review of ecobee weather component (#10903)

* Fix issues from review

* Don't use STATE_UNKNOWN
This commit is contained in:
PhracturedBlue 2017-12-02 13:44:55 -08:00 committed by Martin Hjelmare
parent 475b7896e2
commit 8947052405

View File

@ -4,11 +4,10 @@ Support for displaying weather info from Ecobee API.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/weather.ecobee/ https://home-assistant.io/components/weather.ecobee/
""" """
import logging
from homeassistant.components import ecobee from homeassistant.components import ecobee
from homeassistant.components.weather import ( from homeassistant.components.weather import (
WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME) WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME)
from homeassistant.const import (STATE_UNKNOWN, TEMP_FAHRENHEIT) from homeassistant.const import (TEMP_FAHRENHEIT)
DEPENDENCIES = ['ecobee'] DEPENDENCIES = ['ecobee']
@ -52,8 +51,8 @@ class EcobeeWeather(WeatherEntity):
try: try:
forecast = self.weather['forecasts'][index] forecast = self.weather['forecasts'][index]
return forecast[param] return forecast[param]
except (ValueError, IndexError): except (ValueError, IndexError, KeyError):
return STATE_UNKNOWN raise ValueError
@property @property
def name(self): def name(self):
@ -63,12 +62,18 @@ class EcobeeWeather(WeatherEntity):
@property @property
def condition(self): def condition(self):
"""Return the current condition.""" """Return the current condition."""
return self.get_forecast(0, 'condition') try:
return self.get_forecast(0, 'condition')
except ValueError:
return None
@property @property
def temperature(self): def temperature(self):
"""Return the temperature.""" """Return the temperature."""
return float(self.get_forecast(0, 'temperature')) / 10 try:
return float(self.get_forecast(0, 'temperature')) / 10
except ValueError:
return None
@property @property
def temperature_unit(self): def temperature_unit(self):
@ -78,34 +83,51 @@ class EcobeeWeather(WeatherEntity):
@property @property
def pressure(self): def pressure(self):
"""Return the pressure.""" """Return the pressure."""
return int(self.get_forecast(0, 'pressure')) try:
return int(self.get_forecast(0, 'pressure'))
except ValueError:
return None
@property @property
def humidity(self): def humidity(self):
"""Return the humidity.""" """Return the humidity."""
return int(self.get_forecast(0, 'relativeHumidity')) try:
return int(self.get_forecast(0, 'relativeHumidity'))
except ValueError:
return None
@property @property
def visibility(self): def visibility(self):
"""Return the visibility.""" """Return the visibility."""
return int(self.get_forecast(0, 'visibility')) try:
return int(self.get_forecast(0, 'visibility'))
except ValueError:
return None
@property @property
def wind_speed(self): def wind_speed(self):
"""Return the wind speed.""" """Return the wind speed."""
return int(self.get_forecast(0, 'windSpeed')) try:
return int(self.get_forecast(0, 'windSpeed'))
except ValueError:
return None
@property @property
def wind_bearing(self): def wind_bearing(self):
"""Return the wind direction.""" """Return the wind direction."""
return int(self.get_forecast(0, 'windBearing')) try:
return int(self.get_forecast(0, 'windBearing'))
except ValueError:
return None
@property @property
def attribution(self): def attribution(self):
"""Return the attribution.""" """Return the attribution."""
station = self.weather['weatherStation'] if self.weather:
time = self.weather['timestamp'] station = self.weather.get('weatherStation', "UNKNOWN")
return "Ecobee weather provided by " + station + " at " + time time = self.weather.get('timestamp', "UNKNOWN")
return "Ecobee weather provided by {} at {}".format(station, time)
return None
@property @property
def forecast(self): def forecast(self):
@ -134,8 +156,8 @@ class EcobeeWeather(WeatherEntity):
int(day['relativeHumidity']) int(day['relativeHumidity'])
forecasts.append(forecast) forecasts.append(forecast)
return forecasts return forecasts
except (ValueError, IndexError): except (ValueError, IndexError, KeyError):
return STATE_UNKNOWN return None
def update(self): def update(self):
"""Get the latest state of the sensor.""" """Get the latest state of the sensor."""
@ -143,4 +165,3 @@ class EcobeeWeather(WeatherEntity):
data.update() data.update()
thermostat = data.ecobee.get_thermostat(self._index) thermostat = data.ecobee.get_thermostat(self._index)
self.weather = thermostat.get('weather', None) self.weather = thermostat.get('weather', None)
logging.error("Weather Update")