Add currency to location data (#53575)

This commit is contained in:
Joakim Sørensen 2021-07-28 06:05:16 +02:00 committed by GitHub
parent 127c9fc877
commit 85c1614204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 3 deletions

View File

@ -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)

View File

@ -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"),

View File

@ -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,

View File

@ -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",

View File

@ -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",

View File

@ -3,6 +3,7 @@
"city": "Gotham",
"continent": "Earth",
"country": "XX",
"currency": "XXX",
"latitude": "12.34567",
"longitude": "12.34567",
"postal_code": "12345",

View File

@ -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)