Skip to content

Commit

Permalink
Add tests for weather radar data
Browse files Browse the repository at this point in the history
  • Loading branch information
jdejaegh committed Dec 28, 2023
1 parent f392e8b commit aae39d8
Show file tree
Hide file tree
Showing 11 changed files with 1,512 additions and 170 deletions.
3 changes: 1 addition & 2 deletions custom_components/irm_kmi/camera.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Create a radar view for IRM KMI weather"""
# File inspired by https://github.com/jodur/imagesdirectory-camera/blob/main/custom_components/imagedirectory/camera.py

import logging

Expand Down Expand Up @@ -47,7 +46,7 @@ def __init__(self,

self._image_index = 0

@property # Baseclass Camera property override
@property
def frame_interval(self) -> float:
"""Return the interval between frames of the mjpeg stream"""
return 0.3
Expand Down
34 changes: 29 additions & 5 deletions custom_components/irm_kmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ async def process_api_data(self, api_data: dict) -> ProcessedCoordinatorData:

async def download_images_from_api(self, animation_data, country, localisation_layer_url):
coroutines = list()
coroutines.append(self._api_client.get_image(f"{localisation_layer_url}&th={'d' if country == 'NL' else 'n'}"))
coroutines.append(
self._api_client.get_image(localisation_layer_url,
params={'th': 'd' if country == 'NL' else 'n'}))

for frame in animation_data:
if frame.get('uri', None) is not None:
coroutines.append(self._api_client.get_image(frame.get('uri')))
Expand Down Expand Up @@ -184,6 +187,7 @@ def current_weather_from_data(api_data: dict) -> CurrentWeatherData:
for current in hourly_forecast_data[:2]:
if datetime.now().strftime('%H') == current['hour']:
now_hourly = current
break
# Get UV index
module_data = api_data.get('module', None)
uv_index = None
Expand All @@ -192,13 +196,33 @@ def current_weather_from_data(api_data: dict) -> CurrentWeatherData:
if module.get('type', None) == 'uv':
uv_index = module.get('data', {}).get('levelValue')

try:
pressure = float(now_hourly.get('pressure', None)) if now_hourly is not None else None
except TypeError:
pressure = None

try:
wind_speed = float(now_hourly.get('windSpeedKm', None)) if now_hourly is not None else None
except TypeError:
wind_speed = None

try:
wind_gust_speed = float(now_hourly.get('windPeakSpeedKm', None)) if now_hourly is not None else None
except TypeError:
wind_gust_speed = None

try:
temperature = float(api_data.get('obs', {}).get('temp'))
except TypeError:
temperature = None

current_weather = CurrentWeatherData(
condition=CDT_MAP.get((api_data.get('obs', {}).get('ww'), api_data.get('obs', {}).get('dayNight')), None),
temperature=api_data.get('obs', {}).get('temp'),
wind_speed=now_hourly.get('windSpeedKm', None) if now_hourly is not None else None,
wind_gust_speed=now_hourly.get('windPeakSpeedKm', None) if now_hourly is not None else None,
temperature=temperature,
wind_speed=wind_speed,
wind_gust_speed=wind_gust_speed,
wind_bearing=now_hourly.get('windDirectionText', {}).get('en') if now_hourly is not None else None,
pressure=now_hourly.get('pressure', None) if now_hourly is not None else None,
pressure=pressure,
uv_index=uv_index
)

Expand Down
30 changes: 28 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def mock_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None, MagicMoc
fixture: str = "forecast.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 Down Expand Up @@ -81,8 +80,35 @@ def mock_exception_irm_kmi_api(request: pytest.FixtureRequest) -> Generator[None


@pytest.fixture()
def mock_coordinator(request: pytest.FixtureRequest) -> Generator[None, MagicMock, 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_d.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_coordinator(request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]:
"""Return a mocked coordinator."""
with patch(
"custom_components.irm_kmi.IrmKmiCoordinator", autospec=True
) as coordinator_mock:
Expand Down
Binary file added tests/fixtures/clouds_be.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/fixtures/clouds_nl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 1 addition & 153 deletions tests/fixtures/forecast.json
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@
}
],
"animation": {
"localisationLayer": "https:\/\/app.meteo.be\/services\/appv4\/?s=getLocalizationLayer&ins=92094&f=2&k=2c886c51e74b671c8fc3865f4a0e9318",
"localisationLayer": "https:\/\/app.meteo.be\/services\/appv4\/?s=getLocalizationLayerBE&ins=92094&f=2&k=2c886c51e74b671c8fc3865f4a0e9318",
"localisationLayerRatioX": 0.6667,
"localisationLayerRatioY": 0.523,
"speed": 0.3,
Expand Down Expand Up @@ -1459,158 +1459,6 @@
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T18:50:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261800&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:00:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261810&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:10:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261820&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:20:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261830&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:30:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261840&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:40:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261850&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T19:50:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261900&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:00:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261910&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:10:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261920&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:20:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261930&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:30:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261940&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:40:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312261950&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T20:50:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262000&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:00:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262010&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:10:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262020&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:20:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262030&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:30:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262040&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:40:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262050&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
},
{
"time": "2023-12-26T21:50:00+01:00",
"uri": "https:\/\/app.meteo.be\/services\/appv4\/?s=getIncaImage&i=202312262100&f=2&k=4a71be18d6cb09f98c49c53f59902f8c&d=202312261720",
"value": 0,
"position": 0,
"positionLower": 0,
"positionHigher": 0
}
],
"threshold": [],
Expand Down
Loading

0 comments on commit aae39d8

Please sign in to comment.