Skip to content

Commit

Permalink
Bump to aiohttp 3.8.0 (home-assistant#58974)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 4, 2021
1 parent 23cb396 commit 10d6247
Show file tree
Hide file tree
Showing 106 changed files with 221 additions and 142 deletions.
67 changes: 67 additions & 0 deletions homeassistant/async_timeout_backcompat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Provide backwards compat for async_timeout."""
from __future__ import annotations

import asyncio
import contextlib
import logging
from typing import Any

import async_timeout

from homeassistant.helpers.frame import (
MissingIntegrationFrame,
get_integration_frame,
report_integration,
)

_LOGGER = logging.getLogger(__name__)


def timeout(
delay: float | None, loop: asyncio.AbstractEventLoop | None = None
) -> async_timeout.Timeout:
"""Backwards compatible timeout context manager that warns with loop usage."""
if loop is None:
loop = asyncio.get_running_loop()
else:
_report(
"called async_timeout.timeout with loop keyword argument. The loop keyword argument is deprecated and calls will fail after Home Assistant 2022.2"
)
if delay is not None:
deadline: float | None = loop.time() + delay
else:
deadline = None
return async_timeout.Timeout(deadline, loop)


def current_task(loop: asyncio.AbstractEventLoop) -> asyncio.Task[Any] | None:
"""Backwards compatible current_task."""
_report(
"called async_timeout.current_task. The current_task call is deprecated and calls will fail after Home Assistant 2022.2; use asyncio.current_task instead"
)
return asyncio.current_task()


def enable() -> None:
"""Enable backwards compat transitions."""
async_timeout.timeout = timeout
async_timeout.current_task = current_task # type: ignore[attr-defined]


def _report(what: str) -> None:
"""Report incorrect usage.
Async friendly.
"""
integration_frame = None

with contextlib.suppress(MissingIntegrationFrame):
integration_frame = get_integration_frame()

if not integration_frame:
_LOGGER.warning(
"Detected code that %s; Please report this issue", what, stack_info=True
)
return

report_integration(what, integration_frame)
2 changes: 1 addition & 1 deletion homeassistant/components/ads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async def async_event_set():
self._ads_hub.add_device_notification, ads_var, plctype, update
)
try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
await self._event.wait()
except asyncio.TimeoutError:
_LOGGER.debug("Variable %s: Timeout during first update", ads_var)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, hass, aemet, latitude, longitude, station_updates):

async def _async_update_data(self):
data = {}
with async_timeout.timeout(120):
async with async_timeout.timeout(120):
weather_response = await self._get_aemet_weather()
data = self._convert_weather_response(weather_response)
return data
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/airly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def _async_update_data(self) -> dict[str, str | float | int]:
measurements = self.airly.create_measurements_session_point(
self.latitude, self.longitude
)
with async_timeout.timeout(20):
async with async_timeout.timeout(20):
try:
await measurements.update()
except (AirlyError, ClientConnectorError) as error:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/airly/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async def test_location(
measurements = airly.create_measurements_session_point(
latitude=latitude, longitude=longitude
)
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
await measurements.update()

current = measurements.current
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/alexa/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def _async_request_new_token(self, lwa_params):

try:
session = aiohttp_client.async_get_clientsession(self.hass)
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
response = await session.post(
LWA_TOKEN_URI,
headers=LWA_HEADERS,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/alexa/state_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def async_send_changereport_message(
session = hass.helpers.aiohttp_client.async_get_clientsession()

try:
with async_timeout.timeout(DEFAULT_TIMEOUT):
async with async_timeout.timeout(DEFAULT_TIMEOUT):
response = await session.post(
config.endpoint,
headers=headers,
Expand Down Expand Up @@ -263,7 +263,7 @@ async def async_send_doorbell_event_message(hass, config, alexa_entity):
session = hass.helpers.aiohttp_client.async_get_clientsession()

try:
with async_timeout.timeout(DEFAULT_TIMEOUT):
async with async_timeout.timeout(DEFAULT_TIMEOUT):
response = await session.post(
config.endpoint,
headers=headers,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/almond/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async def _configure_almond_for_ha(

# Store token in Almond
try:
with async_timeout.timeout(30):
async with async_timeout.timeout(30):
await api.async_create_device(
{
"kind": "io.home-assistant",
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/almond/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def async_verify_local_connection(hass: core.HomeAssistant, host: str):
api = WebAlmondAPI(AlmondLocalAuth(host, websession))

try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
await api.async_list_apps()

return True
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/analytics/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async def send_analytics(self, _=None) -> None:
)

try:
with async_timeout.timeout(30):
async with async_timeout.timeout(30):
response = await self.session.post(self.endpoint, json=payload)
if response.status == 200:
LOGGER.info(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async def forward_events(event):

while True:
try:
with async_timeout.timeout(STREAM_PING_INTERVAL):
async with async_timeout.timeout(STREAM_PING_INTERVAL):
payload = await to_write.get()

if payload is stop_obj:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/arcam_fmj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _listen(_):

while True:
try:
with async_timeout.timeout(interval):
async with async_timeout.timeout(interval):
await client.start()

_LOGGER.debug("Client connected %s", client.host)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/atag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

async def _async_update_data():
"""Update data via library."""
with async_timeout.timeout(20):
async with async_timeout.timeout(20):
try:
await atag.update()
except AtagException as err:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/awair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, hass, config_entry, session) -> None:

async def _async_update_data(self) -> Any | None:
"""Update data via Awair client library."""
with timeout(API_TIMEOUT):
async with timeout(API_TIMEOUT):
try:
LOGGER.debug("Fetching users and devices")
user = await self._awair.user()
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/axis/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ async def get_device(hass, host, port, username, password):
)

try:
with async_timeout.timeout(30):
async with async_timeout.timeout(30):
await device.vapix.initialize()

return device
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/bluesound/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ async def send_bluesound_command(

try:
websession = async_get_clientsession(self._hass)
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
response = await websession.get(url)

if response.status == HTTPStatus.OK:
Expand Down Expand Up @@ -400,7 +400,7 @@ async def async_update_status(self):

try:

with async_timeout.timeout(125):
async with async_timeout.timeout(125):
response = await self._polling_session.get(
url, headers={CONNECTION: KEEP_ALIVE}
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/buienradar/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def get_data(self, url):
resp = None
try:
websession = async_get_clientsession(self.hass)
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
resp = await websession.get(url)

result[STATUS_CODE] = resp.status
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/citybikes/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def async_citybikes_request(hass, uri, schema):
try:
session = async_get_clientsession(hass)

with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
req = await session.get(DEFAULT_ENDPOINT.format(uri=uri))

json_response = await req.json()
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/cloud/alexa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ async def _sync_helper(self, to_update, to_remove) -> bool:
)

try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)

return True
Expand Down
16 changes: 8 additions & 8 deletions homeassistant/components/cloud/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ async def post(self, request):
hass = request.app["hass"]
cloud = hass.data[DOMAIN]

with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
await cloud.logout()

return self.json_message("ok")
Expand All @@ -230,7 +230,7 @@ async def post(self, request, data):
hass = request.app["hass"]
cloud = hass.data[DOMAIN]

with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
await cloud.auth.async_register(data["email"], data["password"])

return self.json_message("ok")
Expand All @@ -249,7 +249,7 @@ async def post(self, request, data):
hass = request.app["hass"]
cloud = hass.data[DOMAIN]

with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
await cloud.auth.async_resend_email_confirm(data["email"])

return self.json_message("ok")
Expand All @@ -268,7 +268,7 @@ async def post(self, request, data):
hass = request.app["hass"]
cloud = hass.data[DOMAIN]

with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
await cloud.auth.async_forgot_password(data["email"])

return self.json_message("ok")
Expand Down Expand Up @@ -314,7 +314,7 @@ async def websocket_subscription(hass, connection, msg):
"""Handle request for account info."""
cloud = hass.data[DOMAIN]
try:
with async_timeout.timeout(REQUEST_TIMEOUT):
async with async_timeout.timeout(REQUEST_TIMEOUT):
data = await cloud_api.async_subscription_info(cloud)
except aiohttp.ClientError:
connection.send_error(
Expand Down Expand Up @@ -353,7 +353,7 @@ async def websocket_update_prefs(hass, connection, msg):
if changes.get(PREF_ALEXA_REPORT_STATE):
alexa_config = await cloud.client.get_alexa_config()
try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
await alexa_config.async_get_access_token()
except asyncio.TimeoutError:
connection.send_error(
Expand Down Expand Up @@ -574,7 +574,7 @@ async def alexa_sync(hass, connection, msg):
cloud = hass.data[DOMAIN]
alexa_config = await cloud.client.get_alexa_config()

with async_timeout.timeout(10):
async with async_timeout.timeout(10):
try:
success = await alexa_config.async_sync_entities()
except alexa_errors.NoTokenAvailable:
Expand All @@ -597,7 +597,7 @@ async def thingtalk_convert(hass, connection, msg):
"""Convert a query."""
cloud = hass.data[DOMAIN]

with async_timeout.timeout(10):
async with async_timeout.timeout(10):
try:
connection.send_result(
msg["id"], await thingtalk.async_convert(cloud, msg["query"])
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/color_extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async def async_extract_color_from_url(url):
try:
session = aiohttp_client.async_get_clientsession(hass)

with async_timeout.timeout(10):
async with async_timeout.timeout(10):
response = await session.get(url)

except (asyncio.TimeoutError, aiohttp.ClientError) as err:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/comed_hourly_pricing/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def async_update(self):
else:
url_string += "?type=currenthouraverage"

with async_timeout.timeout(60):
async with async_timeout.timeout(60):
response = await self.websession.get(url_string)
# The API responds with MIME type 'text/html'
text = await response.text()
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/coronavirus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def get_coordinator(
return hass.data[DOMAIN]

async def async_get_cases():
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
return {
case.country: case
for case in await coronavirus.get_cases(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/daikin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def daikin_api_setup(hass, host, key, uuid, password):

session = hass.helpers.aiohttp_client.async_get_clientsession()
try:
with timeout(TIMEOUT):
async with timeout(TIMEOUT):
device = await Appliance.factory(
host, session, key=key, uuid=uuid, password=password
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/daikin/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def _create_device(self, host, key=None, password=None):
password = None

try:
with timeout(TIMEOUT):
async with timeout(TIMEOUT):
device = await Appliance.factory(
host,
self.hass.helpers.aiohttp_client.async_get_clientsession(),
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def async_step_user(self, user_input=None):
session = aiohttp_client.async_get_clientsession(self.hass)

try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
self.bridges = await deconz_discovery(session)

except (asyncio.TimeoutError, ResponseError):
Expand Down Expand Up @@ -141,7 +141,7 @@ async def async_step_link(self, user_input=None):
)

try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
api_key = await deconz_session.get_api_key()

except (ResponseError, RequestError, asyncio.TimeoutError):
Expand All @@ -159,7 +159,7 @@ async def _create_entry(self):
session = aiohttp_client.async_get_clientsession(self.hass)

try:
with async_timeout.timeout(10):
async with async_timeout.timeout(10):
self.bridge_id = await deconz_get_bridge_id(
session, **self.deconz_config
)
Expand Down
Loading

0 comments on commit 10d6247

Please sign in to comment.