core/homeassistant/bootstrap.py

146 lines
4.4 KiB
Python
Raw Normal View History

"""
Provides methods to bootstrap a home assistant instance.
"""
import ConfigParser
import logging
import homeassistant as ha
from homeassistant.components import (general, chromecast,
device_sun_light_trigger, device,
downloader, keyboard, light, sun,
browser, httpinterface)
2013-11-11 00:46:48 +00:00
2014-01-04 21:48:17 +00:00
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
def from_config_file(config_path):
""" Starts home assistant with all possible functionality
based on a config file. """
2014-01-04 21:48:17 +00:00
logger = logging.getLogger(__name__)
statusses = []
# Read config
config = ConfigParser.SafeConfigParser()
config.read(config_path)
# Init core
bus = ha.Bus()
statemachine = ha.StateMachine(bus)
2014-01-04 21:48:17 +00:00
has_opt = config.has_option
get_opt = config.get
has_section = config.has_section
add_status = lambda name, result: statusses.append((name, result))
# Device scanner
2014-01-04 21:48:17 +00:00
dev_scan = None
2014-01-04 21:48:17 +00:00
try:
# For the error message if not all option fields exist
opt_fields = "host, username, password"
2014-01-04 21:48:17 +00:00
if has_section('tomato'):
dev_scan_name = "Tomato"
opt_fields += ", http_id"
2014-01-04 21:48:17 +00:00
dev_scan = device.TomatoDeviceScanner(
get_opt('tomato', 'host'),
get_opt('tomato', 'username'),
get_opt('tomato', 'password'),
get_opt('tomato', 'http_id'))
2014-01-04 21:48:17 +00:00
elif has_section('netgear'):
dev_scan_name = "Netgear"
2014-01-04 21:48:17 +00:00
dev_scan = device.NetgearDeviceScanner(
get_opt('netgear', 'host'),
get_opt('netgear', 'username'),
get_opt('netgear', 'password'))
2014-01-04 21:48:17 +00:00
except ConfigParser.NoOptionError:
# If one of the options didn't exist
logger.exception(("Error initializing {}DeviceScanner, "
"could not find one of the following config "
"options: {}".format(dev_scan_name, opt_fields)))
add_status("Device Scanner - {}".format(dev_scan_name), False)
if dev_scan:
add_status("Device Scanner - {}".format(dev_scan_name),
dev_scan.success_init)
2014-01-04 21:48:17 +00:00
if not dev_scan.success_init:
dev_scan = None
# Device Tracker
2014-01-04 21:48:17 +00:00
if dev_scan:
device.DeviceTracker(bus, statemachine, dev_scan)
2014-01-04 21:48:17 +00:00
add_status("Device Tracker", True)
# Sun tracker
2014-01-04 21:48:17 +00:00
if has_opt("common", "latitude") and \
has_opt("common", "longitude"):
2014-01-04 21:48:17 +00:00
add_status("Weather - Ephem",
sun.setup(
bus, statemachine,
get_opt("common", "latitude"),
get_opt("common", "longitude")))
# Chromecast
2014-01-04 21:48:17 +00:00
if has_opt("chromecast", "host"):
chromecast_started = chromecast.setup(bus, statemachine,
2014-01-04 21:48:17 +00:00
get_opt("chromecast", "host"))
2014-01-04 21:48:17 +00:00
add_status("Chromecast", chromecast_started)
else:
chromecast_started = False
2013-12-07 19:42:13 +00:00
# Light control
2014-01-04 21:48:17 +00:00
if has_section("hue"):
if has_opt("hue", "host"):
light_control = light.HueLightControl(get_opt("hue", "host"))
else:
light_control = light.HueLightControl()
2014-01-04 21:48:17 +00:00
add_status("Light Control - Hue", light_control.success_init)
2014-01-04 21:48:17 +00:00
light.setup(bus, statemachine, light_control)
else:
light_control = None
# Light trigger
if light_control:
2014-01-04 21:48:17 +00:00
add_status("Light Trigger",
device_sun_light_trigger.setup(bus, statemachine))
2014-01-04 21:48:17 +00:00
if has_opt("downloader", "download_dir"):
add_status("Downloader", downloader.setup(
bus, get_opt("downloader", "download_dir")))
# Currently only works with Chromecast or Light_Control
if chromecast_started or light_control:
2014-01-04 21:48:17 +00:00
add_status("General", general.setup(bus, statemachine))
2014-01-04 21:48:17 +00:00
add_status("Browser", browser.setup(bus))
2014-01-04 21:48:17 +00:00
add_status("Media Buttons", keyboard.setup(bus))
# Init HTTP interface
2014-01-04 21:48:17 +00:00
if has_opt("httpinterface", "api_password"):
2013-11-11 00:46:48 +00:00
httpinterface.HTTPInterface(
bus, statemachine,
2014-01-04 21:48:17 +00:00
get_opt("httpinterface", "api_password"))
2014-01-04 21:48:17 +00:00
add_status("HTTPInterface", True)
for component, success_init in statusses:
status = "initialized" if success_init else "Failed to initialize"
logger.info("{}: {}".format(component, status))
ha.start_home_assistant(bus)