diff --git a/homeassistant/components/generic/camera.py b/homeassistant/components/generic/camera.py index 1ec7f0874e0..56b490e165a 100644 --- a/homeassistant/components/generic/camera.py +++ b/homeassistant/components/generic/camera.py @@ -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): diff --git a/tests/components/generic/test_camera.py b/tests/components/generic/test_camera.py index deb8049da33..1a1edc4eece 100644 --- a/tests/components/generic/test_camera.py +++ b/tests/components/generic/test_camera.py @@ -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")