Skip to content

Commit

Permalink
Prevent re-authorization on configure
Browse files Browse the repository at this point in the history
  • Loading branch information
natekspencer committed Sep 16, 2021
1 parent c8e2376 commit d1b13b8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
14 changes: 8 additions & 6 deletions custom_components/vivint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,18 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
)

hub: VivintHub = hass.data[DOMAIN][entry.entry_id]
hub.remove_cache_file()
await hub.account.disconnect()
hub.undo_listener()

if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
await hub.disconnect()

return unload_ok


async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle removal of an entry."""
hub: VivintHub = hass.data[DOMAIN][entry.entry_id]
await hub.disconnect(remove_cache=True)
hass.data[DOMAIN].pop(entry.entry_id)


async def update_listener(hass, entry: ConfigEntry):
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
13 changes: 8 additions & 5 deletions custom_components/vivint/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
import logging
from typing import Any, Dict, Optional

import voluptuous as vol
from aiohttp import ClientResponseError
from aiohttp.client_exceptions import ClientConnectorError
from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback
from vivintpy.exceptions import (
VivintSkyApiAuthenticationError,
VivintSkyApiError,
VivintSkyApiMfaRequiredError,
)
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback

from .const import DOMAIN # pylint:disable=unused-import
from .const import (
CONF_HD_STREAM,
CONF_MFA,
Expand All @@ -23,6 +23,7 @@
DEFAULT_RTSP_STREAM,
RTSP_STREAM_TYPES,
)
from .const import DOMAIN # pylint:disable=unused-import
from .hub import VivintHub

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,6 +64,7 @@ async def async_create_entry(self):
await self.hass.config_entries.async_reload(existing_entry.entry_id)
return self.async_abort(reason="reauth_successful")

await self._hub.disconnect()
return super().async_create_entry(
title=config_data[CONF_USERNAME], data=config_data
)
Expand Down Expand Up @@ -119,6 +121,7 @@ async def async_step_mfa(self, user_input=None):
data_schema=STEP_MFA_DATA_SCHEMA,
errors={"base": "unknown"},
)

return await self.async_create_entry()

async def async_step_reauth(self, user_input=None):
Expand Down
19 changes: 17 additions & 2 deletions custom_components/vivint/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def __init__(
):
"""Initialize the Vivint hub."""
self._data = data
self.undo_listener = undo_listener
self.__undo_listener = undo_listener
self.account: Account = None
self.logged_in = False
self.session: ClientSession = None

async def _async_update_data():
"""Update all device states from the Vivint API."""
Expand All @@ -75,11 +76,13 @@ async def login(
except:
_LOGGER.debug("No previous session found")

self.session = ClientSession(cookie_jar=abs_cookie_jar)

self.account = Account(
username=self._data[CONF_USERNAME],
password=self._data[CONF_PASSWORD],
persist_session=True,
client_session=ClientSession(cookie_jar=abs_cookie_jar),
client_session=self.session,
)
try:
await self.account.connect(
Expand All @@ -96,6 +99,18 @@ async def login(
_LOGGER.error("Unable to connect to the Vivint API")
raise ex

async def disconnect(self, remove_cache: bool = False) -> None:
"""Disconnect from Vivint, close the session and optionally remove cache."""
if self.account.connected:
await self.account.disconnect()
if not self.session.closed:
await self.session.close()
if remove_cache:
self.remove_cache_file()
if self.__undo_listener:
self.__undo_listener()
self.__undo_listener = None

async def verify_mfa(self, code: str):
try:
await self.account.verify_mfa(code)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/vivint/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"domain": "vivint",
"name": "Vivint",
"version": "2021.9.3",
"version": "2021.9.4",
"config_flow": true,
"documentation": "https://github.com/natekspencer/hacs-vivint",
"issue_tracker": "https://github.com/natekspencer/hacs-vivint/issues",
"requirements": ["vivintpy==2021.9.5"],
"requirements": ["vivintpy==2021.9.7"],
"dependencies": ["ffmpeg"],
"codeowners": ["@natekspencer"]
}

0 comments on commit d1b13b8

Please sign in to comment.