Skip to content

Commit 415329a

Browse files
authored
Merge pull request #143 from golles/clear-night-fix
Fix: Set weather state to clear night when API gives sunny after sunset
2 parents d2db2b3 + a43a77e commit 415329a

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

custom_components/knmi/binary_sensor.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1414
from homeassistant.helpers.typing import StateType
1515
from homeassistant.helpers.update_coordinator import CoordinatorEntity
16-
from homeassistant.util import dt
1716

1817
from .const import DEFAULT_NAME, DOMAIN
1918
from .coordinator import KnmiDataUpdateCoordinator
@@ -39,11 +38,7 @@
3938
KnmiSensorDescription(
4039
key="sun",
4140
translation_key="sun",
42-
value_fn=lambda coordinator: coordinator.get_value_datetime(
43-
["liveweer", 0, "sup"]
44-
)
45-
< dt.now()
46-
< coordinator.get_value_datetime(["liveweer", 0, "sunder"]),
41+
value_fn=lambda coordinator: coordinator.get_is_sun_up(),
4742
attr_fn=lambda coordinator: {
4843
"sunrise": coordinator.get_value_datetime(["liveweer", 0, "sup"]),
4944
"sunset": coordinator.get_value_datetime(["liveweer", 0, "sunder"]),

custom_components/knmi/coordinator.py

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from homeassistant.core import HomeAssistant
1010
from homeassistant.helpers.entity import DeviceInfo
1111
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
12+
from homeassistant.util import dt
1213
import pytz
1314

1415
from .api import KnmiApiClient
@@ -48,6 +49,12 @@ async def _async_update_data(self) -> dict[str, Any]:
4849
_LOGGER.error("Update failed! - %s", exception)
4950
raise UpdateFailed() from exception
5051

52+
def get_is_sun_up(self) -> bool:
53+
"""Helper to get if the sun is currently up"""
54+
sun_up = self.get_value_datetime(["liveweer", 0, "sup"])
55+
sun_under = self.get_value_datetime(["liveweer", 0, "sunder"])
56+
return sun_up < dt.now() < sun_under
57+
5158
def get_value(self, path: list[int | str], default=None) -> Any:
5259
"""
5360
Get a value from the data by a given path.

custom_components/knmi/weather.py

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def condition(self) -> str | None:
123123
self.coordinator.get_value(["liveweer", 0, "image"])
124124
)
125125

126+
if condition == ATTR_CONDITION_SUNNY and not self.coordinator.get_is_sun_up():
127+
condition = ATTR_CONDITION_CLEAR_NIGHT
128+
126129
if condition == ATTR_CONDITION_SNOWY and self.native_temperature > 6:
127130
condition = ATTR_CONDITION_RAINY
128131

tests/fixtures/clear_night_fix.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"liveweer": [
3+
{
4+
"sup": "07:57",
5+
"sunder": "17:51",
6+
"image": "zonnig"
7+
}
8+
]
9+
}

tests/test_weather.py

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from decimal import Decimal
44

5+
from freezegun import freeze_time
56
from homeassistant.components.weather import (
67
ATTR_CONDITION_CLEAR_NIGHT,
78
ATTR_CONDITION_CLOUDY,
@@ -283,3 +284,29 @@ async def test_real_snow(hass: HomeAssistant, mocked_data):
283284

284285
assert await config_entry.async_unload(hass)
285286
await hass.async_block_till_done()
287+
288+
289+
@freeze_time("2023-02-05T15:30:00+00:00")
290+
@pytest.mark.fixture("clear_night_fix.json")
291+
async def test_sunny_during_day(hass: HomeAssistant, mocked_data):
292+
"""When the API returns sunny when the sun isn't set, the weather state should be sunny"""
293+
config_entry = await setup_component(hass)
294+
295+
state = hass.states.get("weather.knmi_home")
296+
assert state.state == ATTR_CONDITION_SUNNY
297+
298+
assert await config_entry.async_unload(hass)
299+
await hass.async_block_till_done()
300+
301+
302+
@freeze_time("2023-02-05T03:30:00+01:00")
303+
@pytest.mark.fixture("clear_night_fix.json")
304+
async def test_clear_night_during_night(hass: HomeAssistant, mocked_data):
305+
"""When the API returns sunny when the sun is set, the weather state should be clear night"""
306+
config_entry = await setup_component(hass)
307+
308+
state = hass.states.get("weather.knmi_home")
309+
assert state.state == ATTR_CONDITION_CLEAR_NIGHT
310+
311+
assert await config_entry.async_unload(hass)
312+
await hass.async_block_till_done()

0 commit comments

Comments
 (0)