core/homeassistant/components/mjpeg/camera.py

187 lines
6.1 KiB
Python
Raw Normal View History

"""Support for IP Cameras."""
from __future__ import annotations
import asyncio
2016-02-19 05:27:50 +00:00
from contextlib import closing
import logging
2015-11-29 21:49:05 +00:00
import aiohttp
2017-01-19 18:18:32 +00:00
import async_timeout
2015-11-29 21:49:05 +00:00
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import voluptuous as vol
2015-11-29 21:49:05 +00:00
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
from homeassistant.const import (
CONF_AUTHENTICATION,
2019-07-31 19:25:30 +00:00
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
CONF_VERIFY_SSL,
2019-07-31 19:25:30 +00:00
HTTP_BASIC_AUTHENTICATION,
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import (
2019-07-31 19:25:30 +00:00
async_aiohttp_proxy_web,
async_get_clientsession,
2019-07-31 19:25:30 +00:00
)
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_MJPEG_URL = "mjpeg_url"
CONF_STILL_IMAGE_URL = "still_image_url"
CONTENT_TYPE_HEADER = "Content-Type"
2015-11-09 04:15:06 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Mjpeg Camera"
DEFAULT_VERIFY_SSL = True
2015-11-09 04:15:06 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MJPEG_URL): cv.url,
vol.Optional(CONF_STILL_IMAGE_URL): cv.url,
vol.Optional(CONF_AUTHENTICATION, default=HTTP_BASIC_AUTHENTICATION): vol.In(
[HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION]
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up a MJPEG IP Camera."""
filter_urllib3_logging()
if discovery_info:
config = PLATFORM_SCHEMA(discovery_info)
async_add_entities([MjpegCamera(config)])
def filter_urllib3_logging():
"""Filter header errors from urllib3 due to a urllib3 bug."""
urllib3_logger = logging.getLogger("urllib3.connectionpool")
2019-07-31 19:25:30 +00:00
if not any(isinstance(x, NoHeaderErrorFilter) for x in urllib3_logger.filters):
urllib3_logger.addFilter(NoHeaderErrorFilter())
2015-11-09 04:15:06 +00:00
def extract_image_from_mjpeg(stream):
"""Take in a MJPEG stream object, return the jpg from it."""
2019-07-31 19:25:30 +00:00
data = b""
2018-12-12 10:44:50 +00:00
for chunk in stream:
data += chunk
2019-07-31 19:25:30 +00:00
jpg_end = data.find(b"\xff\xd9")
2018-12-12 10:44:50 +00:00
if jpg_end == -1:
continue
2019-07-31 19:25:30 +00:00
jpg_start = data.find(b"\xff\xd8")
2018-12-12 10:44:50 +00:00
if jpg_start == -1:
continue
2019-07-31 19:25:30 +00:00
return data[jpg_start : jpg_end + 2]
2015-11-09 04:15:06 +00:00
class MjpegCamera(Camera):
2016-03-07 19:29:54 +00:00
"""An implementation of an IP camera that is reachable over a URL."""
2015-11-09 04:15:06 +00:00
def __init__(self, device_info):
2016-03-07 19:29:54 +00:00
"""Initialize a MJPEG camera."""
2015-11-09 04:15:06 +00:00
super().__init__()
self._name = device_info.get(CONF_NAME)
self._authentication = device_info.get(CONF_AUTHENTICATION)
self._username = device_info.get(CONF_USERNAME)
self._password = device_info.get(CONF_PASSWORD)
self._mjpeg_url = device_info[CONF_MJPEG_URL]
2017-01-24 19:25:51 +00:00
self._still_image_url = device_info.get(CONF_STILL_IMAGE_URL)
2015-11-09 04:15:06 +00:00
self._auth = None
if (
self._username
and self._password
and self._authentication == HTTP_BASIC_AUTHENTICATION
):
self._auth = aiohttp.BasicAuth(self._username, password=self._password)
self._verify_ssl = device_info.get(CONF_VERIFY_SSL)
async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return a still image response from the camera."""
# DigestAuth is not supported
2019-07-31 19:25:30 +00:00
if (
self._authentication == HTTP_DIGEST_AUTHENTICATION
or self._still_image_url is None
):
image = await self.hass.async_add_executor_job(self.camera_image)
return image
2019-07-31 19:25:30 +00:00
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
try:
2021-11-04 15:07:50 +00:00
async with async_timeout.timeout(10):
2019-07-31 19:25:30 +00:00
response = await websession.get(self._still_image_url, auth=self._auth)
image = await response.read()
return image
except asyncio.TimeoutError:
_LOGGER.error("Timeout getting camera image from %s", self._name)
except aiohttp.ClientError as err:
_LOGGER.error("Error getting new camera image from %s: %s", self._name, err)
return None
def camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return a still image response from the camera."""
if self._username and self._password:
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
auth: HTTPDigestAuth | HTTPBasicAuth = HTTPDigestAuth(
self._username, self._password
)
else:
auth = HTTPBasicAuth(self._username, self._password)
req = requests.get(
self._mjpeg_url,
auth=auth,
stream=True,
timeout=10,
2019-07-31 19:25:30 +00:00
verify=self._verify_ssl,
)
else:
req = requests.get(self._mjpeg_url, stream=True, timeout=10)
with closing(req) as response:
return extract_image_from_mjpeg(response.iter_content(102400))
async def handle_async_mjpeg_stream(self, request):
2016-03-07 16:45:06 +00:00
"""Generate an HTTP MJPEG stream from the camera."""
# aiohttp don't support DigestAuth -> Fallback
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
return await super().handle_async_mjpeg_stream(request)
# connect to stream
2019-07-31 19:25:30 +00:00
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
stream_coro = websession.get(self._mjpeg_url, auth=self._auth)
return await async_aiohttp_proxy_web(self.hass, request, stream_coro)
2015-11-09 04:15:06 +00:00
@property
def name(self):
2016-03-07 16:45:06 +00:00
"""Return the name of this camera."""
2015-11-09 04:15:06 +00:00
return self._name
class NoHeaderErrorFilter(logging.Filter):
"""Filter out urllib3 Header Parsing Errors due to a urllib3 bug."""
def filter(self, record):
"""Filter out Header Parsing Errors."""
return "Failed to parse headers" not in record.getMessage()