Skip to content

Commit

Permalink
Fix MJPEG fallback when still image URL is missing with basic auth (#…
Browse files Browse the repository at this point in the history
…112861)

* Fix MJPEG fallback when still image URL is missing with basic auth

I picked up an old DCS-930L (circa 2010) camera to test with
to fix #94877

* Fix MJPEG fallback when still image URL is missing with basic auth

I picked up an old DCS-930L (circa 2010) camera to test with
to fix #94877

* Fix MJPEG fallback when still image URL is missing with basic auth

I picked up an old DCS-930L (circa 2010) camera to test with
to fix #94877

* Fix MJPEG fallback when still image URL is missing with basic auth

I picked up an old DCS-930L (circa 2010) camera to test with
to fix #94877
  • Loading branch information
bdraco authored and frenck committed Mar 13, 2024
1 parent 2d7de21 commit fc2ca16
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions homeassistant/components/mjpeg/camera.py
Expand Up @@ -134,12 +134,11 @@ async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return a still image response from the camera."""
# DigestAuth is not supported
if (
self._authentication == HTTP_DIGEST_AUTHENTICATION
or self._still_image_url is None
):
return await self._async_digest_camera_image()
return await self._async_digest_or_fallback_camera_image()

websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
try:
Expand All @@ -157,15 +156,17 @@ async def async_camera_image(

return None

def _get_digest_auth(self) -> httpx.DigestAuth:
"""Return a DigestAuth object."""
def _get_httpx_auth(self) -> httpx.Auth:
"""Return a httpx auth object."""
username = "" if self._username is None else self._username
return httpx.DigestAuth(username, self._password)
digest_auth = self._authentication == HTTP_DIGEST_AUTHENTICATION
cls = httpx.DigestAuth if digest_auth else httpx.BasicAuth
return cls(username, self._password)

async def _async_digest_camera_image(self) -> bytes | None:
async def _async_digest_or_fallback_camera_image(self) -> bytes | None:
"""Return a still image response from the camera using digest authentication."""
client = get_async_client(self.hass, verify_ssl=self._verify_ssl)
auth = self._get_digest_auth()
auth = self._get_httpx_auth()
try:
if self._still_image_url:
# Fallback to MJPEG stream if still image URL is not available
Expand Down Expand Up @@ -196,7 +197,7 @@ async def _handle_async_mjpeg_digest_stream(
) -> web.StreamResponse | None:
"""Generate an HTTP MJPEG stream from the camera using digest authentication."""
async with get_async_client(self.hass, verify_ssl=self._verify_ssl).stream(
"get", self._mjpeg_url, auth=self._get_digest_auth(), timeout=TIMEOUT
"get", self._mjpeg_url, auth=self._get_httpx_auth(), timeout=TIMEOUT
) as stream:
response = web.StreamResponse(headers=stream.headers)
await response.prepare(request)
Expand Down

0 comments on commit fc2ca16

Please sign in to comment.