Use more lazy imports

Speeds up esphome invocation a lot
This commit is contained in:
Otto Winter 2019-04-08 22:19:21 +02:00
parent a14d12b0c1
commit dc4c1bc225
No known key found for this signature in database
GPG key ID: DB66C0BE6013F97E
3 changed files with 22 additions and 8 deletions

View file

@ -7,7 +7,7 @@ import os
import random import random
import sys import sys
from esphome import const, core_config, platformio_api, wizard, writer, yaml_util from esphome import const, core_config, writer, yaml_util
from esphome.config import get_component, iter_components, read_config, strip_default_ids from esphome.config import get_component, iter_components, read_config, strip_default_ids
from esphome.const import CONF_BAUD_RATE, CONF_BROKER, CONF_ESPHOME, CONF_LOGGER, \ from esphome.const import CONF_BAUD_RATE, CONF_BROKER, CONF_ESPHOME, CONF_LOGGER, \
CONF_USE_CUSTOM_CODE CONF_USE_CUSTOM_CODE
@ -92,6 +92,8 @@ def get_port_type(port):
def run_miniterm(config, port): def run_miniterm(config, port):
import serial import serial
from esphome import platformio_api
if CONF_LOGGER not in config: if CONF_LOGGER not in config:
_LOGGER.info("Logger is not enabled. Not starting UART logs.") _LOGGER.info("Logger is not enabled. Not starting UART logs.")
return return
@ -155,6 +157,8 @@ def write_cpp(config):
def compile_program(args, config): def compile_program(args, config):
from esphome import platformio_api
_LOGGER.info("Compiling app...") _LOGGER.info("Compiling app...")
rc = platformio_api.run_compile(config, args.verbose) rc = platformio_api.run_compile(config, args.verbose)
if rc != 0 and CORE.is_dev_esphome_core_version and not is_dev_esphome_version(): if rc != 0 and CORE.is_dev_esphome_core_version and not is_dev_esphome_version():
@ -185,6 +189,8 @@ def upload_using_esptool(config, port):
def upload_program(config, args, host): def upload_program(config, args, host):
# if upload is to a serial port use platformio, otherwise assume ota # if upload is to a serial port use platformio, otherwise assume ota
if get_port_type(host) == 'SERIAL': if get_port_type(host) == 'SERIAL':
from esphome import platformio_api
if CORE.is_esp8266: if CORE.is_esp8266:
return upload_using_esptool(config, host) return upload_using_esptool(config, host)
return platformio_api.run_upload(config, args.verbose, host) return platformio_api.run_upload(config, args.verbose, host)
@ -268,6 +274,8 @@ def setup_log(debug=False):
def command_wizard(args): def command_wizard(args):
from esphome import wizard
return wizard.wizard(args.configuration) return wizard.wizard(args.configuration)

View file

@ -1,13 +1,9 @@
from __future__ import print_function from __future__ import print_function
import errno
import logging import logging
import os import os
import socket
import subprocess
from esphome.py_compat import char_to_byte, text_type from esphome.py_compat import char_to_byte, text_type
from esphome.zeroconf import Zeroconf
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -73,6 +69,8 @@ def color(the_color, message=''):
def run_system_command(*args): def run_system_command(*args):
import subprocess
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
rc = p.returncode rc = p.returncode
@ -80,6 +78,8 @@ def run_system_command(*args):
def mkdir_p(path): def mkdir_p(path):
import errno
try: try:
os.makedirs(path) os.makedirs(path)
except OSError as exc: except OSError as exc:
@ -103,6 +103,8 @@ def is_ip_address(host):
def _resolve_with_zeroconf(host): def _resolve_with_zeroconf(host):
from esphome.core import EsphomeError from esphome.core import EsphomeError
from esphome.zeroconf import Zeroconf
try: try:
zc = Zeroconf() zc = Zeroconf()
except Exception: except Exception:
@ -122,6 +124,7 @@ def _resolve_with_zeroconf(host):
def resolve_ip_address(host): def resolve_ip_address(host):
from esphome.core import EsphomeError from esphome.core import EsphomeError
import socket
try: try:
ip = socket.gethostbyname(host) ip = socket.gethostbyname(host)

View file

@ -1,11 +1,9 @@
from __future__ import print_function from __future__ import print_function
import codecs import codecs
import json
import logging import logging
import os import os
import re import re
import shutil
from esphome.config import iter_components from esphome.config import iter_components
from esphome.const import ARDUINO_VERSION_ESP32_1_0_0, ARDUINO_VERSION_ESP8266_2_5_0, \ from esphome.const import ARDUINO_VERSION_ESP32_1_0_0, ARDUINO_VERSION_ESP8266_2_5_0, \
@ -15,7 +13,6 @@ from esphome.const import ARDUINO_VERSION_ESP32_1_0_0, ARDUINO_VERSION_ESP8266_2
from esphome.core import CORE, EsphomeError from esphome.core import CORE, EsphomeError
from esphome.core_config import GITHUB_ARCHIVE_ZIP, LIBRARY_URI_REPO, VERSION_REGEX from esphome.core_config import GITHUB_ARCHIVE_ZIP, LIBRARY_URI_REPO, VERSION_REGEX
from esphome.helpers import mkdir_p, run_system_command from esphome.helpers import mkdir_p, run_system_command
from esphome.symlink_ops import symlink, islink, readlink, unlink
from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS
from esphome.py_compat import IS_PY3, string_types from esphome.py_compat import IS_PY3, string_types
from esphome.storage_json import StorageJSON, storage_path from esphome.storage_json import StorageJSON, storage_path
@ -217,6 +214,8 @@ def update_storage_json():
def symlink_esphome_core_version(esphome_core_version): def symlink_esphome_core_version(esphome_core_version):
from esphome.symlink_ops import symlink, islink, readlink, unlink
lib_path = CORE.relative_build_path('lib') lib_path = CORE.relative_build_path('lib')
dst_path = CORE.relative_build_path('lib', 'esphome-core') dst_path = CORE.relative_build_path('lib', 'esphome-core')
if CORE.is_local_esphome_core_copy: if CORE.is_local_esphome_core_copy:
@ -250,6 +249,8 @@ def format_ini(data):
def gather_lib_deps(): def gather_lib_deps():
import json
lib_deps = set() lib_deps = set()
if CONF_REPOSITORY in CORE.esphome_core_version: if CONF_REPOSITORY in CORE.esphome_core_version:
repo = CORE.esphome_core_version[CONF_REPOSITORY] repo = CORE.esphome_core_version[CONF_REPOSITORY]
@ -509,6 +510,8 @@ def write_cpp(code_s):
def clean_build(): def clean_build():
import shutil
pioenvs = CORE.relative_pioenvs_path() pioenvs = CORE.relative_pioenvs_path()
if os.path.isdir(pioenvs): if os.path.isdir(pioenvs):
_LOGGER.info("Deleting %s", pioenvs) _LOGGER.info("Deleting %s", pioenvs)