Skip to content

Commit

Permalink
Merge pull request #143 from golles/clear-night-fix
Browse files Browse the repository at this point in the history
Fix: Set weather state to clear night when API gives sunny after sunset
  • Loading branch information
golles authored Apr 21, 2024
2 parents d2db2b3 + a43a77e commit 415329a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
7 changes: 1 addition & 6 deletions custom_components/knmi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt

from .const import DEFAULT_NAME, DOMAIN
from .coordinator import KnmiDataUpdateCoordinator
Expand All @@ -39,11 +38,7 @@
KnmiSensorDescription(
key="sun",
translation_key="sun",
value_fn=lambda coordinator: coordinator.get_value_datetime(
["liveweer", 0, "sup"]
)
< dt.now()
< coordinator.get_value_datetime(["liveweer", 0, "sunder"]),
value_fn=lambda coordinator: coordinator.get_is_sun_up(),
attr_fn=lambda coordinator: {
"sunrise": coordinator.get_value_datetime(["liveweer", 0, "sup"]),
"sunset": coordinator.get_value_datetime(["liveweer", 0, "sunder"]),
Expand Down
7 changes: 7 additions & 0 deletions custom_components/knmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt
import pytz

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

def get_is_sun_up(self) -> bool:
"""Helper to get if the sun is currently up"""
sun_up = self.get_value_datetime(["liveweer", 0, "sup"])
sun_under = self.get_value_datetime(["liveweer", 0, "sunder"])
return sun_up < dt.now() < sun_under

def get_value(self, path: list[int | str], default=None) -> Any:
"""
Get a value from the data by a given path.
Expand Down
3 changes: 3 additions & 0 deletions custom_components/knmi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def condition(self) -> str | None:
self.coordinator.get_value(["liveweer", 0, "image"])
)

if condition == ATTR_CONDITION_SUNNY and not self.coordinator.get_is_sun_up():
condition = ATTR_CONDITION_CLEAR_NIGHT

if condition == ATTR_CONDITION_SNOWY and self.native_temperature > 6:
condition = ATTR_CONDITION_RAINY

Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/clear_night_fix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"liveweer": [
{
"sup": "07:57",
"sunder": "17:51",
"image": "zonnig"
}
]
}
27 changes: 27 additions & 0 deletions tests/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from decimal import Decimal

from freezegun import freeze_time
from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
Expand Down Expand Up @@ -283,3 +284,29 @@ async def test_real_snow(hass: HomeAssistant, mocked_data):

assert await config_entry.async_unload(hass)
await hass.async_block_till_done()


@freeze_time("2023-02-05T15:30:00+00:00")
@pytest.mark.fixture("clear_night_fix.json")
async def test_sunny_during_day(hass: HomeAssistant, mocked_data):
"""When the API returns sunny when the sun isn't set, the weather state should be sunny"""
config_entry = await setup_component(hass)

state = hass.states.get("weather.knmi_home")
assert state.state == ATTR_CONDITION_SUNNY

assert await config_entry.async_unload(hass)
await hass.async_block_till_done()


@freeze_time("2023-02-05T03:30:00+01:00")
@pytest.mark.fixture("clear_night_fix.json")
async def test_clear_night_during_night(hass: HomeAssistant, mocked_data):
"""When the API returns sunny when the sun is set, the weather state should be clear night"""
config_entry = await setup_component(hass)

state = hass.states.get("weather.knmi_home")
assert state.state == ATTR_CONDITION_CLEAR_NIGHT

assert await config_entry.async_unload(hass)
await hass.async_block_till_done()

0 comments on commit 415329a

Please sign in to comment.