Hass.io Ingress (#519)

* Hass.io ingress

* Update

* Remove global vars

* Fix

* Fixes

* Fixes

* Upgrade base image to 1.5.1

* Lint
This commit is contained in:
Otto Winter 2019-04-24 17:08:05 +02:00 committed by GitHub
parent e6ff3c287d
commit 5e5b9f2205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 373 additions and 288 deletions

View file

@ -41,11 +41,11 @@ stages:
- | - |
if [[ "${IS_HASSIO}" == "YES" ]]; then if [[ "${IS_HASSIO}" == "YES" ]]; then
BUILD_FROM=esphome/esphome-hassio-base-${BUILD_ARCH}:1.4.3 BUILD_FROM=esphome/esphome-hassio-base-${BUILD_ARCH}:1.5.1
BUILD_TO=esphome/esphome-hassio-${BUILD_ARCH} BUILD_TO=esphome/esphome-hassio-${BUILD_ARCH}
DOCKERFILE=docker/Dockerfile.hassio DOCKERFILE=docker/Dockerfile.hassio
else else
BUILD_FROM=esphome/esphome-base-${BUILD_ARCH}:1.4.3 BUILD_FROM=esphome/esphome-base-${BUILD_ARCH}:1.5.1
if [[ "${BUILD_ARCH}" == "amd64" ]]; then if [[ "${BUILD_ARCH}" == "amd64" ]]; then
BUILD_TO=esphome/esphome BUILD_TO=esphome/esphome
else else

View file

@ -1,9 +1,8 @@
ARG BUILD_FROM=esphome/esphome-base-amd64:1.4.3 ARG BUILD_FROM=esphome/esphome-base-amd64:1.5.1
FROM ${BUILD_FROM} FROM ${BUILD_FROM}
COPY . . COPY . .
RUN \ RUN pip2 install --no-cache-dir -e .
pip2 install --no-cache-dir --no-binary :all: -e .
WORKDIR /config WORKDIR /config
ENTRYPOINT ["esphome"] ENTRYPOINT ["esphome"]

View file

@ -1,4 +1,4 @@
ARG BUILD_FROM=esphome/esphome-hassio-base-amd64:1.4.3 ARG BUILD_FROM
FROM ${BUILD_FROM} FROM ${BUILD_FROM}
# Copy root filesystem # Copy root filesystem
@ -6,8 +6,7 @@ COPY docker/rootfs/ /
COPY setup.py setup.cfg MANIFEST.in /opt/esphome/ COPY setup.py setup.cfg MANIFEST.in /opt/esphome/
COPY esphome /opt/esphome/esphome COPY esphome /opt/esphome/esphome
RUN \ RUN pip2 install --no-cache-dir -e /opt/esphome
pip2 install --no-cache-dir --no-binary :all: -e /opt/esphome
# Build arguments # Build arguments
ARG BUILD_VERSION=dev ARG BUILD_VERSION=dev

View file

@ -7,7 +7,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
python-pil \ python-pil \
git \ git \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/*rm -rf /var/lib/apt/lists/* /tmp/* && \ && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/*rm -rf /var/lib/apt/lists/* /tmp/* && \
pip install --no-cache-dir --no-binary :all: platformio && \ pip install --no-cache-dir platformio && \
platformio settings set enable_telemetry No && \ platformio settings set enable_telemetry No && \
platformio settings set check_libraries_interval 1000000 && \ platformio settings set check_libraries_interval 1000000 && \
platformio settings set check_platformio_interval 1000000 && \ platformio settings set check_platformio_interval 1000000 && \

View file

@ -1,26 +0,0 @@
#!/usr/bin/env bash
# the Docker repository tag being built.
declare CACHE_TAG
echo "CACHE_TAG: ${CACHE_TAG}"
# the name and tag of the Docker repository being built. (This variable is a combination of DOCKER_REPO:CACHE_TAG.)
declare IMAGE_NAME
echo "IMAGE_NAME: ${IMAGE_NAME}"
# the architecture to build
declare BUILD_ARCH
echo "BUILD_ARCH: ${BUILD_ARCH}"
# whether this is a hassio build
declare IS_HASSIO
echo "IS_HASSIO: ${IS_HASSIO}"
echo "PWD: $PWD"
if [[ ${IS_HASSIO} = "YES" ]]; then
docker build \
--build-arg "BUILD_FROM=esphome/esphome-hassio-base-${BUILD_ARCH}:1.4.3" \
--build-arg "BUILD_VERSION=${CACHE_TAG}" \
-t "${IMAGE_NAME}" -f ../docker/Dockerfile.hassio ..
else
docker build \
--build-arg "BUILD_FROM=esphome/esphome-base-${BUILD_ARCH}:1.4.3" \
-t "${IMAGE_NAME}" -f ../docker/Dockerfile ..
fi

View file

@ -1,18 +0,0 @@
#!/usr/bin/env bash
# the architecture to build
declare BUILD_ARCH
echo "BUILD_ARCH: ${BUILD_ARCH}"
if [[ ${BUILD_ARCH} = "amd64" ]]; then
echo "No qemu required..."
exit 0
fi
if [[ ${BUILD_ARCH} = "i386" ]]; then
echo "No qemu required..."
exit 0
fi
echo "Installing qemu..."
docker run --rm --privileged multiarch/qemu-user-static:register --reset

View file

@ -6,21 +6,29 @@
declare certfile declare certfile
declare keyfile declare keyfile
declare port declare direct_port
declare ingress_interface
declare ingress_port
mkdir -p /var/log/nginx mkdir -p /var/log/nginx
# Enable SSL direct_port=$(bashio::addon.port 6052)
if bashio::var.has_value "${direct_port}"; then
if bashio::config.true 'ssl'; then if bashio::config.true 'ssl'; then
rm /etc/nginx/nginx.conf
mv /etc/nginx/nginx-ssl.conf /etc/nginx/nginx.conf
certfile=$(bashio::config 'certfile') certfile=$(bashio::config 'certfile')
keyfile=$(bashio::config 'keyfile') keyfile=$(bashio::config 'keyfile')
sed -i "s/%%certfile%%/${certfile}/g" /etc/nginx/nginx.conf mv /etc/nginx/servers/direct-ssl.disabled /etc/nginx/servers/direct.conf
sed -i "s/%%keyfile%%/${keyfile}/g" /etc/nginx/nginx.conf sed -i "s/%%certfile%%/${certfile}/g" /etc/nginx/servers/direct.conf
sed -i "s/%%keyfile%%/${keyfile}/g" /etc/nginx/servers/direct.conf
else
mv /etc/nginx/servers/direct.disabled /etc/nginx/servers/direct.conf
fi fi
port=$(bashio::config 'port') sed -i "s/%%port%%/${direct_port}/g" /etc/nginx/servers/direct.conf
sed -i "s/%%port%%/${port}/g" /etc/nginx/nginx.conf fi
ingress_port=$(bashio::addon.ingress_port)
ingress_interface=$(bashio::addon.ip_address)
sed -i "s/%%port%%/${ingress_port}/g" /etc/nginx/servers/ingress.conf
sed -i "s/%%interface%%/${ingress_interface}/g" /etc/nginx/servers/ingress.conf

View file

@ -10,6 +10,6 @@ if bashio::config.has_value 'esphome_version'; then
esphome_version=$(bashio::config 'esphome_version') esphome_version=$(bashio::config 'esphome_version')
full_url="https://github.com/esphome/esphome/archive/${esphome_version}.zip" full_url="https://github.com/esphome/esphome/archive/${esphome_version}.zip"
bashio::log.info "Installing esphome version '${esphome_version}' (${full_url})..." bashio::log.info "Installing esphome version '${esphome_version}' (${full_url})..."
pip2 install --no-cache-dir --no-binary :all: "${full_url}" \ pip2 install -U --no-cache-dir "${full_url}" \
|| bashio::exit.nok "Failed installing esphome pinned version." || bashio::exit.nok "Failed installing esphome pinned version."
fi fi

View file

@ -0,0 +1,96 @@
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
font/woff woff;
font/woff2 woff2;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}

View file

@ -0,0 +1,16 @@
proxy_http_version 1.1;
proxy_ignore_client_abort off;
proxy_read_timeout 86400s;
proxy_redirect off;
proxy_send_timeout 86400s;
proxy_max_temp_file_size 0;
proxy_set_header Accept-Encoding "";
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Authorization "";

View file

@ -0,0 +1,6 @@
root /dev/null;
server_name $hostname;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;

View file

@ -0,0 +1,9 @@
ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA;
ssl_ecdh_curve secp384r1;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;

View file

@ -1,62 +0,0 @@
worker_processes 1;
pid /var/run/nginx.pid;
error_log stderr;
events {
worker_connections 1024;
}
http {
access_log stdout;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream esphome {
ip_hash;
server unix:/var/run/esphome.sock;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name hassio.local;
listen %%port%% default_server ssl;
root /dev/null;
ssl_certificate /ssl/%%certfile%%;
ssl_certificate_key /ssl/%%keyfile%%;
ssl_protocols TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA;
ssl_ecdh_curve secp384r1;
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# Redirect http requests to https on the same port.
# https://rageagainstshell.com/2016/11/redirect-http-to-https-on-the-same-port-in-nginx/
error_page 497 https://$http_host$request_uri;
location / {
proxy_redirect off;
proxy_pass http://esphome;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Authorization "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
}

View file

@ -1,46 +1,33 @@
worker_processes 1; daemon off;
user root;
pid /var/run/nginx.pid; pid /var/run/nginx.pid;
error_log stderr; worker_processes 1;
# Hass.io addon log
error_log /proc/1/fd/1 error;
events { events {
worker_connections 1024; worker_connections 1024;
} }
http { http {
include /etc/nginx/includes/mime.types;
access_log stdout; access_log stdout;
include mime.types;
default_type application/octet-stream; default_type application/octet-stream;
sendfile on; gzip on;
keepalive_timeout 65; keepalive_timeout 65;
sendfile on;
server_tokens off;
upstream esphome {
ip_hash;
server unix:/var/run/esphome.sock;
}
map $http_upgrade $connection_upgrade { map $http_upgrade $connection_upgrade {
default upgrade; default upgrade;
'' close; '' close;
} }
server { # Use Hass.io supervisor as resolver
server_name hassio.local; resolver 172.30.32.2;
listen %%port%% default_server;
root /dev/null;
location / { upstream esphome {
proxy_redirect off; server unix:/var/run/esphome.sock;
proxy_pass http://esphome; }
proxy_http_version 1.1; include /etc/nginx/servers/*.conf;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Authorization "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
} }

View file

@ -0,0 +1,17 @@
server {
listen %%port%% default_server ssl http2;
include /etc/nginx/includes/server_params.conf;
include /etc/nginx/includes/proxy_params.conf;
include /etc/nginx/includes/ssl_params.conf;
# Clear Hass.io Ingress header
proxy_set_header X-Hassio-Ingress "";
# Redirect http requests to https on the same port.
# https://rageagainstshell.com/2016/11/redirect-http-to-https-on-the-same-port-in-nginx/
error_page 497 https://$http_host$request_uri;
location / {
proxy_pass http://esphome;
}
}

View file

@ -0,0 +1,12 @@
server {
listen %%port%% default_server;
include /etc/nginx/includes/server_params.conf;
include /etc/nginx/includes/proxy_params.conf;
# Clear Hass.io Ingress header
proxy_set_header X-Hassio-Ingress "";
location / {
proxy_pass http://esphome;
}
}

View file

@ -0,0 +1,16 @@
server {
listen %%interface%%:%%port%% default_server;
include /etc/nginx/includes/server_params.conf;
include /etc/nginx/includes/proxy_params.conf;
# Set Hass.io Ingress header
proxy_set_header X-Hassio-Ingress "YES";
location / {
# Only allow from Hass.io supervisor
allow 172.30.32.2;
deny all;
proxy_pass http://esphome;
}
}

View file

@ -4,5 +4,11 @@
# Runs the NGINX proxy # Runs the NGINX proxy
# ============================================================================== # ==============================================================================
bashio::log.info "Waiting for dashboard to come up..."
while [[ ! -S /var/run/esphome.sock ]]; do
sleep 0.5
done
bashio::log.info "Starting NGINX..." bashio::log.info "Starting NGINX..."
exec nginx -g "daemon off;" exec nginx

View file

@ -13,9 +13,7 @@ extern uint8_t next_ledc_channel;
class LEDCOutput : public output::FloatOutput, public Component { class LEDCOutput : public output::FloatOutput, public Component {
public: public:
explicit LEDCOutput(GPIOPin *pin) : pin_(pin) { explicit LEDCOutput(GPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
this->channel_ = next_ledc_channel++;
}
void set_channel(uint8_t channel) { this->channel_ = channel; } void set_channel(uint8_t channel) { this->channel_ = channel; }
void set_bit_depth(uint8_t bit_depth) { this->bit_depth_ = bit_depth; } void set_bit_depth(uint8_t bit_depth) { this->bit_depth_ = bit_depth; }

View file

@ -3,6 +3,7 @@ from __future__ import print_function
import codecs import codecs
import collections import collections
import functools
import hashlib import hashlib
import hmac import hmac
import json import json
@ -39,15 +40,73 @@ from typing import Optional # noqa
from esphome.zeroconf import DashboardStatus, Zeroconf from esphome.zeroconf import DashboardStatus, Zeroconf
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONFIG_DIR = ''
PASSWORD_DIGEST = ''
COOKIE_SECRET = None class DashboardSettings(object):
USING_PASSWORD = False def __init__(self):
ON_HASSIO = False self.config_dir = ''
USING_HASSIO_AUTH = True self.password_digest = ''
HASSIO_MQTT_CONFIG = None self.using_password = False
RELATIVE_URL = os.getenv('ESPHOME_DASHBOARD_RELATIVE_URL', '/') self.on_hassio = False
STATUS_USE_PING = get_bool_env('ESPHOME_DASHBOARD_USE_PING') self.cookie_secret = None
def parse_args(self, args):
self.on_hassio = args.hassio
if not self.on_hassio:
self.using_password = bool(args.password)
if self.using_password:
if IS_PY2:
self.password_digest = hmac.new(args.password).digest()
else:
self.password_digest = hmac.new(args.password.encode()).digest()
self.config_dir = args.configuration
@property
def relative_url(self):
return os.getenv('ESPHOME_DASHBOARD_RELATIVE_URL', '/')
@property
def status_use_ping(self):
return get_bool_env('ESPHOME_DASHBOARD_USE_PING')
@property
def using_hassio_auth(self):
if not self.on_hassio:
return False
return not get_bool_env('DISABLE_HA_AUTHENTICATION')
@property
def using_auth(self):
return self.using_password or self.using_hassio_auth
def check_password(self, password):
if not self.using_auth:
return True
if IS_PY2:
password = hmac.new(password).digest()
else:
password = hmac.new(password.encode()).digest()
return hmac.compare_digest(self.password_digest, password)
def rel_path(self, *args):
return os.path.join(self.config_dir, *args)
def list_yaml_files(self):
files = []
for file in os.listdir(self.config_dir):
if not file.endswith('.yaml'):
continue
if file.startswith('.'):
continue
if file == 'secrets.yaml':
continue
files.append(file)
files.sort()
return files
settings = DashboardSettings()
if IS_PY2: if IS_PY2:
cookie_authenticated_yes = 'yes' cookie_authenticated_yes = 'yes'
@ -61,22 +120,29 @@ def template_args():
'version': version, 'version': version,
'docs_link': 'https://beta.esphome.io/' if 'b' in version else 'https://esphome.io/', 'docs_link': 'https://beta.esphome.io/' if 'b' in version else 'https://esphome.io/',
'get_static_file_url': get_static_file_url, 'get_static_file_url': get_static_file_url,
'relative_url': RELATIVE_URL, 'relative_url': settings.relative_url,
'streamer_mode': get_bool_env('ESPHOME_STREAMER_MODE'), 'streamer_mode': get_bool_env('ESPHOME_STREAMER_MODE'),
} }
def authenticated(func): def authenticated(func):
@functools.wraps(func)
def decorator(self, *args, **kwargs): def decorator(self, *args, **kwargs):
if not is_authenticated(self): if not is_authenticated(self):
self.redirect(RELATIVE_URL + 'login') self.redirect('./login')
return None return None
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
return decorator return decorator
def is_authenticated(request_handler): def is_authenticated(request_handler):
if USING_HASSIO_AUTH or USING_PASSWORD: if settings.on_hassio:
# Handle ingress - disable auth on ingress port
# X-Hassio-Ingress is automatically stripped on the non-ingress server in nginx
header = request_handler.request.headers.get('X-Hassio-Ingress', 'NO')
if str(header) == 'YES':
return True
if settings.using_auth:
return request_handler.get_secure_cookie('authenticated') == cookie_authenticated_yes return request_handler.get_secure_cookie('authenticated') == cookie_authenticated_yes
return True return True
@ -126,10 +192,8 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
self._proc = None self._proc = None
self._is_closed = False self._is_closed = False
@authenticated
def on_message(self, message): def on_message(self, message):
if USING_HASSIO_AUTH or USING_PASSWORD:
if self.get_secure_cookie('authenticated') != cookie_authenticated_yes:
return
# Messages are always JSON, 500 when not # Messages are always JSON, 500 when not
json_message = json.loads(message) json_message = json.loads(message)
type_ = json_message['type'] type_ = json_message['type']
@ -188,7 +252,7 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
def _proc_on_exit(self, returncode): def _proc_on_exit(self, returncode):
if not self._is_closed: if not self._is_closed:
# Check if the proc was not forcibly closed # Check if the proc was not forcibly closed
_LOGGER.debug("Process exited with return code %s", returncode) _LOGGER.info("Process exited with return code %s", returncode)
self.write_message({'event': 'exit', 'code': returncode}) self.write_message({'event': 'exit', 'code': returncode})
def on_close(self): def on_close(self):
@ -205,39 +269,39 @@ class EsphomeCommandWebSocket(tornado.websocket.WebSocketHandler):
class EsphomeLogsHandler(EsphomeCommandWebSocket): class EsphomeLogsHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "logs", '--serial-port', return ["esphome", "--dashboard", config_file, "logs", '--serial-port',
json_message["port"]] json_message["port"]]
class EsphomeUploadHandler(EsphomeCommandWebSocket): class EsphomeUploadHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "run", '--upload-port', return ["esphome", "--dashboard", config_file, "run", '--upload-port',
json_message["port"]] json_message["port"]]
class EsphomeCompileHandler(EsphomeCommandWebSocket): class EsphomeCompileHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "compile"] return ["esphome", "--dashboard", config_file, "compile"]
class EsphomeValidateHandler(EsphomeCommandWebSocket): class EsphomeValidateHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "config"] return ["esphome", "--dashboard", config_file, "config"]
class EsphomeCleanMqttHandler(EsphomeCommandWebSocket): class EsphomeCleanMqttHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "clean-mqtt"] return ["esphome", "--dashboard", config_file, "clean-mqtt"]
class EsphomeCleanHandler(EsphomeCommandWebSocket): class EsphomeCleanHandler(EsphomeCommandWebSocket):
def build_command(self, json_message): def build_command(self, json_message):
config_file = os.path.join(CONFIG_DIR, json_message['configuration']) config_file = settings.rel_path(json_message['configuration'])
return ["esphome", "--dashboard", config_file, "clean"] return ["esphome", "--dashboard", config_file, "clean"]
@ -270,9 +334,9 @@ class WizardRequestHandler(BaseHandler):
from esphome import wizard from esphome import wizard
kwargs = {k: u''.join(decode_text(x) for x in v) for k, v in self.request.arguments.items()} kwargs = {k: u''.join(decode_text(x) for x in v) for k, v in self.request.arguments.items()}
destination = os.path.join(CONFIG_DIR, kwargs['name'] + u'.yaml') destination = settings.rel_path(kwargs['name'] + u'.yaml')
wizard.wizard_write(path=destination, **kwargs) wizard.wizard_write(path=destination, **kwargs)
self.redirect('/?begin=True') self.redirect('./?begin=True')
class DownloadBinaryRequestHandler(BaseHandler): class DownloadBinaryRequestHandler(BaseHandler):
@ -280,7 +344,7 @@ class DownloadBinaryRequestHandler(BaseHandler):
@bind_config @bind_config
def get(self, configuration=None): def get(self, configuration=None):
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
storage_path = ext_storage_path(CONFIG_DIR, configuration) storage_path = ext_storage_path(settings.config_dir, configuration)
storage_json = StorageJSON.load(storage_path) storage_json = StorageJSON.load(storage_path)
if storage_json is None: if storage_json is None:
self.send_error() self.send_error()
@ -299,22 +363,8 @@ class DownloadBinaryRequestHandler(BaseHandler):
self.finish() self.finish()
def _list_yaml_files():
files = []
for file in os.listdir(CONFIG_DIR):
if not file.endswith('.yaml'):
continue
if file.startswith('.'):
continue
if file == 'secrets.yaml':
continue
files.append(file)
files.sort()
return files
def _list_dashboard_entries(): def _list_dashboard_entries():
files = _list_yaml_files() files = settings.list_yaml_files()
return [DashboardEntry(file) for file in files] return [DashboardEntry(file) for file in files]
@ -326,12 +376,12 @@ class DashboardEntry(object):
@property @property
def full_path(self): # type: () -> str def full_path(self): # type: () -> str
return os.path.join(CONFIG_DIR, self.filename) return os.path.join(settings.config_dir, self.filename)
@property @property
def storage(self): # type: () -> Optional[StorageJSON] def storage(self): # type: () -> Optional[StorageJSON]
if not self._loaded_storage: if not self._loaded_storage:
self._storage = StorageJSON.load(ext_storage_path(CONFIG_DIR, self.filename)) self._storage = StorageJSON.load(ext_storage_path(settings.config_dir, self.filename))
self._loaded_storage = True self._loaded_storage = True
return self._storage return self._storage
@ -474,7 +524,7 @@ class EditRequestHandler(BaseHandler):
@bind_config @bind_config
def get(self, configuration=None): def get(self, configuration=None):
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
with open(os.path.join(CONFIG_DIR, configuration), 'r') as f: with open(settings.rel_path(configuration), 'r') as f:
content = f.read() content = f.read()
self.write(content) self.write(content)
@ -482,7 +532,7 @@ class EditRequestHandler(BaseHandler):
@bind_config @bind_config
def post(self, configuration=None): def post(self, configuration=None):
# pylint: disable=no-value-for-parameter # pylint: disable=no-value-for-parameter
with open(os.path.join(CONFIG_DIR, configuration), 'wb') as f: with open(settings.rel_path(configuration), 'wb') as f:
f.write(self.request.body) f.write(self.request.body)
self.set_status(200) self.set_status(200)
@ -491,20 +541,20 @@ class DeleteRequestHandler(BaseHandler):
@authenticated @authenticated
@bind_config @bind_config
def post(self, configuration=None): def post(self, configuration=None):
config_file = os.path.join(CONFIG_DIR, configuration) config_file = settings.rel_path(configuration)
storage_path = ext_storage_path(CONFIG_DIR, configuration) storage_path = ext_storage_path(settings.config_dir, configuration)
storage_json = StorageJSON.load(storage_path) storage_json = StorageJSON.load(storage_path)
if storage_json is None: if storage_json is None:
self.set_status(500) self.set_status(500)
return return
name = storage_json.name name = storage_json.name
trash_path = trash_storage_path(CONFIG_DIR) trash_path = trash_storage_path(settings.config_dir)
mkdir_p(trash_path) mkdir_p(trash_path)
shutil.move(config_file, os.path.join(trash_path, configuration)) shutil.move(config_file, os.path.join(trash_path, configuration))
# Delete build folder (if exists) # Delete build folder (if exists)
build_folder = os.path.join(CONFIG_DIR, name) build_folder = os.path.join(settings.config_dir, name)
if build_folder is not None: if build_folder is not None:
shutil.rmtree(build_folder, os.path.join(trash_path, name)) shutil.rmtree(build_folder, os.path.join(trash_path, name))
@ -513,8 +563,8 @@ class UndoDeleteRequestHandler(BaseHandler):
@authenticated @authenticated
@bind_config @bind_config
def post(self, configuration=None): def post(self, configuration=None):
config_file = os.path.join(CONFIG_DIR, configuration) config_file = settings.rel_path(configuration)
trash_path = trash_storage_path(CONFIG_DIR) trash_path = trash_storage_path(settings.config_dir)
shutil.move(os.path.join(trash_path, configuration), config_file) shutil.move(os.path.join(trash_path, configuration), config_file)
@ -525,10 +575,10 @@ PING_REQUEST = threading.Event()
class LoginHandler(BaseHandler): class LoginHandler(BaseHandler):
def get(self): def get(self):
if USING_HASSIO_AUTH: if settings.using_hassio_auth:
self.render_hassio_login() self.render_hassio_login()
return return
self.write('<html><body><form action="' + RELATIVE_URL + 'login" method="post">' self.write('<html><body><form action="./login" method="post">'
'Password: <input type="password" name="password">' 'Password: <input type="password" name="password">'
'<input type="submit" value="Sign in">' '<input type="submit" value="Sign in">'
'</form></body></html>') '</form></body></html>')
@ -561,16 +611,12 @@ class LoginHandler(BaseHandler):
self.render_hassio_login(error="Invalid username or password") self.render_hassio_login(error="Invalid username or password")
def post(self): def post(self):
if USING_HASSIO_AUTH: if settings.using_hassio_auth:
self.post_hassio_login() self.post_hassio_login()
return return
password = str(self.get_argument("password", '')) password = str(self.get_argument("password", ''))
if IS_PY2: if settings.check_password(password):
password = hmac.new(password).digest()
else:
password = hmac.new(password.encode()).digest()
if hmac.compare_digest(PASSWORD_DIGEST, password):
self.set_secure_cookie("authenticated", cookie_authenticated_yes) self.set_secure_cookie("authenticated", cookie_authenticated_yes)
self.redirect("/") self.redirect("/")
@ -587,7 +633,7 @@ def get_static_file_url(name):
with open(path, 'rb') as f_handle: with open(path, 'rb') as f_handle:
hash_ = hashlib.md5(f_handle.read()).hexdigest()[:8] hash_ = hashlib.md5(f_handle.read()).hexdigest()[:8]
_STATIC_FILE_HASHES[name] = hash_ _STATIC_FILE_HASHES[name] = hash_
return RELATIVE_URL + u'static/{}?hash={}'.format(name, hash_) return u'./static/{}?hash={}'.format(name, hash_)
def make_app(debug=False): def make_app(debug=False):
@ -615,31 +661,32 @@ def make_app(debug=False):
self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
static_path = os.path.join(os.path.dirname(__file__), 'static') static_path = os.path.join(os.path.dirname(__file__), 'static')
settings = { app_settings = {
'debug': debug, 'debug': debug,
'cookie_secret': COOKIE_SECRET, 'cookie_secret': settings.cookie_secret,
'log_function': log_function, 'log_function': log_function,
'websocket_ping_interval': 30.0, 'websocket_ping_interval': 30.0,
} }
rel = settings.relative_url
app = tornado.web.Application([ app = tornado.web.Application([
(RELATIVE_URL + "", MainRequestHandler), (rel + "", MainRequestHandler),
(RELATIVE_URL + "login", LoginHandler), (rel + "login", LoginHandler),
(RELATIVE_URL + "logs", EsphomeLogsHandler), (rel + "logs", EsphomeLogsHandler),
(RELATIVE_URL + "upload", EsphomeUploadHandler), (rel + "upload", EsphomeUploadHandler),
(RELATIVE_URL + "compile", EsphomeCompileHandler), (rel + "compile", EsphomeCompileHandler),
(RELATIVE_URL + "validate", EsphomeValidateHandler), (rel + "validate", EsphomeValidateHandler),
(RELATIVE_URL + "clean-mqtt", EsphomeCleanMqttHandler), (rel + "clean-mqtt", EsphomeCleanMqttHandler),
(RELATIVE_URL + "clean", EsphomeCleanHandler), (rel + "clean", EsphomeCleanHandler),
(RELATIVE_URL + "vscode", EsphomeVscodeHandler), (rel + "vscode", EsphomeVscodeHandler),
(RELATIVE_URL + "edit", EditRequestHandler), (rel + "edit", EditRequestHandler),
(RELATIVE_URL + "download.bin", DownloadBinaryRequestHandler), (rel + "download.bin", DownloadBinaryRequestHandler),
(RELATIVE_URL + "serial-ports", SerialPortRequestHandler), (rel + "serial-ports", SerialPortRequestHandler),
(RELATIVE_URL + "ping", PingRequestHandler), (rel + "ping", PingRequestHandler),
(RELATIVE_URL + "delete", DeleteRequestHandler), (rel + "delete", DeleteRequestHandler),
(RELATIVE_URL + "undo-delete", UndoDeleteRequestHandler), (rel + "undo-delete", UndoDeleteRequestHandler),
(RELATIVE_URL + "wizard.html", WizardRequestHandler), (rel + "wizard.html", WizardRequestHandler),
(RELATIVE_URL + r"static/(.*)", StaticFileHandler, {'path': static_path}), (rel + r"static/(.*)", StaticFileHandler, {'path': static_path}),
], **settings) ], **app_settings)
if debug: if debug:
_STATIC_FILE_HASHES.clear() _STATIC_FILE_HASHES.clear()
@ -648,49 +695,27 @@ def make_app(debug=False):
def start_web_server(args): def start_web_server(args):
global CONFIG_DIR settings.parse_args(args)
global PASSWORD_DIGEST mkdir_p(settings.rel_path(".esphome"))
global USING_PASSWORD
global ON_HASSIO
global USING_HASSIO_AUTH
global COOKIE_SECRET
CONFIG_DIR = args.configuration if settings.using_auth:
mkdir_p(CONFIG_DIR) path = esphome_storage_path(settings.config_dir)
mkdir_p(os.path.join(CONFIG_DIR, ".esphome"))
ON_HASSIO = args.hassio
if ON_HASSIO:
USING_HASSIO_AUTH = not get_bool_env('DISABLE_HA_AUTHENTICATION')
USING_PASSWORD = False
else:
USING_HASSIO_AUTH = False
USING_PASSWORD = args.password
if USING_PASSWORD:
if IS_PY2:
PASSWORD_DIGEST = hmac.new(args.password).digest()
else:
PASSWORD_DIGEST = hmac.new(args.password.encode()).digest()
if USING_HASSIO_AUTH or USING_PASSWORD:
path = esphome_storage_path(CONFIG_DIR)
storage = EsphomeStorageJSON.load(path) storage = EsphomeStorageJSON.load(path)
if storage is None: if storage is None:
storage = EsphomeStorageJSON.get_default() storage = EsphomeStorageJSON.get_default()
storage.save(path) storage.save(path)
COOKIE_SECRET = storage.cookie_secret settings.cookie_secret = storage.cookie_secret
app = make_app(args.verbose) app = make_app(args.verbose)
if args.socket is not None: if args.socket is not None:
_LOGGER.info("Starting dashboard web server on unix socket %s and configuration dir %s...", _LOGGER.info("Starting dashboard web server on unix socket %s and configuration dir %s...",
args.socket, CONFIG_DIR) args.socket, settings.config_dir)
server = tornado.httpserver.HTTPServer(app) server = tornado.httpserver.HTTPServer(app)
socket = tornado.netutil.bind_unix_socket(args.socket, mode=0o666) socket = tornado.netutil.bind_unix_socket(args.socket, mode=0o666)
server.add_socket(socket) server.add_socket(socket)
else: else:
_LOGGER.info("Starting dashboard web server on port %s and configuration dir %s...", _LOGGER.info("Starting dashboard web server on port %s and configuration dir %s...",
args.port, CONFIG_DIR) args.port, settings.config_dir)
app.listen(args.port) app.listen(args.port)
if args.open_ui: if args.open_ui:
@ -698,7 +723,7 @@ def start_web_server(args):
webbrowser.open('localhost:{}'.format(args.port)) webbrowser.open('localhost:{}'.format(args.port))
if STATUS_USE_PING: if settings.status_use_ping:
status_thread = PingStatusThread() status_thread = PingStatusThread()
else: else:
status_thread = MDNSStatusThread() status_thread = MDNSStatusThread()

View file

@ -9,7 +9,7 @@ let wsProtocol = "ws:";
if (window.location.protocol === "https:") { if (window.location.protocol === "https:") {
wsProtocol = 'wss:'; wsProtocol = 'wss:';
} }
const wsUrl = `${wsProtocol}//${window.location.hostname}:${window.location.port}${relative_url}`; const wsUrl = `${wsProtocol}//${window.location.host}${window.location.pathname}`;
// ============================= Color Log Parsing ============================= // ============================= Color Log Parsing =============================
@ -192,7 +192,7 @@ const fetchPing = () => {
return; return;
isFetchingPing = true; isFetchingPing = true;
fetch(`${relative_url}ping`, {credentials: "same-origin"}).then(res => res.json()) fetch(`./ping`, {credentials: "same-origin"}).then(res => res.json())
.then(response => { .then(response => {
for (let filename in response) { for (let filename in response) {
let node = document.querySelector(`.status-indicator[data-node="${filename}"]`); let node = document.querySelector(`.status-indicator[data-node="${filename}"]`);
@ -235,7 +235,7 @@ const portSelect = document.querySelector('.nav-wrapper select');
let ports = []; let ports = [];
const fetchSerialPorts = (begin=false) => { const fetchSerialPorts = (begin=false) => {
fetch(`${relative_url}serial-ports`, {credentials: "same-origin"}).then(res => res.json()) fetch(`./serial-ports`, {credentials: "same-origin"}).then(res => res.json())
.then(response => { .then(response => {
if (ports.length === response.length) { if (ports.length === response.length) {
let allEqual = true; let allEqual = true;
@ -333,7 +333,6 @@ class LogModalElem {
} }
_onPress(event) { _onPress(event) {
console.log("_onPress");
this.activeConfig = event.target.getAttribute('data-node'); this.activeConfig = event.target.getAttribute('data-node');
this._setupModalInstance(); this._setupModalInstance();
// clear log // clear log
@ -435,12 +434,12 @@ const validateModal = new LogModalElem({
onProcessExit: (modalElem, code) => { onProcessExit: (modalElem, code) => {
if (code === 0) { if (code === 0) {
M.toast({ M.toast({
html: `<code class="inlinecode">${configuration}</code> is valid 👍`, html: `<code class="inlinecode">${validateModal.activeConfig}</code> is valid 👍`,
displayLength: 5000, displayLength: 5000,
}); });
} else { } else {
M.toast({ M.toast({
html: `<code class="inlinecode">${configuration}</code> is invalid 😕`, html: `<code class="inlinecode">${validateModal.activeConfig}</code> is invalid 😕`,
displayLength: 5000, displayLength: 5000,
}); });
} }
@ -477,7 +476,7 @@ compileModal.setup();
downloadButton.addEventListener('click', () => { downloadButton.addEventListener('click', () => {
const link = document.createElement("a"); const link = document.createElement("a");
link.download = name; link.download = name;
link.href = `${relative_url}download.bin?configuration=${encodeURIComponent(compileModal.activeConfig)}`; link.href = `./download.bin?configuration=${encodeURIComponent(compileModal.activeConfig)}`;
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
link.remove(); link.remove();
@ -520,7 +519,7 @@ document.querySelectorAll(".action-delete").forEach((btn) => {
btn.addEventListener('click', (e) => { btn.addEventListener('click', (e) => {
let configuration = e.target.getAttribute('data-node'); let configuration = e.target.getAttribute('data-node');
fetch(`${relative_url}delete?configuration=${configuration}`, { fetch(`./delete?configuration=${configuration}`, {
credentials: "same-origin", credentials: "same-origin",
method: "POST", method: "POST",
}).then(res => res.text()).then(() => { }).then(res => res.text()).then(() => {
@ -532,7 +531,7 @@ document.querySelectorAll(".action-delete").forEach((btn) => {
document.querySelector(`.entry-row[data-node="${configuration}"]`).remove(); document.querySelector(`.entry-row[data-node="${configuration}"]`).remove();
undoButton.addEventListener('click', () => { undoButton.addEventListener('click', () => {
fetch(`${relative_url}undo-delete?configuration=${configuration}`, { fetch(`./undo-delete?configuration=${configuration}`, {
credentials: "same-origin", credentials: "same-origin",
method: "POST", method: "POST",
}).then(res => res.text()).then(() => { }).then(res => res.text()).then(() => {
@ -554,7 +553,7 @@ editor.session.setOption('tabSize', 2);
const saveButton = editModalElem.querySelector(".save-button"); const saveButton = editModalElem.querySelector(".save-button");
const saveEditor = () => { const saveEditor = () => {
fetch(`${relative_url}edit?configuration=${activeEditorConfig}`, { fetch(`./edit?configuration=${activeEditorConfig}`, {
credentials: "same-origin", credentials: "same-origin",
method: "POST", method: "POST",
body: editor.getValue() body: editor.getValue()
@ -581,7 +580,7 @@ document.querySelectorAll(".action-edit").forEach((btn) => {
const filenameField = editModalElem.querySelector('.filename'); const filenameField = editModalElem.querySelector('.filename');
filenameField.innerHTML = activeEditorConfig; filenameField.innerHTML = activeEditorConfig;
fetch(`${relative_url}edit?configuration=${activeEditorConfig}`, {credentials: "same-origin"}) fetch(`./edit?configuration=${activeEditorConfig}`, {credentials: "same-origin"})
.then(res => res.text()).then(response => { .then(res => res.text()).then(response => {
editor.setValue(response, -1); editor.setValue(response, -1);
}); });

View file

@ -16,7 +16,6 @@
<script src="{{ get_static_file_url('materialize-stepper.min.js') }}"></script> <script src="{{ get_static_file_url('materialize-stepper.min.js') }}"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script>const relative_url = "{{ relative_url }}";</script>
{% if streamer_mode %} {% if streamer_mode %}
<style> <style>
@ -154,7 +153,7 @@
<div id="modal-wizard" class="modal"> <div id="modal-wizard" class="modal">
<div class="modal-content"> <div class="modal-content">
<form action="/wizard.html" method="POST"> <form action="./wizard.html" method="POST">
<ul class="stepper linear"> <ul class="stepper linear">
<li class="step active"> <li class="step active">
<div class="step-title waves-effect">Introduction And Name</div> <div class="step-title waves-effect">Introduction And Name</div>

View file

@ -28,7 +28,7 @@
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col card s10 offset-s1 m10 offset-m1 l8 offset-l2"> <div class="col card s10 offset-s1 m10 offset-m1 l8 offset-l2">
<form action="{{ relative_url }}login" method="post"> <form action="./login" method="post">
<div class="card-content"> <div class="card-content">
<span class="card-title">Enter credentials</span> <span class="card-title">Enter credentials</span>
<p> <p>

View file

@ -73,8 +73,7 @@ setup(
keywords=['home', 'automation'], keywords=['home', 'automation'],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'esphome = esphome.__main__:main', 'esphome = esphome.__main__:main'
'esphomeyaml = esphome.legacy:main'
] ]
}, },
packages=find_packages() packages=find_packages()