Add humidity to weather forecast (#95064)

* allow humidty in forecast

* Add tests

* float

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
pull/94214/head
Arjan 2023-06-23 00:22:07 +02:00 committed by GitHub
parent e5afff7f98
commit 29ef925d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -71,6 +71,7 @@ ATTR_CONDITION_WINDY = "windy"
ATTR_CONDITION_WINDY_VARIANT = "windy-variant"
ATTR_FORECAST = "forecast"
ATTR_FORECAST_CONDITION: Final = "condition"
ATTR_FORECAST_HUMIDITY: Final = "humidity"
ATTR_FORECAST_NATIVE_PRECIPITATION: Final = "native_precipitation"
ATTR_FORECAST_PRECIPITATION: Final = "precipitation"
ATTR_FORECAST_PRECIPITATION_PROBABILITY: Final = "precipitation_probability"
@ -125,6 +126,7 @@ class Forecast(TypedDict, total=False):
condition: str | None
datetime: Required[str]
humidity: float | None
precipitation_probability: int | None
cloud_coverage: int | None
native_precipitation: float | None
@ -869,6 +871,18 @@ class WeatherEntity(Entity):
ROUNDING_PRECISION,
)
if (
forecast_humidity := forecast_entry.pop(
ATTR_FORECAST_HUMIDITY,
None,
)
) is not None:
with suppress(TypeError, ValueError):
forecast_humidity_f = float(forecast_humidity)
forecast_entry[ATTR_FORECAST_HUMIDITY] = round(
forecast_humidity_f
)
forecast.append(forecast_entry)
data[ATTR_FORECAST] = forecast

View File

@ -8,6 +8,7 @@ from homeassistant.components.weather import (
ATTR_FORECAST,
ATTR_FORECAST_APPARENT_TEMP,
ATTR_FORECAST_DEW_POINT,
ATTR_FORECAST_HUMIDITY,
ATTR_FORECAST_PRECIPITATION,
ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
@ -34,6 +35,7 @@ from homeassistant.components.weather import (
from homeassistant.components.weather.const import (
ATTR_WEATHER_CLOUD_COVERAGE,
ATTR_WEATHER_DEW_POINT,
ATTR_WEATHER_HUMIDITY,
)
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
@ -557,6 +559,21 @@ async def test_wind_bearing_ozone_and_cloud_coverage(
assert float(state.attributes[ATTR_WEATHER_CLOUD_COVERAGE]) == 75
async def test_humidity(
hass: HomeAssistant,
enable_custom_integrations: None,
) -> None:
"""Test humidity."""
humidity_value = 80.2
entity0 = await create_entity(hass, humidity=humidity_value)
state = hass.states.get(entity0.entity_id)
forecast = state.attributes[ATTR_FORECAST][0]
assert float(state.attributes[ATTR_WEATHER_HUMIDITY]) == 80
assert float(forecast[ATTR_FORECAST_HUMIDITY]) == 80
async def test_none_forecast(
hass: HomeAssistant,
enable_custom_integrations: None,

View File

@ -6,6 +6,7 @@ from __future__ import annotations
from homeassistant.components.weather import (
ATTR_FORECAST_CLOUD_COVERAGE,
ATTR_FORECAST_HUMIDITY,
ATTR_FORECAST_NATIVE_APPARENT_TEMP,
ATTR_FORECAST_NATIVE_DEW_POINT,
ATTR_FORECAST_NATIVE_PRECIPITATION,
@ -223,6 +224,7 @@ class MockWeatherMockForecast(MockWeather):
ATTR_FORECAST_NATIVE_PRECIPITATION: self._values.get(
"native_precipitation"
),
ATTR_FORECAST_HUMIDITY: self.humidity,
}
]