Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for Abode Security System cameras."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any, cast
7 
8 from jaraco.abode.devices.base import Device
9 from jaraco.abode.devices.camera import Camera as AbodeCam
10 from jaraco.abode.helpers import timeline
11 import requests
12 from requests.models import Response
13 
14 from homeassistant.components.camera import Camera
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import Event, HomeAssistant
17 from homeassistant.helpers.dispatcher import async_dispatcher_connect
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.util import Throttle
20 
21 from . import AbodeSystem
22 from .const import DOMAIN, LOGGER
23 from .entity import AbodeDevice
24 
25 MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=90)
26 
27 
29  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
30 ) -> None:
31  """Set up Abode camera devices."""
32  data: AbodeSystem = hass.data[DOMAIN]
33 
35  AbodeCamera(data, device, timeline.CAPTURE_IMAGE)
36  for device in data.abode.get_devices(generic_type="camera")
37  )
38 
39 
41  """Representation of an Abode camera."""
42 
43  _device: AbodeCam
44  _attr_name = None
45 
46  def __init__(self, data: AbodeSystem, device: Device, event: Event) -> None:
47  """Initialize the Abode device."""
48  AbodeDevice.__init__(self, data, device)
49  Camera.__init__(self)
50  self._event_event = event
51  self._response_response: Response | None = None
52 
53  async def async_added_to_hass(self) -> None:
54  """Subscribe Abode events."""
55  await super().async_added_to_hass()
56 
57  self.hasshass.async_add_executor_job(
58  self._data_data.abode.events.add_timeline_callback,
59  self._event_event,
60  self._capture_callback_capture_callback,
61  )
62 
63  signal = f"abode_camera_capture_{self.entity_id}"
64  self.async_on_removeasync_on_remove(async_dispatcher_connect(self.hasshass, signal, self.capturecapture))
65 
66  def capture(self) -> bool:
67  """Request a new image capture."""
68  return cast(bool, self._device_device.capture())
69 
70  @Throttle(MIN_TIME_BETWEEN_UPDATES)
71  def refresh_image(self) -> None:
72  """Find a new image on the timeline."""
73  if self._device_device.refresh_image():
74  self.get_imageget_image()
75 
76  def get_image(self) -> None:
77  """Attempt to download the most recent capture."""
78  if self._device_device.image_url:
79  try:
80  self._response_response = requests.get(
81  self._device_device.image_url, stream=True, timeout=10
82  )
83 
84  self._response_response.raise_for_status()
85  except requests.HTTPError as err:
86  LOGGER.warning("Failed to get camera image: %s", err)
87  self._response_response = None
88  else:
89  self._response_response = None
90 
92  self, width: int | None = None, height: int | None = None
93  ) -> bytes | None:
94  """Get a camera image."""
95  self.refresh_imagerefresh_image()
96 
97  if self._response_response:
98  return self._response_response.content
99 
100  return None
101 
102  def turn_on(self) -> None:
103  """Turn on camera."""
104  self._device_device.privacy_mode(False)
105 
106  def turn_off(self) -> None:
107  """Turn off camera."""
108  self._device_device.privacy_mode(True)
109 
110  def _capture_callback(self, capture: Any) -> None:
111  """Update the image with the device then refresh device."""
112  self._device_device.update_image_location(capture)
113  self.get_imageget_image()
114  self.schedule_update_ha_stateschedule_update_ha_state()
115 
116  @property
117  def is_on(self) -> bool:
118  """Return true if on."""
119  return cast(bool, self._device_device.is_on)
bytes|None camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:93
None __init__(self, AbodeSystem data, Device device, Event event)
Definition: camera.py:46
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:30
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103