Fix MJPEG fallback when still image URL is missing with basic auth (#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
pull/113250/head
J. Nick Koston 2024-03-09 18:02:42 -10:00 committed by Franck Nijhof
parent 2d7de216a7
commit fc2ca1646a
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
1 changed files with 9 additions and 8 deletions

View File

@ -134,12 +134,11 @@ class MjpegCamera(Camera):
self, width: int | None = None, height: int | None = None self, width: int | None = None, height: int | None = None
) -> bytes | None: ) -> bytes | None:
"""Return a still image response from the camera.""" """Return a still image response from the camera."""
# DigestAuth is not supported
if ( if (
self._authentication == HTTP_DIGEST_AUTHENTICATION self._authentication == HTTP_DIGEST_AUTHENTICATION
or self._still_image_url is None 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) websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
try: try:
@ -157,15 +156,17 @@ class MjpegCamera(Camera):
return None return None
def _get_digest_auth(self) -> httpx.DigestAuth: def _get_httpx_auth(self) -> httpx.Auth:
"""Return a DigestAuth object.""" """Return a httpx auth object."""
username = "" if self._username is None else self._username 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.""" """Return a still image response from the camera using digest authentication."""
client = get_async_client(self.hass, verify_ssl=self._verify_ssl) client = get_async_client(self.hass, verify_ssl=self._verify_ssl)
auth = self._get_digest_auth() auth = self._get_httpx_auth()
try: try:
if self._still_image_url: if self._still_image_url:
# Fallback to MJPEG stream if still image URL is not available # Fallback to MJPEG stream if still image URL is not available
@ -196,7 +197,7 @@ class MjpegCamera(Camera):
) -> web.StreamResponse | None: ) -> web.StreamResponse | None:
"""Generate an HTTP MJPEG stream from the camera using digest authentication.""" """Generate an HTTP MJPEG stream from the camera using digest authentication."""
async with get_async_client(self.hass, verify_ssl=self._verify_ssl).stream( 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: ) as stream:
response = web.StreamResponse(headers=stream.headers) response = web.StreamResponse(headers=stream.headers)
await response.prepare(request) await response.prepare(request)