|
14 | 14 | MontaApiClientCommunicationError, |
15 | 15 | MontaApiClientError, |
16 | 16 | ) |
17 | | -from .const import DOMAIN, LOGGER, STORAGE_KEY, STORAGE_VERSION |
| 17 | +from .const import CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, STORAGE_KEY, STORAGE_VERSION |
18 | 18 |
|
19 | 19 |
|
20 | 20 | class MontaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): |
@@ -66,6 +66,129 @@ async def async_step_user( |
66 | 66 | type=selector.TextSelectorType.PASSWORD |
67 | 67 | ), |
68 | 68 | ), |
| 69 | + vol.Optional( |
| 70 | + CONF_SCAN_INTERVAL, |
| 71 | + default=(user_input or {}).get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL), |
| 72 | + ): selector.NumberSelector( |
| 73 | + selector.NumberSelectorConfig( |
| 74 | + min=30, |
| 75 | + max=3600, |
| 76 | + unit_of_measurement="seconds", |
| 77 | + mode=selector.NumberSelectorMode.BOX, |
| 78 | + ), |
| 79 | + ), |
| 80 | + } |
| 81 | + ), |
| 82 | + errors=_errors, |
| 83 | + ) |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def async_get_options_flow( |
| 87 | + config_entry: config_entries.ConfigEntry, |
| 88 | + ) -> config_entries.OptionsFlow: |
| 89 | + """Get the options flow for this handler.""" |
| 90 | + return MontaOptionsFlowHandler(config_entry) |
| 91 | + |
| 92 | + async def _test_credentials(self, client_id: str, client_secret: str) -> any: |
| 93 | + """Validate credentials.""" |
| 94 | + client = MontaApiClient( |
| 95 | + client_id=client_id, |
| 96 | + client_secret=client_secret, |
| 97 | + session=async_create_clientsession(self.hass), |
| 98 | + store=Store(self.hass, STORAGE_VERSION, STORAGE_KEY), |
| 99 | + ) |
| 100 | + return await client.async_request_token() |
| 101 | + |
| 102 | + |
| 103 | +class MontaOptionsFlowHandler(config_entries.OptionsFlow): |
| 104 | + """Handle options flow for Monta.""" |
| 105 | + |
| 106 | + def __init__(self, config_entry: config_entries.ConfigEntry) -> None: |
| 107 | + """Initialize options flow.""" |
| 108 | + self.config_entry = config_entry |
| 109 | + |
| 110 | + async def async_step_init( |
| 111 | + self, user_input: dict | None = None |
| 112 | + ) -> config_entries.FlowResult: |
| 113 | + """Manage the options.""" |
| 114 | + _errors = {} |
| 115 | + if user_input is not None: |
| 116 | + # Only validate credentials if they were changed |
| 117 | + if ( |
| 118 | + user_input.get(CONF_CLIENT_ID) != self.config_entry.data.get(CONF_CLIENT_ID) |
| 119 | + or user_input.get(CONF_CLIENT_SECRET) != self.config_entry.data.get(CONF_CLIENT_SECRET) |
| 120 | + ): |
| 121 | + try: |
| 122 | + await self._test_credentials( |
| 123 | + client_id=user_input[CONF_CLIENT_ID], |
| 124 | + client_secret=user_input[CONF_CLIENT_SECRET], |
| 125 | + ) |
| 126 | + except MontaApiClientAuthenticationError as exception: |
| 127 | + LOGGER.warning(exception) |
| 128 | + _errors["base"] = "auth" |
| 129 | + except MontaApiClientCommunicationError as exception: |
| 130 | + LOGGER.error(exception) |
| 131 | + _errors["base"] = "connection" |
| 132 | + except MontaApiClientError as exception: |
| 133 | + LOGGER.exception(exception) |
| 134 | + _errors["base"] = "unknown" |
| 135 | + |
| 136 | + if not _errors: |
| 137 | + # Update the config entry data with new credentials if they changed |
| 138 | + if ( |
| 139 | + user_input.get(CONF_CLIENT_ID) != self.config_entry.data.get(CONF_CLIENT_ID) |
| 140 | + or user_input.get(CONF_CLIENT_SECRET) != self.config_entry.data.get(CONF_CLIENT_SECRET) |
| 141 | + ): |
| 142 | + self.hass.config_entries.async_update_entry( |
| 143 | + self.config_entry, |
| 144 | + data={ |
| 145 | + CONF_CLIENT_ID: user_input[CONF_CLIENT_ID], |
| 146 | + CONF_CLIENT_SECRET: user_input[CONF_CLIENT_SECRET], |
| 147 | + CONF_SCAN_INTERVAL: user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL), |
| 148 | + }, |
| 149 | + ) |
| 150 | + return self.async_create_entry(title="", data=user_input) |
| 151 | + |
| 152 | + return self.async_show_form( |
| 153 | + step_id="init", |
| 154 | + data_schema=vol.Schema( |
| 155 | + { |
| 156 | + vol.Required( |
| 157 | + CONF_CLIENT_ID, |
| 158 | + default=(user_input or {}).get( |
| 159 | + CONF_CLIENT_ID, |
| 160 | + self.config_entry.data.get(CONF_CLIENT_ID), |
| 161 | + ), |
| 162 | + ): selector.TextSelector( |
| 163 | + selector.TextSelectorConfig( |
| 164 | + type=selector.TextSelectorType.TEXT |
| 165 | + ), |
| 166 | + ), |
| 167 | + vol.Required( |
| 168 | + CONF_CLIENT_SECRET, |
| 169 | + default=(user_input or {}).get( |
| 170 | + CONF_CLIENT_SECRET, |
| 171 | + self.config_entry.data.get(CONF_CLIENT_SECRET), |
| 172 | + ), |
| 173 | + ): selector.TextSelector( |
| 174 | + selector.TextSelectorConfig( |
| 175 | + type=selector.TextSelectorType.PASSWORD |
| 176 | + ), |
| 177 | + ), |
| 178 | + vol.Optional( |
| 179 | + CONF_SCAN_INTERVAL, |
| 180 | + default=self.config_entry.options.get( |
| 181 | + CONF_SCAN_INTERVAL, |
| 182 | + self.config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL), |
| 183 | + ), |
| 184 | + ): selector.NumberSelector( |
| 185 | + selector.NumberSelectorConfig( |
| 186 | + min=30, |
| 187 | + max=3600, |
| 188 | + unit_of_measurement="seconds", |
| 189 | + mode=selector.NumberSelectorMode.BOX, |
| 190 | + ), |
| 191 | + ), |
69 | 192 | } |
70 | 193 | ), |
71 | 194 | errors=_errors, |
|
0 commit comments