|
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