2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Canary camera."""
|
2018-02-05 13:02:43 +00:00
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
2020-09-19 04:22:19 +00:00
|
|
|
from typing import Callable, List
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2019-10-18 23:01:59 +00:00
|
|
|
from haffmpeg.camera import CameraMjpeg
|
|
|
|
from haffmpeg.tools import IMAGE_JPEG, ImageFrame
|
2018-02-05 13:02:43 +00:00
|
|
|
import voluptuous as vol
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
|
2018-02-05 13:02:43 +00:00
|
|
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-02-05 13:02:43 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2020-10-01 08:26:26 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2018-02-05 13:02:43 +00:00
|
|
|
from homeassistant.util import Throttle
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_FFMPEG_ARGUMENTS,
|
2020-10-01 08:26:26 +00:00
|
|
|
DATA_COORDINATOR,
|
2020-09-19 04:22:19 +00:00
|
|
|
DEFAULT_FFMPEG_ARGUMENTS,
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
DOMAIN,
|
2020-09-19 15:09:40 +00:00
|
|
|
MANUFACTURER,
|
2020-09-19 04:22:19 +00:00
|
|
|
)
|
2020-10-01 08:26:26 +00:00
|
|
|
from .coordinator import CanaryDataUpdateCoordinator
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-02-05 13:02:43 +00:00
|
|
|
MIN_TIME_BETWEEN_SESSION_RENEW = timedelta(seconds=90)
|
|
|
|
|
2020-09-20 22:38:02 +00:00
|
|
|
PLATFORM_SCHEMA = vol.All(
|
|
|
|
cv.deprecated(CONF_FFMPEG_ARGUMENTS, invalidation_version="0.118"),
|
|
|
|
PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_FFMPEG_ARGUMENTS, default=DEFAULT_FFMPEG_ARGUMENTS
|
|
|
|
): cv.string
|
|
|
|
}
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], bool], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up Canary sensors based on a config entry."""
|
2020-10-01 08:26:26 +00:00
|
|
|
coordinator: CanaryDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
DATA_COORDINATOR
|
|
|
|
]
|
2020-09-19 04:22:19 +00:00
|
|
|
ffmpeg_arguments = entry.options.get(
|
|
|
|
CONF_FFMPEG_ARGUMENTS, DEFAULT_FFMPEG_ARGUMENTS
|
|
|
|
)
|
|
|
|
cameras = []
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
for location_id, location in coordinator.data["locations"].items():
|
2018-02-05 13:02:43 +00:00
|
|
|
for device in location.devices:
|
|
|
|
if device.is_online:
|
2020-09-19 04:22:19 +00:00
|
|
|
cameras.append(
|
2019-07-31 19:25:30 +00:00
|
|
|
CanaryCamera(
|
|
|
|
hass,
|
2020-10-01 08:26:26 +00:00
|
|
|
coordinator,
|
|
|
|
location_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
device,
|
|
|
|
DEFAULT_TIMEOUT,
|
2020-09-19 04:22:19 +00:00
|
|
|
ffmpeg_arguments,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
async_add_entities(cameras, True)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
class CanaryCamera(CoordinatorEntity, Camera):
|
2017-12-08 09:40:45 +00:00
|
|
|
"""An implementation of a Canary security camera."""
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
def __init__(self, hass, coordinator, location_id, device, timeout, ffmpeg_args):
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Initialize a Canary security camera."""
|
2020-10-01 08:26:26 +00:00
|
|
|
super().__init__(coordinator)
|
2020-10-31 21:05:59 +00:00
|
|
|
Camera.__init__(self)
|
2018-02-05 13:02:43 +00:00
|
|
|
self._ffmpeg = hass.data[DATA_FFMPEG]
|
|
|
|
self._ffmpeg_arguments = ffmpeg_args
|
2020-10-01 08:26:26 +00:00
|
|
|
self._location_id = location_id
|
2018-02-05 13:02:43 +00:00
|
|
|
self._device = device
|
2020-09-19 15:09:40 +00:00
|
|
|
self._device_id = device.device_id
|
|
|
|
self._device_name = device.name
|
|
|
|
self._device_type_name = device.device_type["name"]
|
2017-12-08 09:40:45 +00:00
|
|
|
self._timeout = timeout
|
2018-02-05 13:02:43 +00:00
|
|
|
self._live_stream_session = None
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
"""Return information about the location."""
|
|
|
|
return self.coordinator.data["locations"][self._location_id]
|
|
|
|
|
2017-12-08 09:40:45 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this device."""
|
2020-09-19 15:09:40 +00:00
|
|
|
return self._device_name
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-14 15:41:05 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique ID of this camera."""
|
2020-09-19 15:09:40 +00:00
|
|
|
return str(self._device_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device_info of the device."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, str(self._device_id))},
|
|
|
|
"name": self._device_name,
|
|
|
|
"model": self._device_type_name,
|
|
|
|
"manufacturer": MANUFACTURER,
|
|
|
|
}
|
2020-09-14 15:41:05 +00:00
|
|
|
|
2017-12-08 09:40:45 +00:00
|
|
|
@property
|
|
|
|
def is_recording(self):
|
|
|
|
"""Return true if the device is recording."""
|
2020-10-01 08:26:26 +00:00
|
|
|
return self.location.is_recording
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def motion_detection_enabled(self):
|
|
|
|
"""Return the camera motion detection status."""
|
2020-10-01 08:26:26 +00:00
|
|
|
return not self.location.is_recording
|
2018-02-05 13:02:43 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_camera_image(self):
|
2018-02-05 13:02:43 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2020-05-02 04:01:29 +00:00
|
|
|
await self.hass.async_add_executor_job(self.renew_live_stream_session)
|
2018-02-05 13:02:43 +00:00
|
|
|
|
|
|
|
ffmpeg = ImageFrame(self._ffmpeg.binary, loop=self.hass.loop)
|
2019-07-31 19:25:30 +00:00
|
|
|
image = await asyncio.shield(
|
|
|
|
ffmpeg.get_image(
|
|
|
|
self._live_stream_session.live_stream_url,
|
|
|
|
output_format=IMAGE_JPEG,
|
|
|
|
extra_cmd=self._ffmpeg_arguments,
|
|
|
|
)
|
|
|
|
)
|
2018-02-05 13:02:43 +00:00
|
|
|
return image
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def handle_async_mjpeg_stream(self, request):
|
2018-02-05 13:02:43 +00:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
|
|
|
if self._live_stream_session is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
stream = CameraMjpeg(self._ffmpeg.binary, loop=self.hass.loop)
|
2018-10-01 06:50:05 +00:00
|
|
|
await stream.open_camera(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._live_stream_session.live_stream_url, extra_cmd=self._ffmpeg_arguments
|
|
|
|
)
|
2018-02-05 13:02:43 +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()
|
2018-02-05 13:02:43 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_SESSION_RENEW)
|
|
|
|
def renew_live_stream_session(self):
|
|
|
|
"""Renew live stream session."""
|
2020-10-01 08:26:26 +00:00
|
|
|
self._live_stream_session = self.coordinator.canary.get_live_stream_session(
|
|
|
|
self._device
|
|
|
|
)
|