diff --git a/homeassistant/components/tado/config_flow.py b/homeassistant/components/tado/config_flow.py index 3e183b0a9b5..f9f4f80bde1 100644 --- a/homeassistant/components/tado/config_flow.py +++ b/homeassistant/components/tado/config_flow.py @@ -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) diff --git a/homeassistant/components/tado/device_tracker.py b/homeassistant/components/tado/device_tracker.py index 426c7d9ed5d..c10ab118060 100644 --- a/homeassistant/components/tado/device_tracker.py +++ b/homeassistant/components/tado/device_tracker.py @@ -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, diff --git a/homeassistant/components/tado/strings.json b/homeassistant/components/tado/strings.json index 157b98e33ea..d50d1490566 100644 --- a/homeassistant/components/tado/strings.json +++ b/homeassistant/components/tado/strings.json @@ -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." } } } diff --git a/tests/components/tado/test_config_flow.py b/tests/components/tado/test_config_flow.py index d83a4b22efc..ac04777dc1c 100644 --- a/tests/components/tado/test_config_flow.py +++ b/tests/components/tado/test_config_flow.py @@ -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(