Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(weather::forecasts): add additional forecast data for clock-weather-card #114

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 22 additions & 5 deletions custom_components/yandex_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import logging

from homeassistant.components.weather import (
ATTR_FORECAST_IS_DAYTIME,
ATTR_WEATHER_PRECIPITATION_UNIT,
ATTR_WEATHER_PRESSURE_UNIT,
ATTR_WEATHER_TEMPERATURE_UNIT,
Expand Down Expand Up @@ -40,6 +39,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 +218,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 +246,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()
Loading