Skip to content

Commit

Permalink
Add new attributes to OpenWeatherMap weather entity
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunglung committed Feb 21, 2024
1 parent ceb1481 commit 13c00f2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
14 changes: 14 additions & 0 deletions custom_components/opencwb/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
49 changes: 49 additions & 0 deletions custom_components/opencwb/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down
13 changes: 12 additions & 1 deletion custom_components/opencwb/weather_update_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 13c00f2

Please sign in to comment.