amazon_dash package

Submodules

amazon_dash.config module

class amazon_dash.config.Config(file, ignore_perms=False, **kwargs)[source]

Bases: dict

Parse and validate yaml Amazon-dash file config. The instance behaves like a dictionary

read()[source]

Parse and validate the config file. The read data is accessible as a dictionary in this instance

Returns:

None

amazon_dash.config.SCHEMA = {'properties': {'confirmations': {'patternProperties': {'^.+$': {'properties': {'is_default': {'type': 'boolean'}, 'service': {'enum': ['telegram', 'pushbullet']}, 'to': {'type': 'integer'}, 'token': {'type': 'string'}}, 'required': ['service'], 'type': 'object'}}, 'properties': {'/': {}}, 'type': 'object'}, 'devices': {'additionalProperties': False, 'patternProperties': {'^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$': {'properties': {'body': {'type': 'string'}, 'cmd': {'type': 'string'}, 'confirmation': {'type': 'string'}, 'content-type': {'type': 'string'}, 'cwd': {'type': 'string'}, 'event': {'type': 'string'}, 'headers': {'type': 'object'}, 'homeassistant': {'type': 'string'}, 'ifttt': {'type': 'string'}, 'method': {'oneOf': [{'pattern': 'GET|get'}, {'pattern': 'HEAD|head'}, {'pattern': 'POST|post'}, {'pattern': 'PUT|put'}, {'pattern': 'DELETE|delete'}, {'pattern': 'CONNECT|connect'}, {'pattern': 'OPTIONS|options'}, {'pattern': 'trace|trace'}, {'pattern': 'PATCH|patch'}], 'type': 'string'}, 'name': {'type': 'string'}, 'url': {'type': 'string'}, 'user': {'type': 'string'}}, 'type': 'object'}}, 'properties': {'/': {}}, 'type': 'object'}, 'settings': {'properties': {'delay': {'type': 'integer'}, 'interface': {'type': 'string'}}, 'type': 'object'}}, 'required': ['devices'], 'title': 'Config', 'type': 'object'}

Json-schema validation

amazon_dash.config.bitperm(s, perm, pos)[source]

Returns zero if there are no permissions for a bit of the perm. of a file. Otherwise it returns a positive value

Parameters:
  • s (os.stat_result) – os.stat(file) object

  • perm (str) – R (Read) or W (Write) or X (eXecute)

  • pos (str) – USR (USeR) or GRP (GRouP) or OTH (OTHer)

Returns:

mask value

Return type:

int

amazon_dash.config.check_config(file, printfn=<built-in function print>)[source]

Command to check configuration file. Raises InvalidConfig on error

Parameters:
  • file (str) – path to config file

  • printfn – print function for success message

Returns:

None

amazon_dash.config.get_file_group(file)[source]

Get file group id

Parameters:

file – Path to file

Returns:

group id

Return type:

int

amazon_dash.config.get_file_owner(file)[source]

Get file owner id

Parameters:

file (str) – Path to file

Returns:

user id

Return type:

int

amazon_dash.config.only_root_write(path)[source]

File is only writable by root

Parameters:

path (str) – Path to file

Returns:

True if only root can write

Return type:

bool

amazon_dash.config.oth_w_perm(file)[source]

Returns True if others have write permission to the file

Parameters:

file (str) – Path to file

Returns:

True if others have permits

Return type:

bool

amazon_dash.discovery module

amazon_dash.discovery.AMAZON_DEVICES = ['F0:D2:F1', '88:71:E5', 'FC:A1:83', 'F0:27:2D', '74:C2:46', '68:37:E9', '78:E1:03', '38:F7:3D', '50:DC:E7', 'A0:02:DC', '0C:47:C9', '74:75:48', 'AC:63:BE', 'FC:A6:67', '18:74:2E', '00:FC:8B', 'FC:65:DE', '6C:56:97', '44:65:0D', '50:F5:DA', '68:54:FD', '40:B4:CD', '00:71:47', '4C:EF:C0', '84:D6:D0', '34:D2:70', 'B4:7C:9C', 'F0:81:73']

Amazon Dash Mac Devices. Source: https://standards.ieee.org/develop/regauth/oui/oui.csv

Snippet for Re-generate this list:

>>> import csv
>>> print('
'.join([':'.join([row[1][i:i+2] for i in range(0, len(row[1]), 2)])
        for row in csv.reader(open('oui.csv')) if row[2] == 'Amazon Technologies Inc.']))
amazon_dash.discovery.BANNED_DEVICES = ['00:00:00:00:00:00']

These mac addresses will not be considered valid results on discovery.

amazon_dash.discovery.discover(interface=None)[source]

Print help and scan devices on screen.

Returns:

None

amazon_dash.discovery.discovery_print(pkt)[source]

Scandevice callback. Register src mac to avoid src repetition. Print device on screen.

