Last active
January 18, 2016 22:25
-
-
Save enavarro222/7ec75bb55714ff1d3dc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
"""Command line tool to configure Homie devices.""" | |
__author__ = "Emmanuel Navarro <enavarro222@gmail.com>" | |
__license__ = "WTFPL" | |
import sys | |
import getpass | |
import argparse | |
import requests | |
def main(): | |
parser = argparse.ArgumentParser(description=__doc__) | |
parser.add_argument('--networks', default=False, action="store_true", | |
help='List the wifi networks that Homie device can see.') | |
parser.add_argument('-N', '--name', help='Device name.') | |
parser.add_argument('-s', '--ssid', help='SSID device should to connect to.') | |
parser.add_argument('-p', '--password', help='Wifi password (let empty to enter it interactively)') | |
parser.add_argument('-H', '--homie-host', help='Homie MQTT server hostname (or IP).') | |
parser.add_argument('-P', '--homie-port', type=int, help='Homie MQTT server port.') | |
parser.add_argument('--homie-ota-path', help='Homie OTA server path.') | |
parser.add_argument('--homie-ota-port', type=int, help='Homie OTA server port.') | |
args = parser.parse_args() | |
HOMIE_CONFIG_URL = "http://homie.config/" | |
try: | |
check = requests.get(HOMIE_CONFIG_URL + "heart") | |
except requests.ConnectionError: | |
print("Homie device not founded !") | |
return | |
if args.networks: | |
networks = requests.get(HOMIE_CONFIG_URL + "networks").json() | |
print("Available networks:") | |
for num, network in enumerate(networks["networks"]): | |
network["num"] = num | |
if "encryption" not in network: | |
network["encryption"] = "none" | |
print(" {num}) {ssid} ({encryption}, signal: {rssi})".format(**network)) | |
else: | |
if not args.name: | |
parser.error("Device name missing") | |
if not args.ssid: | |
parser.error("SSID missing") | |
if not args.homie_host: | |
parser.error("Homie host missing") | |
if not args.password: | |
print("Wifi password:") | |
password = getpass.getpass() | |
else: | |
password = args.password | |
config = { | |
"name": args.name, | |
"wifi_ssid": args.ssid, | |
"wifi_password": password, | |
"homie_host": args.homie_host | |
} | |
if args.homie_port: | |
config["homie_port"] = args.homie_port | |
if args.homie_ota_path: | |
config["homie_ota_path"] = args.homie_ota_path | |
if args.homie_ota_port: | |
config["homie_ota_port"] = args.homie_ota_port | |
from pprint import pprint | |
pprint(config) | |
res = requests.put(HOMIE_CONFIG_URL + "config", json=config) | |
if res.status_code == 200: | |
print("Config succesfuly sent") | |
else: | |
print("Something went wrong... (status: %d)" % res.status_code) | |
return res.status_code | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment