|
| 1 | +# Inspired from https://aarongodfrey.dev/home%20automation |
| 2 | +# /building_a_home_assistant_custom_component_part_3/ |
| 3 | +# from copy import deepcopy |
| 4 | +import logging |
| 5 | +from typing import Any, Optional |
| 6 | + |
| 7 | +from homeassistant import config_entries |
| 8 | + |
| 9 | +from . import DOMAIN |
| 10 | +from . import utils as u |
| 11 | + |
| 12 | +# from homeassistant.const import CONF_ACCESS_TOKEN,CONF_NAME |
| 13 | +# from homeassistant.const import CONF_PATH,CONF_URL |
| 14 | +# from homeassistant.core import callback |
| 15 | +# from homeassistant.helpers.aiohttp_client import async_get_clientsession |
| 16 | +# import homeassistant.helpers.config_validation as cv |
| 17 | +# from homeassistant.helpers.entity_registry import ( |
| 18 | +# async_entries_for_config_entry, |
| 19 | +# async_get_registry, |
| 20 | +# ) |
| 21 | +# import voluptuous as vol |
| 22 | + |
| 23 | + |
| 24 | +_LOGGER = logging.getLogger(__name__) |
| 25 | + |
| 26 | +# INITIAL_CONFIG_SCHEMA = vol.Schema( |
| 27 | +# #{vol.Required(CONF_SKEY): cv.string, vol.Optional(CONF_O_KEY): cv.string} |
| 28 | +# ) |
| 29 | +# EXTRA_CONF_SCHEMA = vol.Schema( |
| 30 | +# { |
| 31 | +# #vol.Required(CONF_PATH): cv.string, |
| 32 | +# #vol.Optional(CONF_NAME): cv.string, |
| 33 | +# #vol.Optional("add_another"): cv.boolean, |
| 34 | +# } |
| 35 | +# ) |
| 36 | + |
| 37 | +# OPTIONS_SCHEMA = vol.Schema({vol.Optional(CONF_NM, default="go"): cv.string}) |
| 38 | + |
| 39 | + |
| 40 | +class ZhaToolkitCustomConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): |
| 41 | + """Zha Toolkit Custom config flow.""" |
| 42 | + |
| 43 | + data: Optional[dict[str, Any]] |
| 44 | + |
| 45 | + async def my_async_create_entry(self): |
| 46 | + self.data["VERSION"] = u.getVersion() |
| 47 | + # Create the configuration entry |
| 48 | + return self.async_create_entry(title="ZHA Toolkit", data=self.data) |
| 49 | + |
| 50 | + async def async_step_user( |
| 51 | + self, user_input: Optional[dict[str, Any]] = None |
| 52 | + ): |
| 53 | + """Invoked when a user initiates a flow via the user interface.""" |
| 54 | + # errors: dict[str, str] = {} |
| 55 | + # Nothing special to configure, end configuration step |
| 56 | + return self.my_async_create_entry() |
| 57 | + |
| 58 | + |
| 59 | +# if user_input is not None: |
| 60 | +# # Initially None, but not None when user entered data. |
| 61 | +# try: |
| 62 | +# await validate_something( |
| 63 | +# user_input[CONF_ACCESS_TOKEN], self.hass |
| 64 | +# ) |
| 65 | +# except ValueError: |
| 66 | +# errors["base"] = "error_message" # key in `strings.json` |
| 67 | +# if not errors: |
| 68 | +# # Input is valid, set data. |
| 69 | +# self.data = user_input |
| 70 | +# self.data[CONF_SOME_KEY] = [] |
| 71 | +# # Return the form of the next step. |
| 72 | +# return await self.async_step_repo() |
| 73 | + |
| 74 | +# return self.async_show_form( |
| 75 | +# step_id="user", data_schema=INITIAL_CONFIG_SCHEMA, errors=errors |
| 76 | +# ) |
| 77 | + |
| 78 | +# async def async_step_repo( |
| 79 | +# self, user_input: Optional[Dict[str, Any]] = None |
| 80 | +# ): |
| 81 | +# """Second step in config flow to add a repo to watch.""" |
| 82 | +# errors: Dict[str, str] = {} |
| 83 | +# if user_input is not None: |
| 84 | +# # Validate the path. |
| 85 | +# try: |
| 86 | +# await validate_path( |
| 87 | +# user_input[CONF_PATH], |
| 88 | +# self.data[CONF_ACCESS_TOKEN], |
| 89 | +# self.hass, |
| 90 | +# ) |
| 91 | +# except ValueError: |
| 92 | +# errors["base"] = "invalid_path" |
| 93 | + |
| 94 | +# if not errors: |
| 95 | +# # Input is valid, set data. |
| 96 | +# self.data[CONF_REPOS].append( |
| 97 | +# { |
| 98 | +# "path": user_input[CONF_PATH], |
| 99 | +# "name": user_input.get( |
| 100 | +# CONF_NAME, user_input[CONF_PATH] |
| 101 | +# ), |
| 102 | +# } |
| 103 | +# ) |
| 104 | +# # If user ticked the box show this form again so they can add |
| 105 | +# # an additional repo. |
| 106 | +# if user_input.get("add_another", False): |
| 107 | +# return await self.async_step_repo() |
| 108 | + |
| 109 | +# # User is done adding repos, create the config entry. |
| 110 | +# return self.async_create_entry( |
| 111 | +# title="GitHub Custom", data=self.data |
| 112 | +# ) |
| 113 | + |
| 114 | +# return self.async_show_form( |
| 115 | +# step_id="repo", data_schema=EXTRA_CONF_SCHEMA, errors=errors |
| 116 | +# ) |
| 117 | + |
| 118 | +# @staticmethod |
| 119 | +# @callback |
| 120 | +# def async_get_options_flow(config_entry): |
| 121 | +# """Get the options flow for this handler.""" |
| 122 | +# return OptionsFlowHandler(config_entry) |
| 123 | + |
| 124 | + |
| 125 | +# class OptionsFlowHandler(config_entries.OptionsFlow): |
| 126 | +# """Handles options flow for the component.""" |
| 127 | + |
| 128 | +# def __init__(self, config_entry: config_entries.ConfigEntry) -> None: |
| 129 | +# self.config_entry = config_entry |
| 130 | + |
| 131 | +# async def async_step_init( |
| 132 | +# self, user_input: Dict[str, Any] = None |
| 133 | +# ) -> Dict[str, Any]: |
| 134 | +# """Manage the options for the custom component.""" |
| 135 | +# errors: Dict[str, str] = {} |
| 136 | +# # Grab all configured repos from the entity registry so we can populate |
| 137 | +# # the multi-select dropdown that will allow a user to remove a repo. |
| 138 | +# entity_registry = await async_get_registry(self.hass) |
| 139 | +# entries = async_entries_for_config_entry( |
| 140 | +# entity_registry, self.config_entry.entry_id |
| 141 | +# ) |
| 142 | +# # Default value for our multi-select. |
| 143 | +# all_repos = {e.entity_id: e.original_name for e in entries} |
| 144 | +# repo_map = {e.entity_id: e for e in entries} |
| 145 | + |
| 146 | +# if user_input is not None: |
| 147 | +# updated_repos = deepcopy(self.config_entry.data[CONF_REPOS]) |
| 148 | + |
| 149 | +# # Remove any unchecked repos. |
| 150 | +# removed_entities = [ |
| 151 | +# entity_id |
| 152 | +# for entity_id in repo_map.keys() |
| 153 | +# if entity_id not in user_input["repos"] |
| 154 | +# ] |
| 155 | +# for entity_id in removed_entities: |
| 156 | +# # Unregister from HA |
| 157 | +# entity_registry.async_remove(entity_id) |
| 158 | +# # Remove from our configured repos. |
| 159 | +# entry = repo_map[entity_id] |
| 160 | +# entry_path = entry.unique_id |
| 161 | +# updated_repos = [ |
| 162 | +# e for e in updated_repos if e["path"] != entry_path |
| 163 | +# ] |
| 164 | + |
| 165 | +# if user_input.get(CONF_PATH): |
| 166 | +# # Validate the path. |
| 167 | +# access_token = self.hass.data[DOMAIN][ |
| 168 | +# self.config_entry.entry_id |
| 169 | +# ][CONF_ACCESS_TOKEN] |
| 170 | +# try: |
| 171 | +# await validate_path( |
| 172 | +# user_input[CONF_PATH], access_token, self.hass |
| 173 | +# ) |
| 174 | +# except ValueError: |
| 175 | +# errors["base"] = "invalid_path" |
| 176 | + |
| 177 | +# if not errors: |
| 178 | +# # Add the new repo. |
| 179 | +# updated_repos.append( |
| 180 | +# { |
| 181 | +# "path": user_input[CONF_PATH], |
| 182 | +# "name": user_input.get( |
| 183 | +# CONF_NAME, user_input[CONF_PATH] |
| 184 | +# ), |
| 185 | +# } |
| 186 | +# ) |
| 187 | + |
| 188 | +# if not errors: |
| 189 | +# # Value of data will be set on the options property of our |
| 190 | +# # config_entry instance. |
| 191 | +# return self.async_create_entry( |
| 192 | +# title="", |
| 193 | +# data={CONF_REPOS: updated_repos}, |
| 194 | +# ) |
| 195 | + |
| 196 | +# options_schema = vol.Schema( |
| 197 | +# { |
| 198 | +# vol.Optional( |
| 199 | +# "repos", default=list(all_repos.keys()) |
| 200 | +# ): cv.multi_select(all_repos), |
| 201 | +# vol.Optional(CONF_PATH): cv.string, |
| 202 | +# vol.Optional(CONF_NAME): cv.string, |
| 203 | +# } |
| 204 | +# ) |
| 205 | +# return self.async_show_form( |
| 206 | +# step_id="init", data_schema=options_schema, errors=errors |
| 207 | +# ) |
0 commit comments