Parameters:

pkt (scapy.packet.Packet) – Scapy Packet

Returns:

None

amazon_dash.discovery.mac_id_list = []

Mac addresses already known. Mac addresses only appear once.

amazon_dash.discovery.pkt_text(pkt)[source]

Return source mac address for this Scapy Packet

Parameters:

pkt (scapy.packet.Packet) – Scapy Packet

Returns:

Mac address. Include (Amazon Device) for these devices

Return type:

str

amazon_dash.exceptions module

exception amazon_dash.exceptions.AmazonDashException[source]

Bases: Exception

Amazon Dash base exception. All the exceptions that use this base are captured by the command line.

error_code = 3

Error code to return

exception amazon_dash.exceptions.AudioError[source]

Bases: AmazonDashException

Error on dash button audio hack error.

error_code = 12

Error code to return

exception amazon_dash.exceptions.ConfigFileNotFoundError(file)[source]

Bases: AmazonDashException, FileNotFoundError

The configuration file was not found

error_code = 5

Error code to return

exception amazon_dash.exceptions.ConfigWifiError[source]

Bases: AmazonDashException

Error on dash button Wifi configuration.

error_code = 11

Error code to return

exception amazon_dash.exceptions.ConfirmationError[source]

Bases: AmazonDashException

A An error occurred while sending the confirmation

error_code = 9

Error code to return

exception amazon_dash.exceptions.ExecuteError[source]

Bases: AmazonDashException

A An error occurred while executing a device

error_code = 10

Error code to return

exception amazon_dash.exceptions.InvalidConfig(file=None, extra_body='')[source]

Bases: AmazonDashException

The configuration file has not passed the yaml validation or json-schema validation or exec. class validation

error_code = 6

Error code to return

exception amazon_dash.exceptions.InvalidDevice[source]

Bases: AmazonDashException

Used on test-device command. The mac address device is not in config file

error_code = 8

Error code to return

exception amazon_dash.exceptions.SecurityException[source]

Bases: AmazonDashException

A configuration fault has been found that puts the system at risk

error_code = 4

Error code to return

exception amazon_dash.exceptions.SocketPermissionError[source]

Bases: AmazonDashException

The program must be run as root or the user needs permissions to sniff the traffic

error_code = 7

Error code to return

amazon_dash.exceptions.catch(fn, exception_cls=None, raises=True)[source]

amazon_dash.execute module

class amazon_dash.execute.Execute(name, data)[source]

Bases: object

Execute base class

execute(root_allowed=False)[source]

Execute using self.data

Parameters:

root_allowed (bool) – Only used for ExecuteCmd

Returns:

validate()[source]

Check self.data. Raise InvalidConfig on error

Returns:

None

class amazon_dash.execute.ExecuteCmd(name, data)[source]

Bases: Execute

Execute systemd command

execute(root_allowed=False)[source]

Execute using self.data

Parameters:

root_allowed (bool) – Allow execute as root commands

Returns:

validate()[source]

Check self.data. Raise InvalidConfig on error

Returns:

None

class amazon_dash.execute.ExecuteHomeAssistant(name, data)[source]

Bases: ExecuteOwnApiBase

Send Home Assistant event

https://home-assistant.io/developers/rest_api/#post-apieventsltevent_type

default_port = 8123
execute_name = 'homeassistant'
get_headers()[source]

Get HTTP Headers to send. By default default_headers

Returns:

HTTP Headers

Return type:

dict

get_url()[source]

Home assistant url

Returns:

url

Return type:

str

class amazon_dash.execute.ExecuteIFTTT(name, data)[source]

Bases: ExecuteOwnApiBase

Send IFTTT Webhook event.

execute_name = 'ifttt'
get_url()[source]

IFTTT Webhook url

Returns:

url

Return type:

str

url_pattern = 'https://maker.ifttt.com/trigger/{event}/with/key/{key}'
class amazon_dash.execute.ExecuteOpenHab(name, data)[source]

Bases: ExecuteOwnApiBase

Send Open Hab event

default_content_type = 'text/plain'

default content type to send

default_port = 8080
execute_name = 'openhab'
get_body()[source]

Return “data” value on self.data

Returns:

data to send

Return type:

str

get_url()[source]

Open Hab url

Returns:

url

Return type:

str

class amazon_dash.execute.ExecuteOwnApiBase(name, data)[source]

Bases: ExecuteUrlServiceBase

default_method = 'POST'

default HTTP method

default_port = 0
default_protocol = 'http'
execute_name = None
get_body()[source]

Return “data” value on self.data

Returns:

data to send

Return type:

str

get_url()[source]

API url

Returns:

url

Return type:

str

class amazon_dash.execute.ExecuteUrl(name, data)[source]

Bases: Execute

Call a url

execute(root_allowed=False)[source]

Execute using self.data

