|
| 1 | +from copy import deepcopy |
| 2 | +import logging |
| 3 | +from typing import Any, Dict, Optional |
| 4 | + |
| 5 | +from homeassistant import config_entries, core |
| 6 | +from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL |
| 7 | +from homeassistant.core import callback |
| 8 | +import homeassistant.helpers.config_validation as cv |
| 9 | +import voluptuous as vol |
| 10 | +from homeassistant.helpers.entity_registry import ( |
| 11 | + async_entries_for_config_entry, |
| 12 | + async_get_registry, |
| 13 | +) |
| 14 | + |
| 15 | +from .const import (CONF_PROPERTIES, DOMAIN, DEFAULT_SCAN_INTERVAL, DEFAULT_NAME, |
| 16 | + CONF_PROPERTY_ID, CONF_SCAN_INTERVAL_MIN, CONF_SCAN_INTERVAL_MAX) |
| 17 | + |
| 18 | +_LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | +CONF_SCHEMA = vol.Schema( |
| 21 | + { |
| 22 | + vol.Required(CONF_NAME, default=DEFAULT_NAME): str, |
| 23 | + vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL |
| 24 | + ): vol.All(vol.Coerce(int), vol.Range(min=CONF_SCAN_INTERVAL_MIN, max=CONF_SCAN_INTERVAL_MAX)) |
| 25 | + } |
| 26 | +) |
| 27 | +PROPERTY_SCHEMA = vol.Schema( |
| 28 | + { |
| 29 | + vol.Optional(CONF_PROPERTY_ID): cv.string, |
| 30 | + vol.Optional("add_another"): cv.boolean, |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +class RedfinConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): |
| 36 | + """Redfin config flow.""" |
| 37 | + |
| 38 | + VERSION = 1 |
| 39 | + |
| 40 | + data: Optional[Dict[str, Any]] |
| 41 | + options: Optional[Dict[str, Any]] = {} |
| 42 | + |
| 43 | + async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None): |
| 44 | + """Invoked when a user initiates a flow via the user interface.""" |
| 45 | + errors: Dict[str, str] = {} |
| 46 | + |
| 47 | + if self._async_current_entries(): |
| 48 | + return self.async_abort(reason="single_instance_allowed") |
| 49 | + if self.hass.data.get(DOMAIN): |
| 50 | + return self.async_abort(reason="single_instance_allowed") |
| 51 | + |
| 52 | + if user_input is not None: |
| 53 | + self.data = dict() # user_input |
| 54 | + self.options = user_input |
| 55 | + self.options[CONF_PROPERTIES] = [] |
| 56 | + # Return the form of the next step. |
| 57 | + return await self.async_step_property() |
| 58 | + |
| 59 | + return self.async_show_form( |
| 60 | + step_id="user", data_schema=CONF_SCHEMA, errors=errors |
| 61 | + ) |
| 62 | + |
| 63 | + async def async_step_property(self, user_input: Optional[Dict[str, Any]] = None): |
| 64 | + """Second step in config flow to add a property id's.""" |
| 65 | + errors: Dict[str, str] = {} |
| 66 | + |
| 67 | + if user_input is not None: |
| 68 | + |
| 69 | + # check for duplicate property id's |
| 70 | + is_dup = False |
| 71 | + for params in self.options[CONF_PROPERTIES]: |
| 72 | + if user_input[CONF_PROPERTY_ID] == params[CONF_PROPERTY_ID]: |
| 73 | + is_dup = True |
| 74 | + if is_dup == True: |
| 75 | + errors["base"] = "duplicate_property_id" |
| 76 | + else: |
| 77 | + self.options[CONF_PROPERTIES].append( |
| 78 | + {"property_id": user_input[CONF_PROPERTY_ID]} |
| 79 | + ) |
| 80 | + |
| 81 | + if not errors: |
| 82 | + # If user ticked the box show this form again so they can add |
| 83 | + if user_input.get("add_another", False): |
| 84 | + return await self.async_step_property() |
| 85 | + |
| 86 | + # User is done adding properties, create the config entry. |
| 87 | + _LOGGER.debug("%s component added a new config entry: %s", DOMAIN, self.options) |
| 88 | + return self.async_create_entry(title=self.options["name"], data=self.data, options=self.options) |
| 89 | + |
| 90 | + return self.async_show_form( |
| 91 | + step_id="property", data_schema=PROPERTY_SCHEMA, errors=errors |
| 92 | + ) |
| 93 | + |
| 94 | + @staticmethod |
| 95 | + @callback |
| 96 | + def async_get_options_flow(config_entry): |
| 97 | + return OptionsFlowHandler(config_entry) |
| 98 | + |
| 99 | + |
| 100 | +class OptionsFlowHandler(config_entries.OptionsFlow): |
| 101 | + """Handles options flow for the component.""" |
| 102 | + |
| 103 | + def __init__(self, config_entry: config_entries.ConfigEntry) -> None: |
| 104 | + self.config_entry = config_entry |
| 105 | + |
| 106 | + async def async_step_init( |
| 107 | + self, user_input: Dict[str, Any] = None |
| 108 | + ) -> Dict[str, Any]: |
| 109 | + """Manage the options for the component.""" |
| 110 | + errors: Dict[str, str] = {} |
| 111 | + # Grab all configured propert id's from the entity registry so we can populate the |
| 112 | + # multi-select dropdown that will allow a user to remove a property. |
| 113 | + entity_registry = await async_get_registry(self.hass) |
| 114 | + entries = async_entries_for_config_entry( |
| 115 | + entity_registry, self.config_entry.entry_id |
| 116 | + ) |
| 117 | + # Default value for our multi-select. |
| 118 | + all_properties = {e.entity_id: e.original_name for e in entries} |
| 119 | + property_map = {e.entity_id: e for e in entries} |
| 120 | + |
| 121 | + if user_input is not None: |
| 122 | + updated_properties = deepcopy(self.config_entry.options[CONF_PROPERTIES]) |
| 123 | + |
| 124 | + # Remove any unchecked properties. |
| 125 | + removed_entities = [ |
| 126 | + entity_id |
| 127 | + for entity_id in property_map.keys() |
| 128 | + if entity_id not in user_input["properties"] |
| 129 | + ] |
| 130 | + for entity_id in removed_entities: |
| 131 | + # Unregister from HA |
| 132 | + entity_registry.async_remove(entity_id) |
| 133 | + # Remove from our configured properties. |
| 134 | + entry = property_map[entity_id] |
| 135 | + property_id = entry.unique_id |
| 136 | + updated_properties = [e for e in updated_properties if e[CONF_PROPERTY_ID] != property_id] |
| 137 | + |
| 138 | + if user_input.get(CONF_PROPERTY_ID): |
| 139 | + |
| 140 | + # check for duplicate property id's |
| 141 | + is_dup = False |
| 142 | + for params in updated_properties: |
| 143 | + if user_input[CONF_PROPERTY_ID] == params[CONF_PROPERTY_ID]: |
| 144 | + is_dup = True |
| 145 | + if is_dup == True: |
| 146 | + errors["base"] = "duplicate_property_id" |
| 147 | + else: |
| 148 | + # Add the new property. |
| 149 | + updated_properties.append( |
| 150 | + {CONF_PROPERTY_ID: user_input[CONF_PROPERTY_ID]} |
| 151 | + ) |
| 152 | + |
| 153 | + if not errors: |
| 154 | + # Value of data will be set on the options property of the config_entry |
| 155 | + # instance. |
| 156 | + return self.async_create_entry( |
| 157 | + title="", |
| 158 | + data={ |
| 159 | + CONF_NAME: user_input[CONF_NAME], |
| 160 | + CONF_SCAN_INTERVAL: user_input[CONF_SCAN_INTERVAL], |
| 161 | + CONF_PROPERTIES: updated_properties |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + options_schema = vol.Schema( |
| 166 | + { |
| 167 | + vol.Optional("properties", default=list(all_properties.keys())): cv.multi_select( |
| 168 | + all_properties |
| 169 | + ), |
| 170 | + vol.Optional(CONF_NAME, default=self.config_entry.options[CONF_NAME]): str, |
| 171 | + vol.Optional(CONF_SCAN_INTERVAL, default=self.config_entry.options[CONF_SCAN_INTERVAL] |
| 172 | + ): vol.All(vol.Coerce(int), vol.Range(min=CONF_SCAN_INTERVAL_MIN, max=CONF_SCAN_INTERVAL_MAX)), |
| 173 | + vol.Optional(CONF_PROPERTY_ID): cv.string, |
| 174 | + } |
| 175 | + ) |
| 176 | + |
| 177 | + return self.async_show_form( |
| 178 | + step_id="init", data_schema=options_schema, errors=errors |
| 179 | + ) |
0 commit comments