From d5963d2604473387b2819707a985fb9b7b234b83 Mon Sep 17 00:00:00 2001 From: Bjarne Riis Date: Sat, 27 Jan 2024 10:32:37 +0000 Subject: [PATCH] Adding option to adjust number of hours returned in hourly forecast. Closing Hourly Forecast exceeds maximum size of 16384 bytes #74 --- .../weatherflow_forecast/__init__.py | 25 ++++++++++++++----- .../weatherflow_forecast/config_flow.py | 6 ++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/custom_components/weatherflow_forecast/__init__.py b/custom_components/weatherflow_forecast/__init__.py index 369988c..c24882b 100644 --- a/custom_components/weatherflow_forecast/__init__.py +++ b/custom_components/weatherflow_forecast/__init__.py @@ -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__) @@ -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, {}) @@ -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)) @@ -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 = [] @@ -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 diff --git a/custom_components/weatherflow_forecast/config_flow.py b/custom_components/weatherflow_forecast/config_flow.py index 5040d5a..36fc1b4 100644 --- a/custom_components/weatherflow_forecast/config_flow.py +++ b/custom_components/weatherflow_forecast/config_flow.py @@ -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], } ) @@ -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, } ), @@ -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)), } ) )