Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Cleanup #9

Merged
merged 2 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
template: |
## Changes

$CHANGES

## Contributors

$CONTRIBUTORS
name-template: 'v$NEXT_PATCH_VERSION'
tag-template: 'v$NEXT_PATCH_VERSION'
14 changes: 14 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -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 }}
8 changes: 0 additions & 8 deletions custom_components/badnest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -36,8 +30,6 @@ def setup(hass, config):

hass.data[DOMAIN] = {
'api': NestAPI(
user_id,
access_token,
issue_token,
cookie,
),
Expand Down
35 changes: 22 additions & 13 deletions custom_components/badnest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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}',
Expand Down
5 changes: 0 additions & 5 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down