Weather platform Forecast (#4721)

* Added forecast functionality to the weather platform, updated OWM to get forecast data

* style fixes

* Changed returned forecast data to a more managable dict

* Fixed line length

* forecast will always be collected, data returned from owm is based on metric setting

* use list comprehension to create the forecast data

* Added forecast data to the weather demo

* Create dict directly in list comprehension

* Minor variable change in weather demo platform

* Convert forecast temperatures in weather component

* set forecast attributes as constants

* Style fixes and tests

* Copied forecast_entry instead of mutating data
This commit is contained in:
Tom Matheussen 2017-02-20 01:42:12 +01:00 committed by Paulus Schoutsen
parent 5d8e219448
commit 66d8787d47
4 changed files with 80 additions and 13 deletions

View file

@ -4,7 +4,10 @@ Demo platform that offers fake meteorological data.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
"""
from homeassistant.components.weather import WeatherEntity
from datetime import datetime, timedelta
from homeassistant.components.weather import (
WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME)
from homeassistant.const import (TEMP_CELSIUS, TEMP_FAHRENHEIT)
CONDITION_CLASSES = {
@ -28,8 +31,10 @@ CONDITION_CLASSES = {
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Demo weather."""
add_devices([
DemoWeather('South', 'Sunshine', 21, 92, 1099, 0.5, TEMP_CELSIUS),
DemoWeather('North', 'Shower rain', -12, 54, 987, 4.8, TEMP_FAHRENHEIT)
DemoWeather('South', 'Sunshine', 21, 92, 1099, 0.5, TEMP_CELSIUS,
[22, 19, 15, 12, 14, 18, 21]),
DemoWeather('North', 'Shower rain', -12, 54, 987, 4.8, TEMP_FAHRENHEIT,
[-10, -13, -18, -23, -19, -14, -9])
])
@ -37,7 +42,7 @@ class DemoWeather(WeatherEntity):
"""Representation of a weather condition."""
def __init__(self, name, condition, temperature, humidity, pressure,
wind_speed, temperature_unit):
wind_speed, temperature_unit, forecast):
"""Initialize the Demo weather."""
self._name = name
self._condition = condition
@ -46,6 +51,7 @@ class DemoWeather(WeatherEntity):
self._humidity = humidity
self._pressure = pressure
self._wind_speed = wind_speed
self._forecast = forecast
@property
def name(self):
@ -92,3 +98,19 @@ class DemoWeather(WeatherEntity):
def attribution(self):
"""Return the attribution."""
return 'Powered by Home Assistant'
@property
def forecast(self):
"""Return the forecast."""
reftime = datetime.now().replace(hour=16, minute=00)
forecast_data = []
for entry in self._forecast:
data_dict = {
ATTR_FORECAST_TIME: reftime.isoformat(),
ATTR_FORECAST_TEMP: entry
}
reftime = reftime + timedelta(hours=4)
forecast_data.append(data_dict)
return forecast_data