2014-11-03 01:27:32 +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
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2014-11-05 15:58:20 +00:00
|
|
|
import argparse
|
2014-11-03 01:27:32 +00:00
|
|
|
|
2015-08-30 00:06:54 +00:00
|
|
|
from homeassistant import bootstrap
|
|
|
|
import homeassistant.config as config_util
|
2015-08-30 03:31:33 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
2015-08-30 00:06:54 +00:00
|
|
|
|
2015-05-01 05:44:24 +00:00
|
|
|
|
2015-01-09 02:45:27 +00:00
|
|
|
def ensure_config_path(config_dir):
|
|
|
|
""" Gets the path to the configuration file.
|
|
|
|
Creates one if it not exists. """
|
|
|
|
|
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):
|
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():
|
|
|
|
""" Get parsed passed in arguments. """
|
2015-08-30 06:02:07 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Home Assistant: Observe, Control, Automate.")
|
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')
|
2015-01-18 05:13:02 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--open-ui',
|
|
|
|
action='store_true',
|
|
|
|
help='Open the webinterface in a browser')
|
2015-01-09 02:45:27 +00:00
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
return parser.parse_args()
|
2015-01-09 02:45:27 +00:00
|
|
|
|
2015-01-21 06:14:10 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Starts Home Assistant. """
|
|
|
|
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
|
|
|
|
2015-01-18 06:23:07 +00:00
|
|
|
if args.demo_mode:
|
|
|
|
hass = bootstrap.from_config_dict({
|
2015-08-30 02:19:52 +00:00
|
|
|
'frontend': {},
|
|
|
|
'demo': {}
|
2015-08-30 01:11:24 +00:00
|
|
|
}, config_dir=config_dir)
|
2015-01-18 06:23:07 +00:00
|
|
|
else:
|
2015-08-30 06:02:07 +00:00
|
|
|
config_file = ensure_config_file(config_dir)
|
|
|
|
hass = bootstrap.from_config_file(config_file)
|
2015-01-18 05:13:02 +00:00
|
|
|
|
|
|
|
if args.open_ui:
|
|
|
|
def open_browser(event):
|
|
|
|
""" Open the webinterface in a browser. """
|
2015-05-16 06:26:22 +00:00
|
|
|
if hass.config.api is not None:
|
2015-01-18 05:13:02 +00:00
|
|
|
import webbrowser
|
2015-05-16 06:26:22 +00:00
|
|
|
webbrowser.open(hass.config.api.base_url)
|
2015-08-30 03:31:33 +00:00
|
|
|
|
2015-01-18 05:13:02 +00:00
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser)
|
|
|
|
|
2014-11-23 20:57:29 +00:00
|
|
|
hass.start()
|
|
|
|
hass.block_till_stopped()
|
2014-11-03 01:27:32 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|