2018-04-07 01:23:03 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import codecs
|
|
|
|
import os
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
import esphome.config_validation as cv
|
2019-03-03 16:50:06 +01:00
|
|
|
from esphome.helpers import color, get_bool_env
|
2018-04-10 17:17:46 +02:00
|
|
|
# pylint: disable=anomalous-backslash-in-string
|
2019-02-13 16:54:02 +01:00
|
|
|
from esphome.pins import ESP32_BOARD_PINS, ESP8266_BOARD_PINS
|
|
|
|
from esphome.py_compat import safe_input, text_type
|
|
|
|
from esphome.storage_json import StorageJSON, ext_storage_path
|
|
|
|
from esphome.util import safe_print
|
2018-09-23 18:58:41 +02:00
|
|
|
|
2019-01-02 14:11:11 +01:00
|
|
|
CORE_BIG = r""" _____ ____ _____ ______
|
2018-04-07 01:23:03 +02:00
|
|
|
/ ____/ __ \| __ \| ____|
|
2018-04-10 17:17:46 +02:00
|
|
|
| | | | | | |__) | |__
|
|
|
|
| | | | | | _ /| __|
|
|
|
|
| |___| |__| | | \ \| |____
|
2018-04-07 01:23:03 +02:00
|
|
|
\_____\____/|_| \_\______|
|
|
|
|
"""
|
2019-01-02 14:11:11 +01:00
|
|
|
ESP_BIG = r""" ______ _____ _____
|
2018-04-10 17:17:46 +02:00
|
|
|
| ____|/ ____| __ \\
|
2018-04-07 01:23:03 +02:00
|
|
|
| |__ | (___ | |__) |
|
2018-04-10 17:17:46 +02:00
|
|
|
| __| \___ \| ___/
|
|
|
|
| |____ ____) | |
|
|
|
|
|______|_____/|_|
|
2018-04-07 01:23:03 +02:00
|
|
|
"""
|
2019-01-02 14:11:11 +01:00
|
|
|
WIFI_BIG = r""" __ ___ ______ _
|
2018-04-07 01:23:03 +02:00
|
|
|
\ \ / (_) ____(_)
|
2018-04-10 17:17:46 +02:00
|
|
|
\ \ /\ / / _| |__ _
|
2018-04-07 01:23:03 +02:00
|
|
|
\ \/ \/ / | | __| | |
|
|
|
|
\ /\ / | | | | |
|
|
|
|
\/ \/ |_|_| |_|
|
|
|
|
"""
|
2019-01-02 14:11:11 +01:00
|
|
|
OTA_BIG = r""" ____ _______
|
2018-04-10 17:17:46 +02:00
|
|
|
/ __ \__ __|/\\
|
|
|
|
| | | | | | / \\
|
|
|
|
| | | | | | / /\ \\
|
|
|
|
| |__| | | |/ ____ \\
|
2018-04-07 01:23:03 +02:00
|
|
|
\____/ |_/_/ \_\\
|
|
|
|
"""
|
|
|
|
|
2019-02-13 16:54:02 +01:00
|
|
|
BASE_CONFIG = u"""esphome:
|
2018-04-07 01:23:03 +02:00
|
|
|
name: {name}
|
|
|
|
platform: {platform}
|
|
|
|
board: {board}
|
|
|
|
|
|
|
|
wifi:
|
2019-03-05 14:14:38 +01:00
|
|
|
ssid: "{ssid}"
|
|
|
|
password: "{psk}"
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
# Enable logging
|
|
|
|
logger:
|
|
|
|
|
2018-12-18 19:31:43 +01:00
|
|
|
# Enable Home Assistant API
|
|
|
|
api:
|
2018-04-07 01:23:03 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
|
2019-04-15 21:49:21 +02:00
|
|
|
def sanitize_double_quotes(value):
|
|
|
|
return value.replace('\\', '\\\\').replace('"', '\\"')
|
|
|
|
|
|
|
|
|
2018-05-21 16:40:22 +02:00
|
|
|
def wizard_file(**kwargs):
|
|
|
|
config = BASE_CONFIG.format(**kwargs)
|
|
|
|
|
2018-12-18 19:31:43 +01:00
|
|
|
if kwargs['password']:
|
2019-04-15 21:49:21 +02:00
|
|
|
config += u' password: "{0}"\n\nota:\n password: "{0}"\n'.format(kwargs['password'])
|
2018-05-21 16:40:22 +02:00
|
|
|
else:
|
2018-12-18 19:31:43 +01:00
|
|
|
config += u"\nota:\n"
|
2018-05-21 16:40:22 +02:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
def wizard_write(path, **kwargs):
|
|
|
|
name = kwargs['name']
|
|
|
|
board = kwargs['board']
|
2019-04-15 21:49:21 +02:00
|
|
|
|
|
|
|
kwargs['ssid'] = sanitize_double_quotes(kwargs['ssid'])
|
|
|
|
kwargs['psk'] = sanitize_double_quotes(kwargs['psk'])
|
|
|
|
kwargs['password'] = sanitize_double_quotes(kwargs['password'])
|
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
if 'platform' not in kwargs:
|
|
|
|
kwargs['platform'] = 'ESP8266' if board in ESP8266_BOARD_PINS else 'ESP32'
|
|
|
|
platform = kwargs['platform']
|
|
|
|
|
2019-03-31 11:04:41 +02:00
|
|
|
with codecs.open(path, 'w', 'utf-8') as f_handle:
|
2018-12-05 21:22:06 +01:00
|
|
|
f_handle.write(wizard_file(**kwargs))
|
|
|
|
storage = StorageJSON.from_wizard(name, name + '.local', platform, board)
|
|
|
|
storage_path = ext_storage_path(os.path.dirname(path), os.path.basename(path))
|
|
|
|
storage.save(storage_path)
|
|
|
|
|
|
|
|
|
2019-03-03 16:50:06 +01:00
|
|
|
if get_bool_env('ESPHOME_QUICKWIZARD'):
|
2018-04-11 22:51:56 +02:00
|
|
|
def sleep(time):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
from time import sleep
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
def safe_print_step(step, big):
|
|
|
|
safe_print()
|
|
|
|
safe_print()
|
|
|
|
safe_print("============= STEP {} =============".format(step))
|
|
|
|
safe_print(big)
|
|
|
|
safe_print("===================================")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.25)
|
|
|
|
|
|
|
|
|
|
|
|
def default_input(text, default):
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
|
|
|
safe_print(u"Press ENTER for default ({})".format(default))
|
2019-01-02 14:11:11 +01:00
|
|
|
return safe_input(text.format(default)) or default
|
2018-04-07 01:23:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
# From https://stackoverflow.com/a/518232/8924614
|
2018-04-10 17:17:46 +02:00
|
|
|
def strip_accents(string):
|
2019-01-02 14:11:11 +01:00
|
|
|
return u''.join(c for c in unicodedata.normalize('NFD', text_type(string))
|
2018-04-07 01:23:03 +02:00
|
|
|
if unicodedata.category(c) != 'Mn')
|
|
|
|
|
|
|
|
|
|
|
|
def wizard(path):
|
|
|
|
if not path.endswith('.yaml') and not path.endswith('.yml'):
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Please make your configuration file {} have the extension .yaml or .yml"
|
|
|
|
u"".format(color('cyan', path)))
|
2018-04-07 01:23:03 +02:00
|
|
|
return 1
|
|
|
|
if os.path.exists(path):
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Uh oh, it seems like {} already exists, please delete that file first "
|
|
|
|
u"or chose another configuration file.".format(color('cyan', path)))
|
2018-04-07 01:23:03 +02:00
|
|
|
return 1
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Hi there!")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1.5)
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print("I'm the wizard of ESPHome :)")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1.25)
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print("And I'm here to help you get started with ESPHome.")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(2.0)
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print("In 4 steps I'm going to guide you through creating a basic "
|
2018-10-20 12:41:00 +02:00
|
|
|
"configuration file for your custom ESP8266/ESP32 firmware. Yay!")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(3.0)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
|
|
|
safe_print_step(1, CORE_BIG)
|
|
|
|
safe_print("First up, please choose a " + color('green', 'name') + " for your node.")
|
|
|
|
safe_print("It should be a unique name that can be used to identify the device later.")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("For example, I like calling the node in my living room {}.".format(
|
2018-04-07 01:23:03 +02:00
|
|
|
color('bold_white', "livingroom")))
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
2019-01-02 14:11:11 +01:00
|
|
|
name = safe_input(color("bold_white", "(name): "))
|
2018-04-07 01:23:03 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
name = cv.valid_name(name)
|
|
|
|
break
|
|
|
|
except vol.Invalid:
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(color("red", u"Oh noes, \"{}\" isn't a valid name. Names can only include "
|
|
|
|
u"numbers, letters and underscores.".format(name)))
|
2018-04-07 01:23:03 +02:00
|
|
|
name = strip_accents(name).replace(' ', '_')
|
|
|
|
name = u''.join(c for c in name if c in cv.ALLOWED_NAME_CHARS)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Shall I use \"{}\" as the name instead?".format(color('cyan', name)))
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.5)
|
2018-10-20 12:41:00 +02:00
|
|
|
name = default_input(u"(name [{}]): ", name)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Great! Your node is now called \"{}\".".format(color('cyan', name)))
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print_step(2, ESP_BIG)
|
|
|
|
safe_print("Now I'd like to know what microcontroller you're using so that I can compile "
|
|
|
|
"firmwares for it.")
|
|
|
|
safe_print("Are you using an " + color('green', 'ESP32') + " or " +
|
|
|
|
color('green', 'ESP8266') + " platform? (Choose ESP8266 for Sonoff devices)")
|
2018-04-07 01:23:03 +02:00
|
|
|
while True:
|
|
|
|
sleep(0.5)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
|
|
|
safe_print("Please enter either ESP32 or ESP8266.")
|
2019-01-02 14:11:11 +01:00
|
|
|
platform = safe_input(color("bold_white", "(ESP32/ESP8266): "))
|
2018-04-07 01:23:03 +02:00
|
|
|
try:
|
2019-04-17 12:06:00 +02:00
|
|
|
platform = vol.All(vol.Upper, vol.Any('ESP32', 'ESP8266'))(platform)
|
2018-04-07 01:23:03 +02:00
|
|
|
break
|
|
|
|
except vol.Invalid:
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Unfortunately, I can't find an espressif microcontroller called "
|
|
|
|
u"\"{}\". Please try again.".format(platform))
|
|
|
|
safe_print(u"Thanks! You've chosen {} as your platform.".format(color('cyan', platform)))
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
if platform == 'ESP32':
|
2018-04-07 01:23:03 +02:00
|
|
|
board_link = 'http://docs.platformio.org/en/latest/platforms/espressif32.html#boards'
|
|
|
|
else:
|
|
|
|
board_link = 'http://docs.platformio.org/en/latest/platforms/espressif8266.html#boards'
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Next, I need to know what " + color('green', 'board') + " you're using.")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.5)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Please go to {} and choose a board.".format(color('green', board_link)))
|
2019-04-17 12:06:00 +02:00
|
|
|
if platform == 'ESP32':
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("(Type " + color('green', 'esp01_1m') + " for Sonoff devices)")
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
# Don't sleep because user needs to copy link
|
2019-04-17 12:06:00 +02:00
|
|
|
if platform == 'ESP32':
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("For example \"{}\".".format(color("bold_white", 'nodemcu-32s')))
|
2018-09-23 18:58:41 +02:00
|
|
|
boards = list(ESP32_BOARD_PINS.keys())
|
2018-04-07 01:23:03 +02:00
|
|
|
else:
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("For example \"{}\".".format(color("bold_white", 'nodemcuv2')))
|
2018-09-23 18:58:41 +02:00
|
|
|
boards = list(ESP8266_BOARD_PINS.keys())
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print("Options: {}".format(', '.join(boards)))
|
|
|
|
|
2018-04-07 01:23:03 +02:00
|
|
|
while True:
|
2019-01-02 14:11:11 +01:00
|
|
|
board = safe_input(color("bold_white", "(board): "))
|
2018-04-07 01:23:03 +02:00
|
|
|
try:
|
|
|
|
board = vol.All(vol.Lower, vol.Any(*boards))(board)
|
|
|
|
break
|
|
|
|
except vol.Invalid:
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(color('red', "Sorry, I don't think the board \"{}\" exists."))
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.25)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Way to go! You've chosen {} as your board.".format(color('cyan', board)))
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print_step(3, WIFI_BIG)
|
|
|
|
safe_print("In this step, I'm going to create the configuration for "
|
|
|
|
"WiFi.")
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("First, what's the " + color('green', 'SSID') +
|
|
|
|
u" (the name) of the WiFi network {} I should connect to?".format(name))
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1.5)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("For example \"{}\".".format(color('bold_white', "Abraham Linksys")))
|
2018-04-07 01:23:03 +02:00
|
|
|
while True:
|
2019-01-02 14:11:11 +01:00
|
|
|
ssid = safe_input(color('bold_white', "(ssid): "))
|
2018-04-07 01:23:03 +02:00
|
|
|
try:
|
|
|
|
ssid = cv.ssid(ssid)
|
|
|
|
break
|
|
|
|
except vol.Invalid:
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(color('red', u"Unfortunately, \"{}\" doesn't seem to be a valid SSID. "
|
|
|
|
u"Please try again.".format(ssid)))
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1)
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(u"Thank you very much! You've just chosen \"{}\" as your SSID."
|
|
|
|
u"".format(color('cyan', ssid)))
|
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.75)
|
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Now please state the " + color('green', 'password') +
|
|
|
|
" of the WiFi network so that I can connect to it (Leave empty for no password)")
|
|
|
|
safe_print()
|
|
|
|
safe_print("For example \"{}\"".format(color('bold_white', 'PASSWORD42')))
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.5)
|
2019-01-02 14:11:11 +01:00
|
|
|
psk = safe_input(color('bold_white', '(PSK): '))
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Perfect! WiFi is now set up (you can create static IPs and so on later).")
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(1.5)
|
|
|
|
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print_step(4, OTA_BIG)
|
|
|
|
safe_print("Almost there! ESPHome can automatically upload custom firmwares over WiFi "
|
|
|
|
"(over the air) and integrates into Home Assistant with a native API.")
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("This can be insecure if you do not trust the WiFi network. Do you want to set "
|
2018-12-18 19:31:43 +01:00
|
|
|
"a " + color('green', 'password') + " for connecting to this ESP?")
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
2018-04-07 01:23:03 +02:00
|
|
|
sleep(0.25)
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print("Press ENTER for no password")
|
2019-01-02 14:11:11 +01:00
|
|
|
password = safe_input(color('bold_white', '(password): '))
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
wizard_write(path=path, name=name, platform=platform, board=board,
|
2018-12-18 19:31:43 +01:00
|
|
|
ssid=ssid, psk=psk, password=password)
|
2018-04-07 01:23:03 +02:00
|
|
|
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print()
|
|
|
|
safe_print(color('cyan', "DONE! I've now written a new configuration file to ") +
|
|
|
|
color('bold_cyan', path))
|
|
|
|
safe_print()
|
|
|
|
safe_print("Next steps:")
|
2018-12-18 19:31:43 +01:00
|
|
|
safe_print(" > Check your Home Assistant \"integrations\" screen. If all goes well, you "
|
|
|
|
"should see your ESP being discovered automatically.")
|
2018-10-20 12:41:00 +02:00
|
|
|
safe_print(" > Then follow the rest of the getting started guide:")
|
2019-02-13 16:54:02 +01:00
|
|
|
safe_print(" > https://esphome.io/guides/getting_started_command_line.html")
|
2018-04-07 01:23:03 +02:00
|
|
|
return 0
|