mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
parent
dbf7430003
commit
e8d7d96231
@ -125,45 +125,32 @@ class GenericCamera(Camera):
|
|||||||
).result()
|
).result()
|
||||||
|
|
||||||
async def async_camera_image(self):
|
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."""
|
"""Return a still image response from the camera."""
|
||||||
try:
|
try:
|
||||||
url = self._still_image_url.async_render(parse_result=False)
|
url = self._still_image_url.async_render(parse_result=False)
|
||||||
except TemplateError as err:
|
except TemplateError as err:
|
||||||
_LOGGER.error("Error parsing template %s: %s", self._still_image_url, 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:
|
if url == self._last_url and self._limit_refetch:
|
||||||
return self._last_url, self._last_image
|
return self._last_image
|
||||||
response = None
|
|
||||||
try:
|
try:
|
||||||
async_client = get_async_client(self.hass, verify_ssl=self.verify_ssl)
|
async_client = get_async_client(self.hass, verify_ssl=self.verify_ssl)
|
||||||
response = await async_client.get(
|
response = await async_client.get(
|
||||||
url, auth=self._auth, timeout=GET_IMAGE_TIMEOUT
|
url, auth=self._auth, timeout=GET_IMAGE_TIMEOUT
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
image = response.content
|
self._last_image = response.content
|
||||||
except httpx.TimeoutException:
|
except httpx.TimeoutException:
|
||||||
_LOGGER.error("Timeout getting camera image from %s", self._name)
|
_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:
|
except (httpx.RequestError, httpx.HTTPStatusError) as err:
|
||||||
_LOGGER.error("Error getting new camera image from %s: %s", self._name, err)
|
_LOGGER.error("Error getting new camera image from %s: %s", self._name, err)
|
||||||
return self._last_url, self._last_image
|
return self._last_image
|
||||||
finally:
|
|
||||||
if response:
|
self._last_url = url
|
||||||
await response.aclose()
|
return self._last_image
|
||||||
return url, image
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -440,7 +440,7 @@ async def test_timeout_cancelled(hass, hass_client):
|
|||||||
respx.get("http://example.com").respond(text="not hello world")
|
respx.get("http://example.com").respond(text="not hello world")
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.generic.camera.GenericCamera._async_camera_image",
|
"homeassistant.components.generic.camera.GenericCamera.async_camera_image",
|
||||||
side_effect=asyncio.CancelledError(),
|
side_effect=asyncio.CancelledError(),
|
||||||
):
|
):
|
||||||
resp = await client.get("/api/camera_proxy/camera.config_test")
|
resp = await client.get("/api/camera_proxy/camera.config_test")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user