Home Assistant Unofficial Reference 2024.12.1
__main__.py
Go to the documentation of this file.
1 """Start Home Assistant."""
2 
3 from __future__ import annotations
4 
5 import argparse
6 from contextlib import suppress
7 import faulthandler
8 import os
9 import sys
10 import threading
11 
12 from .backup_restore import restore_backup
13 from .const import REQUIRED_PYTHON_VER, RESTART_EXIT_CODE, __version__
14 
15 FAULT_LOG_FILENAME = "home-assistant.log.fault"
16 
17 
18 def validate_os() -> None:
19  """Validate that Home Assistant is running in a supported operating system."""
20  if not sys.platform.startswith(("darwin", "linux")):
21  print(
22  "Home Assistant only supports Linux, OSX and Windows using WSL",
23  file=sys.stderr,
24  )
25  sys.exit(1)
26 
27 
28 def validate_python() -> None:
29  """Validate that the right Python version is running."""
30  if sys.version_info[:3] < REQUIRED_PYTHON_VER:
31  print(
32  "Home Assistant requires at least Python "
33  f"{REQUIRED_PYTHON_VER[0]}.{REQUIRED_PYTHON_VER[1]}.{REQUIRED_PYTHON_VER[2]}",
34  file=sys.stderr,
35  )
36  sys.exit(1)
37 
38 
39 def ensure_config_path(config_dir: str) -> None:
40  """Validate the configuration directory."""
41  # pylint: disable-next=import-outside-toplevel
42  from . import config as config_util
43 
44  lib_dir = os.path.join(config_dir, "deps")
45 
46  # Test if configuration directory exists
47  if not os.path.isdir(config_dir):
48  if config_dir != config_util.get_default_config_dir():
49  if os.path.exists(config_dir):
50  reason = "is not a directory"
51  else:
52  reason = "does not exist"
53  print(
54  f"Fatal Error: Specified configuration directory {config_dir} {reason}",
55  file=sys.stderr,
56  )
57  sys.exit(1)
58 
59  try:
60  os.mkdir(config_dir)
61  except OSError as ex:
62  print(
63  "Fatal Error: Unable to create default configuration "
64  f"directory {config_dir}: {ex}",
65  file=sys.stderr,
66  )
67  sys.exit(1)
68 
69  # Test if library directory exists
70  if not os.path.isdir(lib_dir):
71  try:
72  os.mkdir(lib_dir)
73  except OSError as ex:
74  print(
75  f"Fatal Error: Unable to create library directory {lib_dir}: {ex}",
76  file=sys.stderr,
77  )
78  sys.exit(1)
79 
80 
81 def get_arguments() -> argparse.Namespace:
82  """Get parsed passed in arguments."""
83  # pylint: disable-next=import-outside-toplevel
84  from . import config as config_util
85 
86  parser = argparse.ArgumentParser(
87  description="Home Assistant: Observe, Control, Automate.",
88  epilog=f"If restart is requested, exits with code {RESTART_EXIT_CODE}",
89  )
90  parser.add_argument("--version", action="version", version=__version__)
91  parser.add_argument(
92  "-c",
93  "--config",
94  metavar="path_to_config_dir",
95  default=config_util.get_default_config_dir(),
96  help="Directory that contains the Home Assistant configuration",
97  )
98  parser.add_argument(
99  "--recovery-mode",
100  action="store_true",
101  help="Start Home Assistant in recovery mode",
102  )
103  parser.add_argument(
104  "--debug", action="store_true", help="Start Home Assistant in debug mode"
105  )
106  parser.add_argument(
107  "--open-ui", action="store_true", help="Open the webinterface in a browser"
108  )
109 
110  skip_pip_group = parser.add_mutually_exclusive_group()
111  skip_pip_group.add_argument(
112  "--skip-pip",
113  action="store_true",
114  help="Skips pip install of required packages on startup",
115  )
116  skip_pip_group.add_argument(
117  "--skip-pip-packages",
118  metavar="package_names",
119  type=lambda arg: arg.split(","),
120  default=[],
121  help="Skip pip install of specific packages on startup",
122  )
123 
124  parser.add_argument(
125  "-v", "--verbose", action="store_true", help="Enable verbose logging to file."
126  )
127  parser.add_argument(
128  "--log-rotate-days",
129  type=int,
130  default=None,
131  help="Enables daily log rotation and keeps up to the specified days",
132  )
133  parser.add_argument(
134  "--log-file",
135  type=str,
136  default=None,
137  help="Log file to write to. If not set, CONFIG/home-assistant.log is used",
138  )
139  parser.add_argument(
140  "--log-no-color", action="store_true", help="Disable color logs"
141  )
142  parser.add_argument(
143  "--script", nargs=argparse.REMAINDER, help="Run one of the embedded scripts"
144  )
145  parser.add_argument(
146  "--ignore-os-check",
147  action="store_true",
148  help="Skips validation of operating system",
149  )
150 
151  return parser.parse_args()
152 
153 
154 def check_threads() -> None:
155  """Check if there are any lingering threads."""
156  try:
157  nthreads = sum(
158  thread.is_alive() and not thread.daemon for thread in threading.enumerate()
159  )
160  if nthreads > 1:
161  sys.stderr.write(f"Found {nthreads} non-daemonic threads.\n")
162 
163  # Somehow we sometimes seem to trigger an assertion in the python threading
164  # module. It seems we find threads that have no associated OS level thread
165  # which are not marked as stopped at the python level.
166  except AssertionError:
167  sys.stderr.write("Failed to count non-daemonic threads.\n")
168 
169 
170 def main() -> int:
171  """Start Home Assistant."""
173 
174  args = get_arguments()
175 
176  if not args.ignore_os_check:
177  validate_os()
178 
179  if args.script is not None:
180  # pylint: disable-next=import-outside-toplevel
181  from . import scripts
182 
183  return scripts.run(args.script)
184 
185  config_dir = os.path.abspath(os.path.join(os.getcwd(), args.config))
186  if restore_backup(config_dir):
187  return RESTART_EXIT_CODE
188 
189  ensure_config_path(config_dir)
190 
191  # pylint: disable-next=import-outside-toplevel
192  from . import config, runner
193 
194  safe_mode = config.safe_mode_enabled(config_dir)
195 
196  runtime_conf = runner.RuntimeConfig(
197  config_dir=config_dir,
198  verbose=args.verbose,
199  log_rotate_days=args.log_rotate_days,
200  log_file=args.log_file,
201  log_no_color=args.log_no_color,
202  skip_pip=args.skip_pip,
203  skip_pip_packages=args.skip_pip_packages,
204  recovery_mode=args.recovery_mode,
205  debug=args.debug,
206  open_ui=args.open_ui,
207  safe_mode=safe_mode,
208  )
209 
210  fault_file_name = os.path.join(config_dir, FAULT_LOG_FILENAME)
211  with open(fault_file_name, mode="a", encoding="utf8") as fault_file:
212  faulthandler.enable(fault_file)
213  exit_code = runner.run(runtime_conf)
214  faulthandler.disable()
215 
216  # It's possible for the fault file to disappear, so suppress obvious errors
217  with suppress(FileNotFoundError):
218  if os.path.getsize(fault_file_name) == 0:
219  os.remove(fault_file_name)
220 
221  check_threads()
222 
223  return exit_code
224 
225 
226 if __name__ == "__main__":
227  sys.exit(main())
None ensure_config_path(str config_dir)
Definition: __main__.py:39
argparse.Namespace get_arguments()
Definition: __main__.py:81
bool restore_backup(str config_dir_path)
None open(self, **Any kwargs)
Definition: lock.py:86