Skip to content

Commit 51f68f2

Browse files
authored
Force httpx client to use IPv4 for waze_travel_time (home-assistant#156526)
1 parent 773cb74 commit 51f68f2

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

homeassistant/components/waze_travel_time/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
ServiceResponse,
1515
SupportsResponse,
1616
)
17-
from homeassistant.helpers.httpx_client import get_async_client
1817
from homeassistant.helpers.location import find_coordinates
1918
from homeassistant.helpers.selector import (
2019
BooleanSelector,
@@ -47,6 +46,7 @@
4746
VEHICLE_TYPES,
4847
)
4948
from .coordinator import WazeTravelTimeCoordinator, async_get_travel_times
49+
from .httpx_client import create_httpx_client
5050

5151
PLATFORMS = [Platform.SENSOR]
5252

@@ -106,7 +106,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
106106
if SEMAPHORE not in hass.data.setdefault(DOMAIN, {}):
107107
hass.data.setdefault(DOMAIN, {})[SEMAPHORE] = asyncio.Semaphore(1)
108108

109-
httpx_client = get_async_client(hass)
109+
httpx_client = await create_httpx_client(hass)
110+
110111
client = WazeRouteCalculator(
111112
region=config_entry.data[CONF_REGION].upper(), client=httpx_client
112113
)
@@ -119,7 +120,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
119120
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
120121

121122
async def async_get_travel_times_service(service: ServiceCall) -> ServiceResponse:
122-
httpx_client = get_async_client(hass)
123+
httpx_client = await create_httpx_client(hass)
123124
client = WazeRouteCalculator(
124125
region=service.data[CONF_REGION].upper(), client=httpx_client
125126
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Special httpx client for Waze Travel Time integration."""
2+
3+
import httpx
4+
5+
from homeassistant.core import HomeAssistant
6+
from homeassistant.helpers.httpx_client import create_async_httpx_client
7+
from homeassistant.util.hass_dict import HassKey
8+
9+
from .const import DOMAIN
10+
11+
DATA_HTTPX_ASYNC_CLIENT: HassKey[httpx.AsyncClient] = HassKey("httpx_async_client")
12+
13+
14+
def create_transport() -> httpx.AsyncHTTPTransport:
15+
"""Create a httpx transport which enforces the use of IPv4."""
16+
return httpx.AsyncHTTPTransport(local_address="0.0.0.0")
17+
18+
19+
async def create_httpx_client(hass: HomeAssistant) -> httpx.AsyncClient:
20+
"""Create a httpx client which enforces the use of IPv4."""
21+
if (client := hass.data[DOMAIN].get(DATA_HTTPX_ASYNC_CLIENT)) is None:
22+
transport = await hass.async_add_executor_job(create_transport)
23+
client = hass.data[DOMAIN][DATA_HTTPX_ASYNC_CLIENT] = create_async_httpx_client(
24+
hass, transport=transport
25+
)
26+
return client

0 commit comments

Comments
 (0)