From 27fab148b63dcc7475b1c6bd24c19e40c77e7227 Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Sun, 31 Dec 2023 23:36:57 +0100 Subject: [PATCH] Prepare data structures for rain graph --- custom_components/irm_kmi/coordinator.py | 7 +++---- custom_components/irm_kmi/data.py | 8 +++++++- requirements.txt | 4 +++- tests/test_coordinator.py | 8 ++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/custom_components/irm_kmi/coordinator.py b/custom_components/irm_kmi/coordinator.py index d051bfb..9459db3 100644 --- a/custom_components/irm_kmi/coordinator.py +++ b/custom_components/irm_kmi/coordinator.py @@ -194,15 +194,14 @@ async def merge_frames_from_api(self, ) if most_recent_frame is None and current_time < time_image: - recent_idx = idx - 1 if idx > 0 else idx - most_recent_frame = sequence[recent_idx].get('image', None) + most_recent_frame = idx - 1 if idx > 0 else idx background.close() - most_recent_frame = most_recent_frame if most_recent_frame is not None else sequence[-1].get('image') + most_recent_frame = most_recent_frame if most_recent_frame is not None else -1 return RadarAnimationData( sequence=sequence, - most_recent_image=most_recent_frame + most_recent_image_idx=most_recent_frame ) @staticmethod diff --git a/custom_components/irm_kmi/data.py b/custom_components/irm_kmi/data.py index 539124d..3f3978e 100644 --- a/custom_components/irm_kmi/data.py +++ b/custom_components/irm_kmi/data.py @@ -28,13 +28,19 @@ class AnimationFrameData(TypedDict, total=False): """Holds one single frame of the radar camera, along with the timestamp of the frame""" time: datetime | None image: bytes | None + value: float | None + position: float | None + position_higher: float | None + position_lower: float | None + rain_graph: bytes | None class RadarAnimationData(TypedDict, total=False): """Holds frames and additional data for the animation to be rendered""" sequence: List[AnimationFrameData] | None - most_recent_image: bytes | None + most_recent_image_idx: int | None hint: str | None + unit: str | None class ProcessedCoordinatorData(TypedDict, total=False): diff --git a/requirements.txt b/requirements.txt index d370254..9aae101 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,6 @@ async_timeout==4.0.3 homeassistant==2023.12.3 voluptuous==0.13.1 Pillow==10.1.0 -pytz==2023.3.post1 \ No newline at end of file +pytz==2023.3.post1 +svgwrite==1.4.3 +CairoSVG==2.7.1 \ No newline at end of file diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index 9b46a46..d2b5b6a 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -135,8 +135,8 @@ async def test_get_image_nl( result_image = Image.open(BytesIO(result['sequence'][-1]['image'])).convert('RGBA') assert list(result_image.getdata()) == list(expected.getdata()) - - thumb_image = Image.open(BytesIO(result['most_recent_image'])).convert('RGBA') + most_recent_image = result['sequence'][result['most_recent_image_idx']]['image'] + thumb_image = Image.open(BytesIO(most_recent_image)).convert('RGBA') assert list(thumb_image.getdata()) == list(expected.getdata()) assert result['hint'] == "No rain forecasted shortly" @@ -170,8 +170,8 @@ async def test_get_image_be( result_image = Image.open(BytesIO(result['sequence'][9]['image'])).convert('RGBA') assert list(result_image.getdata()) == list(expected.getdata()) - - thumb_image = Image.open(BytesIO(result['most_recent_image'])).convert('RGBA') + most_recent_image = result['sequence'][result['most_recent_image_idx']]['image'] + thumb_image = Image.open(BytesIO(most_recent_image)).convert('RGBA') assert list(thumb_image.getdata()) == list(expected.getdata()) assert result['hint'] == "No rain forecasted shortly"