Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for Yale doorbell camera."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from aiohttp import ClientSession
8 from yalexs.activity import ActivityType
9 from yalexs.doorbell import Doorbell
10 from yalexs.util import update_doorbell_image_from_activity
11 
12 from homeassistant.components.camera import Camera
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers import aiohttp_client
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import YaleConfigEntry, YaleData
18 from .const import DEFAULT_NAME, DEFAULT_TIMEOUT
19 from .entity import YaleEntity
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 
25  hass: HomeAssistant,
26  config_entry: YaleConfigEntry,
27  async_add_entities: AddEntitiesCallback,
28 ) -> None:
29  """Set up Yale cameras."""
30  data = config_entry.runtime_data
31  # Create an aiohttp session instead of using the default one since the
32  # default one is likely to trigger yale's WAF if another integration
33  # is also using Cloudflare
34  session = aiohttp_client.async_create_clientsession(hass)
36  YaleCamera(data, doorbell, session, DEFAULT_TIMEOUT)
37  for doorbell in data.doorbells
38  )
39 
40 
42  """An implementation of an Yale security camera."""
43 
44  _attr_translation_key = "camera"
45  _attr_motion_detection_enabled = True
46  _attr_brand = DEFAULT_NAME
47  _image_url: str | None = None
48  _image_content: bytes | None = None
49 
50  def __init__(
51  self, data: YaleData, device: Doorbell, session: ClientSession, timeout: int
52  ) -> None:
53  """Initialize an Yale security camera."""
54  super().__init__(data, device, "camera")
55  self._timeout_timeout = timeout
56  self._session_session = session
57  self._attr_model_attr_model = self._detail_detail.model
58 
59  @property
60  def is_recording(self) -> bool:
61  """Return true if the device is recording."""
62  return self._device_device.has_subscription
63 
64  async def _async_update(self):
65  """Update device."""
66  _LOGGER.debug("async_update called %s", self._detail_detail.device_name)
67  await self._data_data.refresh_camera_by_id(self._device_id_device_id)
68  self._update_from_data_update_from_data_update_from_data()
69 
70  @callback
71  def _update_from_data(self) -> None:
72  """Get the latest state of the sensor."""
73  if doorbell_activity := self._get_latest_get_latest(
74  {ActivityType.DOORBELL_MOTION, ActivityType.DOORBELL_IMAGE_CAPTURE}
75  ):
76  update_doorbell_image_from_activity(self._detail_detail, doorbell_activity)
77 
78  async def async_camera_image(
79  self, width: int | None = None, height: int | None = None
80  ) -> bytes | None:
81  """Return bytes of camera image."""
82  self._update_from_data_update_from_data_update_from_data()
83 
84  if self._image_url_image_url is not self._detail_detail.image_url:
85  self._image_content_image_content = await self._data_data.async_get_doorbell_image(
86  self._device_id_device_id, self._session_session, timeout=self._timeout_timeout
87  )
88  self._image_url_image_url = self._detail_detail.image_url
89 
90  return self._image_content_image_content
None __init__(self, YaleData data, Doorbell device, ClientSession session, int timeout)
Definition: camera.py:52
bytes|None async_camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:80
DoorbellDetail|LockDetail _detail(self)
Definition: entity.py:53
Activity|None _get_latest(self, set[ActivityType] activity_types)
Definition: entity.py:62
None async_setup_entry(HomeAssistant hass, YaleConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:28