Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for QVR Pro streams."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyqvrpro.client import QVRResponseError
8 
9 from homeassistant.components.camera import Camera
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from .const import DOMAIN, SHORT_NAME
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
20  hass: HomeAssistant,
21  config: ConfigType,
22  add_entities: AddEntitiesCallback,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> None:
25  """Set up the QVR Pro camera platform."""
26  if discovery_info is None:
27  return
28 
29  client = hass.data[DOMAIN]["client"]
30 
31  entities = []
32 
33  for channel in hass.data[DOMAIN]["channels"]:
34  stream_source = get_stream_source(channel["guid"], client)
35  entities.append(
36  QVRProCamera(**channel, stream_source=stream_source, client=client)
37  )
38 
39  add_entities(entities)
40 
41 
42 def get_stream_source(guid, client):
43  """Get channel stream source."""
44  try:
45  resp = client.get_channel_live_stream(guid, protocol="rtsp")
46  except QVRResponseError as ex:
47  _LOGGER.error(ex)
48  return None
49 
50  full_url = resp["resourceUris"]
51 
52  protocol = full_url[:7]
53  auth = f"{client.get_auth_string()}@"
54  url = full_url[7:]
55 
56  return f"{protocol}{auth}{url}"
57 
58 
60  """Representation of a QVR Pro camera."""
61 
62  def __init__(self, name, model, brand, channel_index, guid, stream_source, client):
63  """Init QVR Pro camera."""
64 
65  self._name_name = f"{SHORT_NAME} {name}"
66  self._model_model = model
67  self._brand_brand = brand
68  self.indexindex = channel_index
69  self.guidguid = guid
70  self._client_client = client
71  self._stream_source_stream_source = stream_source
72 
73  super().__init__()
74 
75  @property
76  def name(self):
77  """Return the name of the entity."""
78  return self._name_name
79 
80  @property
81  def model(self):
82  """Return the model of the entity."""
83  return self._model_model
84 
85  @property
86  def brand(self):
87  """Return the brand of the entity."""
88  return self._brand_brand
89 
90  @property
92  """Get the state attributes."""
93  return {"qvr_guid": self.guidguid}
94 
96  self, width: int | None = None, height: int | None = None
97  ) -> bytes | None:
98  """Get image bytes from camera."""
99  try:
100  return self._client_client.get_snapshot(self.guidguid)
101 
102  except QVRResponseError as ex:
103  _LOGGER.error("Error getting image: %s", ex)
104  self._client_client.connect()
105 
106  return self._client_client.get_snapshot(self.guidguid)
107 
108  async def stream_source(self):
109  """Get stream source."""
110  return self._stream_source_stream_source
bytes|None camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:97
def __init__(self, name, model, brand, channel_index, guid, stream_source, client)
Definition: camera.py:62
None add_entities(HomeAssistant hass, FreeboxRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
Definition: camera.py:54
def get_stream_source(guid, client)
Definition: camera.py:42
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: camera.py:24