Skip to content

Commit

Permalink
fix[weather::forecasts]: add additional forecast data for clock-weath…
Browse files Browse the repository at this point in the history
…er-card

https://github.com/pkissling/clock-weather-card with
```yaml
type: custom:clock-weather-card
entity: weather.yandex_weather
hourly_forecast: true
```
now will show pretty forecast from the integration.

Fix #112
  • Loading branch information
IATkachenko committed Apr 7, 2024
1 parent f92b39c commit deab4fd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions custom_components/yandex_weather/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def dst(self) -> str:
"wind_dir", ATTR_FORECAST_WIND_BEARING, mapping=WIND_DIRECTION_MAPPING
),
AttributeMapper("temp_avg", ATTR_FORECAST_NATIVE_TEMP),
AttributeMapper("temp_avg", "temperature"),
AttributeMapper("temp_min", ATTR_FORECAST_NATIVE_TEMP_LOW),
AttributeMapper("temp_min", "templow"),
AttributeMapper("pressure_pa", ATTR_FORECAST_NATIVE_PRESSURE),
AttributeMapper("wind_speed", ATTR_FORECAST_NATIVE_WIND_SPEED, default=0),
AttributeMapper("prec_mm", ATTR_FORECAST_NATIVE_PRECIPITATION, default=0),
Expand Down
26 changes: 22 additions & 4 deletions custom_components/yandex_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ATTR_API_PRESSURE,
ATTR_API_TEMP_WATER,
ATTR_API_TEMPERATURE,
ATTR_API_WEATHER_TIME,
ATTR_API_WIND_BEARING,
ATTR_API_WIND_GUST,
ATTR_API_WIND_SPEED,
Expand Down Expand Up @@ -218,7 +219,7 @@ def _handle_coordinator_update(self) -> None:
"wind_gust": self.coordinator.data.get(ATTR_API_WIND_GUST),
"yandex_condition": self.coordinator.data.get(ATTR_API_YA_CONDITION),
"forecast_icons": self.coordinator.data.get(ATTR_API_FORECAST_ICONS),
ATTR_FORECAST_DATA: self._twice_daily_forecast,
ATTR_FORECAST_DATA: self.__forecast_twice_daily(),
}
try:
self._attr_extra_state_attributes["temp_water"] = self.coordinator.data.get(
Expand Down Expand Up @@ -246,14 +247,31 @@ def update_condition_and_fire_event(self, new_condition: str):

self._attr_condition = new_condition

async def async_forecast_twice_daily(self) -> list[Forecast] | None:
def __forecast_twice_daily(self) -> list[Forecast] | None:
"""Return the daily forecast in native units."""
_LOGGER.debug(f"async_forecast_twice_daily: {self._twice_daily_forecast=}")
# we must return at least three elements in forecast
# https://github.com/home-assistant/frontend/blob/dev/src/data/weather.ts#L548
if len(result := self._twice_daily_forecast) < 3:
_LOGGER.debug(
"Have not enough forecast data. Adding empty element to forecast..."
"Have not enough forecast data. Adding current weather to forecast..."
)
result.insert(
0,
Forecast(
datetime=self.coordinator.data.get(ATTR_API_WEATHER_TIME),
wind_bearing=self.wind_bearing,
native_temperature=self.native_temperature,
temperatrue=self.native_temperature,
native_templow=self.native_temperature,
templow=self.native_temperature,
native_pressure=self.native_pressure,
native_wind_speed=self.native_wind_speed,
condition=self.condition,
# is_daytime=self.is_daytime,
),
)
result.append({ATTR_FORECAST_IS_DAYTIME: False})
return result

async def async_forecast_twice_daily(self) -> list[Forecast] | None:
return self.__forecast_twice_daily()

0 comments on commit deab4fd

Please sign in to comment.