diff --git a/custom_components/rademacher/__init__.py b/custom_components/rademacher/__init__.py index 92d2c08..a292bd9 100644 --- a/custom_components/rademacher/__init__.py +++ b/custom_components/rademacher/__init__.py @@ -50,7 +50,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.data[CONF_HOST], entry.data[CONF_PASSWORD] if CONF_PASSWORD in entry.data else "", ) - manager = await HomePilotManager.async_build_manager(api) + try: + manager = await HomePilotManager.async_build_manager(api) + except AuthError as err: + # Raising ConfigEntryAuthFailed will cancel future updates + # and start a config flow with SOURCE_REAUTH (async_step_reauth) + raise ConfigEntryAuthFailed from err + _LOGGER.info("Manager instance created, found %s devices", len(manager.devices)) _LOGGER.debug("Device IDs: %s", list(manager.devices)) diff --git a/custom_components/rademacher/config_flow.py b/custom_components/rademacher/config_flow.py index 827f35a..7f58506 100644 --- a/custom_components/rademacher/config_flow.py +++ b/custom_components/rademacher/config_flow.py @@ -3,6 +3,7 @@ import socket import voluptuous as vol +from homeassistant.config_entries import ConfigEntry from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant import config_entries, exceptions, data_entry_flow @@ -33,6 +34,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL host: str = "" password: str = "" + reauth_entry: ConfigEntry | None = None exclude_devices: list[str] = [] ternary_contact_sensors: list[str] = [] @@ -75,6 +77,44 @@ async def async_step_config(self, user_input=None): errors=errors, ) + async def async_step_reauth(self, user_input=None): + self.reauth_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + self.host=self.reauth_entry.data[CONF_HOST] + self.password=self.reauth_entry.data[CONF_PASSWORD] + errors={} + + try: + conn_test = await HomePilotApi.test_connection(user_input[CONF_HOST]) + if conn_test == "ok": + data = { + CONF_HOST: self.host, + CONF_PASSWORD: "", + } + self.hass.config_entries.async_update_entry(self.reauth_entry, data=data) + await self.hass.config_entries.async_reload(self.reauth_entry.entry_id) + return self.async_abort(reason="reauth_successful") + + await HomePilotApi.test_auth(self.host, self.password) + return self.async_abort(reason="reauth_successful") + except CannotConnect: + _LOGGER.warning("Connect error (IP %s)", self.host) + errors["base"] = "cannot_connect" + except InvalidHost: + _LOGGER.warning("Invalid Host (IP %s)", self.host) + errors["base"] = "cannot_connect" + except AuthError: + _LOGGER.warning("Wrong Password (IP %s)", self.host) + errors["base"] = "auth_error" + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Unexpected exception", exc_info=True) + errors["base"] = "unknown" + + return self.async_show_form( + step_id="user_password", data_schema=DATA_SCHEMA_PASSWORD, errors=errors + ) + async def async_step_user_password(self, user_input=None): errors = {} if user_input is not None and CONF_PASSWORD in user_input: @@ -85,6 +125,14 @@ async def async_step_user_password(self, user_input=None): "Password correct (IP %s), creating entries", self.host, ) + if self.reauth_entry: + data = { + CONF_HOST: self.host, + CONF_PASSWORD: self.password, + } + self.hass.config_entries.async_update_entry(self.reauth_entry, data=data) + await self.hass.config_entries.async_reload(self.reauth_entry.entry_id) + return self.async_abort(reason="reauth_successful") return await self.async_step_config(user_input=user_input) except CannotConnect: _LOGGER.warning("Connect error (IP %s)", self.host) diff --git a/custom_components/rademacher/translations/de.json b/custom_components/rademacher/translations/de.json index 1ce2ecd..de37a2b 100644 --- a/custom_components/rademacher/translations/de.json +++ b/custom_components/rademacher/translations/de.json @@ -45,7 +45,8 @@ }, "abort": { "no_devices_found": "Es wurden keine Ger\u00e4te gefunden.", - "cannot_connect": "Fehler bei Verbindung zur Bridge. Bitte stelle sicher, dass die Bridge im Netzwerk verbunden und Hostname/IP korrekt ist." + "cannot_connect": "Fehler bei Verbindung zur Bridge. Bitte stelle sicher, dass die Bridge im Netzwerk verbunden und Hostname/IP korrekt ist.", + "reauth_successful": "Die erneute Authentifizierung war erfolgreich." } } } diff --git a/custom_components/rademacher/translations/en.json b/custom_components/rademacher/translations/en.json index d8df07a..807ee1b 100644 --- a/custom_components/rademacher/translations/en.json +++ b/custom_components/rademacher/translations/en.json @@ -45,7 +45,8 @@ }, "abort": { "no_devices_found": "No devices were found.", - "cannot_connect": "Error connecting to the bridge. Please verify the bridge is connected to the network, and verify that the Hostname/IP is correct." + "cannot_connect": "Error connecting to the bridge. Please verify the bridge is connected to the network, and verify that the Hostname/IP is correct.", + "reauth_successful": "Reauthentication was successful." } } } diff --git a/custom_components/rademacher/translations/es.json b/custom_components/rademacher/translations/es.json index 5699c52..4d27bff 100644 --- a/custom_components/rademacher/translations/es.json +++ b/custom_components/rademacher/translations/es.json @@ -1,51 +1,53 @@ { - "title": "Rademacher HomePilot", - "options": { - "step": { - "init": { - "title": "Configuración", - "description": "", - "data": { - "exclude": "EXCLUIR dispositivos (marca los dispositivos que NO quieres agregar):", - "sensor_type": "Selecciona los sensores de Contacto con posición inclinada:" + "title": "Rademacher HomePilot", + "options": { + "step": { + "init": { + "title": "Configuración", + "description": "", + "data": { + "exclude": "EXCLUIR dispositivos (marca los dispositivos que NO quieres agregar):", + "sensor_type": "Selecciona los sensores de Contacto con posición inclinada:" + } } } - } - }, - "config": { - "step": { - "user": { - "title": "Introduce el Hostname/IP del dispositivo Bridge", - "description": "Por favor, escribe el Hostname o dirección IP de tu dispositivo Rademacher Bridge (Start2Smart or HomePilot).", - "data": { - "host": "Hostname/IP" + }, + "config": { + "step": { + "user": { + "title": "Introduce el Hostname/IP del dispositivo Bridge", + "description": "Por favor, escribe el Hostname o dirección IP de tu dispositivo Rademacher Bridge (Start2Smart or HomePilot).", + "data": { + "host": "Hostname/IP" + } + }, + "user_password": { + "title": "Introducir contraseña", + "description": "Por favor, escribe la contraseña de tu Rademacher Bridge (Start2Smart or HomePilot).", + "data": { + "password": "Contraseña" + } + }, + "config": { + "title": "Configuración", + "description": "", + "data": { + "exclude": "EXCLUIR dispositivos (marca los dispositivos que NO quieres agregar):", + "sensor_type": "Selecciona los sensores de Contacto con posición inclinada:" + } } }, - "user_password": { - "title": "Introducir contraseña", - "description": "Por favor, escribe la contraseña de tu Rademacher Bridge (Start2Smart or HomePilot).", - "data": { - "password": "Contraseña" - } + "error": { + "no_device_selected": "Tienes que seleccionar al menos un dispositivo.", + "cannot_connect": "Error al conectarse al bridge. Por favor, comprueba que el bridge está conectado a la red, y que el Hostname/IP son correctos.", + "auth_error": "Contraseña inválida. Asegúrate de introducir la contraseña correcta.", + "unknown": "Error desconocido." }, - "config": { - "title": "Configuración", - "description": "", - "data": { - "exclude": "EXCLUIR dispositivos (marca los dispositivos que NO quieres agregar):", - "sensor_type": "Selecciona los sensores de Contacto con posición inclinada:" - } + "abort": { + "no_devices_found": "No se han encontrado dispositivos.", + "cannot_connect": "Error al conectarse al bridge. Por favor, comprueba que el bridge está conectado a la red, y que el Hostname/IP son correctos.", + "reauth_successful": "La reautenticación fue exitosa." } - }, - "error": { - "no_device_selected": "Tienes que seleccionar al menos un dispositivo.", - "cannot_connect": "Error al conectarse al bridge. Por favor, comprueba que el bridge está conectado a la red, y que el Hostname/IP son correctos.", - "auth_error": "Contraseña inválida. Asegúrate de introducir la contraseña correcta.", - "unknown": "Error desconocido." - }, - "abort": { - "no_devices_found": "No se han encontrado dispositivos.", - "cannot_connect": "Error al conectarse al bridge. Por favor, comprueba que el bridge está conectado a la red, y que el Hostname/IP son correctos." } } -} + \ No newline at end of file diff --git a/custom_components/rademacher/translations/pt-BR.json b/custom_components/rademacher/translations/pt-BR.json index 6ce123e..2ccb450 100644 --- a/custom_components/rademacher/translations/pt-BR.json +++ b/custom_components/rademacher/translations/pt-BR.json @@ -45,7 +45,8 @@ }, "abort": { "no_devices_found": "Nenhum dispositivo encontrado.", - "cannot_connect": "Erro ao contactar a bridge. Por favor verifique que a bridge est\u00e1 ligada \u00e0 rede, e que o Nome de Servidor / IP provideciado est\u00e1 correto." + "cannot_connect": "Erro ao contactar a bridge. Por favor verifique que a bridge est\u00e1 ligada \u00e0 rede, e que o Nome de Servidor / IP provideciado est\u00e1 correto.", + "reauth_successful": "Re-autentica\u00e7\u00e3o foi bem sucedida." } } } diff --git a/custom_components/rademacher/translations/pt.json b/custom_components/rademacher/translations/pt.json index 522b6f1..8be4ce2 100644 --- a/custom_components/rademacher/translations/pt.json +++ b/custom_components/rademacher/translations/pt.json @@ -45,7 +45,8 @@ }, "abort": { "no_devices_found": "Nenhum dispositivo encontrado.", - "cannot_connect": "Erro ao contactar a bridge. Por favor verifique que a bridge est\u00e1 ligada \u00e0 rede, e que o Nome de Servidor / IP provideciado est\u00e1 correto." + "cannot_connect": "Erro ao contactar a bridge. Por favor verifique que a bridge est\u00e1 ligada \u00e0 rede, e que o Nome de Servidor / IP provideciado est\u00e1 correto.", + "reauth_successful": "Re-autentica\u00e7\u00e3o foi bem sucedida." } } } diff --git a/custom_components/rademacher/translations/sk.json b/custom_components/rademacher/translations/sk.json index 29c94f9..b22eb2c 100644 --- a/custom_components/rademacher/translations/sk.json +++ b/custom_components/rademacher/translations/sk.json @@ -45,7 +45,8 @@ }, "abort": { "no_devices_found": "Nenašli sa žiadne zariadenia.", - "cannot_connect": "Chyba pri pripájaní k bridge. Skontrolujte, či je bridge pripojený k sieti a či je názov hostiteľa/IP správna." + "cannot_connect": "Chyba pri pripájaní k bridge. Skontrolujte, či je bridge pripojený k sieti a či je názov hostiteľa/IP správna.", + "reauth_successful": "Opätovná autentifikácia bola úspešná." } } }