Skip to content

Commit

Permalink
Use asyncio.timeout [f-h] (#98449)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Aug 15, 2023
1 parent a9ade1f commit 5dd3f05
Show file tree
Hide file tree
Showing 28 changed files with 48 additions and 61 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/faa_delays/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""The FAA Delays integration."""
import asyncio
from datetime import timedelta
import logging

from aiohttp import ClientConnectionError
from async_timeout import timeout
from faadelays import Airport

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(self, hass, code):

async def _async_update_data(self):
try:
async with timeout(10):
async with asyncio.timeout(10):
await self.data.update()
except ClientConnectionError as err:
raise UpdateFailed(err) from err
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/flick_electric/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import asyncio
import logging

import async_timeout
from pyflick.authentication import AuthException, SimpleFlickAuth
from pyflick.const import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET
import voluptuous as vol
Expand Down Expand Up @@ -45,7 +44,7 @@ async def _validate_input(self, user_input):
)

try:
async with async_timeout.timeout(60):
async with asyncio.timeout(60):
token = await auth.async_get_access_token()
except asyncio.TimeoutError as err:
raise CannotConnect() from err
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/flick_electric/sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Support for Flick Electric Pricing data."""
import asyncio
from datetime import timedelta
import logging
from typing import Any

import async_timeout
from pyflick import FlickAPI, FlickPrice

from homeassistant.components.sensor import SensorEntity
Expand Down Expand Up @@ -58,7 +58,7 @@ async def async_update(self) -> None:
if self._price and self._price.end_at >= utcnow():
return # Power price data is still valid

async with async_timeout.timeout(60):
async with asyncio.timeout(60):
self._price = await self._api.getPricing()

_LOGGER.debug("Pricing data: %s", self._price)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/flo/device.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Flo device object."""
from __future__ import annotations

import asyncio
from datetime import datetime, timedelta
from typing import Any

from aioflo.api import API
from aioflo.errors import RequestError
from async_timeout import timeout

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(
async def _async_update_data(self):
"""Update data via library."""
try:
async with timeout(20):
async with asyncio.timeout(20):
await self.send_presence_ping()
await self._update_device()
await self._update_consumption_data()
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/flock/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from http import HTTPStatus
import logging

import async_timeout
import voluptuous as vol

from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService
Expand Down Expand Up @@ -49,7 +48,7 @@ async def async_send_message(self, message, **kwargs):
_LOGGER.debug("Attempting to call Flock at %s", self._url)

try:
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
response = await self._session.post(self._url, json=payload)
result = await response.json()

Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/forked_daapd/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import logging
from typing import Any

import async_timeout
from pyforked_daapd import ForkedDaapdAPI
from pylibrespot_java import LibrespotJavaAPI

Expand Down Expand Up @@ -667,7 +666,7 @@ async def _pause_and_wait_for_callback(self):
self._pause_requested = True
await self.async_media_pause()
try:
async with async_timeout.timeout(CALLBACK_TIMEOUT):
async with asyncio.timeout(CALLBACK_TIMEOUT):
await self._paused_event.wait() # wait for paused
except asyncio.TimeoutError:
self._pause_requested = False
Expand Down Expand Up @@ -762,7 +761,7 @@ async def _async_announce(self, media_id: str) -> None:
await sleep_future
await self.api.add_to_queue(uris=media_id, playback="start", clear=True)
try:
async with async_timeout.timeout(TTS_TIMEOUT):
async with asyncio.timeout(TTS_TIMEOUT):
await self._tts_playing_event.wait()
# we have started TTS, now wait for completion
except asyncio.TimeoutError:
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/freedns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

import aiohttp
import async_timeout
import voluptuous as vol

from homeassistant.const import CONF_ACCESS_TOKEN, CONF_SCAN_INTERVAL, CONF_URL
Expand Down Expand Up @@ -76,7 +75,7 @@ async def _update_freedns(hass, session, url, auth_token):
params[auth_token] = ""

try:
async with async_timeout.timeout(TIMEOUT):
async with asyncio.timeout(TIMEOUT):
resp = await session.get(url, params=params)
body = await resp.text()

Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/fully_kiosk/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from typing import Any

from aiohttp.client_exceptions import ClientConnectorError
from async_timeout import timeout
from fullykiosk import FullyKiosk
from fullykiosk.exceptions import FullyKioskError
import voluptuous as vol
Expand Down Expand Up @@ -42,7 +41,7 @@ async def _create_entry(
)

try:
async with timeout(15):
async with asyncio.timeout(15):
device_info = await fully.getDeviceInfo()
except (
ClientConnectorError,
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/fully_kiosk/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import asyncio
from typing import Any, cast

from async_timeout import timeout
from fullykiosk import FullyKiosk
from fullykiosk.exceptions import FullyKioskError

Expand Down Expand Up @@ -36,7 +35,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
async def _async_update_data(self) -> dict[str, Any]:
"""Update data via library."""
try:
async with timeout(15):
async with asyncio.timeout(15):
# Get device info and settings in parallel
result = await asyncio.gather(
self.fully.getDeviceInfo(), self.fully.getSettings()
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/garages_amsterdam/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""The Garages Amsterdam integration."""
import asyncio
from datetime import timedelta
import logging

import async_timeout
from odp_amsterdam import ODPAmsterdam

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -40,7 +40,7 @@ async def get_coordinator(
return hass.data[DOMAIN]

async def async_get_garages():
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
return {
garage.garage_name: garage
for garage in await ODPAmsterdam(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/generic/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Config flow for generic (IP Camera)."""
from __future__ import annotations

import asyncio
from collections.abc import Mapping
import contextlib
from datetime import datetime
Expand All @@ -10,7 +11,6 @@
from typing import Any

from aiohttp import web
from async_timeout import timeout
from httpx import HTTPStatusError, RequestError, TimeoutException
import PIL.Image
import voluptuous as vol
Expand Down Expand Up @@ -171,7 +171,7 @@ async def async_test_still(
auth = generate_auth(info)
try:
async_client = get_async_client(hass, verify_ssl=verify_ssl)
async with timeout(GET_IMAGE_TIMEOUT):
async with asyncio.timeout(GET_IMAGE_TIMEOUT):
response = await async_client.get(url, auth=auth, timeout=GET_IMAGE_TIMEOUT)
response.raise_for_status()
image = response.content
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/gios/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""The GIOS component."""
from __future__ import annotations

import asyncio
import logging

from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientConnectorError
from async_timeout import timeout
from gios import Gios
from gios.exceptions import GiosError
from gios.model import GiosSensors
Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(
async def _async_update_data(self) -> GiosSensors:
"""Update data via library."""
try:
async with timeout(API_TIMEOUT):
async with asyncio.timeout(API_TIMEOUT):
return await self.gios.async_update()
except (GiosError, ClientConnectorError) as error:
raise UpdateFailed(error) from error
3 changes: 1 addition & 2 deletions homeassistant/components/gios/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Any

from aiohttp.client_exceptions import ClientConnectorError
from async_timeout import timeout
from gios import ApiError, Gios, InvalidSensorsDataError, NoStationError
import voluptuous as vol

Expand Down Expand Up @@ -37,7 +36,7 @@ async def async_step_user(

websession = async_get_clientsession(self.hass)

async with timeout(API_TIMEOUT):
async with asyncio.timeout(API_TIMEOUT):
gios = Gios(user_input[CONF_STATION_ID], websession)
await gios.async_update()

Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/google_cloud/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os

import async_timeout
from google.cloud import texttospeech
import voluptuous as vol

Expand Down Expand Up @@ -286,7 +285,7 @@ async def async_get_tts_audio(self, message, language, options):
"input": synthesis_input,
}

async with async_timeout.timeout(10):
async with asyncio.timeout(10):
assert self.hass
response = await self.hass.async_add_executor_job(
self._client.synthesize_speech, request
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/google_domains/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

import aiohttp
import async_timeout
import voluptuous as vol

from homeassistant.const import CONF_DOMAIN, CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
Expand Down Expand Up @@ -69,7 +68,7 @@ async def _update_google_domains(hass, session, domain, user, password, timeout)
params = {"hostname": domain}

try:
async with async_timeout.timeout(timeout):
async with asyncio.timeout(timeout):
resp = await session.get(url, params=params)
body = await resp.text()

Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/hlk_sw16/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Config flow for HLK-SW16."""
import asyncio

import async_timeout
from hlk_sw16 import create_hlk_sw16_connection
import voluptuous as vol

Expand Down Expand Up @@ -36,7 +35,7 @@ async def connect_client(hass, user_input):
reconnect_interval=DEFAULT_RECONNECT_INTERVAL,
keep_alive_interval=DEFAULT_KEEP_ALIVE_INTERVAL,
)
async with async_timeout.timeout(CONNECTION_TIMEOUT):
async with asyncio.timeout(CONNECTION_TIMEOUT):
return await client_aw


Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/home_plus_control/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""The Legrand Home+ Control integration."""
import asyncio
from datetime import timedelta
import logging

import async_timeout
from homepluscontrol.homeplusapi import HomePlusControlApiError
import voluptuous as vol

Expand Down Expand Up @@ -100,7 +100,7 @@ async def async_update_data():
try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
return await api.async_get_modules()
except HomePlusControlApiError as err:
raise UpdateFailed(
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/homeassistant_yellow/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Config flow for the Home Assistant Yellow integration."""
from __future__ import annotations

import asyncio
import logging
from typing import Any

import aiohttp
import async_timeout
import voluptuous as vol

from homeassistant.components.hassio import (
Expand Down Expand Up @@ -80,15 +80,15 @@ async def async_step_hardware_settings(
if self._hw_settings == user_input:
return self.async_create_entry(data={})
try:
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
await async_set_yellow_settings(self.hass, user_input)
except (aiohttp.ClientError, TimeoutError, HassioAPIError) as err:
_LOGGER.warning("Failed to write hardware settings", exc_info=err)
return self.async_abort(reason="write_hw_settings_error")
return await self.async_step_confirm_reboot()

try:
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
self._hw_settings: dict[str, bool] = await async_get_yellow_settings(
self.hass
)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/hue/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from aiohttp import client_exceptions
from aiohue import HueBridgeV1, HueBridgeV2, LinkButtonNotPressed, Unauthorized
from aiohue.errors import AiohueException, BridgeBusy
import async_timeout

from homeassistant import core
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
Expand Down Expand Up @@ -73,7 +72,7 @@ def api_version(self) -> int:
async def async_initialize_bridge(self) -> bool:
"""Initialize Connection with the Hue API."""
try:
async with async_timeout.timeout(10):
async with asyncio.timeout(10):
await self.api.initialize()

except (LinkButtonNotPressed, Unauthorized):
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/hue/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from aiohue import LinkButtonNotPressed, create_app_key
from aiohue.discovery import DiscoveredHueBridge, discover_bridge, discover_nupnp
from aiohue.util import normalize_bridge_id
import async_timeout
import slugify as unicode_slug
import voluptuous as vol

Expand Down Expand Up @@ -110,7 +109,7 @@ async def async_step_init(

# Find / discover bridges
try:
async with async_timeout.timeout(5):
async with asyncio.timeout(5):
bridges = await discover_nupnp(
websession=aiohttp_client.async_get_clientsession(self.hass)
)
Expand Down
Loading

0 comments on commit 5dd3f05

Please sign in to comment.