Skip to content

Commit

Permalink
Merge pull request #89 from bachya/hass-auth
Browse files Browse the repository at this point in the history
Added support for long-lived Home Assistant access tokens
  • Loading branch information
Nekmo authored Sep 19, 2018
2 parents 143718c + 681a6e4 commit 96e5493
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
10 changes: 7 additions & 3 deletions amazon_dash/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,13 @@ def get_url(self):
return url

def get_headers(self):
return {
'x-ha-access': self.data['access']
} if 'access' in self.data else {}
headers = {}
if 'access_token' in self.data:
headers['Authorization'] = 'Bearer {0}'.format(self.data['access_token'])
elif 'access' in self.data:
headers['x-ha-access'] = self.data['access']

return headers


class ExecuteOpenHab(ExecuteOwnApiBase):
Expand Down
9 changes: 8 additions & 1 deletion amazon_dash/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_get_shell(self):
class TestExecuteUrl(unittest.TestCase):
no_body_methods = ['get', 'head', 'delete', 'connect', 'options', 'trace']
url = 'http://domain.com'

def setUp(self):
super(TestExecuteUrl, self).setUp()
self.session_mock = requests_mock.Mocker()
Expand Down Expand Up @@ -297,6 +297,13 @@ def test_execute_with_access(self):
assis.execute()
self.assertTrue(m.called_once)

def test_execute_with_access_token(self):
with requests_mock.mock() as m:
m.post(self.url, text='success', request_headers={'Authorization': 'Bearer abcde12345'})
assis = ExecuteHomeAssistant('key', self.default_data(extra_data={'access_token': 'abcde12345'}))
assis.execute()
self.assertTrue(m.called_once)


class TestExecuteOpenHab(unittest.TestCase):
path = '/rest/items/test'
Expand Down
14 changes: 11 additions & 3 deletions docs/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ for each device. The available exection methods are:

* **cmd**: local command line command. Arguments can be placed after the command.
* **url**: Call a url.
* **homeassistant**: send event to Homeassistant. This argument must be the address to the hass server (protocol and
* **homeassistant**: send event to Home Assistant. This argument must be the address to the hass server (protocol and
port are optional. By default http and 8123, respectively).
* **openhab**: send event to OpenHAB. This argument must be the address to the hass server (protocol and
port are optional. By default http and 8080, respectively).
Expand Down Expand Up @@ -248,13 +248,21 @@ Example:
confirmation: send-tg
Homeassistant event
Home Assistant event
~~~~~~~~~~~~~~~~~~~
When the **homeassistant execution method** is used, the following options are available.

* **event** (required): Event name to send.
* **data**: Event data to send. Use json as string.
* **access**: HomeAssistant password for API (``x-ha-access`` header).
* **access_token**: Long-lived Home Assistant access token.
* **access**: Home Assistant legacy API password (``x-ha-access`` header).

Starting with version 0.78 of Home Assistant, there are two ways Amazon Dash can authenticate:

1. By providing a long-lived access token (generated within your Home Assistant profile page) via the ``access_token`` option.
2. By providing the legacy Home Assistant API password via the ``access`` option.

Although both options currently work, the Home Assistant project plans to deprecate (and likely remove) the legacy API password in the future; therefore, to properly future proof your Amazon Dash setup, the long-lived access token option is recommended.

The protocol and the port in the address of the Homeassistant server are optional. The syntax of the address is:
``[<protocol>://]<server>[:<port>]. For example: ``https://hassio.local:1234``.
Expand Down

0 comments on commit 96e5493

Please sign in to comment.