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

remove compression for weather's extra forecast attributes #30

Merged
merged 2 commits into from
Oct 8, 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
6 changes: 6 additions & 0 deletions custom_components/yandex_pogoda/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
UPDATER,
WIND_SPEED_CONVERTER,
YA_CONDITION_STATES,
WEATHER_STATES_CONVERSION,
convert_unit_value,
)
from .updater import WeatherUpdater
Expand Down Expand Up @@ -208,6 +209,11 @@ async def async_added_to_hass(self) -> None:
self.native_unit_of_measurement,
)
)
elif (
self.entity_description.key == ATTR_API_YA_CONDITION
and state.state not in YA_CONDITION_STATES
): # for backward compatibility
self._attr_native_value = WEATHER_STATES_CONVERSION.get(state.state)
else:
self._attr_native_value = state.state

Expand Down
32 changes: 19 additions & 13 deletions custom_components/yandex_pogoda/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
ATTR_API_WIND_SPEED,
ATTR_API_YA_CONDITION,
ATTR_FORECAST_DATA,
ATTR_FORECAST_DATA_COMPRESSED,
ATTR_FORECAST_HOURLY,
ATTR_FORECAST_HOURLY_ICONS,
ATTR_FORECAST_TWICE_DAILY,
Expand All @@ -44,9 +43,7 @@
TEMPERATURE_CONVERTER,
UPDATER,
WIND_SPEED_CONVERTER,
compress_data,
convert_unit_value,
decompress_data,
)
from .device_trigger import TRIGGERS
from .updater import WeatherUpdater
Expand Down Expand Up @@ -87,7 +84,14 @@ class YandexWeather(WeatherEntity, CoordinatorEntity, RestoreEntity):
_attr_supported_features = (
WeatherEntityFeature.FORECAST_HOURLY | WeatherEntityFeature.FORECAST_TWICE_DAILY
)

_unrecorded_attributes = frozenset(
{
"forecast_hourly",
"forecast_twice_daily",
ATTR_FORECAST_HOURLY_ICONS,
ATTR_FORECAST_TWICE_DAILY_ICONS,
}
)
coordinator: WeatherUpdater

def __init__(
Expand Down Expand Up @@ -157,9 +161,12 @@ async def async_added_to_hass(self) -> None:
self._attr_entity_picture = state.attributes.get("entity_picture")

self._attr_extra_state_attributes = {}
self._update_forecast_data(
decompress_data(state.attributes.get(ATTR_FORECAST_DATA_COMPRESSED))
)

f_data = {
ATTR_FORECAST_HOURLY: state.attributes.get("forecast_hourly"),
ATTR_FORECAST_TWICE_DAILY: state.attributes.get("forecast_twice_daily"),
}
self._update_forecast_data(f_data)
for attr in (
ATTR_API_YA_CONDITION,
ATTR_FORECAST_HOURLY_ICONS,
Expand All @@ -184,14 +191,13 @@ async def async_added_to_hass(self) -> None:
)
self.async_write_ha_state()

def _update_forecast_data(self, forecast_data: dict): # uncompressed
def _update_forecast_data(self, forecast_data: dict):
self._hourly_forecast = forecast_data.get(ATTR_FORECAST_HOURLY, [])
self._twice_daily_forecast = forecast_data.get(ATTR_FORECAST_TWICE_DAILY, [])

if forecast_data:
self._attr_extra_state_attributes[ATTR_FORECAST_DATA_COMPRESSED] = (
compress_data(forecast_data)
)
self._attr_extra_state_attributes["forecast_hourly"] = self._hourly_forecast
self._attr_extra_state_attributes["forecast_twice_daily"] = (
self._twice_daily_forecast
)

def _handle_coordinator_update(self) -> None:
self._attr_available = True
Expand Down
Loading