Skip to content

Commit

Permalink
Report correct weather condition at night for OpenWeatherMap (home-as…
Browse files Browse the repository at this point in the history
  • Loading branch information
springstan authored Nov 30, 2020
1 parent 945a0a9 commit f221bfa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/openweathermap/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"zh_tw",
"zu",
]
WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT = 800
CONDITION_CLASSES = {
ATTR_CONDITION_CLOUDY: [803, 804],
ATTR_CONDITION_FOG: [701, 741],
Expand All @@ -160,7 +161,7 @@
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: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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,
Expand All @@ -14,7 +16,9 @@
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,
Expand All @@ -35,6 +39,7 @@
FORECAST_MODE_HOURLY,
FORECAST_MODE_ONECALL_DAILY,
FORECAST_MODE_ONECALL_HOURLY,
WEATHER_CODE_SUNNY_OR_CLEAR_NIGHT,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -139,7 +144,9 @@ def _convert_forecast(self, entry):
),
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")
Expand Down Expand Up @@ -186,9 +193,17 @@ def _calc_precipitation(rain, snow):
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]


Expand Down

0 comments on commit f221bfa

Please sign in to comment.