Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for Freebox cameras."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.camera import CameraEntityFeature
10  CONF_EXTRA_ARGUMENTS,
11  CONF_INPUT,
12  DEFAULT_ARGUMENTS,
13  FFmpegCamera,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import CONF_NAME
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers import entity_platform
19 from homeassistant.helpers.dispatcher import async_dispatcher_connect
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import ATTR_DETECTION, DOMAIN, FreeboxHomeCategory
23 from .entity import FreeboxHomeEntity
24 from .router import FreeboxRouter
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 
30  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
31 ) -> None:
32  """Set up cameras."""
33  router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id]
34  tracked: set[str] = set()
35 
36  @callback
37  def update_callback() -> None:
38  add_entities(hass, router, async_add_entities, tracked)
39 
40  router.listeners.append(
41  async_dispatcher_connect(hass, router.signal_home_device_new, update_callback)
42  )
43  update_callback()
44 
45  entity_platform.async_get_current_platform()
46 
47 
48 @callback
50  hass: HomeAssistant,
51  router: FreeboxRouter,
52  async_add_entities: AddEntitiesCallback,
53  tracked: set[str],
54 ) -> None:
55  """Add new cameras from the router."""
56  new_tracked: list[FreeboxCamera] = []
57 
58  for nodeid, node in router.home_devices.items():
59  if (node["category"] != FreeboxHomeCategory.CAMERA) or (nodeid in tracked):
60  continue
61  new_tracked.append(FreeboxCamera(hass, router, node))
62  tracked.add(nodeid)
63 
64  if new_tracked:
65  async_add_entities(new_tracked, True)
66 
67 
69  """Representation of a Freebox camera."""
70 
71  def __init__(
72  self, hass: HomeAssistant, router: FreeboxRouter, node: dict[str, Any]
73  ) -> None:
74  """Initialize a camera."""
75 
76  super().__init__(hass, router, node)
77  device_info = {
78  CONF_NAME: node["label"].strip(),
79  CONF_INPUT: node["props"]["Stream"],
80  CONF_EXTRA_ARGUMENTS: DEFAULT_ARGUMENTS,
81  }
82  FFmpegCamera.__init__(self, hass, device_info)
83 
84  self._supported_features_supported_features = (
85  CameraEntityFeature.ON_OFF | CameraEntityFeature.STREAM
86  )
87 
88  self._command_motion_detection_command_motion_detection = self.get_command_idget_command_id(
89  node["type"]["endpoints"], "slot", ATTR_DETECTION
90  )
91  self._attr_extra_state_attributes_attr_extra_state_attributes = {}
92  self.update_nodeupdate_node(node)
93 
94  async def async_enable_motion_detection(self) -> None:
95  """Enable motion detection in the camera."""
96  if await self.set_home_endpoint_valueset_home_endpoint_value(self._command_motion_detection_command_motion_detection, True):
97  self._attr_motion_detection_enabled_attr_motion_detection_enabled = True
98 
99  async def async_disable_motion_detection(self) -> None:
100  """Disable motion detection in camera."""
101  if await self.set_home_endpoint_valueset_home_endpoint_value(self._command_motion_detection_command_motion_detection, False):
102  self._attr_motion_detection_enabled_attr_motion_detection_enabled = False
103 
104  async def async_update_signal(self) -> None:
105  """Update the camera node."""
106  self.update_nodeupdate_node(self._router_router.home_devices[self._id_id])
107  self.async_write_ha_stateasync_write_ha_stateasync_write_ha_state()
108 
109  def update_node(self, node: dict[str, Any]) -> None:
110  """Update params."""
111  self._name_name = node["label"].strip()
112 
113  # Get status
114  if self._node_node["status"] == "active":
115  self._attr_is_streaming_attr_is_streaming = True
116  else:
117  self._attr_is_streaming_attr_is_streaming = False
118 
119  # Parse all endpoints values
120  for endpoint in filter(
121  lambda x: (x["ep_type"] == "signal"), node["show_endpoints"]
122  ):
123  self._attr_extra_state_attributes_attr_extra_state_attributes[endpoint["name"]] = endpoint["value"]
124 
125  # Get motion detection status
126  self._attr_motion_detection_enabled_attr_motion_detection_enabled = self._attr_extra_state_attributes_attr_extra_state_attributes[
127  ATTR_DETECTION
128  ]
None update_node(self, dict[str, Any] node)
Definition: camera.py:109
None __init__(self, HomeAssistant hass, FreeboxRouter router, dict[str, Any] node)
Definition: camera.py:73
int|None get_command_id(self, nodes, str ep_type, str name)
Definition: entity.py:104
bool set_home_endpoint_value(self, int|None command_id, bool|None value=None)
Definition: entity.py:84
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:31
None add_entities(HomeAssistant hass, FreeboxRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
Definition: camera.py:54
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103