2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Netgear Arlo IP cameras."""
|
2017-05-31 07:25:25 +00:00
|
|
|
import logging
|
|
|
|
|
2017-06-30 06:46:22 +00:00
|
|
|
import voluptuous as vol
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2018-06-12 06:01:26 +00:00
|
|
|
from homeassistant.core import callback
|
2017-10-07 08:59:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-06-12 06:01:26 +00:00
|
|
|
from homeassistant.components.arlo import (
|
|
|
|
DEFAULT_BRAND, DATA_ARLO, SIGNAL_UPDATE_ARLO)
|
2017-06-30 06:46:22 +00:00
|
|
|
from homeassistant.components.camera import Camera, PLATFORM_SCHEMA
|
2017-05-31 07:25:25 +00:00
|
|
|
from homeassistant.components.ffmpeg import DATA_FFMPEG
|
2017-09-24 19:44:34 +00:00
|
|
|
from homeassistant.const import ATTR_BATTERY_LEVEL
|
2017-10-07 08:59:46 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2018-06-12 06:01:26 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-10-07 08:59:46 +00:00
|
|
|
ARLO_MODE_ARMED = 'armed'
|
|
|
|
ARLO_MODE_DISARMED = 'disarmed'
|
2017-10-09 09:35:05 +00:00
|
|
|
|
2017-09-24 19:44:34 +00:00
|
|
|
ATTR_BRIGHTNESS = 'brightness'
|
|
|
|
ATTR_FLIPPED = 'flipped'
|
|
|
|
ATTR_MIRRORED = 'mirrored'
|
2017-10-09 09:35:05 +00:00
|
|
|
ATTR_MOTION = 'motion_detection_sensitivity'
|
|
|
|
ATTR_POWERSAVE = 'power_save_mode'
|
2017-09-24 19:44:34 +00:00
|
|
|
ATTR_SIGNAL_STRENGTH = 'signal_strength'
|
|
|
|
ATTR_UNSEEN_VIDEOS = 'unseen_videos'
|
2017-11-15 22:33:50 +00:00
|
|
|
ATTR_LAST_REFRESH = 'last_refresh'
|
2017-09-24 19:44:34 +00:00
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
CONF_FFMPEG_ARGUMENTS = 'ffmpeg_arguments'
|
2017-09-24 19:44:34 +00:00
|
|
|
|
2017-10-07 08:59:46 +00:00
|
|
|
DEPENDENCIES = ['arlo', 'ffmpeg']
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2017-09-24 19:44:34 +00:00
|
|
|
POWERSAVE_MODE_MAPPING = {
|
|
|
|
1: 'best_battery_life',
|
|
|
|
2: 'optimized',
|
|
|
|
3: 'best_video'
|
|
|
|
}
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2018-06-12 06:01:26 +00:00
|
|
|
vol.Optional(CONF_FFMPEG_ARGUMENTS): cv.string,
|
2017-05-31 07:25:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-31 07:25:25 +00:00
|
|
|
"""Set up an Arlo IP Camera."""
|
2018-06-12 06:01:26 +00:00
|
|
|
arlo = hass.data[DATA_ARLO]
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
cameras = []
|
|
|
|
for camera in arlo.cameras:
|
|
|
|
cameras.append(ArloCam(hass, camera, config))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(cameras)
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ArloCam(Camera):
|
|
|
|
"""An implementation of a Netgear Arlo IP camera."""
|
|
|
|
|
|
|
|
def __init__(self, hass, camera, device_info):
|
|
|
|
"""Initialize an Arlo camera."""
|
|
|
|
super().__init__()
|
|
|
|
self._camera = camera
|
|
|
|
self._name = self._camera.name
|
2017-07-01 04:06:56 +00:00
|
|
|
self._motion_status = False
|
2017-05-31 07:25:25 +00:00
|
|
|
self._ffmpeg = hass.data[DATA_FFMPEG]
|
|
|
|
self._ffmpeg_arguments = device_info.get(CONF_FFMPEG_ARGUMENTS)
|
2017-11-15 22:33:50 +00:00
|
|
|
self._last_refresh = None
|
2017-10-09 09:35:05 +00:00
|
|
|
self.attrs = {}
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
def camera_image(self):
|
2017-06-30 06:46:22 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2018-06-12 06:01:26 +00:00
|
|
|
return self._camera.last_image_from_cache
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_UPDATE_ARLO, self._update_callback)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-31 07:25:25 +00:00
|
|
|
|
2018-06-12 06:01:26 +00:00
|
|
|
async def handle_async_mjpeg_stream(self, request):
|
2017-05-31 07:25:25 +00:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
|
|
|
from haffmpeg import CameraMjpeg
|
|
|
|
video = self._camera.last_video
|
|
|
|
if not video:
|
2018-06-12 06:01:26 +00:00
|
|
|
error_msg = \
|
|
|
|
'Video not found for {0}. Is it older than {1} days?'.format(
|
|
|
|
self.name, self._camera.min_days_vdo_cache)
|
|
|
|
_LOGGER.error(error_msg)
|
2017-05-31 07:25:25 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
stream = CameraMjpeg(self._ffmpeg.binary, loop=self.hass.loop)
|
2018-06-12 06:01:26 +00:00
|
|
|
await stream.open_camera(
|
2017-05-31 07:25:25 +00:00
|
|
|
video.video_url, extra_cmd=self._ffmpeg_arguments)
|
|
|
|
|
2018-11-01 08:28:23 +00:00
|
|
|
try:
|
|
|
|
return await async_aiohttp_proxy_stream(
|
|
|
|
self.hass, request, stream,
|
2019-02-04 17:57:22 +00:00
|
|
|
self._ffmpeg.ffmpeg_stream_content_type)
|
2018-11-01 08:28:23 +00:00
|
|
|
finally:
|
|
|
|
await stream.close()
|
2017-05-31 07:25:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|
|
|
|
|
2017-09-24 19:44:34 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
2017-11-15 22:33:50 +00:00
|
|
|
name: value for name, value in (
|
|
|
|
(ATTR_BATTERY_LEVEL, self._camera.battery_level),
|
|
|
|
(ATTR_BRIGHTNESS, self._camera.brightness),
|
|
|
|
(ATTR_FLIPPED, self._camera.flip_state),
|
|
|
|
(ATTR_MIRRORED, self._camera.mirror_state),
|
|
|
|
(ATTR_MOTION, self._camera.motion_detection_sensitivity),
|
|
|
|
(ATTR_POWERSAVE, POWERSAVE_MODE_MAPPING.get(
|
|
|
|
self._camera.powersave_mode)),
|
|
|
|
(ATTR_SIGNAL_STRENGTH, self._camera.signal_strength),
|
|
|
|
(ATTR_UNSEEN_VIDEOS, self._camera.unseen_videos),
|
|
|
|
) if value is not None
|
2017-09-24 19:44:34 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 07:25:25 +00:00
|
|
|
@property
|
|
|
|
def model(self):
|
2017-10-07 08:59:46 +00:00
|
|
|
"""Return the camera model."""
|
2017-05-31 07:25:25 +00:00
|
|
|
return self._camera.model_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brand(self):
|
2017-10-07 08:59:46 +00:00
|
|
|
"""Return the camera brand."""
|
2017-05-31 07:25:25 +00:00
|
|
|
return DEFAULT_BRAND
|
2017-07-01 04:06:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def motion_detection_enabled(self):
|
2017-10-07 08:59:46 +00:00
|
|
|
"""Return the camera motion detection status."""
|
2017-07-01 04:06:56 +00:00
|
|
|
return self._motion_status
|
|
|
|
|
|
|
|
def set_base_station_mode(self, mode):
|
|
|
|
"""Set the mode in the base station."""
|
2017-07-12 06:25:55 +00:00
|
|
|
# Get the list of base stations identified by library
|
|
|
|
base_stations = self.hass.data[DATA_ARLO].base_stations
|
|
|
|
|
2017-10-07 08:59:46 +00:00
|
|
|
# Some Arlo cameras does not have base station
|
2017-07-12 06:25:55 +00:00
|
|
|
# So check if there is base station detected first
|
|
|
|
# if yes, then choose the primary base station
|
|
|
|
# Set the mode on the chosen base station
|
|
|
|
if base_stations:
|
|
|
|
primary_base_station = base_stations[0]
|
|
|
|
primary_base_station.mode = mode
|
2017-07-01 04:06:56 +00:00
|
|
|
|
|
|
|
def enable_motion_detection(self):
|
|
|
|
"""Enable the Motion detection in base station (Arm)."""
|
|
|
|
self._motion_status = True
|
|
|
|
self.set_base_station_mode(ARLO_MODE_ARMED)
|
|
|
|
|
|
|
|
def disable_motion_detection(self):
|
|
|
|
"""Disable the motion detection in base station (Disarm)."""
|
|
|
|
self._motion_status = False
|
|
|
|
self.set_base_station_mode(ARLO_MODE_DISARMED)
|