From 36e33bf17e07fa342e1e66d9ab007382d5b1b9ea Mon Sep 17 00:00:00 2001 From: dave_albright Date: Fri, 5 Jul 2024 09:03:40 -0700 Subject: [PATCH] v2.0.7 Move sensor translation file initialization to WundergroundPWSUpdateCoordinatorConfig in __init__.py Fixes "Detected blocking call to open with args" warning --- changelog.md | 4 ++++ custom_components/wundergroundpws/__init__.py | 15 ++++++++++++++- .../wundergroundpws/coordinator.py | 17 +++-------------- custom_components/wundergroundpws/manifest.json | 2 +- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/changelog.md b/changelog.md index 0bb0a4e..360e944 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +v2.0.7 +Move sensor translation file initialization to WundergroundPWSUpdateCoordinatorConfig in `__init__.py` +Fixes "Detected blocking call to open with args" warning + v2.0.6 Increase default rest timeout from 10 seconds to 30 seconds Starting with home assistant 2024, rest availability on Home Assistant Operating System (on Raspberry Pi ?) after restart is delayed and causing setup failure. diff --git a/custom_components/wundergroundpws/__init__.py b/custom_components/wundergroundpws/__init__.py index ee60570..2ac2abe 100644 --- a/custom_components/wundergroundpws/__init__.py +++ b/custom_components/wundergroundpws/__init__.py @@ -1,5 +1,6 @@ """The wundergroundpws component.""" import logging +import os.path from typing import Final from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -9,6 +10,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.util.unit_system import METRIC_SYSTEM +from homeassistant.util import json from .coordinator import WundergroundPWSUpdateCoordinator, WundergroundPWSUpdateCoordinatorConfig from .const import ( CONF_LANG, @@ -47,9 +49,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): calendarday=entry.options[CONF_CALENDARDAYTEMPERATURE], latitude=latitude, longitude=longitude, - forecast_enable=entry.options.get(CONF_FORECAST_SENSORS, False) + forecast_enable=entry.options.get(CONF_FORECAST_SENSORS, False), + tranfile="" ) + """get translation file for wupws sensor friendly_name""" + tfiledir = f'{hass.config.config_dir}/custom_components/{DOMAIN}/wupws_translations/' + tfilename = config.lang.split('-', 1)[0] + + if os.path.isfile(f'{tfiledir}{tfilename}.json'): + config.tranfile = await hass.async_add_executor_job(json.load_json, f'{tfiledir}{tfilename}.json') + else: + config.tranfile = await hass.async_add_executor_job(json.load_json, f'{tfiledir}en.json') + _LOGGER.warning(f'Sensor translation file {tfilename}.json does not exist. Defaulting to en-US.') + wupwscoordinator = WundergroundPWSUpdateCoordinator(hass, config) await wupwscoordinator.async_config_entry_first_refresh() if not wupwscoordinator.last_update_success: diff --git a/custom_components/wundergroundpws/coordinator.py b/custom_components/wundergroundpws/coordinator.py index 58b9b47..0d99e13 100755 --- a/custom_components/wundergroundpws/coordinator.py +++ b/custom_components/wundergroundpws/coordinator.py @@ -15,7 +15,6 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from homeassistant.util import json from homeassistant.util.unit_system import METRIC_SYSTEM from homeassistant.const import ( PERCENTAGE, UnitOfPressure, UnitOfTemperature, UnitOfLength, UnitOfSpeed, UnitOfVolumetricFlux) @@ -29,7 +28,7 @@ FIELD_FORECAST_TEMPERATUREMAX, FIELD_FORECAST_TEMPERATUREMIN, FIELD_FORECAST_CALENDARDAYTEMPERATUREMAX, - FIELD_FORECAST_CALENDARDAYTEMPERATUREMIN, DOMAIN, FIELD_LONGITUDE, FIELD_LATITUDE, + FIELD_FORECAST_CALENDARDAYTEMPERATUREMIN, FIELD_LONGITUDE, FIELD_LATITUDE, DEFAULT_TIMEOUT ) @@ -59,6 +58,7 @@ class WundergroundPWSUpdateCoordinatorConfig: longitude: str forecast_enable: bool update_interval = MIN_TIME_BETWEEN_UPDATES + tranfile: str class WundergroundPWSUpdateCoordinator(DataUpdateCoordinator): @@ -84,7 +84,7 @@ def __init__( self._features = set() self.data = None self._session = async_get_clientsession(self._hass) - self._tranfile = self.get_tran_file() + self._tranfile = config.tranfile if self._unit_system_api == 'm': self.units_of_measurement = (UnitOfTemperature.CELSIUS, UnitOfLength.MILLIMETERS, UnitOfLength.METERS, @@ -224,17 +224,6 @@ def _iconcode_to_condition(cls, icon_code): _LOGGER.warning(f'Unmapped iconCode from TWC Api. (44 is Not Available (N/A)) "{icon_code}". ') return None - def get_tran_file(self): - """get translation file for wupws sensor friendly_name""" - tfiledir = f'{self._hass.config.config_dir}/custom_components/{DOMAIN}/wupws_translations/' - tfilename = self._lang.split('-', 1)[0] - try: - tfiledata = json.load_json(f'{tfiledir}{tfilename}.json') - except Exception: # pylint: disable=broad-except - tfiledata = json.load_json(f'{tfiledir}en.json') - _LOGGER.warning(f'Sensor translation file {tfilename}.json does not exist. Defaulting to en-US.') - return tfiledata - class InvalidApiKey(HomeAssistantError): """Error to indicate there is an invalid api key.""" diff --git a/custom_components/wundergroundpws/manifest.json b/custom_components/wundergroundpws/manifest.json index 12d95ab..8b603e0 100644 --- a/custom_components/wundergroundpws/manifest.json +++ b/custom_components/wundergroundpws/manifest.json @@ -1,7 +1,7 @@ { "domain": "wundergroundpws", "name": "Wundergroundpws", - "version": "2.0.6", + "version": "2.0.7", "documentation": "https://github.com/cytech/Home-Assistant-wundergroundpws/", "issue_tracker": "https://github.com/cytech/Home-Assistant-wundergroundpws/discussions/", "requirements": [],