From 886ab957bb61bc0186c58eccd1319d28040ee482 Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Sat, 19 Aug 2023 17:06:58 +0200 Subject: [PATCH] added options flow --- custom_components/bluetti_bt/config_flow.py | 56 ++++++++++++++++++- custom_components/bluetti_bt/const.py | 1 + custom_components/bluetti_bt/coordinator.py | 3 +- .../bluetti_bt/translations/de.json | 11 ++++ .../bluetti_bt/translations/en.json | 11 ++++ 5 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 custom_components/bluetti_bt/translations/de.json create mode 100644 custom_components/bluetti_bt/translations/en.json diff --git a/custom_components/bluetti_bt/config_flow.py b/custom_components/bluetti_bt/config_flow.py index 9823ec5..6e818e5 100644 --- a/custom_components/bluetti_bt/config_flow.py +++ b/custom_components/bluetti_bt/config_flow.py @@ -14,9 +14,10 @@ from homeassistant.components.bluetooth import ( ) from homeassistant.const import CONF_ADDRESS, CONF_TYPE, CONF_NAME from homeassistant.data_entry_flow import FlowResult +from homeassistant.helpers import selector from . import get_type_by_bt_name -from .const import DOMAIN +from .const import DOMAIN, CONF_USE_CONTROLS _LOGGER = logging.getLogger(__name__) @@ -86,3 +87,56 @@ class BluettiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): step_id="user", data_schema=data_schema, ) + + @staticmethod + def async_get_options_flow( + config_entry: config_entries.ConfigEntry, + ) -> config_entries.OptionsFlow: + """Create the options flow.""" + return OptionsFlowHandler(config_entry) + + +class OptionsFlowHandler(config_entries.OptionsFlow): + """Handle a option flow.""" + + 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: + changed = self.hass.config_entries.async_update_entry( + self.config_entry, + data={ + **self.config_entry.data, + **{ + CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS], + }, + }, + ) + if changed is False: + _LOGGER.error( + "Method OptionsFlowHandler.async_step_init: Config entry %s has not been changed", + self.config_entry.entry_id, + ) + return self.async_create_entry( + title="", + data={ + CONF_USE_CONTROLS: user_input[CONF_USE_CONTROLS], + }, + ) + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Required( + CONF_USE_CONTROLS, + default=self.config_entry.data.get(CONF_USE_CONTROLS, False), + ): selector.BooleanSelector(), + } + ), + ) diff --git a/custom_components/bluetti_bt/const.py b/custom_components/bluetti_bt/const.py index 3b11800..9848d21 100644 --- a/custom_components/bluetti_bt/const.py +++ b/custom_components/bluetti_bt/const.py @@ -6,6 +6,7 @@ DOMAIN = "bluetti_bt" MANUFACTURER = "Bluetti" CONF_OPTIONS = "options" +CONF_USE_CONTROLS = "use_controls" DIAGNOSTIC_FIELDS = [ "max_ac_input_power", diff --git a/custom_components/bluetti_bt/coordinator.py b/custom_components/bluetti_bt/coordinator.py index f2338b3..85102bb 100644 --- a/custom_components/bluetti_bt/coordinator.py +++ b/custom_components/bluetti_bt/coordinator.py @@ -1,12 +1,11 @@ """Coordinator for Bluetti integration.""" from __future__ import annotations -from typing import List +from typing import List, cast import asyncio from datetime import timedelta import logging -from typing import cast import async_timeout from bleak import BleakClient, BleakError diff --git a/custom_components/bluetti_bt/translations/de.json b/custom_components/bluetti_bt/translations/de.json new file mode 100644 index 0000000..8b1d377 --- /dev/null +++ b/custom_components/bluetti_bt/translations/de.json @@ -0,0 +1,11 @@ +{ + "options": { + "step": { + "init": { + "data": { + "use_controls": "Steuerung aktivieren (auf eigenes Risiko)" + } + } + } + } +} \ No newline at end of file diff --git a/custom_components/bluetti_bt/translations/en.json b/custom_components/bluetti_bt/translations/en.json new file mode 100644 index 0000000..d3fceda --- /dev/null +++ b/custom_components/bluetti_bt/translations/en.json @@ -0,0 +1,11 @@ +{ + "options": { + "step": { + "init": { + "data": { + "use_controls": "Use controls (use at own risk)" + } + } + } + } +} \ No newline at end of file