Use hass.config option to define config directory when loading files

This commit is contained in:
Jules 2024-04-01 13:56:41 +02:00
parent 23f690027d
commit b2bc3a72ba
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
4 changed files with 15 additions and 15 deletions

View file

@ -347,6 +347,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
bg_size = (640, 490) bg_size = (640, 490)
return RainGraph(radar_animation, image_path, bg_size, return RainGraph(radar_animation, image_path, bg_size,
config_dir=self.hass.config.config_dir,
dark_mode=self._dark_mode, dark_mode=self._dark_mode,
tz=self.hass.config.time_zone) tz=self.hass.config.time_zone)

File diff suppressed because one or more lines are too long

View file

@ -3,6 +3,7 @@
import base64 import base64
import copy import copy
import logging import logging
import os
from typing import List from typing import List
import pytz import pytz
@ -11,15 +12,16 @@ from svgwrite.animate import Animate
from custom_components.irm_kmi.data import (AnimationFrameData, from custom_components.irm_kmi.data import (AnimationFrameData,
RadarAnimationData) RadarAnimationData)
from custom_components.irm_kmi.font_fallback import font_data
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class RainGraph: class RainGraph:
def __init__(self, def __init__(self,
animation_data: RadarAnimationData, animation_data: RadarAnimationData,
background_image_path: str, background_image_path: str,
background_size: (int, int), background_size: (int, int),
config_dir: str = '.',
dark_mode: bool = False, dark_mode: bool = False,
tz: str = 'UTC', tz: str = 'UTC',
svg_width: float = 640, svg_width: float = 640,
@ -35,6 +37,7 @@ class RainGraph:
self._animation_data: RadarAnimationData = animation_data self._animation_data: RadarAnimationData = animation_data
self._background_image_path: str = background_image_path self._background_image_path: str = background_image_path
self._background_size: (int, int) = background_size self._background_size: (int, int) = background_size
self._config_dir: str = config_dir
self._dark_mode: bool = dark_mode self._dark_mode: bool = dark_mode
self._tz = pytz.timezone(tz) self._tz = pytz.timezone(tz)
self._svg_width: float = svg_width self._svg_width: float = svg_width
@ -88,13 +91,9 @@ class RainGraph:
def draw_svg_frame(self): def draw_svg_frame(self):
"""Create the global area to draw the other items""" """Create the global area to draw the other items"""
try: font_file = os.path.join(self._config_dir, 'custom_components/irm_kmi/resources/roboto_medium.ttf')
self._dwg.embed_font(name="Roboto Medium", filename='custom_components/irm_kmi/resources/roboto_medium.ttf') _LOGGER.debug(f"Opening font file at {font_file}")
except FileNotFoundError as err: self._dwg.embed_font(name="Roboto Medium", filename=font_file)
# Workaround for some cases where the font file cannot be opened. The font_data contains the strings
# that must be embedded as a stylesheet for the roboto_medium.ttf font
_LOGGER.warning(f'Could not find font {err}. Loading it using the fallback file.')
self._dwg.embed_stylesheet(font_data)
self._dwg.embed_stylesheet(""" self._dwg.embed_stylesheet("""
.roboto { .roboto {
font-family: "Roboto Medium"; font-family: "Roboto Medium";
@ -301,7 +300,8 @@ class RainGraph:
return self._dwg_still.tostring().encode() if still_image else self._dwg_animated.tostring().encode() return self._dwg_still.tostring().encode() if still_image else self._dwg_animated.tostring().encode()
def insert_background(self): def insert_background(self):
with open(self._background_image_path, 'rb') as f: bg_image_path = os.path.join(self._config_dir, self._background_image_path)
with open(bg_image_path, 'rb') as f:
png_data = base64.b64encode(f.read()).decode('utf-8') png_data = base64.b64encode(f.read()).decode('utf-8')
image = self._dwg.image("data:image/png;base64," + png_data, insert=(0, 0), size=self._background_size) image = self._dwg.image("data:image/png;base64," + png_data, insert=(0, 0), size=self._background_size)
self._dwg.add(image) self._dwg.add(image)

View file

@ -1,3 +1,4 @@
import os
from datetime import datetime from datetime import datetime
from typing import List from typing import List
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
@ -21,6 +22,8 @@ async def test_weather_nl(
0, 0,
{"latitude": 50.738681639, "longitude": 4.054077148}, {"latitude": 50.738681639, "longitude": 4.054077148},
) )
hass.config.config_dir = os.getcwd()
coordinator = IrmKmiCoordinator(hass, mock_config_entry) coordinator = IrmKmiCoordinator(hass, mock_config_entry)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
@ -47,6 +50,8 @@ async def test_weather_higher_temp_at_night(
0, 0,
{"latitude": 50.738681639, "longitude": 4.054077148}, {"latitude": 50.738681639, "longitude": 4.054077148},
) )
hass.config.config_dir = os.getcwd()
coordinator = IrmKmiCoordinator(hass, mock_config_entry) coordinator = IrmKmiCoordinator(hass, mock_config_entry)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()