From 787e9738c7f5c6b0ed4da07f3e24c9a53b9f2b15 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 24 Mar 2025 21:18:54 +0100 Subject: [PATCH] feat: add an option-flow to configure type ID mappings (unused) --- custom_components/ecocito/config_flow.py | 19 ++++- custom_components/ecocito/const.py | 4 + custom_components/ecocito/options_flow.py | 95 +++++++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 custom_components/ecocito/options_flow.py diff --git a/custom_components/ecocito/config_flow.py b/custom_components/ecocito/config_flow.py index 990495d..bac8804 100644 --- a/custom_components/ecocito/config_flow.py +++ b/custom_components/ecocito/config_flow.py @@ -6,9 +6,14 @@ import logging from typing import Any import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_USERNAME -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from .client import EcocitoClient from .const import DOMAIN @@ -24,7 +29,6 @@ STEP_USER_DATA_SCHEMA = vol.Schema( } ) - async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None: """Validate the user input allows us to connect.""" client = EcocitoClient(data[CONF_DOMAIN], data[CONF_USERNAME], data[CONF_PASSWORD]) @@ -59,3 +63,12 @@ class EcocitoConfigFlow(ConfigFlow, domain=DOMAIN): return self.async_show_form( step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) + + @staticmethod + @callback + def async_get_options_flow( + config_entry: ConfigEntry + ) -> OptionsFlow: + """Return the options flow.""" + from .options_flow import EcocitoOptionsFlowHandler + return EcocitoOptionsFlowHandler(config_entry) diff --git a/custom_components/ecocito/const.py b/custom_components/ecocito/const.py index 2ccf4f1..b5278ea 100644 --- a/custom_components/ecocito/const.py +++ b/custom_components/ecocito/const.py @@ -23,9 +23,13 @@ ECOCITO_LOGIN_USERNAME_KEY = "Identifiant" ECOCITO_LOGIN_PASSWORD_KEY = "MotDePasse" # noqa: S105 # Ecocito - Collection types +ECOCITO_GARBAGE_TYPE = "garbage_id" +ECOCITO_RECYCLE_TYPE = "recycle_id" +ECOCITO_REFRESH_MIN_KEY = "refresh_min" ECOCITO_DEFAULT_COLLECTION_TYPE = -1 ECOCITO_GARBAGE_COLLECTION_TYPE = 15 ECOCITO_RECYCLING_COLLECTION_TYPE = 16 +ECOCITO_DEFAULT_REFRESH_MIN = 60 # Ecocito - Collection endpoint ECOCITO_COLLECTION_ENDPOINT = f"https://{ECOCITO_DOMAIN}/Usager/Collecte/GetCollecte" diff --git a/custom_components/ecocito/options_flow.py b/custom_components/ecocito/options_flow.py new file mode 100644 index 0000000..b16970f --- /dev/null +++ b/custom_components/ecocito/options_flow.py @@ -0,0 +1,95 @@ +"""Options flow for ecocito configuration.""" + +from __future__ import annotations + +from typing import Any + +import voluptuous as vol +from homeassistant import config_entries +from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_USERNAME +from homeassistant.helpers import selector + +from .client import EcocitoClient +from .const import ( + ECOCITO_GARBAGE_TYPE, + ECOCITO_RECYCLE_TYPE, + ECOCITO_REFRESH_MIN_KEY, +) + + +def build_schema(type_mapping: dict[int, str], current: dict[str, Any]) -> vol.Schema: + """Build the schema.""" + types_options = [ + {"value": str(type_id), "label": type_label} + for type_id, type_label in type_mapping.items() + ] + return vol.Schema( + { + vol.Optional(ECOCITO_GARBAGE_TYPE, default=current[ECOCITO_GARBAGE_TYPE]): selector.SelectSelector( + selector.SelectSelectorConfig( + options=types_options, + mode=selector.SelectSelectorMode.DROPDOWN, + multiple=False + ) + ), + vol.Optional(ECOCITO_RECYCLE_TYPE, default=current[ECOCITO_RECYCLE_TYPE]): selector.SelectSelector( + selector.SelectSelectorConfig( + options=types_options, + mode=selector.SelectSelectorMode.DROPDOWN, + multiple=False + ) + ), + vol.Required(ECOCITO_REFRESH_MIN_KEY, default=current[ECOCITO_REFRESH_MIN_KEY]): int + } + ) + +class EcocitoOptionsFlowHandler(config_entries.OptionsFlow): + """Handle an option flow for ecocito.""" + + def __init__(self, config_entry: config_entries.ConfigEntry) -> None: + """Init.""" + self._entry = config_entry + + + async def get_type_mapping(self) -> dict[int, str]: + """Build client from config.""" + client = EcocitoClient( + self._entry.data[CONF_DOMAIN], + self._entry.data[CONF_USERNAME], + self._entry.data[CONF_PASSWORD], + ) + await client.authenticate() + return await client.get_collection_types() + + async def update_config(self, user_input: dict[str, Any]) -> None: + """Update configuration with new user input.""" + # TODO Sanitize user input + new_data = dict(self._entry.data) + if user_input[ECOCITO_GARBAGE_TYPE] != new_data.get(ECOCITO_GARBAGE_TYPE): + new_data[ECOCITO_GARBAGE_TYPE] = int(user_input[ECOCITO_GARBAGE_TYPE]) + if user_input[ECOCITO_RECYCLE_TYPE] != new_data.get(ECOCITO_RECYCLE_TYPE): + new_data[ECOCITO_RECYCLE_TYPE] = int(user_input[ECOCITO_RECYCLE_TYPE]) + if user_input[ECOCITO_REFRESH_MIN_KEY] != new_data.get(ECOCITO_REFRESH_MIN_KEY): + new_data[ECOCITO_REFRESH_MIN_KEY] = int(user_input[ECOCITO_REFRESH_MIN_KEY]) + self.hass.config_entries.async_update_entry( + self._entry, data=new_data + ) + await self.hass.config_entries.async_reload(self._entry.entry_id) + + async def async_step_init(self, user_input: dict[str, Any] | None = None): + """Display configuration menu.""" + errors: dict[str, str] = {} + if user_input is not None: + await self.update_config(user_input) + return self.async_abort(reason="Changes saved.") + placeholders = { + ECOCITO_GARBAGE_TYPE: str(self._entry.data.get(ECOCITO_GARBAGE_TYPE, 15)), + ECOCITO_RECYCLE_TYPE: str(self._entry.data.get(ECOCITO_RECYCLE_TYPE, 16)), + ECOCITO_REFRESH_MIN_KEY: int(self._entry.data.get(ECOCITO_REFRESH_MIN_KEY, 60)), + } + schema = build_schema(await self.get_type_mapping(), placeholders) + return self.async_show_form( + step_id="init", + data_schema=schema, + errors=errors + )