From 29ef925d7352f8643952011c3f7577eae85b19da Mon Sep 17 00:00:00 2001 From: Arjan <44190435+vingerha@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:22:07 +0200 Subject: [PATCH] Add humidity to weather forecast (#95064) * allow humidty in forecast * Add tests * float --------- Co-authored-by: G Johansson --- homeassistant/components/weather/__init__.py | 14 ++++++++++++++ tests/components/weather/test_init.py | 17 +++++++++++++++++ .../custom_components/test/weather.py | 2 ++ 3 files changed, 33 insertions(+) diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index 351f61600f9..8640e90c639 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -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 diff --git a/tests/components/weather/test_init.py b/tests/components/weather/test_init.py index af2e1a5d6cb..37f0fd62328 100644 --- a/tests/components/weather/test_init.py +++ b/tests/components/weather/test_init.py @@ -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, diff --git a/tests/testing_config/custom_components/test/weather.py b/tests/testing_config/custom_components/test/weather.py index 344e879dc08..e4c7e380663 100644 --- a/tests/testing_config/custom_components/test/weather.py +++ b/tests/testing_config/custom_components/test/weather.py @@ -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, } ]