Skip to content

Commit 8ce4e31

Browse files
committed
Allow updating credentials via options flow
Extended the options flow to include client_id and client_secret fields, allowing users to update their Monta API credentials without reinstalling the integration. Changes: - Add client_id and client_secret fields to options flow - Validate credentials when changed (only if they differ from current) - Update config entry data when credentials change - Integration reloads automatically via existing update listener - Updated translations (EN and PT) with descriptions for credential fields Users can now update all integration settings (credentials and scan interval) from the integration's options menu in Home Assistant.
1 parent e54420f commit 8ce4e31

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

custom_components/monta/config_flow.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,70 @@ async def async_step_init(
111111
self, user_input: dict | None = None
112112
) -> config_entries.FlowResult:
113113
"""Manage the options."""
114+
_errors = {}
114115
if user_input is not None:
115-
return self.async_create_entry(title="", data=user_input)
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)
116151

117152
return self.async_show_form(
118153
step_id="init",
119154
data_schema=vol.Schema(
120155
{
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+
),
121178
vol.Optional(
122179
CONF_SCAN_INTERVAL,
123180
default=self.config_entry.options.get(
@@ -134,4 +191,15 @@ async def async_step_init(
134191
),
135192
}
136193
),
194+
errors=_errors,
195+
)
196+
197+
async def _test_credentials(self, client_id: str, client_secret: str) -> any:
198+
"""Validate credentials."""
199+
client = MontaApiClient(
200+
client_id=client_id,
201+
client_secret=client_secret,
202+
session=async_create_clientsession(self.hass),
203+
store=Store(self.hass, STORAGE_VERSION, STORAGE_KEY),
137204
)
205+
return await client.async_request_token()

custom_components/monta/translations/en.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
"options": {
2323
"step": {
2424
"init": {
25-
"description": "Configure Monta integration options",
25+
"description": "Configure Monta integration options. You can update your credentials or adjust the scan interval here.",
2626
"data": {
27+
"client_id": "Client id",
28+
"client_secret": "Client secret",
2729
"scan_interval": "Scan interval (seconds)"
2830
},
2931
"data_description": {
32+
"client_id": "Your Monta API client ID from https://portal2.monta.app/applications",
33+
"client_secret": "Your Monta API client secret from https://portal2.monta.app/applications",
3034
"scan_interval": "How often to fetch data from Monta API. Monta has a rate limit of 10 requests per minute. Recommended: 120 seconds or higher for multiple chargers."
3135
}
3236
}

custom_components/monta/translations/pt.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
"options": {
2323
"step": {
2424
"init": {
25-
"description": "Configurar opções de integração Monta",
25+
"description": "Configurar opções de integração Monta. Você pode atualizar suas credenciais ou ajustar o intervalo de varredura aqui.",
2626
"data": {
27+
"client_id": "ID do cliente",
28+
"client_secret": "Segredo do cliente",
2729
"scan_interval": "Intervalo de varredura (segundos)"
2830
},
2931
"data_description": {
32+
"client_id": "Seu ID de cliente da API Monta de https://portal2.monta.app/applications",
33+
"client_secret": "Seu segredo de cliente da API Monta de https://portal2.monta.app/applications",
3034
"scan_interval": "Com que frequência buscar dados da API Monta. A Monta tem um limite de 10 solicitações por minuto. Recomendado: 120 segundos ou mais para vários carregadores."
3135
}
3236
}

0 commit comments

Comments
 (0)