Parameters:

root_allowed (bool) – Only used for ExecuteCmd

Returns:

validate()[source]

Check self.data. Raise InvalidConfig on error

Returns:

None

class amazon_dash.execute.ExecuteUrlServiceBase(name, data)[source]

Bases: ExecuteUrl

Base class to create services execute classes

default_body = None

default body to send

default_content_type = 'application/json'

default content type to send

default_headers = None

default HTTP headers to send

default_method = 'GET'

default HTTP method

default_url = None

default url to call

get_body()[source]

Get body to send. By default default_body

Returns:

body content

Return type:

str

get_content_type()[source]

Get HTTP content type to send. By default default_content_type

Returns:

HTTP content type

Return type:

str

get_headers()[source]

Get HTTP Headers to send. By default default_headers

Returns:

HTTP Headers

Return type:

dict

get_method()[source]

Get HTTP method. By default default_method

Returns:

HTTP method

Return type:

str

get_url()[source]

Get url to call. By default default_url

Returns:

url

Return type:

str

amazon_dash.execute.execute_cmd(cmd, cwd=None, timeout=5)[source]

Excecute command on thread

Parameters:
  • cmd – Command to execute

  • cwd – current working directory

Returns:

None

amazon_dash.execute.execute_over_ssh(cmd, ssh, cwd=None, shell='bash')[source]

Excecute command on remote machine using SSH

Parameters:
  • cmd – Command to execute

  • ssh – Server to connect. Port is optional

  • cwd – current working directory

Returns:

None

amazon_dash.execute.get_shell(name)[source]

Absolute path to command

Parameters:

name (str) – command

Returns:

command args

Return type:

list

amazon_dash.execute.run_as_cmd(cmd, user, shell='bash')[source]

Get the arguments to execute a command as a user

Parameters:
  • cmd (str) – command to execute

  • user – User for use

  • shell – Bash, zsh, etc.

Returns:

arguments

Return type:

list

amazon_dash.listener module

amazon_dash.listener.DEFAULT_DELAY = 10

On seconds. By default, 10 seconds. Minimum time that must pass between pulsations of the Amazon Dash button.

class amazon_dash.listener.Device(src, data=None, config=None)[source]

Bases: object

Set the execution method for the device

execute(root_allowed=False)[source]

Execute this device

Parameters:

root_allowed (bool) – Only used for ExecuteCmd

Returns:

None

execute_instance = None

Execute cls instance

property name

Name on self.data or mac address

Returns:

name

Return type:

str

send_confirmation(message, success=True)[source]

Send success or error message to configured confirmation

Parameters:
  • message (str) – Body message to send

  • success (bool) – Device executed successfully to personalize message

Returns:

None

amazon_dash.listener.EXECUTE_CLS = {'cmd': <class 'amazon_dash.execute.ExecuteCmd'>, 'homeassistant': <class 'amazon_dash.execute.ExecuteHomeAssistant'>, 'ifttt': <class 'amazon_dash.execute.ExecuteIFTTT'>, 'openhab': <class 'amazon_dash.execute.ExecuteOpenHab'>, 'url': <class 'amazon_dash.execute.ExecuteUrl'>}

Execute classes registered.

class amazon_dash.listener.Listener(config_path, ignore_perms=False)[source]

Bases: object

Start listener daemon for execute on button press

execute(device)[source]

Execute a device. Used if the time between executions is greater than DEFAULT_DELAY

Parameters:

device (scapy.packet.Packet) – Scapy packet

Returns:

None

on_push(device)[source]

Press button. Check DEFAULT_DELAY.

Parameters:

device (scapy.packet.Packet) – Scapy packet

Returns:

None

root_allowed = False

Only used for ExecuteCmd

run(root_allowed=False)[source]

Start daemon mode

Parameters:

root_allowed (bool) – Only used for ExecuteCmd

Returns:

loop

amazon_dash.listener.last_execution = {}

Last time a device was executed. Value on unix time.

amazon_dash.listener.test_device(device, file, root_allowed=False)[source]

Test the execution of a device without pressing the associated button

Parameters:
  • device (str) – mac address

  • file (str) – config file

  • root_allowed (bool) – only used for ExecuteCmd

Returns:

None

amazon_dash.management module

Amazon Dash.

amazon_dash.management.blue_light_confirm(no_input)[source]
amazon_dash.management.create_logger(name, level=20)[source]

Create a Logger and set handler and formatter

Parameters:
  • name – logger name

  • level – logging level

Returns:

None

amazon_dash.management.latest_release(package)[source]
amazon_dash.management.print_version(ctx, param, value)[source]

amazon_dash.scan module

amazon_dash.scan.scan_devices(fn, lfilter, iface=None)[source]

Sniff packages

Parameters:
  • fn – callback on packet

  • lfilter – filter packages

Returns:

loop

Module contents