Skip to content

Commit

Permalink
Night Icons (#94)
Browse files Browse the repository at this point in the history
* Add Night Icons

* Add cloudy night

* Little fix
  • Loading branch information
GuyKh committed Jul 28, 2024
1 parent 806b5ad commit 5f75cfd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions custom_components/ims/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion custom_components/ims/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"

Expand Down Expand Up @@ -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},
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ims/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"name": "אינדקס קרינה מירבי"
},
"ims_max_uv_index_en": {
"name": "Current UV Index"
"name": "Maximum UV Index"
},
"ims_current_uv_level_he": {
"name": "רמת קרינה נוכחית",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ims/translations/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"name": "אינדקס קרינה מירבי"
},
"ims_max_uv_index_en": {
"name": "Current UV Index"
"name": "Maximum UV Index"
},
"ims_current_uv_level_he": {
"name": "רמת קרינה נוכחית",
Expand Down
14 changes: 14 additions & 0 deletions custom_components/ims/utils.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 12 additions & 3 deletions custom_components/ims/weather.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
import logging

import pytz
from weatheril import *
import voluptuous as vol
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5f75cfd

Please sign in to comment.