2020-05-01 06:15:40 +00:00
|
|
|
"""The ONVIF integration."""
|
2020-10-19 03:29:53 +00:00
|
|
|
from onvif.exceptions import ONVIFAuthError, ONVIFError, ONVIFTimeoutError
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
|
2022-05-15 15:58:57 +00:00
|
|
|
from homeassistant.components.stream import CONF_RTSP_TRANSPORT, RTSP_TRANSPORTS
|
2022-04-23 01:35:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-05-01 18:35:30 +00:00
|
|
|
from homeassistant.const import (
|
2020-05-11 17:12:12 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2020-09-03 20:41:24 +00:00
|
|
|
HTTP_BASIC_AUTHENTICATION,
|
|
|
|
HTTP_DIGEST_AUTHENTICATION,
|
2021-12-06 03:06:35 +00:00
|
|
|
Platform,
|
2020-05-01 18:35:30 +00:00
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-05-06 16:29:59 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-05-15 06:31:18 +00:00
|
|
|
from .const import CONF_SNAPSHOT_AUTH, DEFAULT_ARGUMENTS, DOMAIN
|
2020-05-06 16:29:59 +00:00
|
|
|
from .device import ONVIFDevice
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Set up ONVIF from a config entry."""
|
2020-05-06 16:29:59 +00:00
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
2020-05-01 06:15:40 +00:00
|
|
|
if not entry.options:
|
|
|
|
await async_populate_options(hass, entry)
|
|
|
|
|
2020-05-06 16:29:59 +00:00
|
|
|
device = ONVIFDevice(hass, entry)
|
|
|
|
|
|
|
|
if not await device.async_setup():
|
2020-10-19 03:29:53 +00:00
|
|
|
await device.device.close()
|
2020-05-06 16:29:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if not device.available:
|
|
|
|
raise ConfigEntryNotReady()
|
|
|
|
|
2020-09-03 20:41:24 +00:00
|
|
|
if not entry.data.get(CONF_SNAPSHOT_AUTH):
|
|
|
|
await async_populate_snapshot_auth(hass, device, entry)
|
|
|
|
|
2020-05-06 16:29:59 +00:00
|
|
|
hass.data[DOMAIN][entry.unique_id] = device
|
|
|
|
|
2022-01-24 14:07:06 +00:00
|
|
|
platforms = [Platform.BUTTON, Platform.CAMERA]
|
2020-05-11 17:12:12 +00:00
|
|
|
|
2020-10-19 03:29:53 +00:00
|
|
|
if device.capabilities.events:
|
2021-12-06 03:06:35 +00:00
|
|
|
platforms += [Platform.BINARY_SENSOR, Platform.SENSOR]
|
2020-05-11 17:12:12 +00:00
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, platforms)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2021-04-20 16:15:17 +00:00
|
|
|
entry.async_on_unload(
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.async_stop)
|
|
|
|
)
|
2020-05-22 23:46:11 +00:00
|
|
|
|
2020-05-01 06:15:40 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Unload a config entry."""
|
2020-05-11 17:12:12 +00:00
|
|
|
|
|
|
|
device = hass.data[DOMAIN][entry.unique_id]
|
|
|
|
platforms = ["camera"]
|
|
|
|
|
|
|
|
if device.capabilities.events and device.events.started:
|
|
|
|
platforms += ["binary_sensor", "sensor"]
|
|
|
|
await device.events.async_stop()
|
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, platforms)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
2020-10-19 03:29:53 +00:00
|
|
|
async def _get_snapshot_auth(device):
|
|
|
|
"""Determine auth type for snapshots."""
|
|
|
|
if not device.capabilities.snapshot or not (device.username and device.password):
|
2020-09-03 20:41:24 +00:00
|
|
|
return HTTP_DIGEST_AUTHENTICATION
|
|
|
|
|
|
|
|
try:
|
2020-10-19 03:29:53 +00:00
|
|
|
snapshot = await device.device.get_snapshot(device.profiles[0].token)
|
2020-09-03 20:41:24 +00:00
|
|
|
|
2020-10-19 03:29:53 +00:00
|
|
|
if snapshot:
|
|
|
|
return HTTP_DIGEST_AUTHENTICATION
|
|
|
|
return HTTP_BASIC_AUTHENTICATION
|
|
|
|
except (ONVIFAuthError, ONVIFTimeoutError):
|
2020-09-03 20:41:24 +00:00
|
|
|
return HTTP_BASIC_AUTHENTICATION
|
2020-10-19 03:29:53 +00:00
|
|
|
except ONVIFError:
|
2020-09-03 20:41:24 +00:00
|
|
|
return HTTP_DIGEST_AUTHENTICATION
|
|
|
|
|
|
|
|
|
|
|
|
async def async_populate_snapshot_auth(hass, device, entry):
|
|
|
|
"""Check if digest auth for snapshots is possible."""
|
2020-10-19 03:29:53 +00:00
|
|
|
auth = await _get_snapshot_auth(device)
|
2020-09-03 20:41:24 +00:00
|
|
|
new_data = {**entry.data, CONF_SNAPSHOT_AUTH: auth}
|
|
|
|
hass.config_entries.async_update_entry(entry, data=new_data)
|
|
|
|
|
|
|
|
|
2020-05-01 06:15:40 +00:00
|
|
|
async def async_populate_options(hass, entry):
|
|
|
|
"""Populate default options for device."""
|
|
|
|
options = {
|
|
|
|
CONF_EXTRA_ARGUMENTS: DEFAULT_ARGUMENTS,
|
2022-05-15 06:31:18 +00:00
|
|
|
CONF_RTSP_TRANSPORT: next(iter(RTSP_TRANSPORTS)),
|
2020-05-01 06:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(entry, options=options)
|