Moved bootstrap code to a seperate bootstrap module.

This commit is contained in:
Paulus Schoutsen 2013-10-13 10:42:22 -07:00
parent 7dfbff31f5
commit 0d156ecbf0
4 changed files with 99 additions and 41 deletions

View File

@ -1,8 +1,12 @@
[common] [common]
latitude=32.87336 latitude=32.87336
longitude=-117.22743 longitude=-117.22743
[httpinterface]
api_password=mypass api_password=mypass
[hue]
[tomato] [tomato]
host=192.168.1.1 host=192.168.1.1
username=admin username=admin

View File

@ -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)

View File

@ -21,8 +21,8 @@ import homeassistant as ha
import homeassistant.util as util import homeassistant.util as util
STATE_CATEGORY_SUN = "weather.sun" STATE_CATEGORY_SUN = "weather.sun"
STATE_CATEGORY_NEXT_SUN_RISING = "weather.next_sun_rising" STATE_CATEGORY_NEXT_SUN_RISING = "weather.sun.next_rising"
STATE_CATEGORY_NEXT_SUN_SETTING = "weather.next_sun_setting" STATE_CATEGORY_NEXT_SUN_SETTING = "weather.sun.next_setting"
STATE_CATEGORY_ALL_DEVICES = 'all_devices' STATE_CATEGORY_ALL_DEVICES = 'all_devices'
STATE_CATEGORY_DEVICE_FORMAT = '{}' STATE_CATEGORY_DEVICE_FORMAT = '{}'

View File

@ -1,43 +1,6 @@
#!/usr/bin/python2 #!/usr/bin/python2
""" Starts home assistant with all possible functionality. """ """ Starts home assistant with all possible functionality. """
from ConfigParser import SafeConfigParser import homeassistant.bootstrap
from homeassistant import StateMachine, EventBus, start_home_assistant homeassistant.bootstrap.from_config_file("home-assistant.conf")
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)