Report correct weather condition at night for OpenWeatherMap (#42982)
This commit is contained in:
parent
945a0a9f7e
commit
f221bfae52
2 changed files with 20 additions and 4 deletions
|
@ -149,6 +149,7 @@ LANGUAGES = [
|
|||
"zh_tw",
|
||||
"zu",
|
||||
]
|
||||
WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT = 800
|
||||
CONDITION_CLASSES = {
|
||||
ATTR_CONDITION_CLOUDY: [803, 804],
|
||||
ATTR_CONDITION_FOG: [701, 741],
|
||||
|
@ -160,7 +161,7 @@ CONDITION_CLASSES = {
|
|||
ATTR_CONDITION_RAINY: [300, 301, 302, 310, 311, 312, 313, 500, 501, 520, 521],
|
||||
ATTR_CONDITION_SNOWY: [600, 601, 602, 611, 612, 620, 621, 622],
|
||||
ATTR_CONDITION_SNOWY_RAINY: [511, 615, 616],
|
||||
ATTR_CONDITION_SUNNY: [800],
|
||||
ATTR_CONDITION_SUNNY: [WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT],
|
||||
ATTR_CONDITION_WINDY: [905, 951, 952, 953, 954, 955, 956, 957],
|
||||
ATTR_CONDITION_WINDY_VARIANT: [958, 959, 960, 961],
|
||||
ATTR_CONDITION_EXCEPTIONAL: [
|
||||
|
|
|
@ -6,6 +6,8 @@ import async_timeout
|
|||
from pyowm.commons.exceptions import APIRequestError, UnauthorizedError
|
||||
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_CONDITION_CLEAR_NIGHT,
|
||||
ATTR_CONDITION_SUNNY,
|
||||
ATTR_FORECAST_CONDITION,
|
||||
ATTR_FORECAST_PRECIPITATION,
|
||||
ATTR_FORECAST_TEMP,
|
||||
|
@ -14,7 +16,9 @@ from homeassistant.components.weather import (
|
|||
ATTR_FORECAST_WIND_BEARING,
|
||||
ATTR_FORECAST_WIND_SPEED,
|
||||
)
|
||||
from homeassistant.helpers import sun
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.util import dt
|
||||
|
||||
from .const import (
|
||||
ATTR_API_CLOUDS,
|
||||
|
@ -35,6 +39,7 @@ from .const import (
|
|||
FORECAST_MODE_HOURLY,
|
||||
FORECAST_MODE_ONECALL_DAILY,
|
||||
FORECAST_MODE_ONECALL_HOURLY,
|
||||
WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -139,7 +144,9 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
),
|
||||
ATTR_FORECAST_WIND_SPEED: entry.wind().get("speed"),
|
||||
ATTR_FORECAST_WIND_BEARING: entry.wind().get("deg"),
|
||||
ATTR_FORECAST_CONDITION: self._get_condition(entry.weather_code),
|
||||
ATTR_FORECAST_CONDITION: self._get_condition(
|
||||
entry.weather_code, entry.reference_time("unix")
|
||||
),
|
||||
}
|
||||
|
||||
temperature_dict = entry.temperature("celsius")
|
||||
|
@ -186,9 +193,17 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
return None
|
||||
return round(rain_value + snow_value, 1)
|
||||
|
||||
@staticmethod
|
||||
def _get_condition(weather_code):
|
||||
def _get_condition(self, weather_code, timestamp=None):
|
||||
"""Get weather condition from weather data."""
|
||||
if weather_code == WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT:
|
||||
|
||||
if timestamp:
|
||||
timestamp = dt.utc_from_timestamp(timestamp)
|
||||
|
||||
if sun.is_up(self.hass, timestamp):
|
||||
return ATTR_CONDITION_SUNNY
|
||||
return ATTR_CONDITION_CLEAR_NIGHT
|
||||
|
||||
return [k for k, v in CONDITION_CLASSES.items() if weather_code in v][0]
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue