Skip to content

Commit

Permalink
Add try-catch for invalid auth to Tado (home-assistant#106774)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
erwindouna and MartinHjelmare authored Jan 3, 2024
1 parent 7b3ec60 commit 59a01fc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions homeassistant/components/tado/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from typing import Any

import PyTado
from PyTado.interface import Tado
import requests.exceptions
import voluptuous as vol
Expand Down Expand Up @@ -136,6 +137,8 @@ async def async_step_import(self, import_config: dict[str, Any]) -> FlowResult:
)
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)
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/tado/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/tado/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
22 changes: 22 additions & 0 deletions tests/components/tado/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ipaddress import ip_address
from unittest.mock import MagicMock, patch

import PyTado
import pytest
import requests

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 59a01fc

Please sign in to comment.