Add currency to location data (#53575)
This commit is contained in:
parent
127c9fc877
commit
85c1614204
7 changed files with 32 additions and 3 deletions
|
@ -89,4 +89,7 @@ async def websocket_detect_config(hass, connection, msg):
|
||||||
if location_info.time_zone:
|
if location_info.time_zone:
|
||||||
info["time_zone"] = 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)
|
connection.send_result(msg["id"], info)
|
||||||
|
|
|
@ -12,7 +12,10 @@ from typing import Any
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
|
from homeassistant.const import __version__ as HA_VERSION
|
||||||
|
|
||||||
WHOAMI_URL = "https://whoami.home-assistant.io/v1"
|
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
|
# Constants from https://github.com/maurycyp/vincenty
|
||||||
# Earth ellipsoid according to WGS 84
|
# Earth ellipsoid according to WGS 84
|
||||||
|
@ -32,6 +35,7 @@ LocationInfo = collections.namedtuple(
|
||||||
[
|
[
|
||||||
"ip",
|
"ip",
|
||||||
"country_code",
|
"country_code",
|
||||||
|
"currency",
|
||||||
"region_code",
|
"region_code",
|
||||||
"region_name",
|
"region_name",
|
||||||
"city",
|
"city",
|
||||||
|
@ -161,7 +165,9 @@ def vincenty(
|
||||||
async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None:
|
async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None:
|
||||||
"""Query whoami.home-assistant.io for location data."""
|
"""Query whoami.home-assistant.io for location data."""
|
||||||
try:
|
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):
|
except (aiohttp.ClientError, asyncio.TimeoutError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -173,6 +179,7 @@ async def _get_whoami(session: aiohttp.ClientSession) -> dict[str, Any] | None:
|
||||||
return {
|
return {
|
||||||
"ip": raw_info.get("ip"),
|
"ip": raw_info.get("ip"),
|
||||||
"country_code": raw_info.get("country"),
|
"country_code": raw_info.get("country"),
|
||||||
|
"currency": raw_info.get("currency"),
|
||||||
"region_code": raw_info.get("region_code"),
|
"region_code": raw_info.get("region_code"),
|
||||||
"region_name": raw_info.get("region"),
|
"region_name": raw_info.get("region"),
|
||||||
"city": raw_info.get("city"),
|
"city": raw_info.get("city"),
|
||||||
|
|
|
@ -144,6 +144,7 @@ async def test_detect_config_fail(hass, client):
|
||||||
return_value=location.LocationInfo(
|
return_value=location.LocationInfo(
|
||||||
ip=None,
|
ip=None,
|
||||||
country_code=None,
|
country_code=None,
|
||||||
|
currency=None,
|
||||||
region_code=None,
|
region_code=None,
|
||||||
region_name=None,
|
region_name=None,
|
||||||
city=None,
|
city=None,
|
||||||
|
|
|
@ -64,6 +64,7 @@ MOCK_MANUAL = {"Config Mode": "Manual Entry", CONF_IP_ADDRESS: MOCK_HOST}
|
||||||
MOCK_LOCATION = location.LocationInfo(
|
MOCK_LOCATION = location.LocationInfo(
|
||||||
"0.0.0.0",
|
"0.0.0.0",
|
||||||
"US",
|
"US",
|
||||||
|
"USD",
|
||||||
"CA",
|
"CA",
|
||||||
"California",
|
"California",
|
||||||
"San Diego",
|
"San Diego",
|
||||||
|
|
|
@ -56,6 +56,7 @@ MOCK_CONFIG = MockConfigEntry(domain=DOMAIN, data=MOCK_DATA, entry_id=MOCK_ENTRY
|
||||||
MOCK_LOCATION = location.LocationInfo(
|
MOCK_LOCATION = location.LocationInfo(
|
||||||
"0.0.0.0",
|
"0.0.0.0",
|
||||||
"US",
|
"US",
|
||||||
|
"USD",
|
||||||
"CA",
|
"CA",
|
||||||
"California",
|
"California",
|
||||||
"San Diego",
|
"San Diego",
|
||||||
|
|
1
tests/fixtures/whoami.json
vendored
1
tests/fixtures/whoami.json
vendored
|
@ -3,6 +3,7 @@
|
||||||
"city": "Gotham",
|
"city": "Gotham",
|
||||||
"continent": "Earth",
|
"continent": "Earth",
|
||||||
"country": "XX",
|
"country": "XX",
|
||||||
|
"currency": "XXX",
|
||||||
"latitude": "12.34567",
|
"latitude": "12.34567",
|
||||||
"longitude": "12.34567",
|
"longitude": "12.34567",
|
||||||
"postal_code": "12345",
|
"postal_code": "12345",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
"""Test Home Assistant location util methods."""
|
"""Test Home Assistant location util methods."""
|
||||||
from unittest.mock import Mock
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import pytest
|
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."""
|
"""Test detect location info using whoami.home-assistant.io."""
|
||||||
aioclient_mock.get(location_util.WHOAMI_URL, text=load_fixture("whoami.json"))
|
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 is not None
|
||||||
assert info.ip == "1.2.3.4"
|
assert info.ip == "1.2.3.4"
|
||||||
assert info.country_code == "XX"
|
assert info.country_code == "XX"
|
||||||
|
assert info.currency == "XXX"
|
||||||
assert info.region_code == "00"
|
assert info.region_code == "00"
|
||||||
assert info.city == "Gotham"
|
assert info.city == "Gotham"
|
||||||
assert info.zip_code == "12345"
|
assert info.zip_code == "12345"
|
||||||
|
@ -90,6 +94,17 @@ async def test_detect_location_info_whoami(aioclient_mock, session):
|
||||||
assert info.use_metric
|
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):
|
async def test_whoami_query_raises(raising_session):
|
||||||
"""Test whoami query when the request to API fails."""
|
"""Test whoami query when the request to API fails."""
|
||||||
info = await location_util._get_whoami(raising_session)
|
info = await location_util._get_whoami(raising_session)
|
||||||
|
|
Loading…
Add table
Reference in a new issue