Skip to content
Open
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
79 changes: 79 additions & 0 deletions bridgeBackup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'''
Echo Echo Bridge Backup/Restore: Script to backup or restore Echo Bridge configuration
Python Version: >= 3.0
'''
import argparse
import requests
import json
import csv
import sys


parser = argparse.ArgumentParser(description='EchOHBridge Configurator')
parser.add_argument('echoBridgeIP', type=str,
help='Echo HA Bridge IP address')
parser.add_argument('echoBridgePort', type=str,
help='Echo HA Bridge port')
parser.add_argument('mode', type=str,
help='(R)estore or (B)ackup')
parser.add_argument('file', type=str,
help='File to restore from or backup to')

args = parser.parse_args()
mode = args.mode.lower()


if (mode == 'b') or (mode == 'backup'):
print ('Backing up Amazon Echo Bridge configuration from: ',args.echoBridgeIP,':',args.echoBridgePort,sep="")
outputDict = csv.DictWriter(open(args.file, 'w'),fieldnames=['name','deviceType','onUrl','offUrl','contentType','contentBody','httpVerb'])
outputDict.writeheader()
r = requests.get('http://' + args.echoBridgeIP + ':' + args.echoBridgePort + '/api/devices')
for entry in r.json()[0]['content']:
tempDict = {'name':entry['name'],
'deviceType':entry['deviceType'],
'onUrl':entry['onUrl'],
'offUrl':entry['offUrl'],
'contentType':entry['contentType'],
'contentBody':entry['contentBody'],
'httpVerb':entry['httpVerb']
}
outputDict.writerow(tempDict)
print (' Backed up item:',entry['name'])

if (mode == 'r') or (mode == 'restore'):
print ('Restoring Amazon Echo Bridge configuration to: ',args.echoBridgeIP,':',args.echoBridgePort,sep="")
inputDict = csv.DictReader(open (args.file))
if inputDict.fieldnames != ['name','deviceType','onUrl','offUrl','contentType','contentBody','httpVerb']:
print (args.input, 'input file formatted incorrectly')
print (' First line of input file should be: "name,deviceType,onUrl,offUrl,contentType,contentBody,httpVerb"' )
print (' Each line after should contain the Echo item name followed by a comma then the OpenHAB switch to be controlled')
print (' Aborting restore')
sys.exit()
for entry in inputDict:
if not entry['contentType']:
contentType = None
else:
contentType=entry['contentType']
if not entry['contentBody']:
contentBody = None
else:
contentBody=entry['contentBody']
if not entry['httpVerb']:
httpVerb = None
else:
httpVerb = entry['httpVerb']

values = {'name':entry['name'],
'deviceType':entry['deviceType'],
'onUrl':entry['onUrl'],
'offUrl':entry['offUrl'],
'contentType':contentType,
'contentBody':contentBody,
'httpVerb':httpVerb
}

r = requests.post('http://' + args.echoBridgeIP + ':' + args.echoBridgePort + '/api/devices', data=json.dumps(values), headers = {'Content-Type':'application/json'})
if r.status_code == 201:
print (' Succesfully restored item:',entry['name'])
else:
print (' Failed to restore item:',entry['name'])
46 changes: 46 additions & 0 deletions openHabConfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
EchOHBridge Configurator: Script to automate configuration of Echo Bridge to interface with OpenHAB
Python Version: >= 3.0
'''
import argparse
import requests
import json
import csv
import sys


parser = argparse.ArgumentParser(description='EchOHBridge Configurator')
parser.add_argument('echoBridgeIP', type=str,
help='Echo HA Bridge IP address')
parser.add_argument('echoBridgePort', type=str,
help='Echo HA Bridge port')
parser.add_argument('openHabIP', type=str,
help='OpenHAB IP address')
parser.add_argument('openHabPort', type=str,
help='OpenHAB Port')
parser.add_argument('input', type=str,
help='CSV file containing echo name (name) and OpenHAB name (item)')

args = parser.parse_args()

print ('Configuring Amazon Echo Bridge at: ',args.echoBridgeIP,':',args.echoBridgePort,sep="")
inputDict = csv.DictReader(open (args.input))
if inputDict.fieldnames != ['name', 'item']:
print (args.input, 'input file formatted incorrectly')
print (' First line of input file should be: "name,item"' )
print (' Each line after should contain the Echo item name followed by a comma then the OpenHAB switch to be controlled')
sys.exit()

url = 'http://' + args.echoBridgeIP + ':' + args.echoBridgePort + '/api/devices'
headers = {'Content-Type':'application/json'}

for entry in inputDict:
values = {'name' : entry['name'],
'deviceType' : 'switch',
'onUrl' : 'http://' + args.openHabIP + ':' + args.openHabPort + '/CMD?' + entry['item'] + '=ON',
'offUrl' : 'http://' + args.openHabIP + ':' + args.openHabPort + '/CMD?' + entry['item'] + '=OFF' }
r = requests.post(url, data=json.dumps(values), headers=headers)
if r.status_code == 201:
print('Created item: ', entry['name'])
else:
print('Item creation failed: ', entry['name'])