2019-04-03 17:40:03 +02:00
|
|
|
"""This component provides support to the Ring Door Bell camera."""
|
2017-10-21 10:08:40 -04:00
|
|
|
import asyncio
|
2017-11-08 19:01:20 -05:00
|
|
|
from datetime import timedelta
|
2019-03-20 22:56:46 -07:00
|
|
|
import logging
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-12-05 06:13:28 +01:00
|
|
|
from haffmpeg.camera import CameraMjpeg
|
|
|
|
from haffmpeg.tools import IMAGE_JPEG, ImageFrame
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
from homeassistant.components.camera import Camera
|
2017-10-21 10:08:40 -04:00
|
|
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
2019-07-31 19:08:40 +01:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2019-12-05 06:13:28 +01:00
|
|
|
from homeassistant.core import callback
|
2017-10-21 10:08:40 -04:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2019-07-31 19:08:40 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-12-05 06:13:28 +01:00
|
|
|
from homeassistant.util import dt as dt_util
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
from . import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
DATA_RING_DOORBELLS,
|
|
|
|
DATA_RING_STICKUP_CAMS,
|
2020-01-11 16:04:39 -08:00
|
|
|
DOMAIN,
|
2019-07-31 12:25:30 -07:00
|
|
|
SIGNAL_UPDATE_RING,
|
|
|
|
)
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-11-08 19:01:20 -05:00
|
|
|
FORCE_REFRESH_INTERVAL = timedelta(minutes=45)
|
|
|
|
|
2017-10-21 10:08:40 -04:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2017-10-21 10:08:40 -04:00
|
|
|
"""Set up a Ring Door Bell and StickUp Camera."""
|
2019-07-31 19:08:40 +01:00
|
|
|
ring_doorbell = hass.data[DATA_RING_DOORBELLS]
|
|
|
|
ring_stickup_cams = hass.data[DATA_RING_STICKUP_CAMS]
|
2017-10-21 10:08:40 -04:00
|
|
|
|
|
|
|
cams = []
|
2019-07-31 19:08:40 +01:00
|
|
|
for camera in ring_doorbell + ring_stickup_cams:
|
2020-01-10 21:35:31 +01:00
|
|
|
if not camera.has_subscription:
|
|
|
|
continue
|
2017-11-25 06:15:12 -05:00
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
camera = await hass.async_add_executor_job(RingCam, hass, camera)
|
|
|
|
cams.append(camera)
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
async_add_entities(cams, True)
|
2017-10-21 10:08:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
class RingCam(Camera):
|
|
|
|
"""An implementation of a Ring Door Bell camera."""
|
|
|
|
|
2020-01-10 21:35:31 +01:00
|
|
|
def __init__(self, hass, camera):
|
2017-10-21 10:08:40 -04:00
|
|
|
"""Initialize a Ring Door Bell camera."""
|
2019-09-25 00:38:20 +02:00
|
|
|
super().__init__()
|
2017-10-21 10:08:40 -04:00
|
|
|
self._camera = camera
|
|
|
|
self._hass = hass
|
|
|
|
self._name = self._camera.name
|
|
|
|
self._ffmpeg = hass.data[DATA_FFMPEG]
|
|
|
|
self._last_video_id = self._camera.last_recording_id
|
|
|
|
self._video_url = self._camera.recording_url(self._last_video_id)
|
2017-11-08 19:01:20 -05:00
|
|
|
self._utcnow = dt_util.utcnow()
|
|
|
|
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = None
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 19:08:40 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-01-10 21:35:31 +01:00
|
|
|
self._disp_disconnect = async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_RING, self._update_callback
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Disconnect callbacks."""
|
|
|
|
if self._disp_disconnect:
|
|
|
|
self._disp_disconnect()
|
|
|
|
self._disp_disconnect = None
|
2019-07-31 19:08:40 +01:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
_LOGGER.debug("Updating Ring camera %s (callback)", self.name)
|
|
|
|
|
2017-10-21 10:08:40 -04:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|
|
|
|
|
2018-10-16 01:06:00 -07:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._camera.id
|
|
|
|
|
2020-01-11 16:04:39 -08:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self._camera.id)},
|
|
|
|
"sw_version": self._camera.firmware,
|
|
|
|
"name": self._camera.name,
|
|
|
|
"model": self._camera.kind,
|
|
|
|
"manufacturer": "Ring",
|
|
|
|
}
|
|
|
|
|
2017-10-21 10:08:40 -04:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
2019-02-14 22:09:22 +01:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2019-07-31 12:25:30 -07:00
|
|
|
"timezone": self._camera.timezone,
|
|
|
|
"video_url": self._video_url,
|
|
|
|
"last_video_id": self._last_video_id,
|
2017-10-21 10:08:40 -04:00
|
|
|
}
|
|
|
|
|
2018-10-01 08:50:05 +02:00
|
|
|
async def async_camera_image(self):
|
2017-10-21 10:08:40 -04:00
|
|
|
"""Return a still image response from the camera."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2017-10-21 10:08:40 -04:00
|
|
|
ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
|
|
|
|
|
|
|
|
if self._video_url is None:
|
|
|
|
return
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
image = await asyncio.shield(
|
2020-01-10 21:35:31 +01:00
|
|
|
ffmpeg.get_image(self._video_url, output_format=IMAGE_JPEG,)
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2017-10-21 10:08:40 -04:00
|
|
|
return image
|
|
|
|
|
2018-10-01 08:50:05 +02:00
|
|
|
async def handle_async_mjpeg_stream(self, request):
|
2017-10-21 10:08:40 -04: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 21:35:31 +01:00
|
|
|
await stream.open_camera(self._video_url)
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2018-11-01 01:28:23 -07:00
|
|
|
try:
|
2019-03-27 07:55:05 +01:00
|
|
|
stream_reader = await stream.get_reader()
|
2018-11-01 01:28:23 -07:00
|
|
|
return await async_aiohttp_proxy_stream(
|
2019-07-31 12:25:30 -07:00
|
|
|
self.hass,
|
|
|
|
request,
|
|
|
|
stream_reader,
|
|
|
|
self._ffmpeg.ffmpeg_stream_content_type,
|
|
|
|
)
|
2018-11-01 01:28:23 -07:00
|
|
|
finally:
|
|
|
|
await stream.close()
|
2017-10-21 10:08:40 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2019-07-31 22:21:36 +03:00
|
|
|
"""Return False, updates are controlled via the hub."""
|
2019-07-31 19:08:40 +01:00
|
|
|
return False
|
2017-10-21 10:08:40 -04:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update camera entity and refresh attributes."""
|
2017-11-08 19:01:20 -05:00
|
|
|
_LOGGER.debug("Checking if Ring DoorBell needs to refresh video_url")
|
2017-10-21 10:08:40 -04:00
|
|
|
|
|
|
|
self._utcnow = dt_util.utcnow()
|
|
|
|
|
2019-03-29 21:10:00 +02:00
|
|
|
try:
|
|
|
|
last_event = self._camera.history(limit=1)[0]
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
return
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
last_recording_id = last_event["id"]
|
|
|
|
video_status = last_event["recording"]["status"]
|
2017-10-21 10:08:40 -04:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
if video_status == "ready" and (
|
|
|
|
self._last_video_id != last_recording_id or self._utcnow >= self._expires_at
|
|
|
|
):
|
2017-11-08 19:01:20 -05:00
|
|
|
|
2019-03-29 21:10:00 +02:00
|
|
|
video_url = self._camera.recording_url(last_recording_id)
|
|
|
|
if video_url:
|
|
|
|
_LOGGER.info("Ring DoorBell properties refreshed")
|
2017-11-08 19:01:20 -05:00
|
|
|
|
2019-03-29 21:10:00 +02:00
|
|
|
# update attributes if new video or if URL has expired
|
|
|
|
self._last_video_id = last_recording_id
|
|
|
|
self._video_url = video_url
|
|
|
|
self._expires_at = FORCE_REFRESH_INTERVAL + self._utcnow
|