diff --git a/custom_components/garo_wallbox/config_flow.py b/custom_components/garo_wallbox/config_flow.py index 38c6550..0c9c010 100644 --- a/custom_components/garo_wallbox/config_flow.py +++ b/custom_components/garo_wallbox/config_flow.py @@ -1,6 +1,7 @@ """Config flow for the Garo Wallbox platform.""" import asyncio import logging +from typing import Any, Dict, Optional from aiohttp import ClientConnectionError from async_timeout import timeout @@ -8,38 +9,54 @@ from homeassistant import config_entries from homeassistant.const import CONF_HOST, CONF_NAME +from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import TIMEOUT +from .const import TIMEOUT, DOMAIN, CONF_DEVICE_FETCH_INTERVAL, CONF_METER_FETCH_INTERVAL, DEFAULT_DEVICE_FETCH_INTERVAL, DEFAULT_METER_FETCH_INTERVAL from .garo import ApiClient +from . import GaroConfigEntry _LOGGER = logging.getLogger(__name__) -@config_entries.HANDLERS.register("garo_wallbox") -class FlowHandler(config_entries.ConfigFlow): +class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow.""" VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL - async def _create_entry(self, host, name): + @staticmethod + @callback + def async_get_options_flow(config_entry): + """Get the options flow for this handler.""" + return GaroOptionsFlowHandler(config_entry) + + async def _create_entry(self, host, name, device_fetch_interval = None, meter_fetch_interval = None): """Register new entry.""" # Check if ip already is registered for entry in self._async_current_entries(): if entry.data[CONF_HOST] == host: return self.async_abort(reason="already_configured") - return self.async_create_entry(title=host, data={CONF_HOST: host, CONF_NAME: name}) + return self.async_create_entry( + title=host, + data={ + CONF_HOST: host, + CONF_NAME: name, + }, + options={ + CONF_DEVICE_FETCH_INTERVAL: device_fetch_interval or DEFAULT_DEVICE_FETCH_INTERVAL, + CONF_METER_FETCH_INTERVAL: meter_fetch_interval or DEFAULT_METER_FETCH_INTERVAL + }) - async def _create_device(self, host, name): + async def _create_device(self, host, name, device_fetch_interval = None, meter_fetch_interval= None): """Create device.""" session = async_get_clientsession(self.hass) api_client = ApiClient(session, host) try: with timeout(TIMEOUT): await api_client.async_get_configuration() - return await self._create_entry(host, name) + return await self._create_entry(host, name,device_fetch_interval, meter_fetch_interval) except asyncio.TimeoutError: _LOGGER.debug("Connection to %s timed out", host) return self.async_abort(reason="device_timeout") @@ -56,10 +73,22 @@ async def async_step_user(self, user_input=None): return self.async_show_form( step_id="user", data_schema=vol.Schema({ vol.Required(CONF_HOST): str, - vol.Optional(CONF_NAME): str - }) + vol.Optional(CONF_NAME): str, + vol.Optional( + CONF_DEVICE_FETCH_INTERVAL, + default=DEFAULT_DEVICE_FETCH_INTERVAL, + ): int, + vol.Optional( + CONF_METER_FETCH_INTERVAL, + default=DEFAULT_METER_FETCH_INTERVAL, + ): int, + }) ) - return await self._create_device(user_input[CONF_HOST], user_input[CONF_NAME]) + return await self._create_device( + user_input[CONF_HOST], + user_input[CONF_NAME], + user_input[CONF_DEVICE_FETCH_INTERVAL], + user_input[CONF_METER_FETCH_INTERVAL]) async def async_step_import(self, user_input): """Import a config entry.""" @@ -72,3 +101,45 @@ async def async_step_discovery(self, user_input): """Initialize step from discovery.""" _LOGGER.info("Discovered device: %s", user_input) return await self._create_entry(user_input[CONF_HOST], None) + +class GaroOptionsFlowHandler(config_entries.OptionsFlow): + """Handle Garo options.""" + + def __init__(self, config_entry: GaroConfigEntry): + """Initialize Garo options flow.""" + self.config_entry = config_entry + + async def async_step_init( + self, user_input: Optional[Dict[str, Any]] = None + ): + """Manage Garo 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( + CONF_HOST, + default=self.config_entry.data.get(CONF_HOST), + ): str, + vol.Optional( + CONF_NAME, + default=self.config_entry.data.get(CONF_NAME), + ): str, + vol.Optional( + CONF_DEVICE_FETCH_INTERVAL, + default=self.config_entry.options.get( + CONF_DEVICE_FETCH_INTERVAL, DEFAULT_DEVICE_FETCH_INTERVAL + ), + ): int, + vol.Optional( + CONF_METER_FETCH_INTERVAL, + default=self.config_entry.options.get( + CONF_METER_FETCH_INTERVAL, DEFAULT_METER_FETCH_INTERVAL + ), + ): int, + } + ), + ) \ No newline at end of file diff --git a/custom_components/garo_wallbox/strings.json b/custom_components/garo_wallbox/strings.json index 4782f34..b156870 100644 --- a/custom_components/garo_wallbox/strings.json +++ b/custom_components/garo_wallbox/strings.json @@ -6,7 +6,9 @@ "description": "Enter IP address of your Garo Wallbox.", "data": { "host": "Host", - "name": "Friendly name" + "name": "Friendly name", + "device_fetch_interval": "Fetch interval (seconds)", + "meter_fetch_interval": "Meter fetch interval (seconds)" } } }, @@ -16,6 +18,18 @@ "already_configured": "Device is already configured" } }, + "options": { + "step": { + "init": { + "data": { + "host": "Host", + "name": "Friendly name", + "device_fetch_interval": "Fetch interval (seconds)", + "meter_fetch_interval": "Meter fetch interval (seconds)" + } + } + } + }, "entity": { "sensor": { "sensor": { diff --git a/custom_components/garo_wallbox/translations/en.json b/custom_components/garo_wallbox/translations/en.json index 4782f34..b156870 100644 --- a/custom_components/garo_wallbox/translations/en.json +++ b/custom_components/garo_wallbox/translations/en.json @@ -6,7 +6,9 @@ "description": "Enter IP address of your Garo Wallbox.", "data": { "host": "Host", - "name": "Friendly name" + "name": "Friendly name", + "device_fetch_interval": "Fetch interval (seconds)", + "meter_fetch_interval": "Meter fetch interval (seconds)" } } }, @@ -16,6 +18,18 @@ "already_configured": "Device is already configured" } }, + "options": { + "step": { + "init": { + "data": { + "host": "Host", + "name": "Friendly name", + "device_fetch_interval": "Fetch interval (seconds)", + "meter_fetch_interval": "Meter fetch interval (seconds)" + } + } + } + }, "entity": { "sensor": { "sensor": {