Skip to content

Commit

Permalink
Adding option to adjust number of hours returned in hourly forecast. …
Browse files Browse the repository at this point in the history
…Closing Hourly Forecast exceeds maximum size of 16384 bytes #74
  • Loading branch information
briis committed Jan 27, 2024
1 parent 8229ebd commit d5963d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
25 changes: 19 additions & 6 deletions custom_components/weatherflow_forecast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@

from .const import (
DEFAULT_ADD_SENSOR,
DEFAULT_FORECAST_HOURS,
DOMAIN,
CONF_ADD_SENSORS,
CONF_API_TOKEN,
CONF_FORECAST_HOURS,
CONF_STATION_ID,
)

PLATFORMS = [Platform.WEATHER, Platform.SENSOR, Platform.BINARY_SENSOR]
ignore_sensors = False

_LOGGER = logging.getLogger(__name__)

Expand All @@ -48,13 +49,22 @@ def _get_platforms(config_entry: ConfigEntry):

return add_sensors

def _get_forecast_hours(config_entry: ConfigEntry):

forecast_hours = DEFAULT_FORECAST_HOURS if config_entry.options.get(
CONF_FORECAST_HOURS) is None else config_entry.options.get(CONF_FORECAST_HOURS)

return forecast_hours


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up WeatherFlow Forecast as config entry."""

add_sensors = _get_platforms(config_entry)
forecast_hours = _get_forecast_hours(config_entry)

coordinator = WeatherFlowForecastDataUpdateCoordinator(hass, config_entry, add_sensors)
coordinator = WeatherFlowForecastDataUpdateCoordinator(
hass, config_entry, add_sensors, forecast_hours)
await coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})
Expand Down Expand Up @@ -105,13 +115,15 @@ class CannotConnect(HomeAssistantError):
class WeatherFlowForecastDataUpdateCoordinator(DataUpdateCoordinator["WeatherFlowForecastWeatherData"]):
"""Class to manage fetching WeatherFlow data."""

def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, add_sensors: bool) -> None:
def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry, add_sensors: bool, forecast_hours: int) -> None:
"""Initialize global WeatherFlow forecast data updater."""
self.weather = WeatherFlowForecastWeatherData(hass, config_entry.data, add_sensors)
self.weather = WeatherFlowForecastWeatherData(
hass, config_entry.data, add_sensors, forecast_hours)
self.weather.initialize_data()
self.hass = hass
self.config_entry = config_entry
self.add_sensors = add_sensors
self.forecast_hours = forecast_hours

if add_sensors:
update_interval = timedelta(minutes=randrange(1, 5))
Expand All @@ -131,11 +143,12 @@ async def _async_update_data(self) -> WeatherFlowForecastWeatherData:
class WeatherFlowForecastWeatherData:
"""Keep data for WeatherFlow Forecast entity data."""

def __init__(self, hass: HomeAssistant, config: MappingProxyType[str, Any], add_sensors: bool) -> None:
def __init__(self, hass: HomeAssistant, config: MappingProxyType[str, Any], add_sensors: bool, forecast_hours: int) -> None:
"""Initialise the weather entity data."""
self.hass = hass
self._config = config
self._add_sensors = add_sensors
self._forecast_hours = forecast_hours
self._weather_data: WeatherFlow
self.current_weather_data: WeatherFlowForecastData = {}
self.daily_forecast: WeatherFlowForecastDaily = []
Expand All @@ -147,7 +160,7 @@ def initialize_data(self) -> bool:
"""Establish connection to API."""

self._weather_data = WeatherFlow(
self._config[CONF_STATION_ID], self._config[CONF_API_TOKEN], elevation=self.hass.config.elevation, session=async_get_clientsession(self.hass))
self._config[CONF_STATION_ID], self._config[CONF_API_TOKEN], elevation=self.hass.config.elevation, session=async_get_clientsession(self.hass), forecast_hours=self._forecast_hours)

return True

Expand Down
6 changes: 3 additions & 3 deletions custom_components/weatherflow_forecast/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
CONF_SERIAL_NUMBER: station_data.serial_number,
},
options={
CONF_ADD_SENSORS: user_input[CONF_ADD_SENSORS],
CONF_FORECAST_HOURS: user_input[CONF_FORECAST_HOURS],
CONF_ADD_SENSORS: user_input[CONF_ADD_SENSORS],
}
)

Expand All @@ -107,7 +107,7 @@ async def _show_setup_form(self, errors=None):
{
vol.Required(CONF_STATION_ID): int,
vol.Required(CONF_API_TOKEN): str,
vol.Optional(CONF_FORECAST_HOURS, default=DEFAULT_FORECAST_HOURS): vol.All(vol.Coerce(int), vol.Range(min=12, max=72)),
vol.Optional(CONF_FORECAST_HOURS, default=DEFAULT_FORECAST_HOURS): vol.All(vol.Coerce(int), vol.Range(min=12, max=96)),
vol.Optional(CONF_ADD_SENSORS, default=DEFAULT_ADD_SENSOR): bool,
}
),
Expand All @@ -132,8 +132,8 @@ async def async_step_init(self, user_input: dict[str, Any] | None = None) -> Flo
data_schema=vol.Schema(
{
vol.Required(CONF_API_TOKEN, default=self._config_entry.data.get(CONF_API_TOKEN, "")): str,
vol.Optional(CONF_FORECAST_HOURS, default=self._config_entry.options.get(CONF_FORECAST_HOURS, DEFAULT_FORECAST_HOURS)): vol.All(vol.Coerce(int), vol.Range(min=12, max=96)),
vol.Optional(CONF_ADD_SENSORS, default=self._config_entry.options.get(CONF_ADD_SENSORS, DEFAULT_ADD_SENSOR)): bool,
vol.Optional(CONF_FORECAST_HOURS, default=self._config_entry.options.get(CONF_FORECAST_HOURS, DEFAULT_FORECAST_HOURS)): vol.All(vol.Coerce(int), vol.Range(min=12, max=72)),
}
)
)

0 comments on commit d5963d2

Please sign in to comment.