Add try-catch for invalid auth to Tado (#106774)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/106970/head
Erwin Douna 2024-01-03 09:15:39 +01:00 committed by Franck Nijhof
parent 5eb1073b4a
commit 527d9fbb6b
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
4 changed files with 32 additions and 1 deletions

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import logging
from typing import Any
import PyTado
from PyTado.interface import Tado
import requests.exceptions
import voluptuous as vol
@ -136,6 +137,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
)
except exceptions.HomeAssistantError:
return self.async_abort(reason="import_failed")
except PyTado.exceptions.TadoWrongCredentialsException:
return self.async_abort(reason="import_failed_invalid_auth")
home_id = validate_result[UNIQUE_ID]
await self.async_set_unique_id(home_id)

View File

@ -55,6 +55,8 @@ async def async_get_scanner(
translation_key = "import_aborted"
if import_result.get("reason") == "import_failed":
translation_key = "import_failed"
if import_result.get("reason") == "import_failed_invalid_auth":
translation_key = "import_failed_invalid_auth"
async_create_issue(
hass,

View File

@ -133,9 +133,13 @@
"title": "Import aborted",
"description": "Configuring the Tado Device Tracker using YAML is being removed.\n The import was aborted, due to an existing config entry being the same as the data being imported in the YAML. Remove the YAML device tracker configuration and restart Home Assistant. Please use the UI to configure Tado."
},
"failed_to_import": {
"import_failed": {
"title": "Failed to import",
"description": "Failed to import the configuration for the Tado Device Tracker. Please use the UI to configure Tado. Don't forget to delete the YAML configuration."
},
"import_failed_invalid_auth": {
"title": "Failed to import, invalid credentials",
"description": "Failed to import the configuration for the Tado Device Tracker, due to invalid credentials. Please fix the YAML configuration and restart Home Assistant. Alternatively you can use the UI to configure Tado. Don't forget to delete the YAML configuration, once the import is successful."
}
}
}

View File

@ -3,6 +3,7 @@ from http import HTTPStatus
from ipaddress import ip_address
from unittest.mock import MagicMock, patch
import PyTado
import pytest
import requests
@ -346,6 +347,27 @@ async def test_import_step_validation_failed(hass: HomeAssistant) -> None:
assert result["reason"] == "import_failed"
async def test_import_step_device_authentication_failed(hass: HomeAssistant) -> None:
"""Test import step with device tracker authentication failed."""
with patch(
"homeassistant.components.tado.config_flow.Tado",
side_effect=PyTado.exceptions.TadoWrongCredentialsException,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
"username": "test-username",
"password": "test-password",
"home_id": 1,
},
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "import_failed_invalid_auth"
async def test_import_step_unique_id_configured(hass: HomeAssistant) -> None:
"""Test import step with unique ID already configured."""
entry = MockConfigEntry(