diff --git a/custom_components/speedport/__init__.py b/custom_components/speedport/__init__.py index d5cd27f..838c54a 100644 --- a/custom_components/speedport/__init__.py +++ b/custom_components/speedport/__init__.py @@ -1,11 +1,11 @@ """The Speedport integration.""" - from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import aiohttp_client from speedport import Speedport +from .config_flow import OptionsFlowHandler from .const import DOMAIN PLATFORMS: list[Platform] = [ @@ -19,15 +19,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Speedport from a config entry.""" - session = aiohttp_client.async_get_clientsession(hass) hass.data.setdefault(DOMAIN, {}) speedport = await Speedport( - host=entry.data["host"], password=entry.data["password"], session=session + host=entry.data["host"], + password=entry.data["password"], + session=aiohttp_client.async_get_clientsession(hass), + pause_time=entry.options.get("pause_time", 5), ).create() hass.data[DOMAIN][entry.entry_id] = speedport hass.data[DOMAIN]["coordinators"] = {} + entry.async_on_unload(entry.add_update_listener(update_listener)) + for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform) @@ -35,6 +39,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True +async def update_listener(hass, entry): + """Handle options update.""" + speedport: Speedport = hass.data[DOMAIN][entry.entry_id] + speedport.set_pause_time(entry.options.get("pause_time", 5)) + + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): diff --git a/custom_components/speedport/config_flow.py b/custom_components/speedport/config_flow.py index 9294383..00d8399 100644 --- a/custom_components/speedport/config_flow.py +++ b/custom_components/speedport/config_flow.py @@ -8,7 +8,7 @@ import voluptuous as vol from homeassistant import config_entries -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError @@ -76,6 +76,14 @@ async def async_step_user( step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) + @staticmethod + @callback + def async_get_options_flow( + config_entry: config_entries.ConfigEntry, + ) -> config_entries.OptionsFlow: + """Create the options flow.""" + return OptionsFlowHandler(config_entry) + class CannotConnect(HomeAssistantError): """Error to indicate we cannot connect.""" @@ -83,3 +91,28 @@ class CannotConnect(HomeAssistantError): class InvalidAuth(HomeAssistantError): """Error to indicate there is invalid auth.""" + + +class OptionsFlowHandler(config_entries.OptionsFlow): + def __init__(self, config_entry: config_entries.ConfigEntry) -> None: + """Initialize options flow.""" + self.config_entry = config_entry + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> FlowResult: + """Manage the options.""" + if user_input is not None: + return self.async_create_entry(title="", data=user_input) + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Required( + "pause_time", + default=self.config_entry.options.get("pause_time", 5), + ): int + } + ), + ) diff --git a/custom_components/speedport/manifest.json b/custom_components/speedport/manifest.json index 6c546df..85976eb 100644 --- a/custom_components/speedport/manifest.json +++ b/custom_components/speedport/manifest.json @@ -12,7 +12,7 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/Andre0512/speedport/issues", "requirements": [ - "speedport-api==0.5.4" + "speedport-api==0.5.5" ], - "version": "0.3.1" + "version": "0.3.2" } diff --git a/custom_components/speedport/translations/en.json b/custom_components/speedport/translations/en.json index e32d9b8..65fdfb5 100644 --- a/custom_components/speedport/translations/en.json +++ b/custom_components/speedport/translations/en.json @@ -17,5 +17,16 @@ } } } + }, + "options": { + "step": { + "init": { + "data": { + "pause_time": "Pause for minutes" + }, + "title": "Settings", + "description": "Time to stop updating sensors that require authorization when the user logs in to the web interface. (User gets logged out after this time)" + } + } } } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 407c017..589dfca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -speedport-api==0.5.4 +speedport-api==0.5.5 homeassistant~=2023.10 pytz~=2023.3