ha-core/homeassistant/__main__.py

342 lines
9.6 KiB
Python
Raw Normal View History

""" Starts home assistant. """
from __future__ import print_function
2016-02-19 06:27:50 +01:00
import argparse
import os
import signal
import sys
import threading
import time
2016-02-19 06:27:50 +01:00
from multiprocessing import Process
import homeassistant.config as config_util
2016-02-19 06:27:50 +01:00
from homeassistant import bootstrap
from homeassistant.const import (
__version__,
EVENT_HOMEASSISTANT_START,
REQUIRED_PYTHON_VER,
RESTART_EXIT_CODE,
)
2015-05-01 07:44:24 +02:00
2015-08-31 17:53:59 +02:00
def validate_python():
""" Validate we're running the right Python version. """
major, minor = sys.version_info[:2]
req_major, req_minor = REQUIRED_PYTHON_VER
2015-08-31 17:53:59 +02:00
if major < req_major or (major == req_major and minor < req_minor):
print("Home Assistant requires at least Python {}.{}".format(
req_major, req_minor))
2015-08-31 17:53:59 +02:00
sys.exit(1)
def ensure_config_path(config_dir):
2015-08-30 08:35:19 +02:00
""" Validates configuration directory. """
lib_dir = os.path.join(config_dir, 'lib')
# Test if configuration directory exists
2014-11-23 21:57:29 +01:00
if not os.path.isdir(config_dir):
if config_dir != config_util.get_default_config_dir():
print(('Fatal Error: Specified configuration directory does '
'not exist {} ').format(config_dir))
sys.exit(1)
try:
os.mkdir(config_dir)
except OSError:
print(('Fatal Error: Unable to create default configuration '
'directory {} ').format(config_dir))
sys.exit(1)
# Test if library directory exists
if not os.path.isdir(lib_dir):
try:
os.mkdir(lib_dir)
except OSError:
print(('Fatal Error: Unable to create library '
'directory {} ').format(lib_dir))
sys.exit(1)
2015-08-30 08:02:07 +02:00
def ensure_config_file(config_dir):
2015-08-30 08:35:19 +02:00
""" Ensure configuration file exists. """
config_path = config_util.ensure_config_exists(config_dir)
if config_path is None:
print('Error getting configuration path')
sys.exit(1)
2014-11-08 20:01:47 +01:00
return config_path
def get_arguments():
""" Get parsed passed in arguments. """
2015-08-30 08:02:07 +02:00
parser = argparse.ArgumentParser(
description="Home Assistant: Observe, Control, Automate.")
2015-08-30 09:59:27 +02:00
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument(
'-c', '--config',
metavar='path_to_config_dir',
default=config_util.get_default_config_dir(),
help="Directory that contains the Home Assistant configuration")
parser.add_argument(
'--demo-mode',
action='store_true',
help='Start Home Assistant in demo mode')
parser.add_argument(
'--debug',
action='store_true',
help='Start Home Assistant in debug mode. Runs in single process to '
'enable use of interactive debuggers.')
parser.add_argument(
'--open-ui',
action='store_true',
help='Open the webinterface in a browser')
parser.add_argument(
'--skip-pip',
action='store_true',
help='Skips pip install of required packages on startup')
parser.add_argument(
'-v', '--verbose',
action='store_true',
help="Enable verbose logging to file.")
parser.add_argument(
'--pid-file',
metavar='path_to_pid_file',
default=None,
help='Path to PID file useful for running as daemon')
parser.add_argument(
'--log-rotate-days',
type=int,
default=None,
help='Enables daily log rotation and keeps up to the specified days')
2015-09-15 08:09:02 +02:00
parser.add_argument(
'--install-osx',
action='store_true',
help='Installs as a service on OS X and loads on boot.')
parser.add_argument(
'--uninstall-osx',
action='store_true',
help='Uninstalls from OS X.')
2015-09-17 09:25:36 +02:00
parser.add_argument(
'--restart-osx',
action='store_true',
help='Restarts on OS X.')
if os.name != "nt":
parser.add_argument(
'--daemon',
action='store_true',
help='Run Home Assistant as daemon')
arguments = parser.parse_args()
if os.name == "nt":
arguments.daemon = False
return arguments
def daemonize():
""" Move current process to daemon process """
# create first fork
pid = os.fork()
if pid > 0:
sys.exit(0)
# decouple fork
os.setsid()
os.umask(0)
# create second fork
pid = os.fork()
if pid > 0:
sys.exit(0)
def check_pid(pid_file):
""" Check that HA is not already running """
# check pid file
try:
pid = int(open(pid_file, 'r').readline())
except IOError:
2015-09-01 09:22:43 +02:00
# PID File does not exist
return
try:
os.kill(pid, 0)
except OSError:
# PID does not exist
return
print('Fatal Error: HomeAssistant is already running.')
sys.exit(1)
def write_pid(pid_file):
""" Create PID File """
pid = os.getpid()
try:
open(pid_file, 'w').write(str(pid))
except IOError:
print('Fatal Error: Unable to write pid file {}'.format(pid_file))
sys.exit(1)
2015-09-15 08:17:01 +02:00
2015-09-15 08:09:02 +02:00
def install_osx():
2015-09-15 08:35:20 +02:00
""" Setup to run via launchd on OS X """
2015-09-15 08:46:06 +02:00
with os.popen('which hass') as inp:
2015-09-15 08:58:13 +02:00
hass_path = inp.read().strip()
2015-09-15 08:46:06 +02:00
with os.popen('whoami') as inp:
2015-09-15 08:58:13 +02:00
user = inp.read().strip()
2015-09-15 08:09:02 +02:00
2015-09-15 08:30:19 +02:00
cwd = os.path.dirname(__file__)
template_path = os.path.join(cwd, 'startup', 'launchd.plist')
2015-09-15 08:46:06 +02:00
with open(template_path, 'r', encoding='utf-8') as inp:
2015-09-15 08:58:13 +02:00
plist = inp.read()
2015-09-15 08:46:06 +02:00
2015-09-15 08:09:02 +02:00
plist = plist.replace("$HASS_PATH$", hass_path)
plist = plist.replace("$USER$", user)
2015-09-15 08:17:01 +02:00
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
2015-09-15 08:54:22 +02:00
try:
with open(path, 'w', encoding='utf-8') as outp:
2015-09-15 08:58:13 +02:00
outp.write(plist)
2015-09-15 08:54:22 +02:00
except IOError as err:
print('Unable to write to ' + path, err)
return
2015-09-15 08:46:06 +02:00
2015-09-15 08:09:02 +02:00
os.popen('launchctl load -w -F ' + path)
2015-09-15 08:12:31 +02:00
print("Home Assistant has been installed. \
Open it here: http://localhost:8123")
2015-09-15 08:09:02 +02:00
def uninstall_osx():
2015-09-15 08:35:20 +02:00
""" Unload from launchd on OS X """
2015-09-15 08:17:01 +02:00
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
2015-09-15 08:09:02 +02:00
os.popen('launchctl unload ' + path)
print("Home Assistant has been uninstalled.")
2015-09-17 09:52:04 +02:00
def setup_and_run_hass(config_dir, args, top_process=False):
"""
Setup HASS and run. Block until stopped. Will assume it is running in a
subprocess unless top_process is set to true.
"""
if args.demo_mode:
config = {
'frontend': {},
'demo': {}
}
hass = bootstrap.from_config_dict(
config, config_dir=config_dir, daemon=args.daemon,
verbose=args.verbose, skip_pip=args.skip_pip,
log_rotate_days=args.log_rotate_days)
else:
config_file = ensure_config_file(config_dir)
print('Config directory:', config_dir)
hass = bootstrap.from_config_file(
config_file, daemon=args.daemon, verbose=args.verbose,
skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days)
if args.open_ui:
def open_browser(event):
""" Open the webinterface in a browser. """
if hass.config.api is not None:
import webbrowser
webbrowser.open(hass.config.api.base_url)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser)
hass.start()
exit_code = int(hass.block_till_stopped())
if not top_process:
sys.exit(exit_code)
return exit_code
def run_hass_process(hass_proc):
""" Runs a child hass process. Returns True if it should be restarted. """
requested_stop = threading.Event()
hass_proc.daemon = True
2016-02-07 22:05:35 +01:00
def request_stop(*args):
""" request hass stop, *args is for signal handler callback """
requested_stop.set()
hass_proc.terminate()
try:
signal.signal(signal.SIGTERM, request_stop)
except ValueError:
print('Could not bind to SIGTERM. Are you running in a thread?')
hass_proc.start()
try:
hass_proc.join()
except KeyboardInterrupt:
request_stop()
try:
hass_proc.join()
except KeyboardInterrupt:
return False
return (not requested_stop.isSet() and
hass_proc.exitcode == RESTART_EXIT_CODE,
hass_proc.exitcode)
def main():
""" Starts Home Assistant. """
2015-08-31 17:53:59 +02:00
validate_python()
args = get_arguments()
config_dir = os.path.join(os.getcwd(), args.config)
2015-08-30 08:02:07 +02:00
ensure_config_path(config_dir)
2015-09-15 08:09:02 +02:00
# os x launchd functions
if args.install_osx:
install_osx()
return 0
2015-09-15 08:09:02 +02:00
if args.uninstall_osx:
uninstall_osx()
return 0
2015-09-17 09:25:36 +02:00
if args.restart_osx:
uninstall_osx()
# A small delay is needed on some systems to let the unload finish.
time.sleep(0.5)
2015-09-17 09:25:36 +02:00
install_osx()
return 0
2015-09-15 08:09:02 +02:00
# daemon functions
if args.pid_file:
check_pid(args.pid_file)
if args.daemon:
daemonize()
if args.pid_file:
write_pid(args.pid_file)
# Run hass in debug mode if requested
if args.debug:
sys.stderr.write('Running in debug mode. '
'Home Assistant will not be able to restart.\n')
exit_code = setup_and_run_hass(config_dir, args, top_process=True)
if exit_code == RESTART_EXIT_CODE:
sys.stderr.write('Home Assistant requested a '
'restart in debug mode.\n')
return exit_code
# Run hass as child process. Restart if necessary.
keep_running = True
while keep_running:
hass_proc = Process(target=setup_and_run_hass, args=(config_dir, args))
keep_running, exit_code = run_hass_process(hass_proc)
return exit_code
if __name__ == "__main__":
sys.exit(main())