Skip to content

Commit f392e8b

Browse files
committed
Refactor
1 parent ed20cd9 commit f392e8b

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

custom_components/irm_kmi/coordinator.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import logging
44
from datetime import datetime, timedelta
55
from io import BytesIO
6-
from typing import List
6+
from typing import List, Tuple
77

88
import async_timeout
99
import pytz
1010
from homeassistant.components.weather import Forecast
11+
from homeassistant.components.zone import Zone
1112
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
13+
from homeassistant.core import HomeAssistant
1214
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1315
from homeassistant.helpers.update_coordinator import (DataUpdateCoordinator,
1416
UpdateFailed)
@@ -26,7 +28,7 @@
2628
class IrmKmiCoordinator(DataUpdateCoordinator):
2729
"""Coordinator to update data from IRM KMI"""
2830

29-
def __init__(self, hass, zone):
31+
def __init__(self, hass: HomeAssistant, zone: Zone):
3032
"""Initialize my coordinator."""
3133
super().__init__(
3234
hass,
@@ -89,6 +91,15 @@ async def _async_animation_data(self, api_data: dict) -> RadarAnimationData:
8991
radar_animation['hint'] = api_data.get('animation', {}).get('sequenceHint', {}).get('en')
9092
return radar_animation
9193

94+
async def process_api_data(self, api_data: dict) -> ProcessedCoordinatorData:
95+
96+
return ProcessedCoordinatorData(
97+
current_weather=IrmKmiCoordinator.current_weather_from_data(api_data),
98+
daily_forecast=IrmKmiCoordinator.daily_list_to_forecast(api_data.get('for', {}).get('daily')),
99+
hourly_forecast=IrmKmiCoordinator.hourly_list_to_forecast(api_data.get('for', {}).get('hourly')),
100+
animation=await self._async_animation_data(api_data=api_data)
101+
)
102+
92103
async def download_images_from_api(self, animation_data, country, localisation_layer_url):
93104
coroutines = list()
94105
coroutines.append(self._api_client.get_image(f"{localisation_layer_url}&th={'d' if country == 'NL' else 'n'}"))
@@ -101,18 +112,28 @@ async def download_images_from_api(self, animation_data, country, localisation_l
101112
_LOGGER.debug(f"Just downloaded {len(images_from_api)} images")
102113
return images_from_api
103114

104-
async def merge_frames_from_api(self, animation_data, country, images_from_api,
105-
localisation_layer) -> RadarAnimationData:
115+
async def merge_frames_from_api(self,
116+
animation_data: List[dict],
117+
country: str,
118+
images_from_api: Tuple[bytes],
119+
localisation_layer: Image
120+
) -> RadarAnimationData:
121+
122+
background: Image
123+
fill_color: tuple
106124

107125
if country == 'NL':
108126
background = Image.open("custom_components/irm_kmi/resources/nl.png").convert('RGBA')
127+
fill_color = (0, 0, 0)
109128
else:
110129
background = Image.open("custom_components/irm_kmi/resources/be_bw.png").convert('RGBA')
130+
fill_color = (255, 255, 255)
111131

112132
most_recent_frame = None
113133
tz = pytz.timezone(self.hass.config.time_zone)
114134
current_time = datetime.now(tz=tz)
115135
sequence: List[AnimationFrameData] = list()
136+
116137
for (idx, sequence_element) in enumerate(animation_data):
117138
frame = images_from_api[idx]
118139
layer = Image.open(BytesIO(frame)).convert('RGBA')
@@ -126,10 +147,7 @@ async def merge_frames_from_api(self, animation_data, country, images_from_api,
126147

127148
time_str = time_image.isoformat(sep=' ', timespec='minutes')
128149

129-
if country == 'NL':
130-
draw.text((4, 4), time_str, (0, 0, 0), font=font)
131-
else:
132-
draw.text((4, 4), time_str, (255, 255, 255), font=font)
150+
draw.text((4, 4), time_str, fill_color, font=font)
133151

134152
bytes_img = BytesIO()
135153
temp.save(bytes_img, 'png', compress_level=8)
@@ -154,15 +172,6 @@ async def merge_frames_from_api(self, animation_data, country, images_from_api,
154172
most_recent_image=most_recent_frame
155173
)
156174

157-
async def process_api_data(self, api_data: dict) -> ProcessedCoordinatorData:
158-
159-
return ProcessedCoordinatorData(
160-
current_weather=IrmKmiCoordinator.current_weather_from_data(api_data),
161-
daily_forecast=IrmKmiCoordinator.daily_list_to_forecast(api_data.get('for', {}).get('daily')),
162-
hourly_forecast=IrmKmiCoordinator.hourly_list_to_forecast(api_data.get('for', {}).get('hourly')),
163-
animation=await self._async_animation_data(api_data=api_data)
164-
)
165-
166175
@staticmethod
167176
def current_weather_from_data(api_data: dict) -> CurrentWeatherData:
168177
# Process data to get current hour forecast
@@ -183,7 +192,6 @@ def current_weather_from_data(api_data: dict) -> CurrentWeatherData:
183192
if module.get('type', None) == 'uv':
184193
uv_index = module.get('data', {}).get('levelValue')
185194

186-
# TODO NL cities have a better 'obs' section, use that for current weather
187195
current_weather = CurrentWeatherData(
188196
condition=CDT_MAP.get((api_data.get('obs', {}).get('ww'), api_data.get('obs', {}).get('dayNight')), None),
189197
temperature=api_data.get('obs', {}).get('temp'),
@@ -193,6 +201,11 @@ def current_weather_from_data(api_data: dict) -> CurrentWeatherData:
193201
pressure=now_hourly.get('pressure', None) if now_hourly is not None else None,
194202
uv_index=uv_index
195203
)
204+
205+
if api_data.get('country', '') == 'NL':
206+
current_weather['wind_speed'] = api_data.get('obs', {}).get('windSpeedKm')
207+
current_weather['wind_bearing'] = api_data.get('obs', {}).get('windDirectionText', {}).get('en')
208+
196209
return current_weather
197210

198211
@staticmethod

0 commit comments

Comments
 (0)