2015-06-05 12:51:29 +00:00
|
|
|
"""
|
|
|
|
Support for IP Cameras.
|
|
|
|
|
2015-10-13 19:00:28 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/camera.generic/
|
2015-06-05 12:51:29 +00:00
|
|
|
"""
|
2016-10-24 06:48:01 +00:00
|
|
|
import asyncio
|
2015-06-05 12:51:29 +00:00
|
|
|
import logging
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
import aiohttp
|
|
|
|
import async_timeout
|
2015-11-29 21:49:05 +00:00
|
|
|
import requests
|
2016-10-24 06:48:01 +00:00
|
|
|
from requests.auth import HTTPDigestAuth
|
2016-08-21 06:04:55 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-08-27 20:42:34 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_AUTHENTICATION,
|
2018-08-21 13:29:11 +00:00
|
|
|
HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION, CONF_VERIFY_SSL)
|
2016-08-21 06:04:55 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2017-06-25 19:25:14 +00:00
|
|
|
from homeassistant.components.camera import (
|
2019-03-23 16:16:43 +00:00
|
|
|
PLATFORM_SCHEMA, DEFAULT_CONTENT_TYPE, SUPPORT_STREAM, Camera)
|
2016-11-28 00:26:46 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2016-09-28 04:29:55 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2018-03-11 17:01:12 +00:00
|
|
|
from homeassistant.util.async_ import run_coroutine_threadsafe
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2015-06-05 12:51:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-06-25 19:25:14 +00:00
|
|
|
CONF_CONTENT_TYPE = 'content_type'
|
2016-08-21 06:04:55 +00:00
|
|
|
CONF_LIMIT_REFETCH_TO_URL_CHANGE = 'limit_refetch_to_url_change'
|
2016-08-27 20:42:34 +00:00
|
|
|
CONF_STILL_IMAGE_URL = 'still_image_url'
|
2019-03-16 06:19:32 +00:00
|
|
|
CONF_STREAM_SOURCE = 'stream_source'
|
2018-05-10 22:22:02 +00:00
|
|
|
CONF_FRAMERATE = 'framerate'
|
2016-08-27 20:42:34 +00:00
|
|
|
|
2016-08-21 06:04:55 +00:00
|
|
|
DEFAULT_NAME = 'Generic Camera'
|
2016-08-17 17:08:47 +00:00
|
|
|
|
2016-08-21 06:04:55 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2016-09-28 04:29:55 +00:00
|
|
|
vol.Required(CONF_STILL_IMAGE_URL): cv.template,
|
2019-03-16 06:19:32 +00:00
|
|
|
vol.Optional(CONF_STREAM_SOURCE, default=None): vol.Any(None, cv.string),
|
2016-08-27 20:42:34 +00:00
|
|
|
vol.Optional(CONF_AUTHENTICATION, default=HTTP_BASIC_AUTHENTICATION):
|
|
|
|
vol.In([HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]),
|
|
|
|
vol.Optional(CONF_LIMIT_REFETCH_TO_URL_CHANGE, default=False): cv.boolean,
|
2016-08-21 06:04:55 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
2016-08-27 20:42:34 +00:00
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
2017-06-25 19:25:14 +00:00
|
|
|
vol.Optional(CONF_CONTENT_TYPE, default=DEFAULT_CONTENT_TYPE): cv.string,
|
2018-05-10 22:22:02 +00:00
|
|
|
vol.Optional(CONF_FRAMERATE, default=2): cv.positive_int,
|
2018-08-21 13:29:11 +00:00
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
2016-08-21 06:04:55 +00:00
|
|
|
})
|
|
|
|
|
2015-06-05 12:51:29 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
|
|
|
discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a generic IP Camera."""
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([GenericCamera(hass, config)])
|
2015-06-05 12:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenericCamera(Camera):
|
2016-03-07 19:29:54 +00:00
|
|
|
"""A generic implementation of an IP camera."""
|
|
|
|
|
2016-09-25 20:33:01 +00:00
|
|
|
def __init__(self, hass, device_info):
|
2016-03-07 19:29:54 +00:00
|
|
|
"""Initialize a generic camera."""
|
2015-07-11 06:17:12 +00:00
|
|
|
super().__init__()
|
2016-09-25 20:33:01 +00:00
|
|
|
self.hass = hass
|
2016-10-24 06:48:01 +00:00
|
|
|
self._authentication = device_info.get(CONF_AUTHENTICATION)
|
2016-08-21 06:04:55 +00:00
|
|
|
self._name = device_info.get(CONF_NAME)
|
2016-09-28 04:29:55 +00:00
|
|
|
self._still_image_url = device_info[CONF_STILL_IMAGE_URL]
|
2019-03-16 06:19:32 +00:00
|
|
|
self._stream_source = device_info[CONF_STREAM_SOURCE]
|
2016-09-28 04:29:55 +00:00
|
|
|
self._still_image_url.hass = hass
|
2016-08-21 06:04:55 +00:00
|
|
|
self._limit_refetch = device_info[CONF_LIMIT_REFETCH_TO_URL_CHANGE]
|
2018-05-10 22:22:02 +00:00
|
|
|
self._frame_interval = 1 / device_info[CONF_FRAMERATE]
|
2019-03-23 16:16:43 +00:00
|
|
|
self._supported_features = SUPPORT_STREAM if self._stream_source else 0
|
2017-06-25 19:25:14 +00:00
|
|
|
self.content_type = device_info[CONF_CONTENT_TYPE]
|
2018-08-21 13:29:11 +00:00
|
|
|
self.verify_ssl = device_info[CONF_VERIFY_SSL]
|
2015-07-11 06:17:12 +00:00
|
|
|
|
2016-08-21 06:04:55 +00:00
|
|
|
username = device_info.get(CONF_USERNAME)
|
|
|
|
password = device_info.get(CONF_PASSWORD)
|
|
|
|
|
|
|
|
if username and password:
|
2016-10-24 06:48:01 +00:00
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
2016-08-21 06:04:55 +00:00
|
|
|
self._auth = HTTPDigestAuth(username, password)
|
2016-08-17 17:08:47 +00:00
|
|
|
else:
|
2016-10-24 06:48:01 +00:00
|
|
|
self._auth = aiohttp.BasicAuth(username, password=password)
|
2015-07-10 08:03:46 +00:00
|
|
|
else:
|
2016-08-21 06:04:55 +00:00
|
|
|
self._auth = None
|
|
|
|
|
|
|
|
self._last_url = None
|
|
|
|
self._last_image = None
|
|
|
|
|
2019-03-23 16:16:43 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Return supported features for this camera."""
|
|
|
|
return self._supported_features
|
|
|
|
|
2018-05-10 22:22:02 +00:00
|
|
|
@property
|
|
|
|
def frame_interval(self):
|
|
|
|
"""Return the interval between frames of the mjpeg stream."""
|
|
|
|
return self._frame_interval
|
|
|
|
|
2016-08-21 06:04:55 +00:00
|
|
|
def camera_image(self):
|
2016-10-24 06:48:01 +00:00
|
|
|
"""Return bytes of camera image."""
|
|
|
|
return run_coroutine_threadsafe(
|
|
|
|
self.async_camera_image(), self.hass.loop).result()
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
async def async_camera_image(self):
|
2016-08-21 06:04:55 +00:00
|
|
|
"""Return a still image response from the camera."""
|
|
|
|
try:
|
2016-10-24 06:48:01 +00:00
|
|
|
url = self._still_image_url.async_render()
|
2016-08-21 06:04:55 +00:00
|
|
|
except TemplateError as err:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Error parsing template %s: %s", self._still_image_url, err)
|
2016-08-21 06:04:55 +00:00
|
|
|
return self._last_image
|
|
|
|
|
|
|
|
if url == self._last_url and self._limit_refetch:
|
|
|
|
return self._last_image
|
|
|
|
|
2016-11-11 05:28:22 +00:00
|
|
|
# aiohttp don't support DigestAuth yet
|
2016-10-24 06:48:01 +00:00
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
|
|
|
def fetch():
|
|
|
|
"""Read image from a URL."""
|
|
|
|
try:
|
2018-08-21 13:29:11 +00:00
|
|
|
response = requests.get(url, timeout=10, auth=self._auth,
|
|
|
|
verify=self.verify_ssl)
|
2016-10-24 06:48:01 +00:00
|
|
|
return response.content
|
|
|
|
except requests.exceptions.RequestException as error:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Error getting camera image: %s", error)
|
2016-10-24 06:48:01 +00:00
|
|
|
return self._last_image
|
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
self._last_image = await self.hass.async_add_job(
|
2017-05-26 15:28:07 +00:00
|
|
|
fetch)
|
2016-10-24 06:48:01 +00:00
|
|
|
# async
|
|
|
|
else:
|
|
|
|
try:
|
2018-08-21 13:29:11 +00:00
|
|
|
websession = async_get_clientsession(
|
|
|
|
self.hass, verify_ssl=self.verify_ssl)
|
2016-10-24 06:48:01 +00:00
|
|
|
with async_timeout.timeout(10, loop=self.hass.loop):
|
2018-10-01 06:50:05 +00:00
|
|
|
response = await websession.get(
|
2016-11-11 05:28:22 +00:00
|
|
|
url, auth=self._auth)
|
2018-10-01 06:50:05 +00:00
|
|
|
self._last_image = await response.read()
|
2016-10-24 06:48:01 +00:00
|
|
|
except asyncio.TimeoutError:
|
2019-03-04 20:06:28 +00:00
|
|
|
_LOGGER.error("Timeout getting image from: %s", self._name)
|
2016-10-24 06:48:01 +00:00
|
|
|
return self._last_image
|
2017-03-30 07:50:53 +00:00
|
|
|
except aiohttp.ClientError as err:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Error getting new camera image: %s", err)
|
2016-11-11 05:28:22 +00:00
|
|
|
return self._last_image
|
2015-06-05 12:51:29 +00:00
|
|
|
|
2016-08-21 06:04:55 +00:00
|
|
|
self._last_url = url
|
|
|
|
return self._last_image
|
2015-06-05 12:51:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 16:45:06 +00:00
|
|
|
"""Return the name of this device."""
|
2015-07-11 06:17:12 +00:00
|
|
|
return self._name
|
2019-03-16 06:19:32 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def stream_source(self):
|
|
|
|
"""Return the source of the stream."""
|
|
|
|
return self._stream_source
|