mirror of
https://github.com/jdejaegh/irm-kmi-ha.git
synced 2025-06-27 11:39:26 +02:00
When getting daily forecast, the min temperature of the current day should be the min temperature of the coming night
This commit is contained in:
parent
15925baab7
commit
fe0b3419ce
4 changed files with 64 additions and 15 deletions
|
@ -106,6 +106,8 @@ class IrmKmiWeather(CoordinatorEntity, WeatherEntity):
|
|||
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:
|
||||
|
|
|
@ -14,6 +14,21 @@ from custom_components.irm_kmi.api import IrmKmiApiParametersError
|
|||
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
|
||||
|
@ -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:
|
||||
|
@ -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 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
|
||||
|
||||
with open(file_name, "rb") as file:
|
||||
return file.read()
|
||||
|
||||
@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
|
||||
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ from freezegun import freeze_time
|
|||
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
|
||||
|
|
33
tests/test_weather.py
Normal file
33
tests/test_weather.py
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue