1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00
ha-core/homeassistant/__main__.py

105 lines
3.0 KiB
Python
Raw Normal View History

""" Starts home assistant. """
from __future__ import print_function
import sys
import os
2014-11-05 16:58:20 +01:00
import argparse
from homeassistant import bootstrap
import homeassistant.config as config_util
2015-08-30 09:59:27 +02:00
from homeassistant.const import __version__, EVENT_HOMEASSISTANT_START
2015-05-01 07:44:24 +02:00
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(
'--open-ui',
action='store_true',
help='Open the webinterface in a browser')
return parser.parse_args()
def main():
""" Starts Home Assistant. """
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)
if args.demo_mode:
hass = bootstrap.from_config_dict({
'frontend': {},
'demo': {}
}, config_dir=config_dir)
else:
2015-08-30 08:02:07 +02:00
config_file = ensure_config_file(config_dir)
hass = bootstrap.from_config_file(config_file)
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)
2014-11-23 21:57:29 +01:00
hass.start()
hass.block_till_stopped()
if __name__ == "__main__":
main()