2023-03-03 10:26:13 +00:00
|
|
|
"""Component providing support to the Ring Door Bell camera."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-11-09 00:01:20 +00:00
|
|
|
from datetime import timedelta
|
2020-01-14 20:54:45 +00:00
|
|
|
from itertools import chain
|
2019-03-21 05:56:46 +00:00
|
|
|
import logging
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2019-12-05 05:13:28 +00:00
|
|
|
from haffmpeg.camera import CameraMjpeg
|
2020-01-17 22:54:32 +00:00
|
|
|
import requests
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2021-08-11 01:31:11 +00:00
|
|
|
from homeassistant.components import ffmpeg
|
2020-01-10 20:35:31 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2017-10-21 14:08:40 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-12-05 05:13:28 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2022-08-26 19:40:28 +00:00
|
|
|
from . import DOMAIN
|
2020-01-15 16:10:42 +00:00
|
|
|
from .entity import RingEntityMixin
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-06-29 06:57:32 +00:00
|
|
|
FORCE_REFRESH_INTERVAL = timedelta(minutes=3)
|
2017-11-09 00:01:20 +00:00
|
|
|
|
2017-10-21 14:08:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-03 12:22:41 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Set up a Ring Door Bell and StickUp Camera."""
|
2020-01-15 16:10:42 +00:00
|
|
|
devices = hass.data[DOMAIN][config_entry.entry_id]["devices"]
|
2022-06-11 22:05:19 +00:00
|
|
|
ffmpeg_manager = ffmpeg.get_ffmpeg_manager(hass)
|
2017-10-21 14:08:40 +00:00
|
|
|
|
|
|
|
cams = []
|
2020-01-14 20:54:45 +00:00
|
|
|
for camera in chain(
|
|
|
|
devices["doorbots"], devices["authorized_doorbots"], devices["stickup_cams"]
|
|
|
|
):
|
2020-01-10 20:35:31 +00:00
|
|
|
if not camera.has_subscription:
|
|
|
|
continue
|
2017-11-25 11:15:12 +00:00
|
|
|
|
2022-06-11 22:05:19 +00:00
|
|
|
cams.append(RingCam(config_entry.entry_id, ffmpeg_manager, camera))
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2020-01-15 16:10:42 +00:00
|
|
|
async_add_entities(cams)
|
2017-10-21 14:08:40 +00:00
|
|
|
|
|
|
|
|
2020-01-15 16:10:42 +00:00
|
|
|
class RingCam(RingEntityMixin, Camera):
|
2017-10-21 14:08:40 +00:00
|
|
|
"""An implementation of a Ring Door Bell camera."""
|
|
|
|
|
2021-08-11 01:31:11 +00:00
|
|
|
def __init__(self, config_entry_id, ffmpeg_manager, device):
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Initialize a Ring Door Bell camera."""
|
2020-01-15 16:10:42 +00:00
|
|
|
super().__init__(config_entry_id, device)
|
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
self._name = self._device.name
|
2021-08-11 01:31:11 +00:00
|
|
|
self._ffmpeg_manager = ffmpeg_manager
|
2020-01-15 16:10:42 +00:00
|
|
|
self._last_event = None
|
2020-01-14 20:54:45 +00:00
|
|
|
self._last_video_id = None
|
|
|
|
self._video_url = None
|
2021-08-27 16:22:49 +00:00
|
|
|
self._image = None
|
2020-01-19 21:18:11 +00:00
|
|
|
self._expires_at = dt_util.utcnow() - FORCE_REFRESH_INTERVAL
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2022-09-06 07:47:35 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-07-31 18:08:40 +00:00
|
|
|
"""Register callbacks."""
|
2020-01-15 16:10:42 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
await self.ring_objects["history_data"].async_track_device(
|
|
|
|
self._device, self._history_update_callback
|
2020-01-10 20:35:31 +00:00
|
|
|
)
|
|
|
|
|
2022-09-06 07:47:35 +00:00
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
2020-01-10 20:35:31 +00:00
|
|
|
"""Disconnect callbacks."""
|
2020-01-15 16:10:42 +00:00
|
|
|
await super().async_will_remove_from_hass()
|
|
|
|
|
|
|
|
self.ring_objects["history_data"].async_untrack_device(
|
|
|
|
self._device, self._history_update_callback
|
|
|
|
)
|
2019-07-31 18:08:40 +00:00
|
|
|
|
|
|
|
@callback
|
2020-01-15 16:10:42 +00:00
|
|
|
def _history_update_callback(self, history_data):
|
2019-07-31 18:08:40 +00:00
|
|
|
"""Call update method."""
|
2020-01-15 16:10:42 +00:00
|
|
|
if history_data:
|
|
|
|
self._last_event = history_data[0]
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
else:
|
|
|
|
self._last_event = None
|
|
|
|
self._last_video_id = None
|
|
|
|
self._video_url = None
|
2021-08-27 16:22:49 +00:00
|
|
|
self._image = None
|
2020-01-19 21:18:11 +00:00
|
|
|
self._expires_at = dt_util.utcnow()
|
2020-01-15 16:10:42 +00:00
|
|
|
self.async_write_ha_state()
|
2019-07-31 18:08:40 +00:00
|
|
|
|
2017-10-21 14:08:40 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|
|
|
|
|
2018-10-16 08:06:00 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
2020-01-14 20:54:45 +00:00
|
|
|
return self._device.id
|
2018-10-16 08:06:00 +00:00
|
|
|
|
2017-10-21 14:08:40 +00:00
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"video_url": self._video_url,
|
|
|
|
"last_video_id": self._last_video_id,
|
2017-10-21 14:08:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2021-08-27 16:22:49 +00:00
|
|
|
if self._image is None and self._video_url:
|
|
|
|
image = await ffmpeg.async_get_image(
|
|
|
|
self.hass,
|
|
|
|
self._video_url,
|
|
|
|
width=width,
|
|
|
|
height=height,
|
|
|
|
)
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2021-08-27 16:22:49 +00:00
|
|
|
if image:
|
|
|
|
self._image = image
|
|
|
|
|
|
|
|
return self._image
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def handle_async_mjpeg_stream(self, request):
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
|
|
|
if self._video_url is None:
|
|
|
|
return
|
|
|
|
|
2021-08-11 01:31:11 +00:00
|
|
|
stream = CameraMjpeg(self._ffmpeg_manager.binary)
|
2020-01-10 20:35:31 +00:00
|
|
|
await stream.open_camera(self._video_url)
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2018-11-01 08:28:23 +00:00
|
|
|
try:
|
2019-03-27 06:55:05 +00:00
|
|
|
stream_reader = await stream.get_reader()
|
2018-11-01 08:28:23 +00:00
|
|
|
return await async_aiohttp_proxy_stream(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
request,
|
|
|
|
stream_reader,
|
2021-08-11 01:31:11 +00:00
|
|
|
self._ffmpeg_manager.ffmpeg_stream_content_type,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-11-01 08:28:23 +00:00
|
|
|
finally:
|
|
|
|
await stream.close()
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2022-09-06 07:47:35 +00:00
|
|
|
async def async_update(self) -> None:
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Update camera entity and refresh attributes."""
|
2020-01-15 16:10:42 +00:00
|
|
|
if self._last_event is None:
|
2019-03-29 19:10:00 +00:00
|
|
|
return
|
|
|
|
|
2020-01-15 16:10:42 +00:00
|
|
|
if self._last_event["recording"]["status"] != "ready":
|
|
|
|
return
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2020-01-19 21:18:11 +00:00
|
|
|
utcnow = dt_util.utcnow()
|
|
|
|
if self._last_video_id == self._last_event["id"] and utcnow <= self._expires_at:
|
2020-01-15 16:10:42 +00:00
|
|
|
return
|
2017-11-09 00:01:20 +00:00
|
|
|
|
2021-08-27 16:22:49 +00:00
|
|
|
if self._last_video_id != self._last_event["id"]:
|
|
|
|
self._image = None
|
|
|
|
|
2020-01-17 22:54:32 +00:00
|
|
|
try:
|
|
|
|
video_url = await self.hass.async_add_executor_job(
|
|
|
|
self._device.recording_url, self._last_event["id"]
|
|
|
|
)
|
|
|
|
except requests.Timeout:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Time out fetching recording url for camera %s", self.entity_id
|
|
|
|
)
|
|
|
|
video_url = None
|
2017-11-09 00:01:20 +00:00
|
|
|
|
2020-01-15 16:10:42 +00:00
|
|
|
if video_url:
|
|
|
|
self._last_video_id = self._last_event["id"]
|
|
|
|
self._video_url = video_url
|
2020-01-19 21:18:11 +00:00
|
|
|
self._expires_at = FORCE_REFRESH_INTERVAL + utcnow
|