Add precipitation probability to weather forcast (#36019)

* Add precipitation probability attribute

* bump env_canada version to 0.0.38 in manifest

* bump env_canada version to 0.0.38 in requirements

* Add support for percipiation probability

* formated project with black

* make sure prob is an int

* fix build break

* update usage to correct naming

* revert bad change and apply fix build break

* add tests
This commit is contained in:
marawan31 2020-06-17 01:39:33 -04:00 committed by GitHub
parent 2b06fbbcf0
commit dba326f16b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 14 deletions

View file

@ -4,6 +4,7 @@ from datetime import timedelta
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
@ -48,13 +49,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
0.5,
TEMP_CELSIUS,
[
["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],
["rainy", 1, 22, 15, 60],
["rainy", 5, 19, 8, 30],
["cloudy", 0, 15, 9, 10],
["sunny", 0, 12, 6, 0],
["partlycloudy", 2, 14, 7, 20],
["rainy", 15, 18, 7, 0],
["fog", 0.2, 21, 12, 100],
],
),
DemoWeather(
@ -66,13 +67,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
4.8,
TEMP_FAHRENHEIT,
[
["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],
["snowy", 2, -10, -15, 60],
["partlycloudy", 1, -13, -14, 25],
["sunny", 0, -18, -22, 70],
["sunny", 0.1, -23, -23, 90],
["snowy", 4, -19, -20, 40],
["sunny", 0.3, -14, -19, 0],
["sunny", 0, -9, -12, 0],
],
),
]
@ -163,6 +164,7 @@ class DemoWeather(WeatherEntity):
ATTR_FORECAST_PRECIPITATION: entry[1],
ATTR_FORECAST_TEMP: entry[2],
ATTR_FORECAST_TEMP_LOW: entry[3],
ATTR_FORECAST_PRECIPITATION_PROBABILITY: entry[4],
}
reftime = reftime + timedelta(hours=4)
forecast_data.append(data_dict)

View file

@ -8,6 +8,7 @@ import voluptuous as vol
from homeassistant.components.weather import (
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
@ -183,6 +184,9 @@ def get_forecast(ec_data, forecast_type):
ATTR_FORECAST_CONDITION: icon_code_to_condition(
int(half_days[0]["icon_code"])
),
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
half_days[0]["precip_probability"]
),
}
)
half_days = half_days[2:]
@ -200,6 +204,9 @@ def get_forecast(ec_data, forecast_type):
ATTR_FORECAST_CONDITION: icon_code_to_condition(
int(half_days[high]["icon_code"])
),
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
half_days[high]["precip_probability"]
),
}
)
@ -215,6 +222,9 @@ def get_forecast(ec_data, forecast_type):
ATTR_FORECAST_CONDITION: icon_code_to_condition(
int(hours[hour]["icon_code"])
),
ATTR_FORECAST_PRECIPITATION_PROBABILITY: int(
hours[hour]["precip_probability"]
),
}
)

View file

@ -19,6 +19,7 @@ ATTR_CONDITION_CLASS = "condition_class"
ATTR_FORECAST = "forecast"
ATTR_FORECAST_CONDITION = "condition"
ATTR_FORECAST_PRECIPITATION = "precipitation"
ATTR_FORECAST_PRECIPITATION_PROBABILITY = "precipitation_probability"
ATTR_FORECAST_TEMP = "temperature"
ATTR_FORECAST_TEMP_LOW = "templow"
ATTR_FORECAST_TIME = "datetime"

View file

@ -6,6 +6,7 @@ from homeassistant.components.weather import (
ATTR_FORECAST,
ATTR_FORECAST_CONDITION,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
ATTR_FORECAST_TEMP,
ATTR_FORECAST_TEMP_LOW,
ATTR_WEATHER_ATTRIBUTION,
@ -56,12 +57,20 @@ class TestWeather(unittest.TestCase):
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_PRECIPITATION_PROBABILITY)
== 60
)
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 (
data.get(ATTR_FORECAST)[6].get(ATTR_FORECAST_PRECIPITATION_PROBABILITY)
== 100
)
assert len(data.get(ATTR_FORECAST)) == 7
def test_temperature_convert(self):