From 0d156ecbf0bc5a27612fea535ed16ff268a61c6d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 13 Oct 2013 10:42:22 -0700 Subject: [PATCH] Moved bootstrap code to a seperate bootstrap module. --- home-assistant.conf.default | 4 ++ homeassistant/bootstrap.py | 91 +++++++++++++++++++++++++++++++++++++ homeassistant/observers.py | 4 +- start.py | 41 +---------------- 4 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 homeassistant/bootstrap.py diff --git a/home-assistant.conf.default b/home-assistant.conf.default index f6441f662b4..f33d1bf256d 100644 --- a/home-assistant.conf.default +++ b/home-assistant.conf.default @@ -1,8 +1,12 @@ [common] latitude=32.87336 longitude=-117.22743 + +[httpinterface] api_password=mypass +[hue] + [tomato] host=192.168.1.1 username=admin diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py new file mode 100644 index 00000000000..a5e64046b9d --- /dev/null +++ b/homeassistant/bootstrap.py @@ -0,0 +1,91 @@ +""" +Provides methods to bootstrap a home assistant instance. +""" + +import ConfigParser + +import homeassistant as ha +import homeassistant.observers as observers +import homeassistant.actors as actors +import homeassistant.httpinterface as httpinterface + +def from_config_file(config_path): + """ Starts home assistant with all possible functionality + based on a config file. """ + + # Read config + config = ConfigParser.SafeConfigParser() + config.read(config_path) + + # Init core + eventbus = ha.EventBus() + statemachine = ha.StateMachine(eventbus) + + # Init observers + # Device scanner + if config.has_option('tomato', 'host') and \ + config.has_option('tomato', 'username') and \ + config.has_option('tomato', 'password') and \ + config.has_option('tomato', 'http_id'): + + device_scanner = observers.TomatoDeviceScanner( + config.get('tomato','host'), + config.get('tomato','username'), + config.get('tomato','password'), + config.get('tomato','http_id')) + + else: + device_scanner = None + + + # Device Tracker + if device_scanner: + device_tracker = observers.DeviceTracker(eventbus, statemachine, + device_scanner) + else: + device_tracker = None + + + # Sun tracker + if config.has_option("common", "latitude") and \ + config.has_option("common", "longitude"): + + observers.track_sun(eventbus, statemachine, + config.get("common","latitude"), + config.get("common","longitude")) + + # Init actors + # Light control + if config.has_section("hue"): + if config.has_option("hue", "host"): + hue_host = config.get("hue", "host") + else: + hue_host = None + + light_control = actors.HueLightControl(hue_host) + + + # Light trigger + if light_control: + actors.LightTrigger(eventbus, statemachine, + device_tracker, light_control) + + + if config.has_option("chromecast", "host"): + actors.setup_chromecast(eventbus, config.get("chromecast", "host")) + + + if config.has_option("downloader", "download_dir"): + actors.setup_file_downloader(eventbus, + config.get("downloader", "download_dir")) + + actors.setup_webbrowser(eventbus) + actors.setup_media_buttons(eventbus) + + # Init HTTP interface + if config.has_option("httpinterface", "api_password"): + httpinterface.HTTPInterface(eventbus, statemachine, + config.get("httpinterface","api_password")) + + + ha.start_home_assistant(eventbus) diff --git a/homeassistant/observers.py b/homeassistant/observers.py index e26c60d7810..c809c878330 100644 --- a/homeassistant/observers.py +++ b/homeassistant/observers.py @@ -21,8 +21,8 @@ import homeassistant as ha import homeassistant.util as util STATE_CATEGORY_SUN = "weather.sun" -STATE_CATEGORY_NEXT_SUN_RISING = "weather.next_sun_rising" -STATE_CATEGORY_NEXT_SUN_SETTING = "weather.next_sun_setting" +STATE_CATEGORY_NEXT_SUN_RISING = "weather.sun.next_rising" +STATE_CATEGORY_NEXT_SUN_SETTING = "weather.sun.next_setting" STATE_CATEGORY_ALL_DEVICES = 'all_devices' STATE_CATEGORY_DEVICE_FORMAT = '{}' diff --git a/start.py b/start.py index d51419521de..80815eec65e 100644 --- a/start.py +++ b/start.py @@ -1,43 +1,6 @@ #!/usr/bin/python2 """ Starts home assistant with all possible functionality. """ -from ConfigParser import SafeConfigParser +import homeassistant.bootstrap -from homeassistant import StateMachine, EventBus, start_home_assistant -from homeassistant import observers -from homeassistant import actors -from homeassistant.httpinterface import HTTPInterface - -# Read config -config = SafeConfigParser() -config.read("home-assistant.conf") - -# Init core -eventbus = EventBus() -statemachine = StateMachine(eventbus) - -# Init observers -tomato = observers.TomatoDeviceScanner(config.get('tomato','host'), - config.get('tomato','username'), - config.get('tomato','password'), - config.get('tomato','http_id')) - -devicetracker = observers.DeviceTracker(eventbus, statemachine, tomato) - -observers.track_sun(eventbus, statemachine, - config.get("common","latitude"), - config.get("common","longitude")) - -# Init actors -actors.LightTrigger(eventbus, statemachine, - devicetracker, actors.HueLightControl()) - -actors.setup_chromecast(eventbus, config.get("chromecast", "host")) -actors.setup_file_downloader(eventbus, config.get("downloader", "download_dir")) -actors.setup_webbrowser(eventbus) -actors.setup_media_buttons(eventbus) - -# Init HTTP interface -HTTPInterface(eventbus, statemachine, config.get("common","api_password")) - -start_home_assistant(eventbus) +homeassistant.bootstrap.from_config_file("home-assistant.conf")