Skip to content

Commit

Permalink
Исправление асинхронных вызовов в weather updater
Browse files Browse the repository at this point in the history
  • Loading branch information
mazixs committed Sep 17, 2024
1 parent 1c1d2d6 commit 14a05d3
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions custom_components/yandex_weather/updater.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Weather data updater."""

import aiofiles
from __future__ import annotations

from dataclasses import dataclass
Expand Down Expand Up @@ -59,7 +59,6 @@
API_VERSION = "2"
_LOGGER = logging.getLogger(__name__)


WIND_DIRECTION_MAPPING: dict[str, int | None] = {
"nw": 315,
"n": 360,
Expand Down Expand Up @@ -129,19 +128,22 @@ def dst(self) -> str:
]


def translate_condition(value: str, _language: str) -> str:
async def translate_condition(value: str, _language: str) -> str:
"""Translate Yandex condition."""
_my_location = os.path.dirname(os.path.realpath(__file__))
_translation_location = f"{_my_location}/translations/{_language.lower()}.json"
try:
with open(_translation_location) as f:
value = json.loads(f.read())["entity"]["sensor"][ATTR_API_YA_CONDITION][
async with aiofiles.open(_translation_location, mode='r') as f:
content = await f.read()
value = json.loads(content)["entity"]["sensor"][ATTR_API_YA_CONDITION][
"state"
][value]
except FileNotFoundError:
_LOGGER.debug(f"We have no translation for {_language=} in {_my_location}")
return value # Возвращаем исходное значение, если перевод не найден
except KeyError:
_LOGGER.debug(f"Have no translation for {value} in {_translation_location}")
return value # Возвращаем исходное значение, если перевод не найден
return value


Expand Down Expand Up @@ -191,11 +193,11 @@ def __init__(
)
self.data = {}

def process_data(self, dst: dict, src: dict, attributes: list[AttributeMapper]):
async def process_data(self, dst: dict, src: dict, attributes: list[AttributeMapper]):
"""Convert Yandex API weather state to HA friendly.
:param dst: weather data for HomeAssistant
:param src: weather data form Yandex
:param src: weather data from Yandex
:param attributes: how to translate src to dst
"""

Expand All @@ -208,7 +210,7 @@ def process_data(self, dst: dict, src: dict, attributes: list[AttributeMapper]):
is_day=(src["daytime"] == "d"),
)
if attribute.should_translate and value is not None:
value = translate_condition(
value = await translate_condition(
value=value,
_language=self._language,
)
Expand Down Expand Up @@ -257,13 +259,13 @@ async def update(self):
ATTR_API_FORECAST_ICONS: [],
ATTR_FORECAST_DATA: [],
}
self.process_data(result, r["fact"], CURRENT_WEATHER_ATTRIBUTE_TRANSLATION)
await self.process_data(result, r["fact"], CURRENT_WEATHER_ATTRIBUTE_TRANSLATION)

f_datetime = datetime.now(tz=local_tz)
for f in r["forecast"]["parts"]:
f_datetime += timedelta(hours=24 / 4)
forecast = Forecast(datetime=f_datetime.isoformat())
self.process_data(forecast, f, FORECAST_ATTRIBUTE_TRANSLATION)
await self.process_data(forecast, f, FORECAST_ATTRIBUTE_TRANSLATION)
forecast[ATTR_FORECAST_IS_DAYTIME] = f["daytime"] == "d"
result[ATTR_FORECAST_DATA].append(forecast)
result[ATTR_API_FORECAST_ICONS].append(f.get("icon", "no_image"))
Expand Down

0 comments on commit 14a05d3

Please sign in to comment.