Roll back #47852 (shield httpx in generic) (#50562)

pull/50601/head
uvjustin 2021-05-14 11:32:06 +08:00 committed by GitHub
parent dbf7430003
commit e8d7d96231
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 23 deletions

View File

@ -125,45 +125,32 @@ class GenericCamera(Camera):
).result()
async def async_camera_image(self):
"""Wrap _async_camera_image with an asyncio.shield."""
# Shield the request because of https://github.com/encode/httpx/issues/1461
try:
self._last_url, self._last_image = await asyncio.shield(
self._async_camera_image()
)
except asyncio.CancelledError as err:
_LOGGER.warning("Timeout getting camera image from %s", self._name)
raise err
return self._last_image
async def _async_camera_image(self):
"""Return a still image response from the camera."""
try:
url = self._still_image_url.async_render(parse_result=False)
except TemplateError as err:
_LOGGER.error("Error parsing template %s: %s", self._still_image_url, err)
return self._last_url, self._last_image
return self._last_image
if url == self._last_url and self._limit_refetch:
return self._last_url, self._last_image
response = None
return self._last_image
try:
async_client = get_async_client(self.hass, verify_ssl=self.verify_ssl)
response = await async_client.get(
url, auth=self._auth, timeout=GET_IMAGE_TIMEOUT
)
response.raise_for_status()
image = response.content
self._last_image = response.content
except httpx.TimeoutException:
_LOGGER.error("Timeout getting camera image from %s", self._name)
return self._last_url, self._last_image
return self._last_image
except (httpx.RequestError, httpx.HTTPStatusError) as err:
_LOGGER.error("Error getting new camera image from %s: %s", self._name, err)
return self._last_url, self._last_image
finally:
if response:
await response.aclose()
return url, image
return self._last_image
self._last_url = url
return self._last_image
@property
def name(self):

View File

@ -440,7 +440,7 @@ async def test_timeout_cancelled(hass, hass_client):
respx.get("http://example.com").respond(text="not hello world")
with patch(
"homeassistant.components.generic.camera.GenericCamera._async_camera_image",
"homeassistant.components.generic.camera.GenericCamera.async_camera_image",
side_effect=asyncio.CancelledError(),
):
resp = await client.get("/api/camera_proxy/camera.config_test")