2017-04-30 05:04:49 +00:00
|
|
|
"""Start Home Assistant."""
|
2021-03-17 16:34:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-02-19 05:27:50 +00:00
|
|
|
import argparse
|
2021-09-28 07:05:06 +00:00
|
|
|
import faulthandler
|
2016-02-19 05:27:50 +00:00
|
|
|
import os
|
2014-11-03 01:27:32 +00:00
|
|
|
import sys
|
2016-01-27 03:39:59 +00:00
|
|
|
import threading
|
2016-07-21 05:38:52 +00:00
|
|
|
|
2021-12-23 19:14:47 +00:00
|
|
|
from .const import REQUIRED_PYTHON_VER, RESTART_EXIT_CODE, __version__
|
2015-08-30 00:06:54 +00:00
|
|
|
|
2021-09-28 07:05:06 +00:00
|
|
|
FAULT_LOG_FILENAME = "home-assistant.log.fault"
|
|
|
|
|
2015-05-01 05:44:24 +00:00
|
|
|
|
2022-01-18 21:16:23 +00:00
|
|
|
def validate_os() -> None:
|
|
|
|
"""Validate that Home Assistant is running in a supported operating system."""
|
|
|
|
if not sys.platform.startswith(("darwin", "linux")):
|
|
|
|
print("Home Assistant only supports Linux, OSX and Windows using WSL")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2016-07-21 05:38:52 +00:00
|
|
|
def validate_python() -> None:
|
2017-06-08 13:53:12 +00:00
|
|
|
"""Validate that the right Python version is running."""
|
2018-02-23 07:22:27 +00:00
|
|
|
if sys.version_info[:3] < REQUIRED_PYTHON_VER:
|
2019-07-31 19:25:30 +00:00
|
|
|
print(
|
2020-04-05 15:48:55 +00:00
|
|
|
"Home Assistant requires at least Python "
|
|
|
|
f"{REQUIRED_PYTHON_VER[0]}.{REQUIRED_PYTHON_VER[1]}.{REQUIRED_PYTHON_VER[2]}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-08-31 15:53:59 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2016-07-21 05:38:52 +00:00
|
|
|
def ensure_config_path(config_dir: str) -> None:
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Validate the configuration directory."""
|
2020-04-04 15:07:36 +00:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2021-12-23 19:14:47 +00:00
|
|
|
from . import config as config_util
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
lib_dir = os.path.join(config_dir, "deps")
|
2015-08-30 00:06:54 +00:00
|
|
|
|
2015-01-09 02:45:27 +00:00
|
|
|
# Test if configuration directory exists
|
2014-11-23 20:57:29 +00:00
|
|
|
if not os.path.isdir(config_dir):
|
2015-08-30 03:31:33 +00:00
|
|
|
if config_dir != config_util.get_default_config_dir():
|
2019-07-31 19:25:30 +00:00
|
|
|
print(
|
2020-01-03 13:47:06 +00:00
|
|
|
f"Fatal Error: Specified configuration directory {config_dir} "
|
|
|
|
"does not exist"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-08-30 03:31:33 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(config_dir)
|
|
|
|
except OSError:
|
2019-07-31 19:25:30 +00:00
|
|
|
print(
|
2020-01-03 13:47:06 +00:00
|
|
|
"Fatal Error: Unable to create default configuration "
|
|
|
|
f"directory {config_dir}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2015-08-30 03:31:33 +00:00
|
|
|
sys.exit(1)
|
2015-08-30 00:06:54 +00:00
|
|
|
|
|
|
|
# Test if library directory exists
|
|
|
|
if not os.path.isdir(lib_dir):
|
|
|
|
try:
|
|
|
|
os.mkdir(lib_dir)
|
|
|
|
except OSError:
|
2020-01-03 13:47:06 +00:00
|
|
|
print(f"Fatal Error: Unable to create library directory {lib_dir}")
|
2015-08-30 03:31:33 +00:00
|
|
|
sys.exit(1)
|
2015-04-26 17:05:01 +00:00
|
|
|
|
2015-08-30 06:02:07 +00:00
|
|
|
|
2016-07-21 05:38:52 +00:00
|
|
|
def get_arguments() -> argparse.Namespace:
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Get parsed passed in arguments."""
|
2020-04-04 15:07:36 +00:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2021-12-23 19:14:47 +00:00
|
|
|
from . import config as config_util
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2015-08-30 06:02:07 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2022-01-18 14:18:35 +00:00
|
|
|
description="Home Assistant: Observe, Control, Automate.",
|
|
|
|
epilog=f"If restart is requested, exits with code {RESTART_EXIT_CODE}",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
parser.add_argument("--version", action="version", version=__version__)
|
2015-01-09 02:45:27 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
metavar="path_to_config_dir",
|
2015-08-30 01:11:24 +00:00
|
|
|
default=config_util.get_default_config_dir(),
|
2019-07-31 19:25:30 +00:00
|
|
|
help="Directory that contains the Home Assistant configuration",
|
|
|
|
)
|
2015-01-18 06:23:07 +00:00
|
|
|
parser.add_argument(
|
2020-01-14 21:03:02 +00:00
|
|
|
"--safe-mode", action="store_true", help="Start Home Assistant in safe mode"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-06 14:48:36 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--debug", action="store_true", help="Start Home Assistant in debug mode"
|
|
|
|
)
|
2015-01-18 05:13:02 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--open-ui", action="store_true", help="Open the webinterface in a browser"
|
|
|
|
)
|
2015-09-04 21:50:57 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--skip-pip",
|
|
|
|
action="store_true",
|
|
|
|
help="Skips pip install of required packages on startup",
|
|
|
|
)
|
2015-09-01 06:12:00 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"-v", "--verbose", action="store_true", help="Enable verbose logging to file."
|
|
|
|
)
|
2015-09-04 22:22:42 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--log-rotate-days",
|
2015-09-04 22:22:42 +00:00
|
|
|
type=int,
|
|
|
|
default=None,
|
2019-07-31 19:25:30 +00:00
|
|
|
help="Enables daily log rotation and keeps up to the specified days",
|
|
|
|
)
|
2017-09-14 04:22:42 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--log-file",
|
2017-09-14 04:22:42 +00:00
|
|
|
type=str,
|
|
|
|
default=None,
|
2020-01-02 19:17:10 +00:00
|
|
|
help="Log file to write to. If not set, CONFIG/home-assistant.log is used",
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-04-18 14:18:44 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--log-no-color", action="store_true", help="Disable color logs"
|
|
|
|
)
|
2016-07-03 18:38:14 +00:00
|
|
|
parser.add_argument(
|
2019-07-31 19:25:30 +00:00
|
|
|
"--script", nargs=argparse.REMAINDER, help="Run one of the embedded scripts"
|
|
|
|
)
|
2022-01-18 21:16:23 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--ignore-os-check",
|
|
|
|
action="store_true",
|
|
|
|
help="Skips validation of operating system",
|
|
|
|
)
|
2015-09-01 06:12:00 +00:00
|
|
|
|
|
|
|
arguments = parser.parse_args()
|
2016-05-20 06:20:59 +00:00
|
|
|
|
2015-09-01 06:12:00 +00:00
|
|
|
return arguments
|
|
|
|
|
|
|
|
|
2021-03-17 16:34:55 +00:00
|
|
|
def cmdline() -> list[str]:
|
2016-05-20 06:20:59 +00:00
|
|
|
"""Collect path and arguments to re-execute the current hass instance."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if os.path.basename(sys.argv[0]) == "__main__.py":
|
2016-05-20 18:45:16 +00:00
|
|
|
modulepath = os.path.dirname(sys.argv[0])
|
2019-07-31 19:25:30 +00:00
|
|
|
os.environ["PYTHONPATH"] = os.path.dirname(modulepath)
|
2022-01-18 13:01:57 +00:00
|
|
|
return [sys.executable, "-m", "homeassistant"] + list(sys.argv[1:])
|
2017-07-06 03:02:16 +00:00
|
|
|
|
2022-01-18 13:01:57 +00:00
|
|
|
return sys.argv
|
2016-05-20 06:20:59 +00:00
|
|
|
|
|
|
|
|
2022-01-18 14:18:35 +00:00
|
|
|
def check_threads() -> None:
|
|
|
|
"""Check if there are any lingering threads."""
|
2016-05-22 04:35:33 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
nthreads = sum(
|
|
|
|
thread.is_alive() and not thread.daemon for thread in threading.enumerate()
|
|
|
|
)
|
2016-05-22 04:35:33 +00:00
|
|
|
if nthreads > 1:
|
2019-08-23 16:53:33 +00:00
|
|
|
sys.stderr.write(f"Found {nthreads} non-daemonic threads.\n")
|
2016-05-22 04:35:33 +00:00
|
|
|
|
|
|
|
# Somehow we sometimes seem to trigger an assertion in the python threading
|
|
|
|
# module. It seems we find threads that have no associated OS level thread
|
|
|
|
# which are not marked as stopped at the python level.
|
|
|
|
except AssertionError:
|
|
|
|
sys.stderr.write("Failed to count non-daemonic threads.\n")
|
2016-05-20 06:20:59 +00:00
|
|
|
|
2016-01-27 03:39:59 +00:00
|
|
|
|
2016-07-21 05:38:52 +00:00
|
|
|
def main() -> int:
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Start Home Assistant."""
|
2017-04-06 06:23:02 +00:00
|
|
|
validate_python()
|
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
args = get_arguments()
|
|
|
|
|
2022-01-18 21:16:23 +00:00
|
|
|
if not args.ignore_os_check:
|
|
|
|
validate_os()
|
|
|
|
|
2016-07-03 18:38:14 +00:00
|
|
|
if args.script is not None:
|
2020-04-04 15:07:36 +00:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2021-12-23 19:14:47 +00:00
|
|
|
from . import scripts
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-07-03 18:38:14 +00:00
|
|
|
return scripts.run(args.script)
|
|
|
|
|
2020-01-14 21:03:02 +00:00
|
|
|
config_dir = os.path.abspath(os.path.join(os.getcwd(), args.config))
|
2015-08-30 06:02:07 +00:00
|
|
|
ensure_config_path(config_dir)
|
2015-01-09 02:45:27 +00:00
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
# pylint: disable=import-outside-toplevel
|
2021-12-23 19:14:47 +00:00
|
|
|
from . import runner
|
2020-07-06 22:58:53 +00:00
|
|
|
|
|
|
|
runtime_conf = runner.RuntimeConfig(
|
|
|
|
config_dir=config_dir,
|
|
|
|
verbose=args.verbose,
|
|
|
|
log_rotate_days=args.log_rotate_days,
|
|
|
|
log_file=args.log_file,
|
|
|
|
log_no_color=args.log_no_color,
|
|
|
|
skip_pip=args.skip_pip,
|
|
|
|
safe_mode=args.safe_mode,
|
|
|
|
debug=args.debug,
|
|
|
|
open_ui=args.open_ui,
|
|
|
|
)
|
|
|
|
|
2021-09-28 07:05:06 +00:00
|
|
|
fault_file_name = os.path.join(config_dir, FAULT_LOG_FILENAME)
|
|
|
|
with open(fault_file_name, mode="a", encoding="utf8") as fault_file:
|
|
|
|
faulthandler.enable(fault_file)
|
|
|
|
exit_code = runner.run(runtime_conf)
|
|
|
|
faulthandler.disable()
|
|
|
|
|
|
|
|
if os.path.getsize(fault_file_name) == 0:
|
|
|
|
os.remove(fault_file_name)
|
|
|
|
|
2022-01-18 14:18:35 +00:00
|
|
|
check_threads()
|
2016-05-20 06:20:59 +00:00
|
|
|
|
2019-12-16 06:29:19 +00:00
|
|
|
return exit_code
|
2015-08-30 03:31:33 +00:00
|
|
|
|
2014-11-03 01:27:32 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-02-06 14:48:36 +00:00
|
|
|
sys.exit(main())
|