From 0aead64e95209c9c5d8393e073beef608b4532cf Mon Sep 17 00:00:00 2001 From: Jordan H Date: Fri, 9 Feb 2024 23:03:54 +0000 Subject: [PATCH] bump pyfuelprices complete optionsflow --- custom_components/fuel_prices/__init__.py | 1 + custom_components/fuel_prices/config_flow.py | 50 ++++++++++++++++++-- custom_components/fuel_prices/manifest.json | 2 +- requirements.txt | 2 +- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/custom_components/fuel_prices/__init__.py b/custom_components/fuel_prices/__init__.py index 023a7b5..432653d 100644 --- a/custom_components/fuel_prices/__init__.py +++ b/custom_components/fuel_prices/__init__.py @@ -74,6 +74,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def update_listener(hass: HomeAssistant, entry: ConfigEntry): """Update listener.""" + await hass.data[DOMAIN][entry.entry_id].api.client_session.close() await hass.config_entries.async_reload(entry.entry_id) entry.async_on_unload(entry.add_update_listener(update_listener)) diff --git a/custom_components/fuel_prices/config_flow.py b/custom_components/fuel_prices/config_flow.py index e3f0ff2..2d583bc 100644 --- a/custom_components/fuel_prices/config_flow.py +++ b/custom_components/fuel_prices/config_flow.py @@ -107,6 +107,8 @@ async def async_step_sources(self, user_input: dict[str, Any] | None = None): """Set data source config.""" if user_input is not None: self.configured_sources = user_input[CONF_SOURCES] + self.timeout = user_input[CONF_TIMEOUT] + self.interval = user_input[CONF_SCAN_INTERVAL] return await self.async_step_main_menu(None) return self.async_show_form( step_id="sources", @@ -138,7 +140,7 @@ async def async_step_sources(self, user_input: dict[str, Any] | None = None): ): selector.NumberSelector( selector.NumberSelectorConfig( mode=selector.NumberSelectorMode.BOX, - min=120, + min=1, max=1440, unit_of_measurement="m", ) @@ -287,6 +289,8 @@ async def async_step_finished(self, user_input: dict[str, Any] | None = None): else: user_input[CONF_SOURCES] = list(SOURCE_MAP) user_input[CONF_AREAS] = self.configured_areas + user_input[CONF_SCAN_INTERVAL] = self.interval + user_input[CONF_TIMEOUT] = self.timeout return self.async_create_entry(title=NAME, data=user_input) return self.async_show_form(step_id="finished", errors=errors, last_step=True) @@ -304,12 +308,24 @@ class FuelPricesOptionsFlow(config_entries.OptionsFlow): configured_sources = [] configuring_area = {} configuring_index = -1 + timeout = 10 + interval = 1440 def __init__(self, config_entry: config_entries.ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - self.configured_areas = self.config_entry.data.get(CONF_AREAS, []) - self.configured_sources = self.config_entry.data.get(CONF_SOURCES, []) + self.configured_areas = config_entry.options.get( + CONF_AREAS, config_entry.data.get(CONF_AREAS, []) + ) + self.configured_sources = config_entry.options.get( + CONF_SOURCES, config_entry.data.get(CONF_SOURCES, []) + ) + self.timeout = config_entry.options.get( + CONF_TIMEOUT, config_entry.data.get(CONF_TIMEOUT, 10) + ) + self.interval = config_entry.options.get( + CONF_SCAN_INTERVAL, config_entry.data.get(CONF_SCAN_INTERVAL, 1440) + ) @property def configured_area_names(self) -> list[str]: @@ -338,6 +354,8 @@ async def async_step_sources(self, user_input: dict[str, Any] | None = None): """Set data source config.""" if user_input is not None: self.configured_sources = user_input[CONF_SOURCES] + self.timeout = user_input[CONF_TIMEOUT] + self.interval = user_input[CONF_SCAN_INTERVAL] return await self.async_step_main_menu(None) return self.async_show_form( step_id="sources", @@ -351,7 +369,29 @@ async def async_step_sources(self, user_input: dict[str, Any] | None = None): options=list(SOURCE_MAP), multiple=True, ) - ) + ), + vol.Optional( + CONF_TIMEOUT, + default=self.timeout, + ): selector.NumberSelector( + selector.NumberSelectorConfig( + mode=selector.NumberSelectorMode.BOX, + min=5, + max=60, + unit_of_measurement="s", + ) + ), + vol.Optional( + CONF_SCAN_INTERVAL, + default=self.interval, + ): selector.NumberSelector( + selector.NumberSelectorConfig( + mode=selector.NumberSelectorMode.BOX, + min=1, + max=1440, + unit_of_measurement="m", + ) + ), } ), ) @@ -495,6 +535,8 @@ async def async_step_finished(self, user_input: dict[str, Any] | None = None): else list(SOURCE_MAP) ) user_input[CONF_AREAS] = self.configured_areas + user_input[CONF_SCAN_INTERVAL] = self.interval + user_input[CONF_TIMEOUT] = self.timeout return self.async_create_entry(title=NAME, data=user_input) return self.async_show_form(step_id="finished", errors=errors, last_step=True) diff --git a/custom_components/fuel_prices/manifest.json b/custom_components/fuel_prices/manifest.json index e2ffd69..96a00af 100644 --- a/custom_components/fuel_prices/manifest.json +++ b/custom_components/fuel_prices/manifest.json @@ -12,7 +12,7 @@ "requirements": [ "reverse-geocode==1.4.1", "these-united-states==1.1.0.21", - "pyfuelprices==2.2.2" + "pyfuelprices==2.2.3" ], "ssdp": [], "version": "0.0.0", diff --git a/requirements.txt b/requirements.txt index cb14102..60aca4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ colorlog==6.7.0 homeassistant==2023.8.0 pip>=21.0,<23.2 ruff==0.0.292 -pyfuelprices==2.2.2 \ No newline at end of file +pyfuelprices==2.2.3 \ No newline at end of file