diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index cd682057266..2fbb1c447ae 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -21,6 +21,7 @@ from homeassistant.components.google_assistant import helpers as google_helpers from homeassistant.components.http import HomeAssistantView from homeassistant.components.http.data_validator import RequestDataValidator from homeassistant.components.websocket_api import const as ws_const +from homeassistant.util.location import async_detect_location_info from .const import ( DOMAIN, @@ -220,8 +221,23 @@ class CloudRegisterView(HomeAssistantView): hass = request.app["hass"] cloud = hass.data[DOMAIN] + client_metadata = None + + if location_info := await async_detect_location_info( + hass.helpers.aiohttp_client.async_get_clientsession() + ): + client_metadata = { + "NC_COUNTRY_CODE": location_info.country_code, + "NC_REGION_CODE": location_info.region_code, + "NC_ZIP_CODE": location_info.zip_code, + } + async with async_timeout.timeout(REQUEST_TIMEOUT): - await cloud.auth.async_register(data["email"], data["password"]) + await cloud.auth.async_register( + data["email"], + data["password"], + client_metadata=client_metadata, + ) return self.json_message("ok") diff --git a/homeassistant/components/cloud/manifest.json b/homeassistant/components/cloud/manifest.json index 517aa887a30..0bb00cd5ced 100644 --- a/homeassistant/components/cloud/manifest.json +++ b/homeassistant/components/cloud/manifest.json @@ -2,7 +2,7 @@ "domain": "cloud", "name": "Home Assistant Cloud", "documentation": "https://www.home-assistant.io/integrations/cloud", - "requirements": ["hass-nabucasa==0.50.0"], + "requirements": ["hass-nabucasa==0.51.0"], "dependencies": ["http", "webhook"], "after_dependencies": ["google_assistant", "alexa"], "codeowners": ["@home-assistant/cloud"], diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index bae322d3834..bd9f93e2ad1 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -15,7 +15,7 @@ certifi>=2021.5.30 ciso8601==2.2.0 cryptography==35.0.0 emoji==1.5.0 -hass-nabucasa==0.50.0 +hass-nabucasa==0.51.0 home-assistant-frontend==20211229.0 httpx==0.21.0 ifaddr==0.1.7 diff --git a/requirements_all.txt b/requirements_all.txt index fd76da6e483..9a5951e6177 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -806,7 +806,7 @@ habitipy==0.2.0 hangups==0.4.16 # homeassistant.components.cloud -hass-nabucasa==0.50.0 +hass-nabucasa==0.51.0 # homeassistant.components.splunk hass_splunk==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2cc5ae1a484..2abb6e555df 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -516,7 +516,7 @@ habitipy==0.2.0 hangups==0.4.16 # homeassistant.components.cloud -hass-nabucasa==0.50.0 +hass-nabucasa==0.51.0 # homeassistant.components.tasmota hatasmota==0.3.1 diff --git a/tests/components/cloud/test_http_api.py b/tests/components/cloud/test_http_api.py index ab5909c9893..bfc141dcf9b 100644 --- a/tests/components/cloud/test_http_api.py +++ b/tests/components/cloud/test_http_api.py @@ -15,6 +15,7 @@ from homeassistant.components.alexa.entities import LightCapabilities from homeassistant.components.cloud.const import DOMAIN, RequireRelink from homeassistant.components.google_assistant.helpers import GoogleEntity from homeassistant.core import State +from homeassistant.util.location import LocationInfo from . import mock_cloud, mock_cloud_prefs @@ -203,16 +204,60 @@ async def test_logout_view_unknown_error(hass, cloud_client): assert req.status == HTTPStatus.BAD_GATEWAY -async def test_register_view(mock_cognito, cloud_client): - """Test logging out.""" - req = await cloud_client.post( - "/api/cloud/register", json={"email": "hello@bla.com", "password": "falcon42"} - ) +async def test_register_view_no_location(mock_cognito, cloud_client): + """Test register without location.""" + with patch( + "homeassistant.components.cloud.http_api.async_detect_location_info", + return_value=None, + ): + req = await cloud_client.post( + "/api/cloud/register", + json={"email": "hello@bla.com", "password": "falcon42"}, + ) assert req.status == HTTPStatus.OK assert len(mock_cognito.register.mock_calls) == 1 - result_email, result_pass = mock_cognito.register.mock_calls[0][1] + call = mock_cognito.register.mock_calls[0] + result_email, result_pass = call.args assert result_email == "hello@bla.com" assert result_pass == "falcon42" + assert call.kwargs["client_metadata"] is None + + +async def test_register_view_with_location(mock_cognito, cloud_client): + """Test register with location.""" + with patch( + "homeassistant.components.cloud.http_api.async_detect_location_info", + return_value=LocationInfo( + **{ + "country_code": "XX", + "zip_code": "12345", + "region_code": "GH", + "ip": "1.2.3.4", + "city": "Gotham", + "region_name": "Gotham", + "time_zone": "Earth/Gotham", + "currency": "XXX", + "latitude": "12.34567", + "longitude": "12.34567", + "use_metric": True, + } + ), + ): + req = await cloud_client.post( + "/api/cloud/register", + json={"email": "hello@bla.com", "password": "falcon42"}, + ) + assert req.status == HTTPStatus.OK + assert len(mock_cognito.register.mock_calls) == 1 + call = mock_cognito.register.mock_calls[0] + result_email, result_pass = call.args + assert result_email == "hello@bla.com" + assert result_pass == "falcon42" + assert call.kwargs["client_metadata"] == { + "NC_COUNTRY_CODE": "XX", + "NC_REGION_CODE": "GH", + "NC_ZIP_CODE": "12345", + } async def test_register_view_bad_data(mock_cognito, cloud_client):