Skip to content

Commit

Permalink
Merge pull request #35 from PavelChertkov/main
Browse files Browse the repository at this point in the history
Fix daytime recognition for polar days
  • Loading branch information
dimastark authored Nov 8, 2024
2 parents 9d47ce0 + 782bc58 commit 8b092bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
27 changes: 27 additions & 0 deletions custom_components/yandex_pogoda/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import zlib

from collections.abc import Callable
from datetime import datetime

from homeassistant.components.weather import (
ATTR_WEATHER_TEMPERATURE_UNIT,
Expand Down Expand Up @@ -36,6 +37,7 @@
ATTR_API_DAYTIME = "daytime"
ATTR_API_CONDITION = "condition"
ATTR_API_IMAGE = "icon"
ATTR_API_POLAR = "polar"
ATTR_API_SERVER_TIME = "serverTime"
ATTR_API_TIME = "time"
ATTR_API_SUNRISE_BEGIN_TIME = "sunriseBeginTime"
Expand Down Expand Up @@ -218,3 +220,28 @@ def decompress_data(compressed: str | None) -> dict:
return pickle.loads(zlib.decompress(base64.b64decode(compressed)))
except TypeError: # for backward compatibility
return {}


def is_daytime(
hour: datetime,
day: datetime,
sunrise_begin: datetime,
sunset_end: datetime,
polar: str,
) -> bool:
"""returns whether it is daytime for a given hour of the day"""

if polar:
if day == sunrise_begin and day == sunset_end:
return polar == "DAY"

if day == sunrise_begin:
return hour < sunset_end

if day == sunset_end:
return hour >= sunrise_begin

if sunrise_begin <= sunset_end:
return hour >= sunrise_begin and hour < sunset_end
else:
return hour < sunset_end or hour >= sunrise_begin
2 changes: 1 addition & 1 deletion custom_components/yandex_pogoda/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/yandex/pogoda-home-assistant/issues",
"requirements": [],
"version": "0.0.4"
"version": "0.0.5"
}
10 changes: 8 additions & 2 deletions custom_components/yandex_pogoda/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ATTR_API_DAYTIME,
ATTR_API_FEELS_LIKE_TEMPERATURE,
ATTR_API_IMAGE,
ATTR_API_POLAR,
ATTR_API_SERVER_TIME,
ATTR_API_SUNRISE_BEGIN_TIME,
ATTR_API_SUNRISE_END_TIME,
Expand All @@ -56,6 +57,7 @@
WEATHER_STATES_CONVERSION,
YA_CONDITION_STATE_MAP,
get_wind_intercardinal_direction,
is_daytime,
map_state,
)

Expand All @@ -79,6 +81,7 @@
}}
forecast {{
days {{
{ATTR_API_POLAR}
{ATTR_API_TIME}
{ATTR_API_SUNRISE_BEGIN_TIME}
{ATTR_API_SUNRISE_END_TIME}
Expand Down Expand Up @@ -302,6 +305,8 @@ async def update(self):
for day in weather_by_point["forecast"]["days"]:
sunrise_begin = parser.parse(day[ATTR_API_SUNRISE_BEGIN_TIME])
sunset_end = parser.parse(day[ATTR_API_SUNRISE_END_TIME])
day_dt = parser.parse(day[ATTR_API_TIME])
polar = day.get(ATTR_API_POLAR)
for hour in day["hours"]:
hour_dt = parser.parse(hour[ATTR_API_TIME])
if now_dt > hour_dt:
Expand All @@ -312,12 +317,13 @@ async def update(self):
dst=hour_forecast,
src=hour,
attributes=FORECAST_HOUR_ATTRIBUTE_TRANSLATION,
is_day=sunrise_begin <= hour_dt < sunset_end,
is_day=is_daytime(
hour_dt, day_dt, sunrise_begin, sunset_end, polar
),
)
result[ATTR_FORECAST_DATA][ATTR_FORECAST_HOURLY].append(hour_forecast)
result[ATTR_FORECAST_HOURLY_ICONS].append(hour.get(ATTR_API_IMAGE))

day_dt = parser.parse(day[ATTR_API_TIME]) + timedelta(hours=2)
day_part = day["summary"]["day"]
night_part = day["summary"]["night"]
for part, is_day in ((night_part, False), (day_part, True)):
Expand Down

0 comments on commit 8b092bb

Please sign in to comment.