Skip to content

Commit

Permalink
Fix integrations building on top of mjpeg (home-assistant#66557)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Feb 15, 2022
1 parent 52ebe58 commit cbdbb66
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 68 deletions.
1 change: 1 addition & 0 deletions .core_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ components: &components
- homeassistant/components/logger/*
- homeassistant/components/lovelace/*
- homeassistant/components/media_source/*
- homeassistant/components/mjpeg/*
- homeassistant/components/mqtt/*
- homeassistant/components/network/*
- homeassistant/components/onboarding/*
Expand Down
20 changes: 7 additions & 13 deletions homeassistant/components/agent_dvr/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@
from agent import AgentError

from homeassistant.components.camera import SUPPORT_ON_OFF
from homeassistant.components.mjpeg.camera import (
CONF_MJPEG_URL,
CONF_STILL_IMAGE_URL,
MjpegCamera,
filter_urllib3_logging,
)
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
from homeassistant.components.mjpeg.camera import MjpegCamera, filter_urllib3_logging
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import DeviceInfo

Expand Down Expand Up @@ -70,16 +65,15 @@ class AgentCamera(MjpegCamera):

def __init__(self, device):
"""Initialize as a subclass of MjpegCamera."""
device_info = {
CONF_NAME: device.name,
CONF_MJPEG_URL: f"{device.client._server_url}{device.mjpeg_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
CONF_STILL_IMAGE_URL: f"{device.client._server_url}{device.still_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
}
self.device = device
self._removed = False
self._attr_name = f"{device.client.name} {device.name}"
self._attr_unique_id = f"{device._client.unique}_{device.typeID}_{device.id}"
super().__init__(device_info)
super().__init__(
name=device.name,
mjpeg_url=f"{device.client._server_url}{device.mjpeg_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
still_image_url=f"{device.client._server_url}{device.still_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
)
self._attr_device_info = DeviceInfo(
identifiers={(AGENT_DOMAIN, self.unique_id)},
manufacturer="Agent",
Expand Down
33 changes: 11 additions & 22 deletions homeassistant/components/axis/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@
from urllib.parse import urlencode

from homeassistant.components.camera import SUPPORT_STREAM
from homeassistant.components.mjpeg.camera import (
CONF_MJPEG_URL,
CONF_STILL_IMAGE_URL,
MjpegCamera,
filter_urllib3_logging,
)
from homeassistant.components.mjpeg.camera import MjpegCamera, filter_urllib3_logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_AUTHENTICATION,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.const import HTTP_DIGEST_AUTHENTICATION
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -47,15 +36,15 @@ def __init__(self, device):
"""Initialize Axis Communications camera component."""
AxisEntityBase.__init__(self, device)

config = {
CONF_NAME: device.name,
CONF_USERNAME: device.username,
CONF_PASSWORD: device.password,
CONF_MJPEG_URL: self.mjpeg_source,
CONF_STILL_IMAGE_URL: self.image_source,
CONF_AUTHENTICATION: HTTP_DIGEST_AUTHENTICATION,
}
MjpegCamera.__init__(self, config)
MjpegCamera.__init__(
self,
name=device.name,
username=device.username,
password=device.password,
mjpeg_url=self.mjpeg_source,
still_image_url=self.image_source,
authentication=HTTP_DIGEST_AUTHENTICATION,
)

self._attr_unique_id = f"{device.unique_id}-camera"

Expand Down
25 changes: 13 additions & 12 deletions homeassistant/components/mjpeg/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
[HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PASSWORD, default=""): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
}
Expand All @@ -71,13 +71,13 @@ async def async_setup_platform(
async_add_entities(
[
MjpegCamera(
config[CONF_NAME],
config[CONF_AUTHENTICATION],
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD),
config[CONF_MJPEG_URL],
config.get(CONF_STILL_IMAGE_URL),
config[CONF_VERIFY_SSL],
name=config[CONF_NAME],
authentication=config[CONF_AUTHENTICATION],
username=config.get(CONF_USERNAME),
password=config[CONF_PASSWORD],
mjpeg_url=config[CONF_MJPEG_URL],
still_image_url=config.get(CONF_STILL_IMAGE_URL),
verify_ssl=config[CONF_VERIFY_SSL],
)
]
)
Expand Down Expand Up @@ -116,13 +116,14 @@ class MjpegCamera(Camera):

def __init__(
self,
*,
name: str,
authentication: str,
username: str | None,
password: str | None,
mjpeg_url: str,
still_image_url: str | None,
verify_ssl: bool,
authentication: str | None = None,
username: str | None = None,
password: str = "",
verify_ssl: bool = True,
) -> None:
"""Initialize a MJPEG camera."""
super().__init__()
Expand Down
16 changes: 9 additions & 7 deletions homeassistant/components/motioneye/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from homeassistant.components.mjpeg.camera import (
CONF_MJPEG_URL,
CONF_STILL_IMAGE_URL,
CONF_VERIFY_SSL,
MjpegCamera,
)
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -144,6 +143,8 @@ def camera_add(camera: dict[str, Any]) -> None:
class MotionEyeMjpegCamera(MotionEyeEntity, MjpegCamera):
"""motionEye mjpeg camera."""

_name: str

def __init__(
self,
config_entry_id: str,
Expand Down Expand Up @@ -173,10 +174,8 @@ def __init__(
)
MjpegCamera.__init__(
self,
{
CONF_VERIFY_SSL: False,
**self._get_mjpeg_camera_properties_for_camera(camera),
},
verify_ssl=False,
**self._get_mjpeg_camera_properties_for_camera(camera),
)

@callback
Expand Down Expand Up @@ -207,7 +206,7 @@ def _get_mjpeg_camera_properties_for_camera(
return {
CONF_NAME: camera[KEY_NAME],
CONF_USERNAME: self._surveillance_username if auth is not None else None,
CONF_PASSWORD: self._surveillance_password if auth is not None else None,
CONF_PASSWORD: self._surveillance_password if auth is not None else "",
CONF_MJPEG_URL: streaming_url or "",
CONF_STILL_IMAGE_URL: self._client.get_camera_snapshot_url(camera),
CONF_AUTHENTICATION: auth,
Expand All @@ -227,7 +226,10 @@ def _set_mjpeg_camera_state_for_camera(self, camera: dict[str, Any]) -> None:
self._still_image_url = properties[CONF_STILL_IMAGE_URL]
self._authentication = properties[CONF_AUTHENTICATION]

if self._authentication == HTTP_BASIC_AUTHENTICATION:
if (
self._authentication == HTTP_BASIC_AUTHENTICATION
and self._username is not None
):
self._auth = aiohttp.BasicAuth(self._username, password=self._password)

def _is_acceptable_streaming_camera(self) -> bool:
Expand Down
21 changes: 7 additions & 14 deletions homeassistant/components/zoneminder/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

import logging

from homeassistant.components.mjpeg.camera import (
CONF_MJPEG_URL,
CONF_STILL_IMAGE_URL,
MjpegCamera,
filter_urllib3_logging,
)
from homeassistant.const import CONF_NAME, CONF_VERIFY_SSL
from homeassistant.components.mjpeg.camera import MjpegCamera, filter_urllib3_logging
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
Expand Down Expand Up @@ -44,13 +38,12 @@ class ZoneMinderCamera(MjpegCamera):

def __init__(self, monitor, verify_ssl):
"""Initialize as a subclass of MjpegCamera."""
device_info = {
CONF_NAME: monitor.name,
CONF_MJPEG_URL: monitor.mjpeg_image_url,
CONF_STILL_IMAGE_URL: monitor.still_image_url,
CONF_VERIFY_SSL: verify_ssl,
}
super().__init__(device_info)
super().__init__(
name=monitor.name,
mjpeg_url=monitor.mjpeg_image_url,
still_image_url=monitor.still_image_url,
verify_ssl=verify_ssl,
)
self._is_recording = None
self._is_available = None
self._monitor = monitor
Expand Down

0 comments on commit cbdbb66

Please sign in to comment.