2019-04-03 17:40:03 +02:00
|
|
|
"""Support for IP Cameras."""
|
2021-08-10 19:33:06 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-23 23:48:01 -07:00
|
|
|
import asyncio
|
2022-02-15 12:32:14 +01:00
|
|
|
from collections.abc import Iterable
|
2016-02-18 21:27:50 -08:00
|
|
|
from contextlib import closing
|
2019-12-09 14:20:40 +01:00
|
|
|
import logging
|
2015-11-29 13:49:05 -08:00
|
|
|
|
2016-10-23 23:48:01 -07:00
|
|
|
import aiohttp
|
2022-02-15 12:32:14 +01:00
|
|
|
from aiohttp import web
|
2017-01-19 19:18:32 +01:00
|
|
|
import async_timeout
|
2015-11-29 13:49:05 -08:00
|
|
|
import requests
|
2016-08-17 19:08:47 +02:00
|
|
|
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
2016-08-27 22:42:34 +02:00
|
|
|
import voluptuous as vol
|
2015-11-29 13:49:05 -08:00
|
|
|
|
2019-12-09 14:20:40 +01:00
|
|
|
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
|
2016-08-27 22:42:34 +02:00
|
|
|
from homeassistant.const import (
|
2019-12-09 14:20:40 +01:00
|
|
|
CONF_AUTHENTICATION,
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
2019-12-09 14:20:40 +01:00
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
2019-07-31 12:25:30 -07:00
|
|
|
HTTP_BASIC_AUTHENTICATION,
|
|
|
|
HTTP_DIGEST_AUTHENTICATION,
|
|
|
|
)
|
2022-01-03 13:22:41 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 14:20:40 +01:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2017-01-19 18:55:27 +01:00
|
|
|
from homeassistant.helpers.aiohttp_client import (
|
2019-07-31 12:25:30 -07:00
|
|
|
async_aiohttp_proxy_web,
|
2019-12-09 14:20:40 +01:00
|
|
|
async_get_clientsession,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2022-01-03 13:22:41 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-02-07 01:08:55 -08:00
|
|
|
|
2016-08-27 22:42:34 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_MJPEG_URL = "mjpeg_url"
|
|
|
|
CONF_STILL_IMAGE_URL = "still_image_url"
|
|
|
|
CONTENT_TYPE_HEADER = "Content-Type"
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_NAME = "Mjpeg Camera"
|
2018-12-18 02:22:47 -08:00
|
|
|
DEFAULT_VERIFY_SSL = True
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2019-07-31 12:25:30 -07: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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-03 13:22:41 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-04-30 07:04:49 +02:00
|
|
|
"""Set up a MJPEG IP Camera."""
|
2019-01-03 13:56:36 -05:00
|
|
|
filter_urllib3_logging()
|
|
|
|
|
|
|
|
if discovery_info:
|
|
|
|
config = PLATFORM_SCHEMA(discovery_info)
|
2022-02-15 12:32:14 +01:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
MjpegCamera(
|
|
|
|
config[CONF_NAME],
|
|
|
|
config[CONF_AUTHENTICATION],
|
|
|
|
config.get(CONF_USERNAME),
|
|
|
|
config.get(CONF_PASSWORD),
|
|
|
|
config[CONF_MJPEG_URL],
|
|
|
|
config.get(CONF_STILL_IMAGE_URL),
|
|
|
|
config[CONF_VERIFY_SSL],
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2019-01-03 13:56:36 -05:00
|
|
|
|
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
def filter_urllib3_logging() -> None:
|
2019-01-03 13:56:36 -05:00
|
|
|
"""Filter header errors from urllib3 due to a urllib3 bug."""
|
2018-10-02 04:24:44 -04:00
|
|
|
urllib3_logger = logging.getLogger("urllib3.connectionpool")
|
2019-07-31 12:25:30 -07:00
|
|
|
if not any(isinstance(x, NoHeaderErrorFilter) for x in urllib3_logger.filters):
|
|
|
|
urllib3_logger.addFilter(NoHeaderErrorFilter())
|
2018-10-02 04:24:44 -04:00
|
|
|
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
def extract_image_from_mjpeg(stream: Iterable[bytes]) -> bytes | None:
|
2016-08-11 11:14:24 +02:00
|
|
|
"""Take in a MJPEG stream object, return the jpg from it."""
|
2019-07-31 12:25:30 -07:00
|
|
|
data = b""
|
2018-11-21 22:12:16 +00:00
|
|
|
|
2018-12-12 11:44:50 +01:00
|
|
|
for chunk in stream:
|
2016-08-09 02:34:46 +02:00
|
|
|
data += chunk
|
2019-07-31 12:25:30 -07:00
|
|
|
jpg_end = data.find(b"\xff\xd9")
|
2018-12-12 11:44:50 +01:00
|
|
|
|
|
|
|
if jpg_end == -1:
|
|
|
|
continue
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
jpg_start = data.find(b"\xff\xd8")
|
2018-12-12 11:44:50 +01:00
|
|
|
|
|
|
|
if jpg_start == -1:
|
|
|
|
continue
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
return data[jpg_start : jpg_end + 2]
|
2016-08-09 02:34:46 +02:00
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
return None
|
|
|
|
|
2016-08-09 02:34:46 +02:00
|
|
|
|
2015-11-08 22:15:06 -06:00
|
|
|
class MjpegCamera(Camera):
|
2016-03-07 20:29:54 +01:00
|
|
|
"""An implementation of an IP camera that is reachable over a URL."""
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
authentication: str,
|
|
|
|
username: str | None,
|
|
|
|
password: str | None,
|
|
|
|
mjpeg_url: str,
|
|
|
|
still_image_url: str | None,
|
|
|
|
verify_ssl: bool,
|
|
|
|
) -> None:
|
2016-03-07 20:29:54 +01:00
|
|
|
"""Initialize a MJPEG camera."""
|
2015-11-08 22:15:06 -06:00
|
|
|
super().__init__()
|
2022-02-15 12:32:14 +01:00
|
|
|
self._attr_name = name
|
|
|
|
self._authentication = authentication
|
|
|
|
self._username = username
|
|
|
|
self._password = password
|
|
|
|
self._mjpeg_url = mjpeg_url
|
|
|
|
self._still_image_url = still_image_url
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2016-10-28 06:40:10 +02:00
|
|
|
self._auth = None
|
2021-03-27 10:03:15 +01:00
|
|
|
if (
|
|
|
|
self._username
|
|
|
|
and self._password
|
|
|
|
and self._authentication == HTTP_BASIC_AUTHENTICATION
|
|
|
|
):
|
|
|
|
self._auth = aiohttp.BasicAuth(self._username, password=self._password)
|
2022-02-15 12:32:14 +01:00
|
|
|
self._verify_ssl = verify_ssl
|
2016-10-23 23:48:01 -07:00
|
|
|
|
2021-08-10 19:33:06 -05:00
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-01-19 18:53:08 +01:00
|
|
|
"""Return a still image response from the camera."""
|
|
|
|
# DigestAuth is not supported
|
2019-07-31 12:25:30 -07:00
|
|
|
if (
|
|
|
|
self._authentication == HTTP_DIGEST_AUTHENTICATION
|
|
|
|
or self._still_image_url is None
|
|
|
|
):
|
2020-10-13 14:56:57 -05:00
|
|
|
image = await self.hass.async_add_executor_job(self.camera_image)
|
2017-01-19 18:53:08 +01:00
|
|
|
return image
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
|
2017-01-19 18:53:08 +01:00
|
|
|
try:
|
2021-11-04 10:07:50 -05:00
|
|
|
async with async_timeout.timeout(10):
|
2019-07-31 12:25:30 -07:00
|
|
|
response = await websession.get(self._still_image_url, auth=self._auth)
|
2017-01-19 18:53:08 +01:00
|
|
|
|
2018-10-01 08:50:05 +02:00
|
|
|
image = await response.read()
|
2017-01-19 18:53:08 +01:00
|
|
|
return image
|
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
2022-02-15 12:32:14 +01:00
|
|
|
_LOGGER.error("Timeout getting camera image from %s", self.name)
|
2017-01-19 18:53:08 +01:00
|
|
|
|
2017-03-30 00:50:53 -07:00
|
|
|
except aiohttp.ClientError as err:
|
2022-02-15 12:32:14 +01:00
|
|
|
_LOGGER.error("Error getting new camera image from %s: %s", self.name, err)
|
2017-01-19 18:53:08 +01:00
|
|
|
|
2021-08-10 19:33:06 -05:00
|
|
|
return None
|
|
|
|
|
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2016-10-23 23:48:01 -07:00
|
|
|
"""Return a still image response from the camera."""
|
2016-02-07 01:08:55 -08:00
|
|
|
if self._username and self._password:
|
2016-08-27 22:42:34 +02:00
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
2021-08-10 19:33:06 -05:00
|
|
|
auth: HTTPDigestAuth | HTTPBasicAuth = HTTPDigestAuth(
|
|
|
|
self._username, self._password
|
|
|
|
)
|
2016-08-17 19:08:47 +02:00
|
|
|
else:
|
|
|
|
auth = HTTPBasicAuth(self._username, self._password)
|
2016-10-23 23:48:01 -07:00
|
|
|
req = requests.get(
|
2018-12-18 02:22:47 -08:00
|
|
|
self._mjpeg_url,
|
|
|
|
auth=auth,
|
|
|
|
stream=True,
|
|
|
|
timeout=10,
|
2019-07-31 12:25:30 -07:00
|
|
|
verify=self._verify_ssl,
|
2018-12-18 02:22:47 -08:00
|
|
|
)
|
2016-02-07 01:08:55 -08:00
|
|
|
else:
|
2016-10-23 23:48:01 -07:00
|
|
|
req = requests.get(self._mjpeg_url, stream=True, timeout=10)
|
2016-02-07 01:08:55 -08:00
|
|
|
|
2016-10-23 23:48:01 -07:00
|
|
|
with closing(req) as response:
|
|
|
|
return extract_image_from_mjpeg(response.iter_content(102400))
|
2016-02-07 01:08:55 -08:00
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
async def handle_async_mjpeg_stream(
|
|
|
|
self, request: web.Request
|
|
|
|
) -> web.StreamResponse | None:
|
2016-03-07 17:45:06 +01:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
2016-10-23 23:48:01 -07:00
|
|
|
# aiohttp don't support DigestAuth -> Fallback
|
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
2018-11-01 01:28:23 -07:00
|
|
|
return await super().handle_async_mjpeg_stream(request)
|
2016-10-23 23:48:01 -07:00
|
|
|
|
|
|
|
# connect to stream
|
2019-07-31 12:25:30 -07:00
|
|
|
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
|
2017-01-19 18:55:27 +01:00
|
|
|
stream_coro = websession.get(self._mjpeg_url, auth=self._auth)
|
2016-11-30 13:05:58 -08:00
|
|
|
|
2018-07-23 14:36:36 +02:00
|
|
|
return await async_aiohttp_proxy_web(self.hass, request, stream_coro)
|
2015-11-08 22:15:06 -06:00
|
|
|
|
2018-10-01 14:39:40 -04:00
|
|
|
|
|
|
|
class NoHeaderErrorFilter(logging.Filter):
|
|
|
|
"""Filter out urllib3 Header Parsing Errors due to a urllib3 bug."""
|
|
|
|
|
2022-02-15 12:32:14 +01:00
|
|
|
def filter(self, record: logging.LogRecord) -> bool:
|
2018-10-01 14:39:40 -04:00
|
|
|
"""Filter out Header Parsing Errors."""
|
|
|
|
return "Failed to parse headers" not in record.getMessage()
|