Home Assistant Unofficial Reference 2024.12.1
camera.py
Go to the documentation of this file.
1 """Support for Prosegur cameras."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyprosegur.auth import Auth
8 from pyprosegur.exceptions import ProsegurException
9 from pyprosegur.installation import Camera as InstallationCamera, Installation
10 
11 from homeassistant.components.camera import Camera
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.device_registry import DeviceInfo
16  AddEntitiesCallback,
17  async_get_current_platform,
18 )
19 
20 from . import DOMAIN
21 from .const import SERVICE_REQUEST_IMAGE
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
27  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
28 ) -> None:
29  """Set up the Prosegur camera platform."""
30 
31  platform = async_get_current_platform()
32  platform.async_register_entity_service(
33  SERVICE_REQUEST_IMAGE,
34  None,
35  "async_request_image",
36  )
37 
38  _installation = await Installation.retrieve(
39  hass.data[DOMAIN][entry.entry_id], entry.data["contract"]
40  )
41 
43  [
44  ProsegurCamera(_installation, camera, hass.data[DOMAIN][entry.entry_id])
45  for camera in _installation.cameras
46  ],
47  update_before_add=True,
48  )
49 
50 
52  """Representation of a Smart Prosegur Camera."""
53 
54  _attr_has_entity_name = True
55 
56  def __init__(
57  self, installation: Installation, camera: InstallationCamera, auth: Auth
58  ) -> None:
59  """Initialize Prosegur Camera component."""
60  Camera.__init__(self)
61 
62  self._installation_installation = installation
63  self._camera_camera = camera
64  self._auth_auth = auth
65  self._attr_unique_id_attr_unique_id = f"{installation.contract} {camera.id}"
66  self._attr_name_attr_name = camera.description
67 
68  self._attr_device_info_attr_device_info = DeviceInfo(
69  name=f"Contract {installation.contract}",
70  manufacturer="Prosegur",
71  model="smart",
72  identifiers={(DOMAIN, installation.contract)},
73  configuration_url="https://smart.prosegur.com",
74  )
75 
76  async def async_camera_image(
77  self, width: int | None = None, height: int | None = None
78  ) -> bytes | None:
79  """Return bytes of camera image."""
80 
81  _LOGGER.debug("Get image for %s", self._camera_camera.description)
82  try:
83  return await self._installation_installation.get_image(self._auth_auth, self._camera_camera.id)
84 
85  except ProsegurException as err:
86  _LOGGER.error("Image %s doesn't exist: %s", self._camera_camera.description, err)
87 
88  return None
89 
90  async def async_request_image(self):
91  """Request new image from the camera."""
92 
93  _LOGGER.debug("Request image for %s", self._camera_camera.description)
94  try:
95  await self._installation_installation.request_image(self._auth_auth, self._camera_camera.id)
96 
97  except ProsegurException as err:
98  _LOGGER.error(
99  "Could not request image from camera %s: %s",
100  self._camera_camera.description,
101  err,
102  )
None __init__(self, Installation installation, InstallationCamera camera, Auth auth)
Definition: camera.py:58
bytes|None async_camera_image(self, int|None width=None, int|None height=None)
Definition: camera.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: camera.py:28