Instantiate new httpx client for lamarzocco (#132016)

This commit is contained in:
Josef Zweck 2024-12-03 15:10:58 +01:00 committed by GitHub
parent 9e723752f8
commit e3885b8117
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 6 deletions

View File

@ -22,7 +22,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.httpx_client import create_async_httpx_client
from .const import CONF_USE_BLUETOOTH, DOMAIN from .const import CONF_USE_BLUETOOTH, DOMAIN
from .coordinator import LaMarzoccoConfigEntry, LaMarzoccoUpdateCoordinator from .coordinator import LaMarzoccoConfigEntry, LaMarzoccoUpdateCoordinator
@ -46,11 +46,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
assert entry.unique_id assert entry.unique_id
serial = entry.unique_id serial = entry.unique_id
client = create_async_httpx_client(hass)
cloud_client = LaMarzoccoCloudClient( cloud_client = LaMarzoccoCloudClient(
username=entry.data[CONF_USERNAME], username=entry.data[CONF_USERNAME],
password=entry.data[CONF_PASSWORD], password=entry.data[CONF_PASSWORD],
client=get_async_client(hass), client=client,
) )
# initialize local API # initialize local API
@ -60,7 +60,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
local_client = LaMarzoccoLocalClient( local_client = LaMarzoccoLocalClient(
host=host, host=host,
local_bearer=entry.data[CONF_TOKEN], local_bearer=entry.data[CONF_TOKEN],
client=get_async_client(hass), client=client,
) )
# initialize Bluetooth # initialize Bluetooth

View File

@ -6,6 +6,7 @@ from collections.abc import Mapping
import logging import logging
from typing import Any from typing import Any
from httpx import AsyncClient
from pylamarzocco.client_cloud import LaMarzoccoCloudClient from pylamarzocco.client_cloud import LaMarzoccoCloudClient
from pylamarzocco.client_local import LaMarzoccoLocalClient from pylamarzocco.client_local import LaMarzoccoLocalClient
from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
@ -36,7 +37,7 @@ from homeassistant.const import (
) )
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.httpx_client import create_async_httpx_client
from homeassistant.helpers.selector import ( from homeassistant.helpers.selector import (
SelectOptionDict, SelectOptionDict,
SelectSelector, SelectSelector,
@ -57,6 +58,8 @@ class LmConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 2 VERSION = 2
_client: AsyncClient
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the config flow.""" """Initialize the config flow."""
self._config: dict[str, Any] = {} self._config: dict[str, Any] = {}
@ -79,10 +82,12 @@ class LmConfigFlow(ConfigFlow, domain=DOMAIN):
**user_input, **user_input,
**self._discovered, **self._discovered,
} }
self._client = create_async_httpx_client(self.hass)
cloud_client = LaMarzoccoCloudClient( cloud_client = LaMarzoccoCloudClient(
username=data[CONF_USERNAME], username=data[CONF_USERNAME],
password=data[CONF_PASSWORD], password=data[CONF_PASSWORD],
client=self._client,
) )
try: try:
self._fleet = await cloud_client.get_customer_fleet() self._fleet = await cloud_client.get_customer_fleet()
@ -163,7 +168,7 @@ class LmConfigFlow(ConfigFlow, domain=DOMAIN):
# validate local connection if host is provided # validate local connection if host is provided
if user_input.get(CONF_HOST): if user_input.get(CONF_HOST):
if not await LaMarzoccoLocalClient.validate_connection( if not await LaMarzoccoLocalClient.validate_connection(
client=get_async_client(self.hass), client=self._client,
host=user_input[CONF_HOST], host=user_input[CONF_HOST],
token=selected_device.communication_key, token=selected_device.communication_key,
): ):