Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Camera support for the Skybell HD Doorbell."""
2 
3 from __future__ import annotations
4 
5 from aiohttp import web
6 from haffmpeg.camera import CameraMjpeg
7 
8 from homeassistant.components.camera import Camera, CameraEntityDescription
9 from homeassistant.components.ffmpeg import get_ffmpeg_manager
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
13 from homeassistant.helpers.entity import EntityDescription
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN
17 from .coordinator import SkybellDataUpdateCoordinator
18 from .entity import SkybellEntity
19 
20 CAMERA_TYPES: tuple[CameraEntityDescription, ...] = (
22  key="activity",
23  translation_key="activity",
24  ),
26  key="avatar",
27  translation_key="camera",
28  ),
29 )
30 
31 
33  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
34 ) -> None:
35  """Set up Skybell camera."""
36  entities = []
37  for description in CAMERA_TYPES:
38  for coordinator in hass.data[DOMAIN][entry.entry_id]:
39  if description.key == "avatar":
40  entities.append(SkybellCamera(coordinator, description))
41  else:
42  entities.append(SkybellActivityCamera(coordinator, description))
43  async_add_entities(entities)
44 
45 
47  """A camera implementation for Skybell devices."""
48 
49  def __init__(
50  self,
51  coordinator: SkybellDataUpdateCoordinator,
52  description: EntityDescription,
53  ) -> None:
54  """Initialize a camera for a Skybell device."""
55  super().__init__(coordinator, description)
56  Camera.__init__(self)
57 
58  async def async_camera_image(
59  self, width: int | None = None, height: int | None = None
60  ) -> bytes | None:
61  """Get the latest camera image."""
62  return self._device_device.images[self.entity_descriptionentity_description.key]
63 
64 
66  """A camera implementation for latest Skybell activity."""
67 
69  self, request: web.Request
70  ) -> web.StreamResponse:
71  """Generate an HTTP MJPEG stream from the latest recorded activity."""
72  stream = CameraMjpeg(get_ffmpeg_manager(self.hasshasshass).binary)
73  url = await self.coordinator.device.async_get_activity_video_url()
74  await stream.open_camera(url, extra_cmd="-r 210")
75 
76  try:
77  return await async_aiohttp_proxy_stream(
78  self.hasshasshass,
79  request,
80  await stream.get_reader(),
81  get_ffmpeg_manager(self.hasshasshass).ffmpeg_stream_content_type,
82  )
83  finally:
84  await stream.close()
web.StreamResponse handle_async_mjpeg_stream(self, web.Request request)
Definition: camera.py:70
bytes|None async_camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:60
None __init__(self, SkybellDataUpdateCoordinator coordinator, EntityDescription description)
Definition: camera.py:53
FFmpegManager get_ffmpeg_manager(HomeAssistant hass)
Definition: __init__.py:106
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:34
web.StreamResponse async_aiohttp_proxy_stream(HomeAssistant hass, web.BaseRequest request, aiohttp.StreamReader stream, str|None content_type, int buffer_size=102400, int timeout=10)