2019-04-03 15:40:03 +00:00
|
|
|
"""Support for IP Cameras."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
import asyncio
|
2022-02-15 11:32:14 +00:00
|
|
|
from collections.abc import Iterable
|
2016-02-19 05:27:50 +00:00
|
|
|
from contextlib import closing
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
import aiohttp
|
2022-02-15 11:32:14 +00:00
|
|
|
from aiohttp import web
|
2017-01-19 18:18:32 +00:00
|
|
|
import async_timeout
|
2015-11-29 21:49:05 +00:00
|
|
|
import requests
|
2016-08-17 17:08:47 +00:00
|
|
|
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2022-04-04 15:57:48 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2016-08-27 20:42:34 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-09 13:20:40 +00:00
|
|
|
CONF_AUTHENTICATION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PASSWORD,
|
2019-12-09 13:20:40 +00:00
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
2019-07-31 19:25:30 +00:00
|
|
|
HTTP_BASIC_AUTHENTICATION,
|
|
|
|
HTTP_DIGEST_AUTHENTICATION,
|
|
|
|
)
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2017-01-19 17:55:27 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import (
|
2019-07-31 19:25:30 +00:00
|
|
|
async_aiohttp_proxy_web,
|
2019-12-09 13:20:40 +00:00
|
|
|
async_get_clientsession,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-02-18 08:03:41 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 12:22:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-02-07 09:08:55 +00:00
|
|
|
|
2022-02-18 08:03:41 +00:00
|
|
|
from .const import CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, DOMAIN, LOGGER
|
2016-08-27 20:42:34 +00:00
|
|
|
|
2022-02-18 08:03:41 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up a MJPEG IP Camera based on a config entry."""
|
2022-02-15 11:32:14 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
MjpegCamera(
|
2022-02-18 08:03:41 +00:00
|
|
|
name=entry.title,
|
|
|
|
authentication=entry.options[CONF_AUTHENTICATION],
|
|
|
|
username=entry.options.get(CONF_USERNAME),
|
|
|
|
password=entry.options[CONF_PASSWORD],
|
|
|
|
mjpeg_url=entry.options[CONF_MJPEG_URL],
|
|
|
|
still_image_url=entry.options.get(CONF_STILL_IMAGE_URL),
|
|
|
|
verify_ssl=entry.options[CONF_VERIFY_SSL],
|
|
|
|
unique_id=entry.entry_id,
|
|
|
|
device_info=DeviceInfo(
|
|
|
|
name=entry.title,
|
|
|
|
identifiers={(DOMAIN, entry.entry_id)},
|
|
|
|
),
|
2022-02-15 11:32:14 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2019-01-03 18:56:36 +00:00
|
|
|
|
|
|
|
|
2022-02-15 11:32:14 +00:00
|
|
|
def extract_image_from_mjpeg(stream: Iterable[bytes]) -> bytes | None:
|
2016-08-11 09:14:24 +00:00
|
|
|
"""Take in a MJPEG stream object, return the jpg from it."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = b""
|
2018-11-21 22:12:16 +00:00
|
|
|
|
2018-12-12 10:44:50 +00:00
|
|
|
for chunk in stream:
|
2016-08-09 00:34:46 +00:00
|
|
|
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]
|
2016-08-09 00:34:46 +00:00
|
|
|
|
2022-02-15 11:32:14 +00:00
|
|
|
return None
|
|
|
|
|
2016-08-09 00:34:46 +00:00
|
|
|
|
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
|
|
|
|
2022-02-15 11:32:14 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-15 14:24:52 +00:00
|
|
|
*,
|
2022-02-15 11:32:14 +00:00
|
|
|
name: str,
|
|
|
|
mjpeg_url: str,
|
|
|
|
still_image_url: str | None,
|
2022-02-15 14:24:52 +00:00
|
|
|
authentication: str | None = None,
|
|
|
|
username: str | None = None,
|
|
|
|
password: str = "",
|
|
|
|
verify_ssl: bool = True,
|
2022-02-18 08:03:41 +00:00
|
|
|
unique_id: str | None = None,
|
|
|
|
device_info: DeviceInfo | None = None,
|
2022-02-15 11:32:14 +00:00
|
|
|
) -> None:
|
2016-03-07 19:29:54 +00:00
|
|
|
"""Initialize a MJPEG camera."""
|
2015-11-09 04:15:06 +00:00
|
|
|
super().__init__()
|
2022-02-15 11:32:14 +00: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-09 04:15:06 +00:00
|
|
|
|
2016-10-28 04:40:10 +00:00
|
|
|
self._auth = None
|
2021-03-27 09:03:15 +00: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 11:32:14 +00:00
|
|
|
self._verify_ssl = verify_ssl
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2022-02-18 08:03:41 +00:00
|
|
|
if unique_id is not None:
|
|
|
|
self._attr_unique_id = unique_id
|
|
|
|
if device_info is not None:
|
|
|
|
self._attr_device_info = device_info
|
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-01-19 17:53:08 +00:00
|
|
|
"""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
|
|
|
|
):
|
2020-10-13 19:56:57 +00:00
|
|
|
image = await self.hass.async_add_executor_job(self.camera_image)
|
2017-01-19 17:53:08 +00:00
|
|
|
return image
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
|
2017-01-19 17:53:08 +00:00
|
|
|
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)
|
2017-01-19 17:53:08 +00:00
|
|
|
|
2018-10-01 06:50:05 +00:00
|
|
|
image = await response.read()
|
2017-01-19 17:53:08 +00:00
|
|
|
return image
|
|
|
|
|
|
|
|
except asyncio.TimeoutError:
|
2022-02-18 08:03:41 +00:00
|
|
|
LOGGER.error("Timeout getting camera image from %s", self.name)
|
2017-01-19 17:53:08 +00:00
|
|
|
|
2017-03-30 07:50:53 +00:00
|
|
|
except aiohttp.ClientError as err:
|
2022-02-18 08:03:41 +00:00
|
|
|
LOGGER.error("Error getting new camera image from %s: %s", self.name, err)
|
2017-01-19 17:53:08 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2016-10-24 06:48:01 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2016-02-07 09:08:55 +00:00
|
|
|
if self._username and self._password:
|
2016-08-27 20:42:34 +00:00
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
2021-08-11 00:33:06 +00:00
|
|
|
auth: HTTPDigestAuth | HTTPBasicAuth = HTTPDigestAuth(
|
|
|
|
self._username, self._password
|
|
|
|
)
|
2016-08-17 17:08:47 +00:00
|
|
|
else:
|
|
|
|
auth = HTTPBasicAuth(self._username, self._password)
|
2016-10-24 06:48:01 +00:00
|
|
|
req = requests.get(
|
2018-12-18 10:22:47 +00:00
|
|
|
self._mjpeg_url,
|
|
|
|
auth=auth,
|
|
|
|
stream=True,
|
|
|
|
timeout=10,
|
2019-07-31 19:25:30 +00:00
|
|
|
verify=self._verify_ssl,
|
2018-12-18 10:22:47 +00:00
|
|
|
)
|
2016-02-07 09:08:55 +00:00
|
|
|
else:
|
2016-10-24 06:48:01 +00:00
|
|
|
req = requests.get(self._mjpeg_url, stream=True, timeout=10)
|
2016-02-07 09:08:55 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
with closing(req) as response:
|
|
|
|
return extract_image_from_mjpeg(response.iter_content(102400))
|
2016-02-07 09:08:55 +00:00
|
|
|
|
2022-02-15 11:32:14 +00:00
|
|
|
async def handle_async_mjpeg_stream(
|
|
|
|
self, request: web.Request
|
|
|
|
) -> web.StreamResponse | None:
|
2016-03-07 16:45:06 +00:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
2016-10-24 06:48:01 +00:00
|
|
|
# aiohttp don't support DigestAuth -> Fallback
|
|
|
|
if self._authentication == HTTP_DIGEST_AUTHENTICATION:
|
2018-11-01 08:28:23 +00:00
|
|
|
return await super().handle_async_mjpeg_stream(request)
|
2016-10-24 06:48:01 +00:00
|
|
|
|
|
|
|
# connect to stream
|
2019-07-31 19:25:30 +00:00
|
|
|
websession = async_get_clientsession(self.hass, verify_ssl=self._verify_ssl)
|
2017-01-19 17:55:27 +00:00
|
|
|
stream_coro = websession.get(self._mjpeg_url, auth=self._auth)
|
2016-11-30 21:05:58 +00:00
|
|
|
|
2018-07-23 12:36:36 +00:00
|
|
|
return await async_aiohttp_proxy_web(self.hass, request, stream_coro)
|