2016-03-07 23:06:04 +00:00
|
|
|
"""Starts home assistant."""
|
2015-01-21 06:14:10 +00:00
|
|
|
from __future__ import print_function
|
2014-11-03 01:27:32 +00:00
|
|
|
|
2016-02-19 05:27:50 +00:00
|
|
|
import argparse
|
|
|
|
import os
|
2016-01-27 03:39:59 +00:00
|
|
|
import signal
|
2014-11-03 01:27:32 +00:00
|
|
|
import sys
|
2016-01-27 03:39:59 +00:00
|
|
|
import threading
|
2016-02-10 19:29:25 +00:00
|
|
|
import time
|
2016-02-19 05:27:50 +00:00
|
|
|
from multiprocessing import Process
|
2014-11-03 01:27:32 +00:00
|
|
|
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import (
|
2016-02-28 04:41:03 +00:00
|
|
|
__version__,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
REQUIRED_PYTHON_VER,
|
|
|
|
RESTART_EXIT_CODE,
|
|
|
|
)
|
2015-08-30 00:06:54 +00:00
|
|
|
|
2015-05-01 05:44:24 +00:00
|
|
|
|
2015-08-31 15:53:59 +00:00
|
|
|
def validate_python():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Validate we're running the right Python version."""
|
2015-08-31 15:53:59 +00:00
|
|
|
major, minor = sys.version_info[:2]
|
2016-02-28 04:41:03 +00:00
|
|
|
req_major, req_minor = REQUIRED_PYTHON_VER
|
2015-08-31 15:53:59 +00:00
|
|
|
|
2016-02-28 04:41:03 +00: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 15:53:59 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2015-01-09 02:45:27 +00:00
|
|
|
def ensure_config_path(config_dir):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Validate the configuration directory."""
|
2016-03-27 15:44:15 +00:00
|
|
|
import homeassistant.config as config_util
|
2015-08-30 00:06:54 +00:00
|
|
|
lib_dir = os.path.join(config_dir, 'lib')
|
|
|
|
|
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():
|
2015-08-30 02:19:52 +00:00
|
|
|
print(('Fatal Error: Specified configuration directory does '
|
|
|
|
'not exist {} ').format(config_dir))
|
2015-08-30 03:31:33 +00:00
|
|
|
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)
|
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:
|
|
|
|
print(('Fatal Error: Unable to create library '
|
|
|
|
'directory {} ').format(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
|
|
|
|
|
|
|
def ensure_config_file(config_dir):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Ensure configuration file exists."""
|
2016-03-27 15:44:15 +00:00
|
|
|
import homeassistant.config as config_util
|
2015-04-26 17:05:01 +00:00
|
|
|
config_path = config_util.ensure_config_exists(config_dir)
|
|
|
|
|
|
|
|
if config_path is None:
|
|
|
|
print('Error getting configuration path')
|
2015-08-30 03:31:33 +00:00
|
|
|
sys.exit(1)
|
2014-11-08 19:01:47 +00:00
|
|
|
|
2015-01-09 02:45:27 +00:00
|
|
|
return config_path
|
|
|
|
|
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
def get_arguments():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Get parsed passed in arguments."""
|
2016-03-27 15:44:15 +00:00
|
|
|
import homeassistant.config as config_util
|
2015-08-30 06:02:07 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Home Assistant: Observe, Control, Automate.")
|
2015-08-30 07:59:27 +00:00
|
|
|
parser.add_argument('--version', action='version', version=__version__)
|
2015-01-09 02:45:27 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--config',
|
|
|
|
metavar='path_to_config_dir',
|
2015-08-30 01:11:24 +00:00
|
|
|
default=config_util.get_default_config_dir(),
|
2015-01-09 02:45:27 +00:00
|
|
|
help="Directory that contains the Home Assistant configuration")
|
2015-01-18 06:23:07 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--demo-mode',
|
|
|
|
action='store_true',
|
|
|
|
help='Start Home Assistant in demo mode')
|
2016-02-06 14:48:36 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--debug',
|
|
|
|
action='store_true',
|
|
|
|
help='Start Home Assistant in debug mode. Runs in single process to '
|
|
|
|
'enable use of interactive debuggers.')
|
2015-01-18 05:13:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--open-ui',
|
|
|
|
action='store_true',
|
|
|
|
help='Open the webinterface in a browser')
|
2015-09-04 21:50:57 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--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(
|
|
|
|
'-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')
|
2015-09-04 22:22:42 +00:00
|
|
|
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 06:09:02 +00: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 07:25:36 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--restart-osx',
|
|
|
|
action='store_true',
|
|
|
|
help='Restarts on OS X.')
|
2015-09-01 06:12:00 +00:00
|
|
|
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():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Move current process to daemon process."""
|
|
|
|
# Create first fork
|
2015-09-01 06:12:00 +00:00
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2016-03-07 23:06:04 +00:00
|
|
|
# Decouple fork
|
2015-09-01 06:12:00 +00:00
|
|
|
os.setsid()
|
|
|
|
os.umask(0)
|
|
|
|
|
2016-03-07 23:06:04 +00:00
|
|
|
# Create second fork
|
2015-09-01 06:12:00 +00:00
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def check_pid(pid_file):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Check that HA is not already running."""
|
|
|
|
# Check pid file
|
2015-09-01 06:37:52 +00:00
|
|
|
try:
|
|
|
|
pid = int(open(pid_file, 'r').readline())
|
|
|
|
except IOError:
|
2015-09-01 07:22:43 +00:00
|
|
|
# PID File does not exist
|
2015-09-01 07:29:07 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
|
|
|
# PID does not exist
|
|
|
|
return
|
|
|
|
print('Fatal Error: HomeAssistant is already running.')
|
|
|
|
sys.exit(1)
|
2015-09-01 06:12:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_pid(pid_file):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Create a PID File."""
|
2015-09-01 07:29:07 +00:00
|
|
|
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-01-09 02:45:27 +00:00
|
|
|
|
2015-09-15 06:17:01 +00:00
|
|
|
|
2015-09-15 06:09:02 +00:00
|
|
|
def install_osx():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Setup to run via launchd on OS X."""
|
2015-09-15 06:46:06 +00:00
|
|
|
with os.popen('which hass') as inp:
|
2015-09-15 06:58:13 +00:00
|
|
|
hass_path = inp.read().strip()
|
2015-09-15 06:46:06 +00:00
|
|
|
|
|
|
|
with os.popen('whoami') as inp:
|
2015-09-15 06:58:13 +00:00
|
|
|
user = inp.read().strip()
|
2015-09-15 06:09:02 +00:00
|
|
|
|
2015-09-15 06:30:19 +00:00
|
|
|
cwd = os.path.dirname(__file__)
|
|
|
|
template_path = os.path.join(cwd, 'startup', 'launchd.plist')
|
|
|
|
|
2015-09-15 06:46:06 +00:00
|
|
|
with open(template_path, 'r', encoding='utf-8') as inp:
|
2015-09-15 06:58:13 +00:00
|
|
|
plist = inp.read()
|
2015-09-15 06:46:06 +00:00
|
|
|
|
2015-09-15 06:09:02 +00:00
|
|
|
plist = plist.replace("$HASS_PATH$", hass_path)
|
|
|
|
plist = plist.replace("$USER$", user)
|
|
|
|
|
2015-09-15 06:17:01 +00:00
|
|
|
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
|
|
|
|
2015-09-15 06:54:22 +00:00
|
|
|
try:
|
|
|
|
with open(path, 'w', encoding='utf-8') as outp:
|
2015-09-15 06:58:13 +00:00
|
|
|
outp.write(plist)
|
2015-09-15 06:54:22 +00:00
|
|
|
except IOError as err:
|
|
|
|
print('Unable to write to ' + path, err)
|
|
|
|
return
|
2015-09-15 06:46:06 +00:00
|
|
|
|
2015-09-15 06:09:02 +00:00
|
|
|
os.popen('launchctl load -w -F ' + path)
|
|
|
|
|
2015-09-15 06:12:31 +00:00
|
|
|
print("Home Assistant has been installed. \
|
|
|
|
Open it here: http://localhost:8123")
|
2015-09-15 06:09:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def uninstall_osx():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Unload from launchd on OS X."""
|
2015-09-15 06:17:01 +00:00
|
|
|
path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
|
2015-09-15 06:09:02 +00:00
|
|
|
os.popen('launchctl unload ' + path)
|
|
|
|
|
|
|
|
print("Home Assistant has been uninstalled.")
|
|
|
|
|
2015-09-17 07:52:04 +00:00
|
|
|
|
2016-02-06 14:48:36 +00:00
|
|
|
def setup_and_run_hass(config_dir, args, top_process=False):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Setup HASS and run.
|
|
|
|
|
|
|
|
Block until stopped. Will assume it is running in a subprocess unless
|
|
|
|
top_process is set to true.
|
2016-02-06 14:48:36 +00:00
|
|
|
"""
|
2016-03-27 15:44:15 +00:00
|
|
|
from homeassistant import bootstrap
|
|
|
|
|
2016-01-27 03:39:59 +00:00
|
|
|
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)
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
if hass is None:
|
|
|
|
return
|
|
|
|
|
2016-01-27 03:39:59 +00:00
|
|
|
if args.open_ui:
|
|
|
|
def open_browser(event):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Open the webinterface in a browser."""
|
2016-01-27 03:39:59 +00:00
|
|
|
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()
|
2016-02-06 14:48:36 +00:00
|
|
|
exit_code = int(hass.block_till_stopped())
|
|
|
|
|
|
|
|
if not top_process:
|
|
|
|
sys.exit(exit_code)
|
|
|
|
return exit_code
|
2016-01-27 03:39:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_hass_process(hass_proc):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Run a child hass process. Returns True if it should be restarted."""
|
2016-01-27 03:39:59 +00:00
|
|
|
requested_stop = threading.Event()
|
|
|
|
hass_proc.daemon = True
|
|
|
|
|
2016-02-07 21:05:35 +00:00
|
|
|
def request_stop(*args):
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Request hass stop, *args is for signal handler callback."""
|
2016-01-27 03:39:59 +00:00
|
|
|
requested_stop.set()
|
|
|
|
hass_proc.terminate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
signal.signal(signal.SIGTERM, request_stop)
|
|
|
|
except ValueError:
|
2016-01-30 03:11:11 +00:00
|
|
|
print('Could not bind to SIGTERM. Are you running in a thread?')
|
2016-01-27 03:39:59 +00:00
|
|
|
|
|
|
|
hass_proc.start()
|
|
|
|
try:
|
|
|
|
hass_proc.join()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
request_stop()
|
2016-01-27 03:41:57 +00:00
|
|
|
try:
|
|
|
|
hass_proc.join()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return False
|
2016-02-06 14:48:36 +00:00
|
|
|
|
|
|
|
return (not requested_stop.isSet() and
|
|
|
|
hass_proc.exitcode == RESTART_EXIT_CODE,
|
|
|
|
hass_proc.exitcode)
|
2016-01-27 03:39:59 +00:00
|
|
|
|
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
def main():
|
2016-03-07 23:06:04 +00:00
|
|
|
"""Start Home Assistant."""
|
2015-08-31 15:53:59 +00:00
|
|
|
validate_python()
|
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
args = get_arguments()
|
|
|
|
|
|
|
|
config_dir = 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
|
|
|
|
2016-03-07 23:06:04 +00:00
|
|
|
# OS X launchd functions
|
2015-09-15 06:09:02 +00:00
|
|
|
if args.install_osx:
|
|
|
|
install_osx()
|
2016-02-06 14:48:36 +00:00
|
|
|
return 0
|
2015-09-15 06:09:02 +00:00
|
|
|
if args.uninstall_osx:
|
|
|
|
uninstall_osx()
|
2016-02-06 14:48:36 +00:00
|
|
|
return 0
|
2015-09-17 07:25:36 +00:00
|
|
|
if args.restart_osx:
|
|
|
|
uninstall_osx()
|
2016-02-10 19:32:56 +00:00
|
|
|
# A small delay is needed on some systems to let the unload finish.
|
2016-02-10 19:29:25 +00:00
|
|
|
time.sleep(0.5)
|
2015-09-17 07:25:36 +00:00
|
|
|
install_osx()
|
2016-02-06 14:48:36 +00:00
|
|
|
return 0
|
2015-09-15 06:09:02 +00:00
|
|
|
|
2016-03-07 23:06:04 +00:00
|
|
|
# Daemon functions
|
2015-09-01 06:37:52 +00:00
|
|
|
if args.pid_file:
|
|
|
|
check_pid(args.pid_file)
|
2015-09-01 06:12:00 +00:00
|
|
|
if args.daemon:
|
|
|
|
daemonize()
|
2015-09-01 07:29:07 +00:00
|
|
|
if args.pid_file:
|
|
|
|
write_pid(args.pid_file)
|
2015-09-01 06:12:00 +00:00
|
|
|
|
2016-02-06 14:48:36 +00:00
|
|
|
# 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
|
|
|
|
|
2016-01-30 03:11:11 +00:00
|
|
|
# Run hass as child process. Restart if necessary.
|
2016-01-27 03:39:59 +00:00
|
|
|
keep_running = True
|
|
|
|
while keep_running:
|
|
|
|
hass_proc = Process(target=setup_and_run_hass, args=(config_dir, args))
|
2016-02-06 14:48:36 +00:00
|
|
|
keep_running, exit_code = run_hass_process(hass_proc)
|
|
|
|
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())
|