1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

Handle location API being exhausted (#30798)

This commit is contained in:
Paulus Schoutsen 2020-01-16 08:31:12 -08:00 committed by Pascal Vizeli
parent d9d5e06baf
commit e1205409f3
2 changed files with 17 additions and 0 deletions

View File

@ -174,6 +174,10 @@ async def _get_ipapi(session: aiohttp.ClientSession) -> Optional[Dict[str, Any]]
except (aiohttp.ClientError, ValueError):
return None
# ipapi allows 30k free requests/month. Some users exhaust those.
if raw_info.get("latitude") == "Sign up to access":
return None
return {
"ip": raw_info.get("ip"),
"country_code": raw_info.get("country"),

View File

@ -92,6 +92,19 @@ async def test_detect_location_info_ipapi(aioclient_mock, session):
assert info.use_metric
async def test_detect_location_info_ipapi_exhaust(aioclient_mock, session):
"""Test detect location info using ipapi.co."""
aioclient_mock.get(location_util.IPAPI, json={"latitude": "Sign up to access"})
aioclient_mock.get(location_util.IP_API, text=load_fixture("ip-api.com.json"))
info = await location_util.async_detect_location_info(session, _test_real=True)
assert info is not None
# ip_api result because ipapi got skipped
assert info.country_code == "US"
assert len(aioclient_mock.mock_calls) == 2
async def test_detect_location_info_ip_api(aioclient_mock, session):
"""Test detect location info using ip-api.com."""
aioclient_mock.get(location_util.IP_API, text=load_fixture("ip-api.com.json"))