Fix ONVIF YAML import (#35035)

This commit is contained in:
Franck Nijhof 2020-05-01 20:35:30 +02:00 committed by GitHub
parent b3201523aa
commit e1ae455f1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 23 deletions

View File

@ -5,15 +5,23 @@ import voluptuous as vol
from homeassistant.components.ffmpeg import CONF_EXTRA_ARGUMENTS
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_per_platform
from .const import (
CONF_PROFILE,
CONF_RTSP_TRANSPORT,
DEFAULT_ARGUMENTS,
DEFAULT_PROFILE,
DEFAULT_NAME,
DEFAULT_PASSWORD,
DEFAULT_PORT,
DEFAULT_USERNAME,
DOMAIN,
RTSP_TRANS_PROTOCOLS,
)
@ -32,12 +40,14 @@ async def async_setup(hass: HomeAssistant, config: dict):
continue
config = p_config.copy()
profile = config.get(CONF_PROFILE, DEFAULT_PROFILE)
if config[CONF_HOST] not in configs.keys():
configs[config[CONF_HOST]] = config
configs[config[CONF_HOST]][CONF_PROFILE] = [profile]
else:
configs[config[CONF_HOST]][CONF_PROFILE].append(profile)
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),
}
for conf in configs.values():
hass.async_create_task(
@ -64,7 +74,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
return all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, component)
@ -73,8 +83,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
)
)
return unload_ok
async def async_populate_options(hass, entry):
"""Populate default options for device."""

View File

@ -125,21 +125,20 @@ class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
discovery = await async_discovery(self.hass)
for device in discovery:
configured = False
for entry in self._async_current_entries():
if entry.unique_id == device[CONF_DEVICE_ID]:
configured = True
break
configured = any(
entry.unique_id == device[CONF_DEVICE_ID]
for entry in self._async_current_entries()
)
if not configured:
self.devices.append(device)
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))
if self.devices:
names = []
for device in self.devices:
names.append(f"{device[CONF_NAME]} ({device[CONF_HOST]})")
names = [
f"{device[CONF_NAME]} ({device[CONF_HOST]})" for device in self.devices
]
names.append(CONF_MANUAL_INPUT)
@ -299,7 +298,7 @@ def get_device(hass, host, port, username, password) -> ONVIFCamera:
"""Get ONVIFCamera instance."""
session = async_get_clientsession(hass)
transport = AsyncTransport(None, session=session)
device = ONVIFCamera(
return ONVIFCamera(
host,
port,
username,
@ -307,5 +306,3 @@ def get_device(hass, host, port, username, password) -> ONVIFCamera:
f"{os.path.dirname(onvif.__file__)}/wsdl/",
transport=transport,
)
return device