Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for Tuya cameras."""
2 
3 from __future__ import annotations
4 
5 from tuya_sharing import CustomerDevice, Manager
6 
7 from homeassistant.components import ffmpeg
8 from homeassistant.components.camera import Camera as CameraEntity, CameraEntityFeature
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import TuyaConfigEntry
14 from .const import TUYA_DISCOVERY_NEW, DPCode
15 from .entity import TuyaEntity
16 
17 # All descriptions can be found here:
18 # https://developer.tuya.com/en/docs/iot/standarddescription?id=K9i5ql6waswzq
19 CAMERAS: tuple[str, ...] = (
20  # Smart Camera (including doorbells)
21  # https://developer.tuya.com/en/docs/iot/categorysgbj?id=Kaiuz37tlpbnu
22  "sp",
23 )
24 
25 
27  hass: HomeAssistant, entry: TuyaConfigEntry, async_add_entities: AddEntitiesCallback
28 ) -> None:
29  """Set up Tuya cameras dynamically through Tuya discovery."""
30  hass_data = entry.runtime_data
31 
32  @callback
33  def async_discover_device(device_ids: list[str]) -> None:
34  """Discover and add a discovered Tuya camera."""
35  entities: list[TuyaCameraEntity] = []
36  for device_id in device_ids:
37  device = hass_data.manager.device_map[device_id]
38  if device.category in CAMERAS:
39  entities.append(TuyaCameraEntity(device, hass_data.manager))
40 
41  async_add_entities(entities)
42 
43  async_discover_device([*hass_data.manager.device_map])
44 
45  entry.async_on_unload(
46  async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
47  )
48 
49 
50 class TuyaCameraEntity(TuyaEntity, CameraEntity):
51  """Tuya Camera Entity."""
52 
53  _attr_supported_features = CameraEntityFeature.STREAM
54  _attr_brand = "Tuya"
55  _attr_name = None
56 
57  def __init__(
58  self,
59  device: CustomerDevice,
60  device_manager: Manager,
61  ) -> None:
62  """Init Tuya Camera."""
63  super().__init__(device, device_manager)
64  CameraEntity.__init__(self)
65  self._attr_model_attr_model = device.product_name
66 
67  @property
68  def is_recording(self) -> bool:
69  """Return true if the device is recording."""
70  return self.devicedevice.status.get(DPCode.RECORD_SWITCH, False)
71 
72  @property
73  def motion_detection_enabled(self) -> bool:
74  """Return the camera motion detection status."""
75  return self.devicedevice.status.get(DPCode.MOTION_SWITCH, False)
76 
77  async def stream_source(self) -> str | None:
78  """Return the source of the stream."""
79  return await self.hasshass.async_add_executor_job(
80  self.device_managerdevice_manager.get_device_stream_allocate,
81  self.devicedevice.id,
82  "rtsp",
83  )
84 
85  async def async_camera_image(
86  self, width: int | None = None, height: int | None = None
87  ) -> bytes | None:
88  """Return a still image response from the camera."""
89  stream_source = await self.stream_sourcestream_source()
90  if not stream_source:
91  return None
92  return await ffmpeg.async_get_image(
93  self.hasshass,
94  stream_source,
95  width=width,
96  height=height,
97  )
98 
99  def enable_motion_detection(self) -> None:
100  """Enable motion detection in the camera."""
101  self._send_command_send_command([{"code": DPCode.MOTION_SWITCH, "value": True}])
102 
103  def disable_motion_detection(self) -> None:
104  """Disable motion detection in camera."""
105  self._send_command_send_command([{"code": DPCode.MOTION_SWITCH, "value": False}])
bytes|None async_camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:87
None __init__(self, CustomerDevice device, Manager device_manager)
Definition: camera.py:61
None _send_command(self, list[dict[str, Any]] commands)
Definition: entity.py:295
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, TuyaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:28
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103