mirror of
https://github.com/home-assistant/core.git
synced 2025-08-04 05:05:09 +02:00
Added forecast support to DarkSky
modified: homeassistant/components/sensor/darksky.py modified: tests/components/sensor/test_darksky.py
This commit is contained in:
@@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
CONF_ATTRIBUTION = "Powered by Dark Sky"
|
CONF_ATTRIBUTION = "Powered by Dark Sky"
|
||||||
CONF_UNITS = 'units'
|
CONF_UNITS = 'units'
|
||||||
CONF_UPDATE_INTERVAL = 'update_interval'
|
CONF_UPDATE_INTERVAL = 'update_interval'
|
||||||
|
CONF_FORECAST = 'forecast'
|
||||||
|
|
||||||
DEFAULT_NAME = 'Dark Sky'
|
DEFAULT_NAME = 'Dark Sky'
|
||||||
|
|
||||||
@@ -97,6 +98,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Check to make sure that forecast is in a valid range of 1 week
|
||||||
|
forecast = config.get(CONF_FORECAST)
|
||||||
|
if forecast is not None:
|
||||||
|
for forecast_day in forecast:
|
||||||
|
if forecast_day > 7 or forecast_day < 1:
|
||||||
|
_LOGGER.error("DarkSky only supports 7 day forecast")
|
||||||
|
return False
|
||||||
|
|
||||||
if CONF_UNITS in config:
|
if CONF_UNITS in config:
|
||||||
units = config[CONF_UNITS]
|
units = config[CONF_UNITS]
|
||||||
elif hass.config.units.is_metric:
|
elif hass.config.units.is_metric:
|
||||||
@@ -122,6 +131,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
sensors = []
|
sensors = []
|
||||||
for variable in config[CONF_MONITORED_CONDITIONS]:
|
for variable in config[CONF_MONITORED_CONDITIONS]:
|
||||||
sensors.append(DarkSkySensor(forecast_data, variable, name))
|
sensors.append(DarkSkySensor(forecast_data, variable, name))
|
||||||
|
if forecast is not None and variable in ['temperature_min',
|
||||||
|
'temperature_max',
|
||||||
|
'apparent_temperature_min',
|
||||||
|
'apparent_temperature_max',
|
||||||
|
'precip_intensity_max']:
|
||||||
|
for forecast_day in forecast:
|
||||||
|
sensors.append(DarkSkySensor(forecast_data,
|
||||||
|
variable, name, forecast_day))
|
||||||
|
|
||||||
add_devices(sensors, True)
|
add_devices(sensors, True)
|
||||||
|
|
||||||
@@ -129,19 +146,24 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
class DarkSkySensor(Entity):
|
class DarkSkySensor(Entity):
|
||||||
"""Implementation of a Dark Sky sensor."""
|
"""Implementation of a Dark Sky sensor."""
|
||||||
|
|
||||||
def __init__(self, forecast_data, sensor_type, name):
|
def __init__(self, forecast_data, sensor_type, name, forecast_day=0):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.client_name = name
|
self.client_name = name
|
||||||
self._name = SENSOR_TYPES[sensor_type][0]
|
self._name = SENSOR_TYPES[sensor_type][0]
|
||||||
self.forecast_data = forecast_data
|
self.forecast_data = forecast_data
|
||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
|
self.forecast_day = forecast_day
|
||||||
self._state = None
|
self._state = None
|
||||||
self._unit_of_measurement = None
|
self._unit_of_measurement = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return '{} {}'.format(self.client_name, self._name)
|
if self.forecast_day == 0:
|
||||||
|
return '{} {}'.format(self.client_name, self._name)
|
||||||
|
else:
|
||||||
|
return '{} {} {}'.format(self.client_name, self._name,
|
||||||
|
self.forecast_day)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
@@ -210,7 +232,7 @@ class DarkSkySensor(Entity):
|
|||||||
self._state = getattr(daily, 'summary', '')
|
self._state = getattr(daily, 'summary', '')
|
||||||
else:
|
else:
|
||||||
if hasattr(daily, 'data'):
|
if hasattr(daily, 'data'):
|
||||||
self._state = self.get_state(daily.data[0])
|
self._state = self.get_state(daily.data[self.forecast_day])
|
||||||
else:
|
else:
|
||||||
self._state = 0
|
self._state = 0
|
||||||
else:
|
else:
|
||||||
|
@@ -32,7 +32,8 @@ class TestDarkSkySetup(unittest.TestCase):
|
|||||||
self.key = 'foo'
|
self.key = 'foo'
|
||||||
self.config = {
|
self.config = {
|
||||||
'api_key': 'foo',
|
'api_key': 'foo',
|
||||||
'monitored_conditions': ['summary', 'icon'],
|
'forecast': [1, 2],
|
||||||
|
'monitored_conditions': ['summary', 'icon', 'temperature_max'],
|
||||||
'update_interval': timedelta(seconds=120),
|
'update_interval': timedelta(seconds=120),
|
||||||
}
|
}
|
||||||
self.lat = 37.8267
|
self.lat = 37.8267
|
||||||
@@ -80,4 +81,4 @@ class TestDarkSkySetup(unittest.TestCase):
|
|||||||
darksky.setup_platform(self.hass, self.config, self.add_entities)
|
darksky.setup_platform(self.hass, self.config, self.add_entities)
|
||||||
self.assertTrue(mock_get_forecast.called)
|
self.assertTrue(mock_get_forecast.called)
|
||||||
self.assertEqual(mock_get_forecast.call_count, 1)
|
self.assertEqual(mock_get_forecast.call_count, 1)
|
||||||
self.assertEqual(len(self.entities), 2)
|
self.assertEqual(len(self.entities), 5)
|
||||||
|
Reference in New Issue
Block a user