Skip to content

Commit 850b5cb

Browse files
authored
Config flow for ONVIF (#34520)
1 parent e7157f2 commit 850b5cb

File tree

14 files changed

+1294
-265
lines changed

14 files changed

+1294
-265
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ omit =
507507
homeassistant/components/ombi/*
508508
homeassistant/components/onewire/sensor.py
509509
homeassistant/components/onkyo/media_player.py
510+
homeassistant/components/onvif/__init__.py
510511
homeassistant/components/onvif/camera.py
511512
homeassistant/components/opencv/*
512513
homeassistant/components/openevse/sensor.py

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ homeassistant/components/ohmconnect/* @robbiet480
277277
homeassistant/components/ombi/* @larssont
278278
homeassistant/components/onboarding/* @home-assistant/core
279279
homeassistant/components/onewire/* @garbled1
280+
homeassistant/components/onvif/* @hunterjm
280281
homeassistant/components/openerz/* @misialq
281282
homeassistant/components/opentherm_gw/* @mvn23
282283
homeassistant/components/openuv/* @bachya
Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
1-
"""The onvif component."""
1+
"""The ONVIF integration."""
2+
import asyncio
3+
4+
import voluptuous as vol
5+
6+
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
7+
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
8+
from homeassistant.const import CONF_HOST
9+
from homeassistant.core import HomeAssistant
10+
from homeassistant.helpers import config_per_platform
11+
12+
from .const import (
13+
CONF_PROFILE,
14+
CONF_RTSP_TRANSPORT,
15+
DEFAULT_ARGUMENTS,
16+
DEFAULT_PROFILE,
17+
DOMAIN,
18+
RTSP_TRANS_PROTOCOLS,
19+
)
20+
21+
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
22+
23+
PLATFORMS = ["camera"]
24+
25+
26+
async def async_setup(hass: HomeAssistant, config: dict):
27+
"""Set up the ONVIF component."""
28+
# Import from yaml
29+
configs = {}
30+
for p_type, p_config in config_per_platform(config, "camera"):
31+
if p_type != DOMAIN:
32+
continue
33+
34+
config = p_config.copy()
35+
profile = config.get(CONF_PROFILE, DEFAULT_PROFILE)
36+
if config[CONF_HOST] not in configs.keys():
37+
configs[config[CONF_HOST]] = config
38+
configs[config[CONF_HOST]][CONF_PROFILE] = [profile]
39+
else:
40+
configs[config[CONF_HOST]][CONF_PROFILE].append(profile)
41+
42+
for conf in configs.values():
43+
hass.async_create_task(
44+
hass.config_entries.flow.async_init(
45+
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
46+
)
47+
)
48+
49+
return True
50+
51+
52+
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
53+
"""Set up ONVIF from a config entry."""
54+
if not entry.options:
55+
await async_populate_options(hass, entry)
56+
57+
for component in PLATFORMS:
58+
hass.async_create_task(
59+
hass.config_entries.async_forward_entry_setup(entry, component)
60+
)
61+
62+
return True
63+
64+
65+
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
66+
"""Unload a config entry."""
67+
unload_ok = all(
68+
await asyncio.gather(
69+
*[
70+
hass.config_entries.async_forward_entry_unload(entry, component)
71+
for component in PLATFORMS
72+
]
73+
)
74+
)
75+
76+
return unload_ok
77+
78+
79+
async def async_populate_options(hass, entry):
80+
"""Populate default options for device."""
81+
options = {
82+
CONF_EXTRA_ARGUMENTS: DEFAULT_ARGUMENTS,
83+
CONF_RTSP_TRANSPORT: RTSP_TRANS_PROTOCOLS[0],
84+
}
85+
86+
hass.config_entries.async_update_entry(entry, options=options)

0 commit comments

Comments
 (0)