diff --git a/custom_components/ims/const.py b/custom_components/ims/const.py index 6c3b2f3..064d4c5 100644 --- a/custom_components/ims/const.py +++ b/custom_components/ims/const.py @@ -2,6 +2,7 @@ from __future__ import annotations from homeassistant.components.weather import ( + ATTR_CONDITION_CLEAR_NIGHT, ATTR_CONDITION_CLOUDY, ATTR_CONDITION_EXCEPTIONAL, ATTR_CONDITION_FOG, @@ -154,8 +155,10 @@ "1140": ATTR_CONDITION_POURING, "1160": ATTR_CONDITION_FOG, "1220": ATTR_CONDITION_PARTLYCLOUDY, + "1220-night": ATTR_CONDITION_PARTLYCLOUDY, #no "-night" "1230": ATTR_CONDITION_CLOUDY, "1250": ATTR_CONDITION_SUNNY, + "1250-night": ATTR_CONDITION_CLEAR_NIGHT, "1260": ATTR_CONDITION_WINDY, "1270": ATTR_CONDITION_SUNNY, "1300": ATTR_CONDITION_HAIL, @@ -181,8 +184,10 @@ "1140": "mdi:weather-pouring", "1160": "mdi:weather-fog", "1220": "mdi:weather-partly-cloudy", + "1220-night": "mdi:weather-partly-cloudy-night", "1230": "mdi:weather-cloudy", "1250": "mdi:weather-sunny", + "1250-night": "mdi:clear-night", "1260": "mdi:weather-windy", "1270": "mdi:weather-fog", "1300": "mdi:snowflake-melt", diff --git a/custom_components/ims/sensor.py b/custom_components/ims/sensor.py index 4d7decc..ff9686a 100644 --- a/custom_components/ims/sensor.py +++ b/custom_components/ims/sensor.py @@ -40,6 +40,7 @@ WEATHER_CODE_TO_ICON, TYPE_PRECIPITATION, TYPE_IS_RAINING, TYPE_PRECIPITATION_PROBABILITY, FIELD_NAME_RAIN_CHANCE, ) +from .utils import get_hourly_weather_icon IMS_SENSOR_KEY_PREFIX = "ims_" @@ -323,10 +324,12 @@ def generate_forecast_extra_state_attributes(daily_forecast): elif not last_weather_code: last_weather_code = daily_forecast.weather_code + hourly_weather_code = get_hourly_weather_icon(hour.hour, last_weather_code) + attributes[hour.hour] = { "weather": { "value": last_weather_status, - "icon": WEATHER_CODE_TO_ICON.get(last_weather_code) + "icon": WEATHER_CODE_TO_ICON.get(hourly_weather_code) }, "temperature": {"value": hour.precise_temperature or hour.temperature, "unit": UnitOfTemperature.CELSIUS}, } diff --git a/custom_components/ims/translations/en.json b/custom_components/ims/translations/en.json index 2789ac5..3065d0c 100644 --- a/custom_components/ims/translations/en.json +++ b/custom_components/ims/translations/en.json @@ -118,7 +118,7 @@ "name": "אינדקס קרינה מירבי" }, "ims_max_uv_index_en": { - "name": "Current UV Index" + "name": "Maximum UV Index" }, "ims_current_uv_level_he": { "name": "רמת קרינה נוכחית", diff --git a/custom_components/ims/translations/he.json b/custom_components/ims/translations/he.json index 9f396e8..cc54d67 100644 --- a/custom_components/ims/translations/he.json +++ b/custom_components/ims/translations/he.json @@ -118,7 +118,7 @@ "name": "אינדקס קרינה מירבי" }, "ims_max_uv_index_en": { - "name": "Current UV Index" + "name": "Maximum UV Index" }, "ims_current_uv_level_he": { "name": "רמת קרינה נוכחית", diff --git a/custom_components/ims/utils.py b/custom_components/ims/utils.py new file mode 100644 index 0000000..b126962 --- /dev/null +++ b/custom_components/ims/utils.py @@ -0,0 +1,14 @@ +from datetime import datetime + +night_weather_codes = ["1220", "1250"] + +def get_hourly_weather_icon(hour, weather_code, strptime="%H:%M"): + hourly_weather_code = weather_code + time_object = datetime.strptime(hour, strptime) + if _is_night(time_object.hour) and hourly_weather_code in night_weather_codes: + hourly_weather_code = hourly_weather_code + "-night" + + return hourly_weather_code + +def _is_night(hour): + return hour < 6 or hour > 20 \ No newline at end of file diff --git a/custom_components/ims/weather.py b/custom_components/ims/weather.py index 9985623..d35b02f 100644 --- a/custom_components/ims/weather.py +++ b/custom_components/ims/weather.py @@ -1,5 +1,6 @@ from __future__ import annotations import logging + import pytz from weatheril import * import voluptuous as vol @@ -45,6 +46,7 @@ from homeassistant.const import UnitOfTemperature +from .utils import get_hourly_weather_icon from .weather_update_coordinator import WeatherUpdateCoordinator _LOGGER = logging.getLogger(__name__) @@ -220,14 +222,18 @@ def native_dew_point(self): def wind_bearing(self): """Return the wind bearing.""" return WIND_DIRECTIONS[ - int(self._weather_coordinator.data.current_weather.json["wind_direction_id"]) + int(self._weather_coordinator.data.current_weather.wind_direction_id) ] @property def condition(self): """Return the weather condition.""" + + date_str = self._weather_coordinator.data.current_weather.json["forecast_time"] + weather_code = get_hourly_weather_icon(date_str, self._weather_coordinator.data.current_weather.weather_code, "%Y-%m-%d %H:%M:%S") + condition = WEATHER_CODE_TO_CONDITION[ - self._weather_coordinator.data.current_weather.weather_code + weather_code ] if not condition or condition == "Nothing": condition = WEATHER_CODE_TO_CONDITION[ @@ -264,9 +270,12 @@ def _forecast(self, hourly: bool) -> list[Forecast]: last_weather_code = hourly_forecast.weather_code elif not last_weather_code: last_weather_code = daily_forecast.weather_code + + hourly_weather_code = get_hourly_weather_icon(hourly_forecast.hour, last_weather_code) + data.append( Forecast( - condition=WEATHER_CODE_TO_CONDITION[last_weather_code], + condition=WEATHER_CODE_TO_CONDITION[hourly_weather_code], datetime=hourly_forecast.forecast_time.astimezone(pytz.UTC).isoformat(), native_temperature=hourly_forecast.precise_temperature, native_templow=daily_forecast.minimum_temperature,