Skip to content

Commit 9b3cdeb

Browse files
committed
Merge branch 'develop'
2 parents f6d1830 + fb8c620 commit 9b3cdeb

14 files changed

+166
-13
lines changed

.bumpversion.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.2.0a1
2+
current_version = 1.2.0
33
commit = True
44
tag = True
55

HISTORY.rst

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
History
33
=======
44

5+
v1.3.0 (2018-09-23)
6+
-------------------
7+
8+
- Pushbullet notifications
9+
- Home Assistant ``access_token`` option
10+
- Amazon-dash v2.0.0 disclaimer
11+
12+
513
v1.2.0 (2018-09-03)
614
-------------------
715

README.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Also available on `AUR <https://aur.archlinux.org/packages/amazon-dash-git/>`_.
9393
openhab: 192.168.1.140
9494
item: open_door
9595
state: "ON"
96+
confirmation: send-pb
9697
44:65:0D:75:A7:B2: # IFTTT example
9798
name: Pompadour
9899
ifttt: cdxxx-_gEJ3wdU04yyyzzz
@@ -104,6 +105,10 @@ Also available on `AUR <https://aur.archlinux.org/packages/amazon-dash-git/>`_.
104105
token: '402642618:QwGDgiKE3LqdkNAtBkq0UEeBoDdpZYw8b4h'
105106
to: 24291592
106107
is_default: false
108+
send-pb:
109+
service: pushbullet
110+
token: 'o.BbbPYjJizbPr2gSWgXGmqNTt6T9Rew51'
111+
is_default: false
107112
108113
109114
**UPGRADE** from `previous versions <http://docs.nekmo.org/amazon-dash/installation.html>`_
@@ -119,7 +124,7 @@ The following execution methods are supported with your Amazon Dash button with
119124

120125

121126
Amazon-dash also allows you to **send a confirmation** after pressing a button. You will also receive a message in
122-
case of failure. Currently only **Telegram** is supported.
127+
case of failure. **Telegram** and **Pushbullet** are supported.
123128

124129

125130
For more information see

amazon-dash.png

-2 KB
Loading

amazon_dash/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = '1.2.0a1'
2+
__version__ = '1.2.0'

amazon_dash/config.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@
106106
"type": "object",
107107
"properties": {
108108
"service": {
109-
"enum": ['telegram']
109+
"enum": [
110+
'telegram',
111+
'pushbullet',
112+
]
110113
},
111114
"token": {
112115
"type": "string",

amazon_dash/confirmations.py

+40
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,48 @@ def send(self, message, success=True):
4747
))
4848

4949

50+
class PushbulletConfirmation(ConfirmationBase):
51+
url_base = 'https://api.pushbullet.com/v2/pushes'
52+
name = 'pushbullet'
53+
required_fields = ('token',)
54+
one_field_of = {'device_iden', 'email', 'channel_tag', 'client_id'}
55+
to_field = None
56+
57+
def __init__(self, data):
58+
one_fields = set(data) & self.one_field_of
59+
if len(one_fields) > 1:
60+
raise InvalidConfig(extra_body='Only one in {} is required for {} notifications'.format(
61+
', '.join(one_fields), self.name))
62+
elif one_fields:
63+
self.to_field = one_fields.pop()
64+
super(PushbulletConfirmation, self).__init__(data)
65+
66+
def get_data(self, body, title=''):
67+
data = {
68+
"type": "note",
69+
"body": body,
70+
}
71+
if title:
72+
data["title"] = title
73+
if self.to_field:
74+
data[self.to_field] = self.data[self.to_field]
75+
return data
76+
77+
def send(self, message, success=True):
78+
try:
79+
r = requests.post(self.url_base, json=self.get_data(message),
80+
headers={'Access-Token': self.data['token']})
81+
except RequestException as e:
82+
raise ConfirmationError('Unable to connect to Pushbullet servers on pushbullet confirmation: {}'.format(e))
83+
try:
84+
r.json()
85+
except JSONDecodeError:
86+
raise ConfirmationError('Invalid JSON response in pushbullet confirmation (server error?)')
87+
88+
5089
CONFIRMATIONS = {
5190
'telegram': TelegramConfirmation,
91+
'pushbullet': PushbulletConfirmation,
5292
'disabled': DisabledConfirmation,
5393
}
5494

amazon_dash/execute.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ def get_url(self):
341341
return url
342342

