2019-04-03 15:40:03 +00:00
|
|
|
"""This component provides support to the Ring Door Bell camera."""
|
2017-10-21 14:08:40 +00:00
|
|
|
import asyncio
|
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
|
|
|
|
from haffmpeg.tools import IMAGE_JPEG, ImageFrame
|
2020-01-17 22:54:32 +00:00
|
|
|
import requests
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2017-10-21 14:08:40 +00:00
|
|
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
2019-07-31 18:08:40 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2019-12-05 05:13:28 +00:00
|
|
|
from homeassistant.core import callback
|
2017-10-21 14:08:40 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2019-12-05 05:13:28 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2020-01-15 16:10:42 +00:00
|
|
|
from . import ATTRIBUTION, DOMAIN
|
|
|
|
from .entity import RingEntityMixin
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-11-09 00:01:20 +00:00
|
|
|
FORCE_REFRESH_INTERVAL = timedelta(minutes=45)
|
|
|
|
|
2017-10-21 14:08:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
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"]
|
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
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
cams.append(RingCam(config_entry.entry_id, hass.data[DATA_FFMPEG], 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."""
|
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
def __init__(self, config_entry_id, ffmpeg, 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
|
|
|
|
self._ffmpeg = ffmpeg
|
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
|
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
|
|
|
|
2019-07-31 18:08:40 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""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
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""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
|
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
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
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
|
|
|
}
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_camera_image(self):
|
2017-10-21 14:08:40 +00:00
|
|
|
"""Return a still image response from the camera."""
|
|
|
|
ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
|
|
|
|
|
|
|
|
if self._video_url is None:
|
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
image = await asyncio.shield(
|
2020-08-27 11:56:20 +00:00
|
|
|
ffmpeg.get_image(
|
|
|
|
self._video_url,
|
|
|
|
output_format=IMAGE_JPEG,
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-10-21 14:08:40 +00:00
|
|
|
return image
|
|
|
|
|
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
|
|
|
|
|
|
|
|
stream = CameraMjpeg(self._ffmpeg.binary, loop=self.hass.loop)
|
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,
|
|
|
|
self._ffmpeg.ffmpeg_stream_content_type,
|
|
|
|
)
|
2018-11-01 08:28:23 +00:00
|
|
|
finally:
|
|
|
|
await stream.close()
|
2017-10-21 14:08:40 +00:00
|
|
|
|
2020-01-14 20:54:45 +00:00
|
|
|
async def async_update(self):
|
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
|
|
|
|
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
|