mirror of
https://github.com/home-assistant/core.git
synced 2025-12-25 05:26:47 +00:00
Force httpx client to use IPv4 for waze_travel_time (#156526)
This commit is contained in:
committed by
GitHub
parent
773cb7424c
commit
51f68f2776
@@ -14,7 +14,6 @@ from homeassistant.core import (
|
||||
ServiceResponse,
|
||||
SupportsResponse,
|
||||
)
|
||||
from homeassistant.helpers.httpx_client import get_async_client
|
||||
from homeassistant.helpers.location import find_coordinates
|
||||
from homeassistant.helpers.selector import (
|
||||
BooleanSelector,
|
||||
@@ -47,6 +46,7 @@ from .const import (
|
||||
VEHICLE_TYPES,
|
||||
)
|
||||
from .coordinator import WazeTravelTimeCoordinator, async_get_travel_times
|
||||
from .httpx_client import create_httpx_client
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
@@ -106,7 +106,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
||||
if SEMAPHORE not in hass.data.setdefault(DOMAIN, {}):
|
||||
hass.data.setdefault(DOMAIN, {})[SEMAPHORE] = asyncio.Semaphore(1)
|
||||
|
||||
httpx_client = get_async_client(hass)
|
||||
httpx_client = await create_httpx_client(hass)
|
||||
|
||||
client = WazeRouteCalculator(
|
||||
region=config_entry.data[CONF_REGION].upper(), client=httpx_client
|
||||
)
|
||||
@@ -119,7 +120,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
|
||||
async def async_get_travel_times_service(service: ServiceCall) -> ServiceResponse:
|
||||
httpx_client = get_async_client(hass)
|
||||
httpx_client = await create_httpx_client(hass)
|
||||
client = WazeRouteCalculator(
|
||||
region=service.data[CONF_REGION].upper(), client=httpx_client
|
||||
)
|
||||
|
||||
26
homeassistant/components/waze_travel_time/httpx_client.py
Normal file
26
homeassistant/components/waze_travel_time/httpx_client.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""Special httpx client for Waze Travel Time integration."""
|
||||
|
||||
import httpx
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.httpx_client import create_async_httpx_client
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
DATA_HTTPX_ASYNC_CLIENT: HassKey[httpx.AsyncClient] = HassKey("httpx_async_client")
|
||||
|
||||
|
||||
def create_transport() -> httpx.AsyncHTTPTransport:
|
||||
"""Create a httpx transport which enforces the use of IPv4."""
|
||||
return httpx.AsyncHTTPTransport(local_address="0.0.0.0")
|
||||
|
||||
|
||||
async def create_httpx_client(hass: HomeAssistant) -> httpx.AsyncClient:
|
||||
"""Create a httpx client which enforces the use of IPv4."""
|
||||
if (client := hass.data[DOMAIN].get(DATA_HTTPX_ASYNC_CLIENT)) is None:
|
||||
transport = await hass.async_add_executor_job(create_transport)
|
||||
client = hass.data[DOMAIN][DATA_HTTPX_ASYNC_CLIENT] = create_async_httpx_client(
|
||||
hass, transport=transport
|
||||
)
|
||||
return client
|
||||
Reference in New Issue
Block a user