Skip to content

Commit

Permalink
v2.0.7
Browse files Browse the repository at this point in the history
Move sensor translation file initialization to WundergroundPWSUpdateCoordinatorConfig in __init__.py
Fixes "Detected blocking call to open with args" warning
  • Loading branch information
dave_albright committed Jul 5, 2024
1 parent c2ad041 commit 36e33bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 14 additions & 1 deletion custom_components/wundergroundpws/__init__.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 3 additions & 14 deletions custom_components/wundergroundpws/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)

Expand Down Expand Up @@ -59,6 +58,7 @@ class WundergroundPWSUpdateCoordinatorConfig:
longitude: str
forecast_enable: bool
update_interval = MIN_TIME_BETWEEN_UPDATES
tranfile: str


class WundergroundPWSUpdateCoordinator(DataUpdateCoordinator):
Expand All @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wundergroundpws/manifest.json
Original file line number Diff line number Diff line change
@@ -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": [],
Expand Down

0 comments on commit 36e33bf

Please sign in to comment.