Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Demo camera platform that has a fake camera."""
2 
3 from __future__ import annotations
4 
5 from pathlib import Path
6 
7 from homeassistant.components.camera import Camera, CameraEntityFeature
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 
14  hass: HomeAssistant,
15  config_entry: ConfigEntry,
16  async_add_entities: AddEntitiesCallback,
17 ) -> None:
18  """Set up the Demo config entry."""
20  [
21  DemoCamera("Demo camera", "image/jpg"),
22  DemoCamera("Demo camera png", "image/png"),
23  DemoCameraWithoutStream("Demo camera without stream", "image/jpg"),
24  ]
25  )
26 
27 
29  """The representation of a Demo camera."""
30 
31  _attr_is_streaming = True
32  _attr_motion_detection_enabled = False
33  _attr_supported_features = CameraEntityFeature.ON_OFF | CameraEntityFeature.STREAM
34 
35  def __init__(self, name: str, content_type: str) -> None:
36  """Initialize demo camera component."""
37  super().__init__()
38  self._attr_name_attr_name = name
39  self.content_typecontent_type = content_type
40  self._images_index_images_index = 0
41 
42  async def async_camera_image(
43  self, width: int | None = None, height: int | None = None
44  ) -> bytes:
45  """Return a faked still image response."""
46  self._images_index_images_index = (self._images_index_images_index + 1) % 4
47  ext = "jpg" if self.content_typecontent_type == "image/jpg" else "png"
48  image_path = Path(__file__).parent / f"demo_{self._images_index}.{ext}"
49 
50  return await self.hasshass.async_add_executor_job(image_path.read_bytes)
51 
52  async def async_enable_motion_detection(self) -> None:
53  """Enable the Motion detection in base station (Arm)."""
54  self._attr_motion_detection_enabled_attr_motion_detection_enabled_attr_motion_detection_enabled = True
55  self.async_write_ha_stateasync_write_ha_stateasync_write_ha_state()
56 
57  async def async_disable_motion_detection(self) -> None:
58  """Disable the motion detection in base station (Disarm)."""
59  self._attr_motion_detection_enabled_attr_motion_detection_enabled_attr_motion_detection_enabled = False
60  self.async_write_ha_stateasync_write_ha_stateasync_write_ha_state()
61 
62  async def async_turn_off(self) -> None:
63  """Turn off camera."""
64  self._attr_is_streaming_attr_is_streaming_attr_is_streaming = False
65  self._attr_is_on_attr_is_on = False
66  self.async_write_ha_stateasync_write_ha_stateasync_write_ha_state()
67 
68  async def async_turn_on(self) -> None:
69  """Turn on camera."""
70  self._attr_is_streaming_attr_is_streaming_attr_is_streaming = True
71  self._attr_is_on_attr_is_on = True
72  self.async_write_ha_stateasync_write_ha_stateasync_write_ha_state()
73 
74 
76  """The representation of a Demo camera without stream."""
77 
78  _attr_supported_features = CameraEntityFeature.ON_OFF
bytes async_camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:44
None __init__(self, str name, str content_type)
Definition: camera.py:35
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:17