From 85c1614204ff60639c7149cdc1e8f57bb67ff90c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 28 Jul 2021 06:05:16 +0200 Subject: [PATCH] Add currency to location data (#53575) --- homeassistant/components/config/core.py | 3 +++ homeassistant/util/location.py | 9 ++++++++- tests/components/config/test_core.py | 1 + tests/components/ps4/test_config_flow.py | 1 + tests/components/ps4/test_init.py | 1 + tests/fixtures/whoami.json | 1 + tests/util/test_location.py | 19 +++++++++++++++++-- 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/config/core.py b/homeassistant/components/config/core.py index 89f4edc95d6..3b38ab49c4b 100644 --- a/homeassistant/components/config/core.py +++ b/homeassistant/components/config/core.py @@ -89,4 +89,7 @@ async def websocket_detect_config(hass, connection, msg): if location_info.time_zone: info["time_zone"] = location_info.time_zone + if location_info.currency: + info["currency"] = location_info.currency + connection.send_result(msg["id"], info) diff --git a/homeassistant/util/location.py b/homeassistant/util/location.py index 2a3a4ff0922..abe8fedd21a 100644 --- a/homeassistant/util/location.py +++ b/homeassistant/util/location.py @@ -12,7 +12,10 @@ from typing import Any import aiohttp +from homeassistant.const import __version__ as HA_VERSION + WHOAMI_URL = "https://whoami.home-assistant.io/v1" +WHOAMI_URL_DEV = "https://whoami-v1-dev.home-assistant.workers.dev/v1" # Constants from https://github.com/maurycyp/vincenty # Earth ellipsoid according to WGS 84 @@ -32,6 +35,7 @@ LocationInfo = collections.namedtuple( [ "ip", "country_code", + "currency", "region_code", "region_name", "city", @@ -161,7 +165,9 @@ def vincenty( async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None: """Query whoami.home-assistant.io for location data.""" try: - resp = await session.get(WHOAMI_URL, timeout=30) + resp = await session.get( + WHOAMI_URL_DEV if HA_VERSION.endswith("0.dev0") else WHOAMI_URL, timeout=30 + ) except (aiohttp.ClientError, asyncio.TimeoutError): return None @@ -173,6 +179,7 @@ async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None: return { "ip": raw_info.get("ip"), "country_code": raw_info.get("country"), + "currency": raw_info.get("currency"), "region_code": raw_info.get("region_code"), "region_name": raw_info.get("region"), "city": raw_info.get("city"), diff --git a/tests/components/config/test_core.py b/tests/components/config/test_core.py index 738b1183c14..54fd6a76565 100644 --- a/tests/components/config/test_core.py +++ b/tests/components/config/test_core.py @@ -144,6 +144,7 @@ async def test_detect_config_fail(hass, client): return_value=location.LocationInfo( ip=None, country_code=None, + currency=None, region_code=None, region_name=None, city=None, diff --git a/tests/components/ps4/test_config_flow.py b/tests/components/ps4/test_config_flow.py index 36c38a62b4c..57e65d15be4 100644 --- a/tests/components/ps4/test_config_flow.py +++ b/tests/components/ps4/test_config_flow.py @@ -64,6 +64,7 @@ MOCK_MANUAL = {"Config Mode": "Manual Entry", CONF_IP_ADDRESS: MOCK_HOST} MOCK_LOCATION = location.LocationInfo( "0.0.0.0", "US", + "USD", "CA", "California", "San Diego", diff --git a/tests/components/ps4/test_init.py b/tests/components/ps4/test_init.py index f57fada8c37..8c43bd5df90 100644 --- a/tests/components/ps4/test_init.py +++ b/tests/components/ps4/test_init.py @@ -56,6 +56,7 @@ MOCK_CONFIG = MockConfigEntry(domain=DOMAIN, data=MOCK_DATA, entry_id=MOCK_ENTRY MOCK_LOCATION = location.LocationInfo( "0.0.0.0", "US", + "USD", "CA", "California", "San Diego", diff --git a/tests/fixtures/whoami.json b/tests/fixtures/whoami.json index c805ef30558..f0630101483 100644 --- a/tests/fixtures/whoami.json +++ b/tests/fixtures/whoami.json @@ -3,6 +3,7 @@ "city": "Gotham", "continent": "Earth", "country": "XX", + "currency": "XXX", "latitude": "12.34567", "longitude": "12.34567", "postal_code": "12345", diff --git a/tests/util/test_location.py b/tests/util/test_location.py index 21531a59194..d25e6859727 100644 --- a/tests/util/test_location.py +++ b/tests/util/test_location.py @@ -1,5 +1,5 @@ """Test Home Assistant location util methods.""" -from unittest.mock import Mock +from unittest.mock import Mock, patch import aiohttp import pytest @@ -76,11 +76,15 @@ async def test_detect_location_info_whoami(aioclient_mock, session): """Test detect location info using whoami.home-assistant.io.""" aioclient_mock.get(location_util.WHOAMI_URL, text=load_fixture("whoami.json")) - info = await location_util.async_detect_location_info(session, _test_real=True) + with patch("homeassistant.util.location.HA_VERSION", "1.0"): + info = await location_util.async_detect_location_info(session, _test_real=True) + + assert str(aioclient_mock.mock_calls[-1][1]) == location_util.WHOAMI_URL assert info is not None assert info.ip == "1.2.3.4" assert info.country_code == "XX" + assert info.currency == "XXX" assert info.region_code == "00" assert info.city == "Gotham" assert info.zip_code == "12345" @@ -90,6 +94,17 @@ async def test_detect_location_info_whoami(aioclient_mock, session): assert info.use_metric +async def test_dev_url(aioclient_mock, session): + """Test usage of dev URL.""" + aioclient_mock.get(location_util.WHOAMI_URL_DEV, text=load_fixture("whoami.json")) + with patch("homeassistant.util.location.HA_VERSION", "1.0.dev0"): + info = await location_util.async_detect_location_info(session, _test_real=True) + + assert str(aioclient_mock.mock_calls[-1][1]) == location_util.WHOAMI_URL_DEV + + assert info.currency == "XXX" + + async def test_whoami_query_raises(raising_session): """Test whoami query when the request to API fails.""" info = await location_util._get_whoami(raising_session)