2020-05-01 06:15:40 +00:00
|
|
|
"""The ONVIF integration."""
|
|
|
|
import asyncio
|
|
|
|
|
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
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-05-01 18:35:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
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,
|
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
|
|
|
from homeassistant.helpers import config_per_platform
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_RTSP_TRANSPORT,
|
2020-09-03 20:41:24 +00:00
|
|
|
CONF_SNAPSHOT_AUTH,
|
2020-05-01 06:15:40 +00:00
|
|
|
DEFAULT_ARGUMENTS,
|
2020-05-01 18:35:30 +00:00
|
|
|
DEFAULT_NAME,
|
|
|
|
DEFAULT_PASSWORD,
|
|
|
|
DEFAULT_PORT,
|
|
|
|
DEFAULT_USERNAME,
|
2020-05-01 06:15:40 +00:00
|
|
|
DOMAIN,
|
|
|
|
RTSP_TRANS_PROTOCOLS,
|
|
|
|
)
|
2020-05-06 16:29:59 +00:00
|
|
|
from .device import ONVIFDevice
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up the ONVIF component."""
|
|
|
|
# Import from yaml
|
|
|
|
configs = {}
|
|
|
|
for p_type, p_config in config_per_platform(config, "camera"):
|
|
|
|
if p_type != DOMAIN:
|
|
|
|
continue
|
|
|
|
|
|
|
|
config = p_config.copy()
|
2020-10-28 19:43:48 +00:00
|
|
|
if config[CONF_HOST] not in configs:
|
2020-05-01 18:35:30 +00:00
|
|
|
configs[config[CONF_HOST]] = {
|
|
|
|
CONF_HOST: config[CONF_HOST],
|
|
|
|
CONF_NAME: config.get(CONF_NAME, DEFAULT_NAME),
|
|
|
|
CONF_PASSWORD: config.get(CONF_PASSWORD, DEFAULT_PASSWORD),
|
|
|
|
CONF_PORT: config.get(CONF_PORT, DEFAULT_PORT),
|
|
|
|
CONF_USERNAME: config.get(CONF_USERNAME, DEFAULT_USERNAME),
|
|
|
|
}
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
for conf in configs.values():
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""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
|
|
|
|
|
2020-05-11 17:12:12 +00:00
|
|
|
platforms = ["camera"]
|
|
|
|
|
2020-10-19 03:29:53 +00:00
|
|
|
if device.capabilities.events:
|
2020-05-11 17:12:12 +00:00
|
|
|
platforms += ["binary_sensor", "sensor"]
|
|
|
|
|
2021-03-02 20:43:59 +00:00
|
|
|
for platform in platforms:
|
2020-05-01 06:15:40 +00:00
|
|
|
hass.async_create_task(
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2020-05-22 23:46:11 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.async_stop)
|
|
|
|
|
2020-05-01 06:15:40 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""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()
|
|
|
|
|
2020-05-01 18:35:30 +00:00
|
|
|
return all(
|
2020-05-01 06:15:40 +00:00
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
2021-03-02 20:43:59 +00:00
|
|
|
hass.config_entries.async_forward_entry_unload(entry, platform)
|
|
|
|
for platform in 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,
|
|
|
|
CONF_RTSP_TRANSPORT: RTSP_TRANS_PROTOCOLS[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(entry, options=options)
|