From ee37192f74b99c445d21065f492761e244bd178b Mon Sep 17 00:00:00 2001 From: Matthew Schick Date: Sun, 31 May 2020 17:03:20 -0400 Subject: [PATCH 1/2] Various cleanup and bugfixes --- custom_components/badnest/__init__.py | 8 ------ custom_components/badnest/api.py | 35 +++++++++++++++++---------- info.md | 5 ---- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/custom_components/badnest/__init__.py b/custom_components/badnest/__init__.py index 02e4d2e..68dc7d0 100644 --- a/custom_components/badnest/__init__.py +++ b/custom_components/badnest/__init__.py @@ -9,10 +9,6 @@ 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, @@ -26,8 +22,6 @@ 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: @@ -36,8 +30,6 @@ def setup(hass, config): 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 41f751d..561ad2b 100644 --- a/custom_components/badnest/api.py +++ b/custom_components/badnest/api.py @@ -66,11 +66,11 @@ def wrapper(*args, **kwargs): try: func(*args, **kwargs) except AuthorizationRequired: - _LOGGER.debug('Refreshing login info') + _LOGGER.debug("Refreshing login info") args[0].login() func(*args, **kwargs) except (HTTPError, RetryError) as e: - _LOGGER.error('Upstream error: ' + e) + _LOGGER.error(f"Upstream error: {e}") except RequestException as e: _LOGGER.error(e) return func(*args, **kwargs) @@ -83,14 +83,12 @@ class AuthorizationRequired(Exception): class NestAPI(): def __init__(self, - user_id, - access_token, issue_token, cookie): self.device_data = {} self._wheres = {} - self._user_id = user_id - self._access_token = access_token + self._user_id = None + self._access_token = None self._retries = Retry( total=RETRY_NUM, backoff_factor=RETRY_BACKOFF, @@ -138,18 +136,21 @@ def _check_request(self, r): r.raise_for_status() def login(self): - if self._issue_token and self._cookie: - self._login_google(self._issue_token, self._cookie) - - def _login_google(self, issue_token, cookie): headers = { 'User-Agent': USER_AGENT, 'Sec-Fetch-Mode': 'cors', 'X-Requested-With': 'XmlHttpRequest', 'Referer': 'https://accounts.google.com/o/oauth2/iframe', - 'cookie': cookie + 'cookie': self._cookie } - r = self._session.get(url=issue_token, headers=headers) + try: + r = self._session.get(url=self._issue_token, headers=headers) + r.raise_for_status() + except (HTTPError, RetryError) as e: + _LOGGER.error(f"Upstream error: {e}") + except RequestException as e: + _LOGGER.error(e) + access_token = r.json()['access_token'] headers = { @@ -164,9 +165,17 @@ def _login_google(self, issue_token, cookie): "google_oauth_access_token": access_token, "policy_id": 'authproxy-oauth-policy' } - r = self._session.post(url=URL_JWT, headers=headers, params=params) + try: + r = self._session.post(url=URL_JWT, headers=headers, params=params) + r.raise_for_status() + except (HTTPError, RetryError) as e: + _LOGGER.error(f"Upstream error: {e}") + except RequestException as e: + _LOGGER.error(e) + self._user_id = r.json()['claims']['subject']['nestId']['id'] self._access_token = r.json()['jwt'] + self._session.headers.update({ "Authorization": f"Basic {self._access_token}", "cookie": f'user_token={self._access_token}', diff --git a/info.md b/info.md index ff248a3..b8824cc 100644 --- a/info.md +++ b/info.md @@ -30,17 +30,12 @@ will never be as reliable as the original API ## Configuration -The camera's region is one of `us` or `eu` depending on your region. -If you're not in the US or EU, you should be able to add your -two-character country code, and it should work. - ### Example configuration.yaml - When you are using the Google Auth Login ```yaml badnest: issue_token: "https://accounts.google.com/o/oauth2/iframerpc....." cookie: "OCAK=......" - region: us climate: - platform: badnest From 3647697c1cd7b7a2854efedc4430b46b559eb5c9 Mon Sep 17 00:00:00 2001 From: Matthew Schick Date: Sun, 31 May 2020 17:03:38 -0400 Subject: [PATCH 2/2] Add back release-drafter stuff --- .github/release-drafter.yml | 10 ++++++++++ .github/workflows/release-drafter.yml | 14 ++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .github/release-drafter.yml create mode 100644 .github/workflows/release-drafter.yml diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml new file mode 100644 index 0000000..9c61f2c --- /dev/null +++ b/.github/release-drafter.yml @@ -0,0 +1,10 @@ +template: | + ## Changes + + $CHANGES + + ## Contributors + + $CONTRIBUTORS +name-template: 'v$NEXT_PATCH_VERSION' +tag-template: 'v$NEXT_PATCH_VERSION' diff --git a/.github/workflows/release-drafter.yml b/.github/workflows/release-drafter.yml new file mode 100644 index 0000000..a9c346b --- /dev/null +++ b/.github/workflows/release-drafter.yml @@ -0,0 +1,14 @@ +name: Release Drafter + +on: + push: + branches: + - master + +jobs: + update_release_draft: + runs-on: ubuntu-latest + steps: + - uses: release-drafter/release-drafter@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}