343343
def get_headers(self):
344-
return {
345-
'x-ha-access': self.data['access']
346-
} if 'access' in self.data else {}
344+
headers = {}
345+
if 'access_token' in self.data:
346+
headers['Authorization'] = 'Bearer {0}'.format(self.data['access_token'])
347+
elif 'access' in self.data:
348+
headers['x-ha-access'] = self.data['access']
349+
350+
return headers
347351

348352

349353
class ExecuteOpenHab(ExecuteOwnApiBase):

amazon_dash/install/amazon-dash.yml

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ devices:
5353
# token: '402642618:QwGDgiKE3LqdkNAtBkq0UEeBoDdpZYw8b4h' # Telegram token. Get it from Bothfather
5454
# to: 24291592 # Your Telegram id. You can get it using @get_id_bot
5555
# is_default: true # Use by default this confirmation for all devices
56+
# send-pb:
57+
# service: pushbullet
58+
# token: 'o.BbbPYjJizbPr2gSWgXGmqNTt6T9Rew51'
59+
# is_default: false
5660

5761
# Need help? See the documentation:
5862
# http://docs.nekmo.org/amazon-dash/config_file.html

amazon_dash/tests/test_confirmations.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import requests_mock
55

66
from amazon_dash.confirmations import get_confirmation, DisabledConfirmation, get_confirmation_instance, \
7-
TelegramConfirmation, ConfirmationBase
7+
TelegramConfirmation, ConfirmationBase, PushbulletConfirmation
88
from amazon_dash.exceptions import InvalidConfig, ConfirmationError
99

1010

@@ -71,3 +71,40 @@ def test_server_error(self):
7171
m.post(telegram.url_base.format('foo'), exc=requests.exceptions.ConnectTimeout)
7272
with self.assertRaises(ConfirmationError):
7373
telegram.send('spam')
74+
75+
76+
class TestPushbulletConfirmation(unittest.TestCase):
77+
def get_pushbullet(self, extra=None):
78+
extra = extra or {}
79+
return PushbulletConfirmation(dict({'token': 'foo'}, **extra))
80+
81+
def test_send(self):
82+
pushbullet = self.get_pushbullet()
83+
with requests_mock.mock() as m:
84+
m.post(pushbullet.url_base, text='{}')
85+
pushbullet.send('spam')
86+
self.assertTrue(m.called_once)
87+
88+
def test_invalid_json(self):
89+
pushbullet = self.get_pushbullet()
90+
with requests_mock.mock() as m:
91+
m.post(pushbullet.url_base, text='{"}invalid')
92+
with self.assertRaises(ConfirmationError):
93+
pushbullet.send('spam')
94+
95+
def test_server_error(self):
96+
pushbullet = self.get_pushbullet()
97+
with requests_mock.mock() as m:
98+
m.post(pushbullet.url_base, exc=requests.exceptions.ConnectTimeout)
99+
with self.assertRaises(ConfirmationError):
100+
pushbullet.send('spam')
101+
102+
def test_extra_to(self):
103+
with self.assertRaises(InvalidConfig):
104+
self.get_pushbullet({'device_iden': 'foo', 'email': 'bar'})
105+
106+
def test_to_device_iden(self):
107+
pushbullet = self.get_pushbullet({'device_iden': 'bar'})
108+
with requests_mock.mock() as m:
109+
m.post(pushbullet.url_base, additional_matcher=lambda r: r.json().get('device_iden') == 'bar', text='{}')
110+
pushbullet.send('spam')

amazon_dash/tests/test_execute.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_get_shell(self):
132132
class TestExecuteUrl(unittest.TestCase):
133133
no_body_methods = ['get', 'head', 'delete', 'connect', 'options', 'trace']
134134
url = 'http://domain.com'
135-
135+
136136
def setUp(self):
137137
super(TestExecuteUrl, self).setUp()
138138
self.session_mock = requests_mock.Mocker()
@@ -297,6 +297,13 @@ def test_execute_with_access(self):
297297
assis.execute()
298298
self.assertTrue(m.called_once)
299299

300+
def test_execute_with_access_token(self):
301+
with requests_mock.mock() as m:
302+
m.post(self.url, text='success', request_headers={'Authorization': 'Bearer abcde12345'})
303+
assis = ExecuteHomeAssistant('key', self.default_data(extra_data={'access_token': 'abcde12345'}))
304+
assis.execute()
305+
self.assertTrue(m.called_once)
306+
300307

