From 13c00f2667f10ad8c576d965856f11dbf40d5cf2 Mon Sep 17 00:00:00 2001 From: tsunglung Date: Wed, 21 Feb 2024 09:22:34 +0800 Subject: [PATCH] Add new attributes to OpenWeatherMap weather entity --- custom_components/opencwb/const.py | 14 ++++++ custom_components/opencwb/weather.py | 49 +++++++++++++++++++ .../opencwb/weather_update_coordinator.py | 13 ++++- 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/custom_components/opencwb/const.py b/custom_components/opencwb/const.py index ed9843f..130a678 100755 --- a/custom_components/opencwb/const.py +++ b/custom_components/opencwb/const.py @@ -53,6 +53,7 @@ ATTR_API_WEATHER = "weather" ATTR_API_TEMPERATURE = "temperature" ATTR_API_FEELS_LIKE_TEMPERATURE = "feels_like_temperature" +ATTR_API_WIND_GUST = "wind_gust" ATTR_API_WIND_SPEED = "wind_speed" ATTR_API_WIND_BEARING = "wind_bearing" ATTR_API_HUMIDITY = "humidity" @@ -70,6 +71,19 @@ UPDATE_LISTENER = "update_listener" PLATFORMS = ["sensor", "weather"] +ATTR_API_FORECAST_CLOUDS = "clouds" +ATTR_API_FORECAST_CONDITION = "condition" +ATTR_API_FORECAST_FEELS_LIKE_TEMPERATURE = "feels_like_temperature" +ATTR_API_FORECAST_HUMIDITY = "humidity" +ATTR_API_FORECAST_PRECIPITATION = "precipitation" +ATTR_API_FORECAST_PRECIPITATION_PROBABILITY = "precipitation_probability" +ATTR_API_FORECAST_PRESSURE = "pressure" +ATTR_API_FORECAST_TEMP = "temperature" +ATTR_API_FORECAST_TEMP_LOW = "templow" +ATTR_API_FORECAST_TIME = "datetime" +ATTR_API_FORECAST_WIND_BEARING = "wind_bearing" +ATTR_API_FORECAST_WIND_SPEED = "wind_speed" + FORECAST_MODE_HOURLY = "hourly" FORECAST_MODE_DAILY = "daily" FORECAST_MODE_FREE_DAILY = "freedaily" diff --git a/custom_components/opencwb/weather.py b/custom_components/opencwb/weather.py index 1b77fa2..11d5b8d 100755 --- a/custom_components/opencwb/weather.py +++ b/custom_components/opencwb/weather.py @@ -9,12 +9,16 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import ( + ATTR_API_CLOUDS, ATTR_API_CONDITION, + ATTR_API_DEW_POINT, + ATTR_API_FEELS_LIKE_TEMPERATURE, ATTR_API_FORECAST, ATTR_API_HUMIDITY, ATTR_API_PRESSURE, ATTR_API_TEMPERATURE, ATTR_API_WIND_BEARING, + ATTR_API_WIND_GUST, ATTR_API_WIND_SPEED, ATTRIBUTION, DEFAULT_NAME, @@ -98,6 +102,51 @@ async def async_added_to_hass(self): self.async_write_ha_state) ) + @property + def cloud_coverage(self) -> float | None: + """Return the Cloud coverage in %.""" + return self._weather_coordinator.data[ATTR_API_CLOUDS] + + @property + def native_apparent_temperature(self) -> float | None: + """Return the apparent temperature.""" + return self._weather_coordinator.data[ATTR_API_FEELS_LIKE_TEMPERATURE] + + @property + def native_temperature(self) -> float | None: + """Return the temperature.""" + return self._weather_coordinator.data[ATTR_API_TEMPERATURE] + + @property + def native_pressure(self) -> float | None: + """Return the pressure.""" + return self._weather_coordinator.data[ATTR_API_PRESSURE] + + @property + def humidity(self) -> float | None: + """Return the humidity.""" + return self._weather_coordinator.data[ATTR_API_HUMIDITY] + + @property + def native_dew_point(self) -> float | None: + """Return the dew point.""" + return self._weather_coordinator.data[ATTR_API_DEW_POINT] + + @property + def native_wind_gust_speed(self) -> float | None: + """Return the wind gust speed.""" + return self._weather_coordinator.data[ATTR_API_WIND_GUST] + + @property + def native_wind_speed(self) -> float | None: + """Return the wind speed.""" + return self._weather_coordinator.data[ATTR_API_WIND_SPEED] + + @property + def wind_bearing(self) -> float | str | None: + """Return the wind bearing.""" + return self._weather_coordinator.data[ATTR_API_WIND_BEARING] + @property def forecast(self) -> list[Forecast] | None: """Return the forecast array.""" diff --git a/custom_components/opencwb/weather_update_coordinator.py b/custom_components/opencwb/weather_update_coordinator.py index 08bda4e..d7ef776 100755 --- a/custom_components/opencwb/weather_update_coordinator.py +++ b/custom_components/opencwb/weather_update_coordinator.py @@ -32,6 +32,10 @@ ATTR_API_DEW_POINT, ATTR_API_FEELS_LIKE_TEMPERATURE, ATTR_API_FORECAST, + ATTR_API_FORECAST_CLOUDS, + ATTR_API_FORECAST_CONDITION, + ATTR_API_FORECAST_FEELS_LIKE_TEMPERATURE, + ATTR_API_FORECAST_HUMIDITY, ATTR_API_HUMIDITY, ATTR_API_PRECIPITATION_KIND, ATTR_API_PRESSURE, @@ -42,6 +46,7 @@ ATTR_API_WEATHER, ATTR_API_WEATHER_CODE, ATTR_API_WIND_BEARING, + ATTR_API_WIND_GUST, ATTR_API_WIND_SPEED, CONDITION_CLASSES, DOMAIN, @@ -145,6 +150,7 @@ def _convert_weather_response(self, weather_response): ATTR_API_PRESSURE: current_weather.pressure.get("press"), ATTR_API_HUMIDITY: current_weather.humidity, ATTR_API_WIND_BEARING: current_weather.wind().get("deg"), + ATTR_API_WIND_GUST: current_weather.wind().get("gust"), ATTR_API_WIND_SPEED: current_weather.wind().get("speed"), ATTR_API_CLOUDS: current_weather.clouds, ATTR_API_RAIN: self._get_rain(current_weather.rain), @@ -186,9 +192,14 @@ def _convert_forecast(self, entry): ATTR_FORECAST_PRESSURE: entry.pressure.get("press"), ATTR_FORECAST_WIND_SPEED: entry.wind().get("speed"), ATTR_FORECAST_WIND_BEARING: entry.wind().get("deg"), - ATTR_FORECAST_CONDITION: self._get_condition( + ATTR_API_FORECAST_CONDITION: self._get_condition( entry.weather_code, entry.reference_time("unix") ), + ATTR_API_FORECAST_CLOUDS: entry.clouds, + ATTR_API_FORECAST_FEELS_LIKE_TEMPERATURE: entry.temperature("celsius").get( + "feels_like_day" + ), + ATTR_API_FORECAST_HUMIDITY: entry.humidity, } temperature_dict = entry.temperature("celsius")