Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
session = async_create_clientsession(
self.hass, cookie_jar=CookieJar(unsafe=True)
)
unifiprotect = UpvServer(
session,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
)
try:
unique_id = await unifiprotect.unique_id()
except NotAuthorized:
errors["base"] = "connection_error"
return await self._show_setup_form(errors)
except NvrError:
errors["base"] = "nvr_error"
return await self._show_setup_form(errors)
entries = self._async_current_entries()
for entry in entries:
if entry.data[CONF_ID] == unique_id:
return self.async_abort(reason="server_exists")
return self.async_create_entry(
title=unique_id,
data={
CONF_ID: unique_id,
CONF_HOST: user_input[CONF_HOST],
events_update_interval = entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=DOMAIN,
update_method=protectserver.update,
update_interval=timedelta(seconds=events_update_interval),
)
try:
nvr_info = await protectserver.server_information()
except NotAuthorized:
_LOGGER.error(
"Could not Authorize against Unifi Protect. Please reinstall the Integration."
)
return
except (NvrError, ServerDisconnectedError):
raise ConfigEntryNotReady
await coordinator.async_refresh()
hass.data[DOMAIN][entry.entry_id] = {
"coordinator": coordinator,
"upv": protectserver,
"snapshot_direct": entry.options.get(CONF_SNAPSHOT_DIRECT, False),
}
await _async_get_or_create_nvr_device_in_registry(hass, entry, nvr_info)
)
unifiprotect = UpvServer(
session,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
)
try:
unique_id = await unifiprotect.unique_id()
except NotAuthorized:
errors["base"] = "connection_error"
return await self._show_setup_form(errors)
except NvrError:
errors["base"] = "nvr_error"
return await self._show_setup_form(errors)
entries = self._async_current_entries()
for entry in entries:
if entry.data[CONF_ID] == unique_id:
return self.async_abort(reason="server_exists")
return self.async_create_entry(
title=unique_id,
data={
CONF_ID: unique_id,
CONF_HOST: user_input[CONF_HOST],
CONF_PORT: user_input[CONF_PORT],
CONF_USERNAME: user_input.get(CONF_USERNAME),
CONF_PASSWORD: user_input.get(CONF_PASSWORD),
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name=DOMAIN,
update_method=protectserver.update,
update_interval=timedelta(seconds=events_update_interval),
)
try:
nvr_info = await protectserver.server_information()
except NotAuthorized:
_LOGGER.error(
"Could not Authorize against Unifi Protect. Please reinstall the Integration."
)
return
except (NvrError, ServerDisconnectedError):
raise ConfigEntryNotReady
await coordinator.async_refresh()
hass.data[DOMAIN][entry.entry_id] = {
"coordinator": coordinator,
"upv": protectserver,
"snapshot_direct": entry.options.get(CONF_SNAPSHOT_DIRECT, False),
}
await _async_get_or_create_nvr_device_in_registry(hass, entry, nvr_info)
for platform in UNIFI_PROTECT_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user."""
if user_input is None:
return await self._show_setup_form(user_input)
errors = {}
session = async_create_clientsession(
self.hass, cookie_jar=CookieJar(unsafe=True)
)
unifiprotect = UpvServer(
session,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
)
try:
unique_id = await unifiprotect.unique_id()
except NotAuthorized:
errors["base"] = "connection_error"
return await self._show_setup_form(errors)
except NvrError:
errors["base"] = "nvr_error"
return await self._show_setup_form(errors)
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
"""Set up the Unifi Protect config entries."""
if not entry.options:
hass.config_entries.async_update_entry(
entry,
options={
CONF_SCAN_INTERVAL: entry.data.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
CONF_SNAPSHOT_DIRECT: entry.data.get(CONF_SNAPSHOT_DIRECT, False),
},
)
session = async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True))
protectserver = UpvServer(
session,
entry.data[CONF_HOST],
entry.data[CONF_PORT],
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = protectserver
_LOGGER.debug("Connect to Unfi Protect")
events_update_interval = entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
)
coordinator = DataUpdateCoordinator(
hass,