Replace hass.helpers: async_get_clientsession() (#63910)

This commit is contained in:
Franck Nijhof 2022-01-11 17:33:50 +01:00 committed by GitHub
parent 43aec6e784
commit 20bdcc7fff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 64 additions and 36 deletions

View file

@ -11,6 +11,7 @@ import async_timeout
from homeassistant.const import MATCH_ALL, STATE_ON
from homeassistant.core import HomeAssistant, State, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.significant_change import create_checker
import homeassistant.util.dt as dt_util
@ -129,7 +130,7 @@ async def async_send_changereport_message(
message.set_endpoint_full(token, endpoint)
message_serialized = message.serialize()
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
try:
async with async_timeout.timeout(DEFAULT_TIMEOUT):
@ -198,7 +199,7 @@ async def async_send_add_or_update_message(hass, config, entity_ids):
)
message_serialized = message.serialize()
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
return await session.post(
config.endpoint, headers=headers, json=message_serialized, allow_redirects=True
@ -231,7 +232,7 @@ async def async_send_delete_message(hass, config, entity_ids):
)
message_serialized = message.serialize()
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
return await session.post(
config.endpoint, headers=headers, json=message_serialized, allow_redirects=True
@ -261,7 +262,7 @@ async def async_send_doorbell_event_message(hass, config, alexa_entity):
message.set_endpoint_full(token, endpoint)
message_serialized = message.serialize()
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
try:
async with async_timeout.timeout(DEFAULT_TIMEOUT):

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entityfilter
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass
from homeassistant.util.aiohttp import MockRequest
@ -187,7 +188,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
await prefs.async_initialize()
# Initialize Cloud
websession = hass.helpers.aiohttp_client.async_get_clientsession()
websession = async_get_clientsession(hass)
client = CloudClient(hass, prefs, websession, alexa_conf, google_conf)
cloud = hass.data[DOMAIN] = Cloud(client, **kwargs)

View file

@ -21,6 +21,7 @@ from homeassistant.components.google_assistant import helpers as google_helpers
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.websocket_api import const as ws_const
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util.location import async_detect_location_info
from .const import (
@ -224,7 +225,7 @@ class CloudRegisterView(HomeAssistantView):
client_metadata = None
if location_info := await async_detect_location_info(
hass.helpers.aiohttp_client.async_get_clientsession()
async_get_clientsession(hass)
):
client_metadata = {
"NC_COUNTRY_CODE": location_info.country_code,

View file

@ -7,6 +7,7 @@ from homeassistant.components.http import HomeAssistantView
from homeassistant.config import async_check_ha_config_file
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import location
@ -67,7 +68,7 @@ async def websocket_update_config(hass, connection, msg):
@websocket_api.websocket_command({"type": "config/core/detect"})
async def websocket_detect_config(hass, connection, msg):
"""Detect core config."""
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
location_info = await location.async_detect_location_info(session)
info = {}

View file

@ -11,6 +11,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity import DeviceInfo
@ -63,7 +64,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def daikin_api_setup(hass, host, key, uuid, password):
"""Create a Daikin instance only once."""
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
try:
async with timeout(TIMEOUT):
device = await Appliance.factory(

View file

@ -13,6 +13,7 @@ from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_UUID, DOMAIN, KEY_MAC, TIMEOUT
@ -72,7 +73,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async with timeout(TIMEOUT):
device = await Appliance.factory(
host,
self.hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(self.hass),
key=key,
uuid=uuid,
password=password,

View file

@ -28,6 +28,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -149,7 +150,7 @@ async def async_setup_platform(
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
httpsession = hass.helpers.aiohttp_client.async_get_clientsession()
httpsession = async_get_clientsession(hass)
ebox_data = EBoxData(username, password, httpsession)
name = config.get(CONF_NAME)

View file

@ -27,6 +27,7 @@ from homeassistant.const import (
TIME_MINUTES,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -189,7 +190,7 @@ async def async_setup_platform(
username = config[CONF_USERNAME]
password = config[CONF_PASSWORD]
httpsession = hass.helpers.aiohttp_client.async_get_clientsession()
httpsession = async_get_clientsession(hass)
fido_data = FidoData(username, password, httpsession)
ret = await fido_data.async_update()
if ret is False:

View file

@ -9,6 +9,7 @@ import voluptuous as vol
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_SCAN_INTERVAL, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
@ -44,7 +45,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
auth_token = conf.get(CONF_ACCESS_TOKEN)
update_interval = conf[CONF_SCAN_INTERVAL]
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
result = await _update_freedns(hass, session, url, auth_token)

View file

@ -9,6 +9,7 @@ import voluptuous as vol
from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
@ -42,7 +43,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
password = config[DOMAIN].get(CONF_PASSWORD)
timeout = config[DOMAIN].get(CONF_TIMEOUT)
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
result = await _update_google_domains(
hass, session, domain, user, password, timeout

View file

@ -33,6 +33,7 @@ from homeassistant.core import (
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, recorder
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import (
DeviceEntryType,
DeviceRegistry,
@ -424,7 +425,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
async_load_websocket_api(hass)
host = os.environ["HASSIO"]
websession = hass.helpers.aiohttp_client.async_get_clientsession()
websession = async_get_clientsession(hass)
hass.data[DOMAIN] = hassio = HassIO(hass.loop, websession, host)
if not await hassio.is_connected():

View file

@ -13,6 +13,7 @@ from multidict import CIMultiDict
from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import X_HASSIO, X_INGRESS_PATH
@ -22,7 +23,7 @@ _LOGGER = logging.getLogger(__name__)
@callback
def async_setup_ingress_view(hass: HomeAssistant, host: str):
"""Auth setup."""
websession = hass.helpers.aiohttp_client.async_get_clientsession()
websession = async_get_clientsession(hass)
hassio_ingress = HassIOIngress(host, websession)
hass.http.register_view(hassio_ingress)

View file

@ -15,6 +15,7 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_TOKEN, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity import DeviceInfo
@ -143,7 +144,7 @@ class MelCloudDevice:
async def mel_devices_setup(hass, token) -> list[MelCloudDevice]:
"""Query connected devices from MELCloud."""
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
try:
async with timeout(10):
all_devices = await get_devices(

View file

@ -11,6 +11,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
@ -47,11 +48,11 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
acquired_token = await pymelcloud.login(
username,
password,
self.hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(self.hass),
)
await pymelcloud.get_devices(
acquired_token,
self.hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(self.hass),
)
except ClientResponseError as err:
if err.status in (HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN):

View file

@ -10,6 +10,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import update_coordinator
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN, UPDATE_INTERVAL_IN_MEETING, UPDATE_INTERVAL_NOT_IN_MEETING
@ -21,7 +22,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
client = mutesync.PyMutesync(
entry.data["token"],
entry.data["host"],
hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(hass),
)
async def update_data():

View file

@ -13,6 +13,7 @@ from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
@ -24,7 +25,7 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
try:
async with async_timeout.timeout(5):
token = await mutesync.authenticate(session, data["host"])

View file

@ -11,7 +11,10 @@ import voluptuous as vol
from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
from homeassistant.helpers.aiohttp_client import (
SERVER_SOFTWARE,
async_get_clientsession,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
@ -62,7 +65,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
auth_str = base64.b64encode(f"{user}:{password}".encode())
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
result = await _update_no_ip(hass, session, domain, auth_str, timeout)

View file

@ -18,6 +18,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv, device_registry
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
@ -88,7 +89,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
session = PointSession(
hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(hass),
entry.data["refresh_args"][CONF_CLIENT_ID],
entry.data["refresh_args"][CONF_CLIENT_SECRET],
token=entry.data[CONF_TOKEN],

View file

@ -11,6 +11,7 @@ from homeassistant import config_entries
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import DOMAIN
@ -112,7 +113,7 @@ class PointFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
client_id = flow[CONF_CLIENT_ID]
client_secret = flow[CONF_CLIENT_SECRET]
point_session = PointSession(
self.hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(self.hass),
client_id,
client_secret,
)
@ -144,7 +145,7 @@ class PointFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
client_id = flow[CONF_CLIENT_ID]
client_secret = flow[CONF_CLIENT_SECRET]
point_session = PointSession(
self.hass.helpers.aiohttp_client.async_get_clientsession(),
async_get_clientsession(self.hass),
client_id,
client_secret,
)

View file

@ -24,6 +24,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant, ServiceCall, split_entity_id
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_registry
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import location
from homeassistant.util.json import load_json, save_json
@ -98,9 +99,7 @@ async def async_migrate_entry(hass, entry):
# Migrate Version 1 -> Version 2: New region codes.
if version == 1:
loc = await location.async_detect_location_info(
hass.helpers.aiohttp_client.async_get_clientsession()
)
loc = await location.async_detect_location_info(async_get_clientsession(hass))
if loc:
country = COUNTRYCODE_NAMES.get(loc.country_code)
if country in COUNTRIES:

View file

@ -15,6 +15,7 @@ from homeassistant.const import (
CONF_REGION,
CONF_TOKEN,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import location
from .const import (
@ -180,7 +181,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
# Try to find region automatically.
if not self.location:
self.location = await location.async_detect_location_info(
self.hass.helpers.aiohttp_client.async_get_clientsession()
async_get_clientsession(self.hass)
)
if self.location:
country = COUNTRYCODE_NAMES.get(self.location.country_code)

View file

@ -15,6 +15,7 @@ from homeassistant import exceptions
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import _LOGGER, API_TIMEOUT, DOMAIN, PLATFORMS
from .update_coordinator import SharkIqUpdateCoordinator
@ -45,7 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
ayla_api = get_ayla_api(
username=config_entry.data[CONF_USERNAME],
password=config_entry.data[CONF_PASSWORD],
websession=hass.helpers.aiohttp_client.async_get_clientsession(),
websession=async_get_clientsession(hass),
)
try:

View file

@ -10,6 +10,7 @@ import voluptuous as vol
from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import _LOGGER, DOMAIN
@ -23,7 +24,7 @@ async def validate_input(hass: core.HomeAssistant, data):
ayla_api = get_ayla_api(
username=data[CONF_USERNAME],
password=data[CONF_PASSWORD],
websession=hass.helpers.aiohttp_client.async_get_clientsession(hass),
websession=async_get_clientsession(hass),
)
try:

View file

@ -14,6 +14,7 @@ from homeassistant.components.device_tracker import (
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
@ -34,7 +35,7 @@ async def async_get_scanner(
) -> DeviceScanner | None:
"""Return the UPC device scanner."""
conf = config[DOMAIN]
session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass)
connect_box = ConnectBox(session, conf[CONF_PASSWORD], host=conf[CONF_HOST])
# Check login data

View file

@ -13,6 +13,7 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION, TIME_MINUTES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -79,7 +80,7 @@ async def async_setup_platform(
async def async_http_request(hass, uri):
"""Perform actual request."""
try:
session = hass.helpers.aiohttp_client.async_get_clientsession(hass)
session = async_get_clientsession(hass)
async with async_timeout.timeout(REQUEST_TIMEOUT):
req = await session.get(uri)
if req.status != HTTPStatus.OK:

View file

@ -5,6 +5,7 @@ from unittest.mock import AsyncMock, Mock, patch
import pytest
from homeassistant.components.cloud import ALEXA_SCHEMA, alexa_config
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.util.dt import utcnow
@ -127,7 +128,7 @@ async def test_alexa_config_invalidate_token(hass, cloud_prefs, aioclient_mock):
Mock(
alexa_access_token_url="http://example/alexa_token",
auth=Mock(async_check_token=AsyncMock()),
websession=hass.helpers.aiohttp_client.async_get_clientsession(),
websession=async_get_clientsession(hass),
),
)

View file

@ -6,6 +6,7 @@ import pytest
from homeassistant.components.hassio.handler import HassIO, HassioAPIError
from homeassistant.core import CoreState
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.setup import async_setup_component
from . import HASSIO_TOKEN
@ -70,7 +71,7 @@ def hassio_handler(hass, aioclient_mock):
"""Create mock hassio handler."""
async def get_client_session():
return hass.helpers.aiohttp_client.async_get_clientsession()
return async_get_clientsession(hass)
websession = hass.loop.run_until_complete(get_client_session())

View file

@ -4,6 +4,7 @@ from unittest.mock import Mock, patch
import aiohttp
import pytest
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.util.location as location_util
from tests.common import load_fixture
@ -27,7 +28,7 @@ DISTANCE_MILES = 3632.78
@pytest.fixture
async def session(hass):
"""Return aioclient session."""
return hass.helpers.aiohttp_client.async_get_clientsession()
return async_get_clientsession(hass)
@pytest.fixture