From 8e7f500f28332d218dcfa1b316279d5822127d95 Mon Sep 17 00:00:00 2001 From: escoand Date: Sun, 29 Apr 2018 17:50:49 +0200 Subject: [PATCH] Add precipitation to OpenWeatherMap forecast (#13971) * add initial precipitation support * move attr to component * remove blank line * add forecast attributes to platform and update demo * add tests * break long lines * calc lower temp correctly * move all new attributes to component * convert temp low only when existing --- homeassistant/components/weather/__init__.py | 7 +++++++ homeassistant/components/weather/buienradar.py | 6 ++---- homeassistant/components/weather/demo.py | 18 ++++++++++++++---- homeassistant/components/weather/ecobee.py | 5 ++--- .../components/weather/openweathermap.py | 5 +++-- homeassistant/components/weather/yweather.py | 5 ++--- tests/components/weather/test_weather.py | 12 +++++++++++- 7 files changed, 41 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index b200d634ba9..467a106a6a2 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -22,7 +22,10 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}' ATTR_CONDITION_CLASS = 'condition_class' ATTR_FORECAST = 'forecast' +ATTR_FORECAST_CONDITION = 'condition' +ATTR_FORECAST_PRECIPITATION = 'precipitation' ATTR_FORECAST_TEMP = 'temperature' +ATTR_FORECAST_TEMP_LOW = 'templow' ATTR_FORECAST_TIME = 'datetime' ATTR_WEATHER_ATTRIBUTION = 'attribution' ATTR_WEATHER_HUMIDITY = 'humidity' @@ -144,6 +147,10 @@ class WeatherEntity(Entity): forecast_entry[ATTR_FORECAST_TEMP] = show_temp( self.hass, forecast_entry[ATTR_FORECAST_TEMP], self.temperature_unit, self.precision) + if ATTR_FORECAST_TEMP_LOW in forecast_entry: + forecast_entry[ATTR_FORECAST_TEMP_LOW] = show_temp( + self.hass, forecast_entry[ATTR_FORECAST_TEMP_LOW], + self.temperature_unit, self.precision) forecast.append(forecast_entry) data[ATTR_FORECAST] = forecast diff --git a/homeassistant/components/weather/buienradar.py b/homeassistant/components/weather/buienradar.py index bf1864a9c0f..9b9707e87f6 100644 --- a/homeassistant/components/weather/buienradar.py +++ b/homeassistant/components/weather/buienradar.py @@ -10,7 +10,8 @@ import asyncio import voluptuous as vol from homeassistant.components.weather import ( - WeatherEntity, PLATFORM_SCHEMA, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME) + WeatherEntity, PLATFORM_SCHEMA, ATTR_FORECAST_CONDITION, + ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME) from homeassistant.const import \ CONF_NAME, TEMP_CELSIUS, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.helpers import config_validation as cv @@ -28,9 +29,6 @@ DEFAULT_TIMEFRAME = 60 CONF_FORECAST = 'forecast' -ATTR_FORECAST_CONDITION = 'condition' -ATTR_FORECAST_TEMP_LOW = 'templow' - CONDITION_CLASSES = { 'cloudy': ['c', 'p'], diff --git a/homeassistant/components/weather/demo.py b/homeassistant/components/weather/demo.py index 02e07996213..fffdf03d07d 100644 --- a/homeassistant/components/weather/demo.py +++ b/homeassistant/components/weather/demo.py @@ -7,7 +7,8 @@ https://home-assistant.io/components/demo/ from datetime import datetime, timedelta from homeassistant.components.weather import ( - WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME) + WeatherEntity, ATTR_FORECAST_CONDITION, ATTR_FORECAST_PRECIPITATION, + ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME) from homeassistant.const import (TEMP_CELSIUS, TEMP_FAHRENHEIT) CONDITION_CLASSES = { @@ -32,9 +33,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Demo weather.""" add_devices([ DemoWeather('South', 'Sunshine', 21.6414, 92, 1099, 0.5, TEMP_CELSIUS, - [22, 19, 15, 12, 14, 18, 21]), + [['rainy', 1, 22, 15], ['rainy', 5, 19, 8], + ['cloudy', 0, 15, 9], ['sunny', 0, 12, 6], + ['partlycloudy', 2, 14, 7], ['rainy', 15, 18, 7], + ['fog', 0.2, 21, 12]]), DemoWeather('North', 'Shower rain', -12, 54, 987, 4.8, TEMP_FAHRENHEIT, - [-10, -13, -18, -23, -19, -14, -9]) + [['snowy', 2, -10, -15], ['partlycloudy', 1, -13, -14], + ['sunny', 0, -18, -22], ['sunny', 0.1, -23, -23], + ['snowy', 4, -19, -20], ['sunny', 0.3, -14, -19], + ['sunny', 0, -9, -12]]) ]) @@ -108,7 +115,10 @@ class DemoWeather(WeatherEntity): for entry in self._forecast: data_dict = { ATTR_FORECAST_TIME: reftime.isoformat(), - ATTR_FORECAST_TEMP: entry + ATTR_FORECAST_CONDITION: entry[0], + ATTR_FORECAST_PRECIPITATION: entry[1], + ATTR_FORECAST_TEMP: entry[2], + ATTR_FORECAST_TEMP_LOW: entry[3] } reftime = reftime + timedelta(hours=4) forecast_data.append(data_dict) diff --git a/homeassistant/components/weather/ecobee.py b/homeassistant/components/weather/ecobee.py index 379f5c1211b..80ee4c29fbe 100644 --- a/homeassistant/components/weather/ecobee.py +++ b/homeassistant/components/weather/ecobee.py @@ -6,14 +6,13 @@ https://home-assistant.io/components/weather.ecobee/ """ from homeassistant.components import ecobee from homeassistant.components.weather import ( - WeatherEntity, ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME) + WeatherEntity, ATTR_FORECAST_CONDITION, ATTR_FORECAST_TEMP, + ATTR_FORECAST_TEMP_LOW, ATTR_FORECAST_TIME) from homeassistant.const import (TEMP_FAHRENHEIT) DEPENDENCIES = ['ecobee'] -ATTR_FORECAST_CONDITION = 'condition' -ATTR_FORECAST_TEMP_LOW = 'templow' ATTR_FORECAST_TEMP_HIGH = 'temphigh' ATTR_FORECAST_PRESSURE = 'pressure' ATTR_FORECAST_VISIBILITY = 'visibility' diff --git a/homeassistant/components/weather/openweathermap.py b/homeassistant/components/weather/openweathermap.py index a8e26d39cb3..909f123b52c 100644 --- a/homeassistant/components/weather/openweathermap.py +++ b/homeassistant/components/weather/openweathermap.py @@ -10,7 +10,8 @@ import logging import voluptuous as vol from homeassistant.components.weather import ( - ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME, PLATFORM_SCHEMA, WeatherEntity) + ATTR_FORECAST_CONDITION, ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_TEMP, + ATTR_FORECAST_TIME, PLATFORM_SCHEMA, WeatherEntity) from homeassistant.const import ( CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, STATE_UNKNOWN, TEMP_CELSIUS) @@ -21,7 +22,6 @@ REQUIREMENTS = ['pyowm==2.8.0'] _LOGGER = logging.getLogger(__name__) -ATTR_FORECAST_CONDITION = 'condition' ATTRIBUTION = 'Data provided by OpenWeatherMap' DEFAULT_NAME = 'OpenWeatherMap' @@ -144,6 +144,7 @@ class OpenWeatherMapWeather(WeatherEntity): ATTR_FORECAST_TIME: entry.get_reference_time('unix') * 1000, ATTR_FORECAST_TEMP: entry.get_temperature('celsius').get('temp'), + ATTR_FORECAST_PRECIPITATION: entry.get_rain().get('3h'), ATTR_FORECAST_CONDITION: [k for k, v in CONDITION_CLASSES.items() if entry.get_weather_code() in v][0] diff --git a/homeassistant/components/weather/yweather.py b/homeassistant/components/weather/yweather.py index 5987cf7621f..f9befece5a4 100644 --- a/homeassistant/components/weather/yweather.py +++ b/homeassistant/components/weather/yweather.py @@ -10,7 +10,8 @@ import logging import voluptuous as vol from homeassistant.components.weather import ( - ATTR_FORECAST_TEMP, ATTR_FORECAST_TIME, PLATFORM_SCHEMA, WeatherEntity) + ATTR_FORECAST_CONDITION, ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW, + ATTR_FORECAST_TIME, PLATFORM_SCHEMA, WeatherEntity) from homeassistant.const import CONF_NAME, STATE_UNKNOWN, TEMP_CELSIUS import homeassistant.helpers.config_validation as cv @@ -20,10 +21,8 @@ _LOGGER = logging.getLogger(__name__) DATA_CONDITION = 'yahoo_condition' -ATTR_FORECAST_CONDITION = 'condition' ATTRIBUTION = "Weather details provided by Yahoo! Inc." -ATTR_FORECAST_TEMP_LOW = 'templow' CONF_WOEID = 'woeid' diff --git a/tests/components/weather/test_weather.py b/tests/components/weather/test_weather.py index 9d22b1ad0ae..a88e9979551 100644 --- a/tests/components/weather/test_weather.py +++ b/tests/components/weather/test_weather.py @@ -5,7 +5,8 @@ from homeassistant.components import weather from homeassistant.components.weather import ( ATTR_WEATHER_ATTRIBUTION, ATTR_WEATHER_HUMIDITY, ATTR_WEATHER_OZONE, ATTR_WEATHER_PRESSURE, ATTR_WEATHER_TEMPERATURE, ATTR_WEATHER_WIND_BEARING, - ATTR_WEATHER_WIND_SPEED, ATTR_FORECAST, ATTR_FORECAST_TEMP) + ATTR_WEATHER_WIND_SPEED, ATTR_FORECAST, ATTR_FORECAST_CONDITION, + ATTR_FORECAST_PRECIPITATION, ATTR_FORECAST_TEMP, ATTR_FORECAST_TEMP_LOW) from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.setup import setup_component @@ -45,8 +46,17 @@ class TestWeather(unittest.TestCase): assert data.get(ATTR_WEATHER_OZONE) is None assert data.get(ATTR_WEATHER_ATTRIBUTION) == \ 'Powered by Home Assistant' + assert data.get(ATTR_FORECAST)[0].get(ATTR_FORECAST_CONDITION) == \ + 'rainy' + assert data.get(ATTR_FORECAST)[0].get(ATTR_FORECAST_PRECIPITATION) == 1 assert data.get(ATTR_FORECAST)[0].get(ATTR_FORECAST_TEMP) == 22 + assert data.get(ATTR_FORECAST)[0].get(ATTR_FORECAST_TEMP_LOW) == 15 + assert data.get(ATTR_FORECAST)[6].get(ATTR_FORECAST_CONDITION) == \ + 'fog' + assert data.get(ATTR_FORECAST)[6].get(ATTR_FORECAST_PRECIPITATION) \ + == 0.2 assert data.get(ATTR_FORECAST)[6].get(ATTR_FORECAST_TEMP) == 21 + assert data.get(ATTR_FORECAST)[6].get(ATTR_FORECAST_TEMP_LOW) == 12 assert len(data.get(ATTR_FORECAST)) == 7 def test_temperature_convert(self):