Skip to content

Commit

Permalink
refactor: use YandexWeatherDeviceClass as key for translation
Browse files Browse the repository at this point in the history
  • Loading branch information
IATkachenko committed Aug 17, 2022
1 parent b09a386 commit c3e8b6a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
9 changes: 9 additions & 0 deletions custom_components/yandex_weather/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""General constants."""
from __future__ import annotations

from homeassistant.backports.enum import StrEnum
from homeassistant.const import Platform

DOMAIN = "yandex_weather"
Expand Down Expand Up @@ -185,3 +186,11 @@ def get_image(
)

return CONDITION_IMAGE[image_source]["link"].format(mapped_image)


class YandexWeatherDeviceClass(StrEnum):
"""State class for weather sensors."""

WIND_BEARING = f"{DOMAIN}__wind_bearing"
CONDITION_YA = f"{DOMAIN}__condition_ya"
CONDITION_HA = f"{DOMAIN}__condition_ha"
11 changes: 1 addition & 10 deletions custom_components/yandex_weather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from datetime import datetime
import logging

from homeassistant.backports.enum import StrEnum
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand Down Expand Up @@ -43,18 +42,10 @@
DOMAIN,
ENTRY_NAME,
UPDATER,
YandexWeatherDeviceClass,
)
from .updater import WeatherUpdater


class YandexWeatherDeviceClass(StrEnum):
"""State class for weather sensors."""

WIND_BEARING = f"{DOMAIN}__wind_bearing"
CONDITION_YA = f"{DOMAIN}__condition_ya"
CONDITION_HA = f"{DOMAIN}__condition_ha"


WEATHER_SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key=ATTR_API_TEMPERATURE,
Expand Down
5 changes: 4 additions & 1 deletion custom_components/yandex_weather/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
DOMAIN,
MANUFACTURER,
WEATHER_STATES_CONVERSION,
YandexWeatherDeviceClass,
map_state,
)
from .device_trigger import TRIGGERS
Expand Down Expand Up @@ -80,7 +81,9 @@ def translate_condition(value: str, _language: str) -> str:
)
try:
with open(_translation_location) as f:
value = json.loads(f.read())["state"]["yandex_weather__condition_ya"][value]
value = json.loads(f.read())["state"][
YandexWeatherDeviceClass.CONDITION_YA
][value]
except FileNotFoundError:
_LOGGER.debug(f"We have no translation for {_language=} in {_my_location}")
except KeyError:
Expand Down

0 comments on commit c3e8b6a

Please sign in to comment.