From 0d93369154c1fe4fdda7ccea8edf8b4ec007b0bd Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 1 Jun 2016 09:17:15 -0700 Subject: [PATCH 1/2] Optimize foreacast.io API calls --- homeassistant/components/sensor/forecast.py | 75 ++++++++++++++++----- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/sensor/forecast.py b/homeassistant/components/sensor/forecast.py index 70d8be8deb261..4bfd990bc5462 100644 --- a/homeassistant/components/sensor/forecast.py +++ b/homeassistant/components/sensor/forecast.py @@ -131,26 +131,38 @@ def unit_system(self): """Return the unit system of this entity.""" return self._unit_system - # pylint: disable=too-many-branches + # pylint: disable=too-many-branches,too-many-statements def update(self): """Get the latest data from Forecast.io and updates the states.""" import forecastio self.forecast_client.update() - data = self.forecast_client.data.currently() - data_minutely = self.forecast_client.data.minutely() - data_hourly = self.forecast_client.data.hourly() - data_daily = self.forecast_client.data.daily() try: - if self.type == 'summary': - self._state = data.summary - elif self.type == 'minutely_summary': - self._state = data_minutely.summary + if self.type == 'minutely_summary': + self.forecast_client.update_minutely() + self._state = self.forecast_client.data_minutely.summary + return + elif self.type == 'hourly_summary': - self._state = data_hourly.summary + self.forecast_client.update_hourly() + self._state = self.forecast_client.data_hourly.summary + return + elif self.type == 'daily_summary': - self._state = data_daily.summary + self.forecast_client.update_daily() + self._state = self.forecast_client.data_daily.summary + return + + except forecastio.utils.PropertyUnavailable: + return + + self.forecast_client.update_currently() + data = self.forecast_client.data_currently + + try: + if self.type == 'summary': + self._state = data.summary elif self.type == 'icon': self._state = data.icon elif self.type == 'nearest_storm_distance': @@ -191,14 +203,22 @@ def update(self): class ForeCastData(object): """Gets the latest data from Forecast.io.""" + # pylint: disable=too-many-instance-attributes + def __init__(self, api_key, latitude, longitude, units): """Initialize the data object.""" self._api_key = api_key self.latitude = latitude self.longitude = longitude + self.units = units + self.data = None self.unit_system = None - self.units = units + self.data_currently = None + self.data_minutely = None + self.data_hourly = None + self.data_daily = None + self.update() @Throttle(MIN_TIME_BETWEEN_UPDATES) @@ -206,9 +226,28 @@ def update(self): """Get the latest data from Forecast.io.""" import forecastio - forecast = forecastio.load_forecast(self._api_key, - self.latitude, - self.longitude, - units=self.units) - self.data = forecast - self.unit_system = forecast.json['flags']['units'] + self.data = forecastio.load_forecast(self._api_key, + self.latitude, + self.longitude, + units=self.units) + self.unit_system = self.data.json['flags']['units'] + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update_currently(self): + """Update currently data.""" + self.data_currently = self.data.currently() + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update_minutely(self): + """Update minutely data.""" + self.data_minutely = self.data.minutely() + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update_hourly(self): + """Update hourly data.""" + self.data_hourly = self.data.hourly() + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update_daily(self): + """Update daily data.""" + self.data_daily = self.data.daily() From c3b6086d807278867688be402e836f724f7ae59c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 1 Jun 2016 17:36:04 -0700 Subject: [PATCH 2/2] Version bump to 0.20.3 --- homeassistant/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/const.py b/homeassistant/const.py index 72f963a090f9e..c8d5e070d8747 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1,7 +1,7 @@ # coding: utf-8 """Constants used by Home Assistant components.""" -__version__ = "0.20.2" +__version__ = "0.20.3" REQUIRED_PYTHON_VER = (3, 4) PLATFORM_FORMAT = '{}.{}'