From 5f0f06b22d003692adcbaf8ecca3cf4b117335d2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 17 Jan 2015 22:23:07 -0800 Subject: [PATCH] Add command-line toggle to run in demo mode --- README.md | 9 +++--- homeassistant/__main__.py | 16 +++++++++-- homeassistant/bootstrap.py | 57 ++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1535c3a169e..1259d5f8047 100644 --- a/README.md +++ b/README.md @@ -33,19 +33,18 @@ If you run into issues while using Home Assistant or during development of a com ## Installation instructions / Quick-start guide -Running Home Assistant requires that python3 and the package requests are installed. - -Run the following code to get up and running with the minimum setup: +Running Home Assistant requires that python3 and the package requests are installed. Run the following code to install and start Home Assistant: ```python git clone --recursive https://github.com/balloob/home-assistant.git cd home-assistant pip3 install -r requirements.txt - python3 -m homeassistant --open-ui ``` -This will start the Home Assistant server and launch its webinterface. By default Home Assistant looks for the configuration file `config/home-assistant.conf`. A standard configuration file will be written if none exists. +The last command will start the Home Assistant server and launch its webinterface. By default Home Assistant looks for the configuration file `config/home-assistant.conf`. A standard configuration file will be written if none exists. + +If you are still exploring if you want to use Home Assistant in the first place, you can enable the demo mode by adding the `--demo-mode` argument to the last command. If you're using Docker, you can use diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 5744df4eb7a..8226e1fdc84 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -18,6 +18,7 @@ except ImportError: from homeassistant import bootstrap from homeassistant.const import EVENT_HOMEASSISTANT_START +from homeassistant.components import http, demo def validate_dependencies(): @@ -55,7 +56,7 @@ def ensure_config_path(config_dir): try: with open(config_path, 'w') as conf: conf.write("[http]\n\n") - conf.write("[demo]\n\n") + conf.write("[discovery]\n\n") except IOError: print(('Fatal Error: No configuration file found and unable ' 'to write a default one to {}').format(config_path)) @@ -73,6 +74,10 @@ def main(): metavar='path_to_config_dir', default="config", 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', @@ -86,7 +91,14 @@ def main(): config_path = ensure_config_path(config_dir) - hass = bootstrap.from_config_file(config_path) + if args.demo_mode: + # Demo mode only requires http and demo components. + hass = bootstrap.from_config_dict({ + http.DOMAIN: {}, + demo.DOMAIN: {} + }) + else: + hass = bootstrap.from_config_file(config_path) if args.open_ui: # pylint: disable=unused-argument diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index aa95878c649..61f856b6e3e 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -61,6 +61,8 @@ def from_config_dict(config, hass=None): if hass is None: hass = homeassistant.HomeAssistant() + enable_logging(hass) + loader.prepare(hass) # Make a copy because we are mutating it. @@ -86,7 +88,7 @@ def from_config_dict(config, hass=None): return hass -def from_config_file(config_path, hass=None, enable_logging=True): +def from_config_file(config_path, hass=None): """ Reads the configuration file and tries to start all the required functionality. Will add functionality to 'hass' parameter if given, @@ -98,32 +100,6 @@ def from_config_file(config_path, hass=None, enable_logging=True): # Set config dir to directory holding config file hass.config_dir = os.path.abspath(os.path.dirname(config_path)) - if enable_logging: - # Setup the logging for home assistant. - logging.basicConfig(level=logging.INFO) - - # Log errors to a file if we have write access to file or config dir - err_log_path = hass.get_config_path("home-assistant.log") - err_path_exists = os.path.isfile(err_log_path) - - # Check if we can write to the error log if it exists or that - # we can create files in the containgin directory if not. - if (err_path_exists and os.access(err_log_path, os.W_OK)) or \ - (not err_path_exists and os.access(hass.config_dir, os.W_OK)): - - err_handler = logging.FileHandler( - err_log_path, mode='w', delay=True) - - err_handler.setLevel(logging.WARNING) - err_handler.setFormatter( - logging.Formatter('%(asctime)s %(name)s: %(message)s', - datefmt='%H:%M %d-%m-%y')) - logging.getLogger('').addHandler(err_handler) - - else: - _LOGGER.error( - "Unable to setup error log %s (access denied)", err_log_path) - # Read config config = configparser.ConfigParser() config.read(config_path) @@ -137,3 +113,30 @@ def from_config_file(config_path, hass=None, enable_logging=True): config_dict[section][key] = val return from_config_dict(config_dict, hass) + + +def enable_logging(hass): + """ Setup the logging for home assistant. """ + logging.basicConfig(level=logging.INFO) + + # Log errors to a file if we have write access to file or config dir + err_log_path = hass.get_config_path("home-assistant.log") + err_path_exists = os.path.isfile(err_log_path) + + # Check if we can write to the error log if it exists or that + # we can create files in the containing directory if not. + if (err_path_exists and os.access(err_log_path, os.W_OK)) or \ + (not err_path_exists and os.access(hass.config_dir, os.W_OK)): + + err_handler = logging.FileHandler( + err_log_path, mode='w', delay=True) + + err_handler.setLevel(logging.WARNING) + err_handler.setFormatter( + logging.Formatter('%(asctime)s %(name)s: %(message)s', + datefmt='%H:%M %d-%m-%y')) + logging.getLogger('').addHandler(err_handler) + + else: + _LOGGER.error( + "Unable to setup error log %s (access denied)", err_log_path)