301308
class TestExecuteOpenHab(unittest.TestCase):
302309
path = '/rest/items/test'

docs/config_file.rst

+47-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Real example:
6161
openhab: 192.168.1.140
6262
item: open_door
6363
state: "ON"
64+
confirmation: send-pb
6465
44:65:0D:75:A7:B2:
6566
name: Pompadour
6667
ifttt: cdxxx-_gEJ3wdU04yyyzzz
@@ -72,6 +73,10 @@ Real example:
7273
token: '402642618:QwGDgiKE3LqdkNAtBkq0UEeBoDdpZYw8b4h'
7374
to: 24291592
7475
is_default: false
76+
send-pb:
77+
service: pushbullet
78+
token: 'o.BbbPYjJizbPr2gSWgXGmqNTt6T9Rew51'
79+
is_default: false
7580
7681
7782
Common options
@@ -98,7 +103,7 @@ for each device. The available exection methods are:
98103

99104
* **cmd**: local command line command. Arguments can be placed after the command.
100105
* **url**: Call a url.
101-
* **homeassistant**: send event to Homeassistant. This argument must be the address to the hass server (protocol and
106+
* **homeassistant**: send event to Home Assistant. This argument must be the address to the hass server (protocol and
102107
port are optional. By default http and 8123, respectively).
103108
* **openhab**: send event to OpenHAB. This argument must be the address to the hass server (protocol and
104109
port are optional. By default http and 8080, respectively).
@@ -248,13 +253,21 @@ Example:
248253
confirmation: send-tg
249254
250255
251-
Homeassistant event
252-
~~~~~~~~~~~~~~~~~~~
256+
Home Assistant event
257+
~~~~~~~~~~~~~~~~~~~~
253258
When the **homeassistant execution method** is used, the following options are available.
254259

255260
* **event** (required): Event name to send.
256261
* **data**: Event data to send. Use json as string.
257-
* **access**: HomeAssistant password for API (``x-ha-access`` header).
262+
* **access_token**: Long-lived Home Assistant access token.
263+
* **access**: Home Assistant legacy API password (``x-ha-access`` header).
264+
265+
Starting with version 0.78 of Home Assistant, there are two ways Amazon Dash can authenticate:
266+
267+
1. By providing a long-lived access token (generated within your Home Assistant profile page) via the ``access_token`` option.
268+
2. By providing the legacy Home Assistant API password via the ``access`` option.
269+
270+
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.
258271

259272
The protocol and the port in the address of the Homeassistant server are optional. The syntax of the address is:
260273
``[<protocol>://]<server>[:<port>]. For example: ``https://hassio.local:1234``.
@@ -337,6 +350,7 @@ Confirmations
337350
-------------
338351
The following **services** are supported to send confirmation messages.
339352

353+
340354
Telegram
341355
~~~~~~~~
342356
For use a telegram service you need to define:
@@ -361,3 +375,32 @@ have not started a conversation before.
361375
token: '402642618:QwGDgiKE3LqdkNAtBkq0UEeBoDdpZYw8b4h'
362376
to: 24291592
363377
is_default: false
378+
379+
380+
Pushbullet
381+
~~~~~~~~~~
382+
For use a pushbullet service you need to define:
383+
384+
* **token**: Get it in your pushbullet Access Token (create a token): https://www.pushbullet.com/#settings/account
385+
386+
Optional: set a target (you can only set a target):
387+
388+
* **device_iden**: Device identifier. To get your device identifier:
389+
``$ curl --header 'Access-Token: <YOUR TOKEN>' https://api.pushbullet.com/v2/devices``
390+
* **email**: Useful to send a message to your contacts.
391+
* **channel_tag**: Send to all subscribers to your channel.
392+
* **client_iden**: Send to all users who have granted access to your OAuth client.
393+
394+
.. code-block:: yaml
395+
396+
# amazon-dash.yml
397+
# ---------------
398+
settings:
399+
delay: 10
400+
devices:
401+
# ...
402+
confirmations:
403+
send-pb:
404+
service: pushbullet
405+
token: 'o.BbbPYjJizbPr2gSWgXGmqNTt6T9Rew51'
406+
is_default: false

docs/troubleshooting.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ are:
1111
* Tcpdump.
1212
* Sudo
1313

14+
Amazon-dash v2.0.0 will only be compatible with Python 3.6+. This version is currently in development.
15+
1416

1517
Why root is required
1618
--------------------

images/amazon-dash.xcf

11.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)