mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Make forecast.io update interval configurable (#3520)
Adds a config parameter (`update_interval`) to the `forecast` sensor to set the minimum update interval. The default remains 120 seconds.
This commit is contained in:
parent
1f38e9fa57
commit
9da2d6edd0
@ -23,11 +23,10 @@ REQUIREMENTS = ['python-forecastio==1.3.4']
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_UNITS = 'units'
|
CONF_UNITS = 'units'
|
||||||
|
CONF_UPDATE_INTERVAL = 'update_interval'
|
||||||
|
|
||||||
DEFAULT_NAME = 'Forecast.io'
|
DEFAULT_NAME = 'Forecast.io'
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
|
||||||
|
|
||||||
# Sensor types are defined like so:
|
# Sensor types are defined like so:
|
||||||
# Name, si unit, us unit, ca unit, uk unit, uk2 unit
|
# Name, si unit, us unit, ca unit, uk unit, uk2 unit
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
@ -84,10 +83,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||||
vol.Required(CONF_API_KEY): cv.string,
|
vol.Required(CONF_API_KEY): cv.string,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_UNITS): vol.In(['auto', 'si', 'us', 'ca', 'uk', 'uk2'])
|
vol.Optional(CONF_UNITS): vol.In(['auto', 'si', 'us', 'ca', 'uk', 'uk2']),
|
||||||
|
vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=120)): (
|
||||||
|
vol.All(cv.time_period, cv.positive_timedelta)),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=too-many-arguments
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the Forecast.io sensor."""
|
"""Setup the Forecast.io sensor."""
|
||||||
# Validate the configuration
|
# Validate the configuration
|
||||||
@ -106,8 +108,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
# the first call to init the data and confirm we can connect.
|
# the first call to init the data and confirm we can connect.
|
||||||
try:
|
try:
|
||||||
forecast_data = ForeCastData(
|
forecast_data = ForeCastData(
|
||||||
config.get(CONF_API_KEY, None), hass.config.latitude,
|
api_key=config.get(CONF_API_KEY, None),
|
||||||
hass.config.longitude, units)
|
latitude=hass.config.latitude,
|
||||||
|
longitude=hass.config.longitude,
|
||||||
|
units=units,
|
||||||
|
interval=config.get(CONF_UPDATE_INTERVAL))
|
||||||
forecast_data.update_currently()
|
forecast_data.update_currently()
|
||||||
except ValueError as error:
|
except ValueError as error:
|
||||||
_LOGGER.error(error)
|
_LOGGER.error(error)
|
||||||
@ -247,7 +252,7 @@ class ForeCastData(object):
|
|||||||
|
|
||||||
# pylint: disable=too-many-instance-attributes
|
# pylint: disable=too-many-instance-attributes
|
||||||
|
|
||||||
def __init__(self, api_key, latitude, longitude, units):
|
def __init__(self, api_key, latitude, longitude, units, interval):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self._api_key = api_key
|
self._api_key = api_key
|
||||||
self.latitude = latitude
|
self.latitude = latitude
|
||||||
@ -261,10 +266,16 @@ class ForeCastData(object):
|
|||||||
self.data_hourly = None
|
self.data_hourly = None
|
||||||
self.data_daily = None
|
self.data_daily = None
|
||||||
|
|
||||||
|
# Apply throttling to methods using configured interval
|
||||||
|
self.update = Throttle(interval)(self._update)
|
||||||
|
self.update_currently = Throttle(interval)(self._update_currently)
|
||||||
|
self.update_minutely = Throttle(interval)(self._update_minutely)
|
||||||
|
self.update_hourly = Throttle(interval)(self._update_hourly)
|
||||||
|
self.update_daily = Throttle(interval)(self._update_daily)
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
def _update(self):
|
||||||
def update(self):
|
|
||||||
"""Get the latest data from Forecast.io."""
|
"""Get the latest data from Forecast.io."""
|
||||||
import forecastio
|
import forecastio
|
||||||
|
|
||||||
@ -275,22 +286,18 @@ class ForeCastData(object):
|
|||||||
raise ValueError("Unable to init Forecast.io. - %s", error)
|
raise ValueError("Unable to init Forecast.io. - %s", error)
|
||||||
self.unit_system = self.data.json['flags']['units']
|
self.unit_system = self.data.json['flags']['units']
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
def _update_currently(self):
|
||||||
def update_currently(self):
|
|
||||||
"""Update currently data."""
|
"""Update currently data."""
|
||||||
self.data_currently = self.data.currently()
|
self.data_currently = self.data.currently()
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
def _update_minutely(self):
|
||||||
def update_minutely(self):
|
|
||||||
"""Update minutely data."""
|
"""Update minutely data."""
|
||||||
self.data_minutely = self.data.minutely()
|
self.data_minutely = self.data.minutely()
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
def _update_hourly(self):
|
||||||
def update_hourly(self):
|
|
||||||
"""Update hourly data."""
|
"""Update hourly data."""
|
||||||
self.data_hourly = self.data.hourly()
|
self.data_hourly = self.data.hourly()
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
def _update_daily(self):
|
||||||
def update_daily(self):
|
|
||||||
"""Update daily data."""
|
"""Update daily data."""
|
||||||
self.data_daily = self.data.daily()
|
self.data_daily = self.data.daily()
|
||||||
|
@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
|
|||||||
import forecastio
|
import forecastio
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.components.sensor import forecast
|
from homeassistant.components.sensor import forecast
|
||||||
|
|
||||||
@ -21,7 +22,8 @@ class TestForecastSetup(unittest.TestCase):
|
|||||||
self.key = 'foo'
|
self.key = 'foo'
|
||||||
self.config = {
|
self.config = {
|
||||||
'api_key': 'foo',
|
'api_key': 'foo',
|
||||||
'monitored_conditions': ['summary', 'icon']
|
'monitored_conditions': ['summary', 'icon'],
|
||||||
|
'update_interval': timedelta(seconds=120),
|
||||||
}
|
}
|
||||||
self.lat = 37.8267
|
self.lat = 37.8267
|
||||||
self.lon = -122.423
|
self.lon = -122.423
|
||||||
|
Loading…
x
Reference in New Issue
Block a user