Skip to content

Commit

Permalink
When getting daily forecast, the min temperature of the current day s…
Browse files Browse the repository at this point in the history
…hould be the min temperature of the coming night
  • Loading branch information
jdejaegh committed Dec 29, 2023
1 parent 15925ba commit fe0b341
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
2 changes: 2 additions & 0 deletions custom_components/irm_kmi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ async def async_forecast_daily(self) -> list[Forecast] | None:
data: list[Forecast] = self.coordinator.data.get('daily_forecast')
if not isinstance(data, list):
return None
if len(data) > 1 and data[0].get('native_templow') is None and not data[1].get('is_daytime'):
data[0]['native_templow'] = data[1]['native_templow']
return [f for f in data if f.get('is_daytime')]

async def async_forecast_hourly(self) -> list[Forecast] | None:
Expand Down
45 changes: 30 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
from custom_components.irm_kmi.const import DOMAIN, CONF_STYLE, CONF_STYLE_STD, CONF_DARK_MODE


async def patched(url: str, params: dict | None = None) -> bytes:
if "cdn.knmi.nl" in url:
file_name = "tests/fixtures/clouds_nl.png"
elif "app.meteo.be/services/appv4/?s=getIncaImage" in url:
file_name = "tests/fixtures/clouds_be.png"
elif "getLocalizationLayerBE" in url:
file_name = "tests/fixtures/loc_layer_be_n.png"
elif "getLocalizationLayerNL" in url:
file_name = "tests/fixtures/loc_layer_nl.png"
else:
raise ValueError("Not a valid parameter for the mock")

with open(file_name, "rb") as file:
return file.read()

@pytest.fixture(autouse=True)
def auto_enable_custom_integrations(enable_custom_integrations):
yield
Expand Down Expand Up @@ -61,7 +76,6 @@ def mock_irm_kmi_api_out_benelux(request: pytest.FixtureRequest) -> Generator[No
fixture: str = "forecast_out_of_benelux.json"

forecast = json.loads(load_fixture(fixture))
print(type(forecast))
with patch(
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
) as irm_kmi_api_mock:
Expand All @@ -85,26 +99,27 @@ def mock_exception_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None
def mock_image_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked IrmKmi api client."""

async def patched(url: str, params: dict | None = None) -> bytes:
if "cdn.knmi.nl" in url:
file_name = "tests/fixtures/clouds_nl.png"
elif "app.meteo.be/services/appv4/?s=getIncaImage" in url:
file_name = "tests/fixtures/clouds_be.png"
elif "getLocalizationLayerBE" in url:
file_name = "tests/fixtures/loc_layer_be_n.png"
elif "getLocalizationLayerNL" in url:
file_name = "tests/fixtures/loc_layer_nl.png"
else:
raise ValueError("Not a valid parameter for the mock")

with open(file_name, "rb") as file:
return file.read()
with patch(
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
) as irm_kmi_api_mock:
irm_kmi = irm_kmi_api_mock.return_value
irm_kmi.get_image.side_effect = patched
yield irm_kmi


@pytest.fixture()
def mock_image_and_nl_forecast_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked IrmKmi api client."""
fixture: str = "forecast_nl.json"

forecast = json.loads(load_fixture(fixture))

with patch(
"custom_components.irm_kmi.coordinator.IrmKmiApiClient", autospec=True
) as irm_kmi_api_mock:
irm_kmi = irm_kmi_api_mock.return_value
irm_kmi.get_image.side_effect = patched
irm_kmi.get_forecasts_coord.return_value = forecast
yield irm_kmi


Expand Down
1 change: 0 additions & 1 deletion tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from homeassistant.components.weather import (ATTR_CONDITION_CLOUDY,
ATTR_CONDITION_PARTLYCLOUDY,
ATTR_CONDITION_RAINY, Forecast)
from homeassistant.components.zone import Zone
from homeassistant.core import HomeAssistant
from PIL import Image, ImageDraw, ImageFont
from pytest_homeassistant_custom_component.common import load_fixture, MockConfigEntry
Expand Down
33 changes: 33 additions & 0 deletions tests/test_weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime
from unittest.mock import AsyncMock

from freezegun import freeze_time
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.irm_kmi import IrmKmiCoordinator, IrmKmiWeather


@freeze_time(datetime.fromisoformat("2023-12-28T15:30:00+01:00"))
async def test_weather_nl(
hass: HomeAssistant,
mock_image_and_nl_forecast_irm_kmi_api: AsyncMock,
mock_config_entry: MockConfigEntry
) -> None:
hass.states.async_set(
"zone.home",
0,
{"latitude": 50.738681639, "longitude": 4.054077148},
)
coordinator = IrmKmiCoordinator(hass, mock_config_entry)
await coordinator.async_config_entry_first_refresh()

weather = IrmKmiWeather(coordinator, mock_config_entry)
result = await weather.async_forecast_daily()

assert isinstance(result, list)
assert len(result) == 7

# When getting daily forecast, the min temperature of the current day
# should be the min temperature of the coming night
assert result[0]['native_templow'] == 9

0 comments on commit fe0b341

Please sign in to comment.