diff --git a/custom_components/badnest/__init__.py b/custom_components/badnest/__init__.py index a8e653e..1a4c0b5 100644 --- a/custom_components/badnest/__init__.py +++ b/custom_components/badnest/__init__.py @@ -3,15 +3,19 @@ from homeassistant.helpers import config_validation as cv from .api import NestAPI -from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE +from .const import DOMAIN, CONF_ISSUE_TOKEN, CONF_COOKIE, CONF_USER_ID, CONF_ACCESS_TOKEN CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.All( + { + vol.Required(CONF_USER_ID, default=""): cv.string, + vol.Required(CONF_ACCESS_TOKEN, default=""): cv.string, + }, { vol.Required(CONF_ISSUE_TOKEN, default=""): cv.string, vol.Required(CONF_COOKIE, default=""): cv.string, - } + }, ) }, extra=vol.ALLOW_EXTRA, @@ -21,14 +25,20 @@ def setup(hass, config): """Set up the badnest component.""" if config.get(DOMAIN) is not None: + user_id = config[DOMAIN].get(CONF_USER_ID) + access_token = config[DOMAIN].get(CONF_ACCESS_TOKEN) issue_token = config[DOMAIN].get(CONF_ISSUE_TOKEN) cookie = config[DOMAIN].get(CONF_COOKIE) else: + user_id = None + access_token = None issue_token = None cookie = None hass.data[DOMAIN] = { 'api': NestAPI( + user_id, + access_token, issue_token, cookie, ), diff --git a/custom_components/badnest/api.py b/custom_components/badnest/api.py index f2fba76..f12cbeb 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -83,12 +83,14 @@ class AuthorizationRequired(Exception): class NestAPI(): def __init__(self, + user_id, + access_token, issue_token, cookie): self.device_data = {} self._wheres = {} - self._user_id = None - self._access_token = None + self._user_id = user_id + self._access_token = access_token self._retries = Retry( total=RETRY_NUM, backoff_factor=RETRY_BACKOFF, @@ -137,6 +139,18 @@ def _check_request(self, r): r.raise_for_status() def login(self): + if self._user_id and self._access_token: + self._login_dropcam() + else: + self._login_google() + + def _login_dropcam(self): + self._session.post( + f"{API_URL}/dropcam/api/login", + data={"access_token": self._access_token} + ) + + def _login_google(self): headers = { 'User-Agent': USER_AGENT, 'Sec-Fetch-Mode': 'cors',