Skip to content

Commit e175e3e

Browse files
authored
Enable country site autodetection in Alexa Devices (#150989)
1 parent 7cd767e commit e175e3e

File tree

12 files changed

+92
-35
lines changed

12 files changed

+92
-35
lines changed

homeassistant/components/alexa_devices/__init__.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Alexa Devices integration."""
22

3-
from homeassistant.const import Platform
3+
from homeassistant.const import CONF_COUNTRY, Platform
44
from homeassistant.core import HomeAssistant
55
from homeassistant.helpers import aiohttp_client, config_validation as cv
66
from homeassistant.helpers.typing import ConfigType
77

8-
from .const import DOMAIN
8+
from .const import _LOGGER, COUNTRY_DOMAINS, DOMAIN
99
from .coordinator import AmazonConfigEntry, AmazonDevicesCoordinator
1010
from .services import async_setup_services
1111

@@ -40,6 +40,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: AmazonConfigEntry) -> bo
4040
return True
4141

4242

43+
async def async_migrate_entry(hass: HomeAssistant, entry: AmazonConfigEntry) -> bool:
44+
"""Migrate old entry."""
45+
if entry.version == 1 and entry.minor_version == 0:
46+
_LOGGER.debug(
47+
"Migrating from version %s.%s", entry.version, entry.minor_version
48+
)
49+
50+
# Convert country in domain
51+
country = entry.data[CONF_COUNTRY]
52+
domain = COUNTRY_DOMAINS.get(country, country)
53+
54+
# Save domain and remove country
55+
new_data = entry.data.copy()
56+
new_data.update({"site": f"https://www.amazon.{domain}"})
57+
58+
hass.config_entries.async_update_entry(
59+
entry, data=new_data, version=1, minor_version=1
60+
)
61+
62+
_LOGGER.info(
63+
"Migration to version %s.%s successful", entry.version, entry.minor_version
64+
)
65+
66+
return True
67+
68+
4369
async def async_unload_entry(hass: HomeAssistant, entry: AmazonConfigEntry) -> bool:
4470
"""Unload a config entry."""
4571
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

homeassistant/components/alexa_devices/config_flow.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
CannotAuthenticate,
1111
CannotConnect,
1212
CannotRetrieveData,
13-
WrongCountry,
1413
)
1514
import voluptuous as vol
1615

1716
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
18-
from homeassistant.const import CONF_CODE, CONF_COUNTRY, CONF_PASSWORD, CONF_USERNAME
17+
from homeassistant.const import CONF_CODE, CONF_PASSWORD, CONF_USERNAME
1918
from homeassistant.core import HomeAssistant
2019
from homeassistant.helpers import aiohttp_client
2120
import homeassistant.helpers.config_validation as cv
22-
from homeassistant.helpers.selector import CountrySelector
2321

2422
from .const import CONF_LOGIN_DATA, DOMAIN
2523

@@ -37,7 +35,6 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
3735
session = aiohttp_client.async_create_clientsession(hass)
3836
api = AmazonEchoApi(
3937
session,
40-
data[CONF_COUNTRY],
4138
data[CONF_USERNAME],
4239
data[CONF_PASSWORD],
4340
)
@@ -48,6 +45,9 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
4845
class AmazonDevicesConfigFlow(ConfigFlow, domain=DOMAIN):
4946
"""Handle a config flow for Alexa Devices."""
5047

48+
VERSION = 1
49+
MINOR_VERSION = 1
50+
5151
async def async_step_user(
5252
self, user_input: dict[str, Any] | None = None
5353
) -> ConfigFlowResult:
@@ -62,8 +62,6 @@ async def async_step_user(
6262
errors["base"] = "invalid_auth"
6363
except CannotRetrieveData:
6464
errors["base"] = "cannot_retrieve_data"
65-
except WrongCountry:
66-
errors["base"] = "wrong_country"
6765
else:
6866
await self.async_set_unique_id(data["customer_info"]["user_id"])
6967
self._abort_if_unique_id_configured()
@@ -78,9 +76,6 @@ async def async_step_user(
7876
errors=errors,
7977
data_schema=vol.Schema(
8078
{
81-
vol.Required(
82-
CONF_COUNTRY, default=self.hass.config.country
83-
): CountrySelector(),
8479
vol.Required(CONF_USERNAME): cv.string,
8580
vol.Required(CONF_PASSWORD): cv.string,
8681
vol.Required(CONF_CODE): cv.string,

homeassistant/components/alexa_devices/const.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@
66

77
DOMAIN = "alexa_devices"
88
CONF_LOGIN_DATA = "login_data"
9+
10+
DEFAULT_DOMAIN = {"domain": "com"}
11+
COUNTRY_DOMAINS = {
12+
"ar": DEFAULT_DOMAIN,
13+
"at": DEFAULT_DOMAIN,
14+
"au": {"domain": "com.au"},
15+
"be": {"domain": "com.be"},
16+
"br": DEFAULT_DOMAIN,
17+
"gb": {"domain": "co.uk"},
18+
"il": DEFAULT_DOMAIN,
19+
"jp": {"domain": "co.jp"},
20+
"mx": {"domain": "com.mx"},
21+
"no": DEFAULT_DOMAIN,
22+
"nz": {"domain": "com.au"},
23+
"pl": DEFAULT_DOMAIN,
24+
"tr": {"domain": "com.tr"},
25+
"us": DEFAULT_DOMAIN,
26+
"za": {"domain": "co.za"},
27+
}

homeassistant/components/alexa_devices/coordinator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from aiohttp import ClientSession
1212

1313
from homeassistant.config_entries import ConfigEntry
14-
from homeassistant.const import CONF_COUNTRY, CONF_PASSWORD, CONF_USERNAME
14+
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
1515
from homeassistant.core import HomeAssistant
1616
from homeassistant.exceptions import ConfigEntryAuthFailed
1717
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
@@ -44,7 +44,6 @@ def __init__(
4444
)
4545
self.api = AmazonEchoApi(
4646
session,
47-
entry.data[CONF_COUNTRY],
4847
entry.data[CONF_USERNAME],
4948
entry.data[CONF_PASSWORD],
5049
entry.data[CONF_LOGIN_DATA],

homeassistant/components/alexa_devices/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "cloud_polling",
99
"loggers": ["aioamazondevices"],
1010
"quality_scale": "silver",
11-
"requirements": ["aioamazondevices==4.0.0"]
11+
"requirements": ["aioamazondevices==5.0.0"]
1212
}

homeassistant/components/alexa_devices/strings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"common": {
33
"data_code": "One-time password (OTP code)",
4-
"data_description_country": "The country where your Amazon account is registered.",
54
"data_description_username": "The email address of your Amazon account.",
65
"data_description_password": "The password of your Amazon account.",
76
"data_description_code": "The one-time password to log in to your account. Currently, only tokens from OTP applications are supported.",
@@ -12,13 +11,11 @@
1211
"step": {
1312
"user": {
1413
"data": {
15-
"country": "[%key:common::config_flow::data::country%]",
1614
"username": "[%key:common::config_flow::data::username%]",
1715
"password": "[%key:common::config_flow::data::password%]",
1816
"code": "[%key:component::alexa_devices::common::data_code%]"
1917
},
2018
"data_description": {
21-
"country": "[%key:component::alexa_devices::common::data_description_country%]",
2219
"username": "[%key:component::alexa_devices::common::data_description_username%]",
2320
"password": "[%key:component::alexa_devices::common::data_description_password%]",
2421
"code": "[%key:component::alexa_devices::common::data_description_code%]"
@@ -46,7 +43,6 @@
4643
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
4744
"cannot_retrieve_data": "Unable to retrieve data from Amazon. Please try again later.",
4845
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
49-
"wrong_country": "Wrong country selected. Please select the country where your Amazon account is registered.",
5046
"unknown": "[%key:common::config_flow::error::unknown%]"
5147
}
5248
},

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_test_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/alexa_devices/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import pytest
99

1010
from homeassistant.components.alexa_devices.const import CONF_LOGIN_DATA, DOMAIN
11-
from homeassistant.const import CONF_COUNTRY, CONF_PASSWORD, CONF_USERNAME
11+
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
1212

13-
from .const import TEST_COUNTRY, TEST_PASSWORD, TEST_SERIAL_NUMBER, TEST_USERNAME
13+
from .const import TEST_PASSWORD, TEST_SERIAL_NUMBER, TEST_USERNAME
1414

1515
from tests.common import MockConfigEntry
1616

@@ -80,7 +80,6 @@ def mock_config_entry() -> MockConfigEntry:
8080
domain=DOMAIN,
8181
title="Amazon Test Account",
8282
data={
83-
CONF_COUNTRY: TEST_COUNTRY,
8483
CONF_USERNAME: TEST_USERNAME,
8584
CONF_PASSWORD: TEST_PASSWORD,
8685
CONF_LOGIN_DATA: {"session": "test-session"},

tests/components/alexa_devices/snapshots/test_diagnostics.ambr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
}),
4848
'entry': dict({
4949
'data': dict({
50-
'country': 'IT',
5150
'login_data': dict({
5251
'session': 'test-session',
5352
}),

0 commit comments

Comments
 (0)