2013-10-13 17:42:22 +00:00
|
|
|
"""
|
|
|
|
Provides methods to bootstrap a home assistant instance.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import ConfigParser
|
2013-10-22 05:06:22 +00:00
|
|
|
import logging
|
2013-10-13 17:42:22 +00:00
|
|
|
|
|
|
|
import homeassistant as ha
|
2013-12-11 08:07:30 +00:00
|
|
|
from homeassistant.components import (general, chromecast,
|
|
|
|
device_sun_light_trigger, device,
|
|
|
|
downloader, keyboard, light, sun,
|
|
|
|
browser, httpinterface)
|
2013-10-13 17:42:22 +00:00
|
|
|
|
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
|
2013-10-13 17:42:22 +00:00
|
|
|
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__)
|
|
|
|
|
2013-10-22 05:06:22 +00:00
|
|
|
statusses = []
|
|
|
|
|
2013-10-13 17:42:22 +00:00
|
|
|
# Read config
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
|
|
|
config.read(config_path)
|
|
|
|
|
|
|
|
# Init core
|
2013-11-20 07:48:08 +00:00
|
|
|
bus = ha.Bus()
|
|
|
|
statemachine = ha.StateMachine(bus)
|
2013-10-13 17:42:22 +00:00
|
|
|
|
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))
|
|
|
|
|
2013-10-13 17:42:22 +00:00
|
|
|
# Device scanner
|
2014-01-04 21:48:17 +00:00
|
|
|
dev_scan = None
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
try:
|
|
|
|
# For the error message if not all option fields exist
|
|
|
|
opt_fields = "host, username, password"
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
if has_section('tomato'):
|
|
|
|
dev_scan_name = "Tomato"
|
|
|
|
opt_fields += ", http_id"
|
2013-10-22 05:06:22 +00:00
|
|
|
|
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'))
|
2013-12-12 04:43:26 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
elif has_section('netgear'):
|
|
|
|
dev_scan_name = "Netgear"
|
2013-12-12 04:43:26 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
dev_scan = device.NetgearDeviceScanner(
|
|
|
|
get_opt('netgear', 'host'),
|
|
|
|
get_opt('netgear', 'username'),
|
|
|
|
get_opt('netgear', 'password'))
|
2013-10-22 05:06:22 +00:00
|
|
|
|
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)
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
if not dev_scan.success_init:
|
|
|
|
dev_scan = None
|
2013-12-12 04:43:26 +00:00
|
|
|
|
2013-10-13 17:42:22 +00:00
|
|
|
# Device Tracker
|
2014-01-04 21:48:17 +00:00
|
|
|
if dev_scan:
|
|
|
|
device.DeviceTracker(bus, statemachine, dev_scan)
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Device Tracker", True)
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2013-10-13 17:42:22 +00:00
|
|
|
# Sun tracker
|
2014-01-04 21:48:17 +00:00
|
|
|
if has_opt("common", "latitude") and \
|
|
|
|
has_opt("common", "longitude"):
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Weather - Ephem",
|
|
|
|
sun.setup(
|
|
|
|
bus, statemachine,
|
|
|
|
get_opt("common", "latitude"),
|
|
|
|
get_opt("common", "longitude")))
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
# Chromecast
|
2014-01-04 21:48:17 +00:00
|
|
|
if has_opt("chromecast", "host"):
|
2013-12-11 08:07:30 +00:00
|
|
|
chromecast_started = chromecast.setup(bus, statemachine,
|
2014-01-04 21:48:17 +00:00
|
|
|
get_opt("chromecast", "host"))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Chromecast", chromecast_started)
|
2013-12-11 08:07:30 +00:00
|
|
|
else:
|
|
|
|
chromecast_started = False
|
2013-12-07 19:42:13 +00:00
|
|
|
|
2013-10-13 17:42:22 +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"))
|
2013-11-01 01:30:45 +00:00
|
|
|
else:
|
2013-12-11 08:07:30 +00:00
|
|
|
light_control = light.HueLightControl()
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Light Control - Hue", light_control.success_init)
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
light.setup(bus, statemachine, light_control)
|
2013-10-22 05:06:22 +00:00
|
|
|
else:
|
|
|
|
light_control = None
|
2013-10-13 17:42:22 +00:00
|
|
|
|
|
|
|
# Light trigger
|
|
|
|
if light_control:
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Light Trigger",
|
|
|
|
device_sun_light_trigger.setup(bus, statemachine))
|
2013-10-22 05:06:22 +00:00
|
|
|
|
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")))
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2013-12-11 08:07:30 +00:00
|
|
|
# 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))
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Browser", browser.setup(bus))
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("Media Buttons", keyboard.setup(bus))
|
2013-10-22 05:06:22 +00:00
|
|
|
|
2013-10-13 17:42:22 +00:00
|
|
|
# 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(
|
2013-11-20 07:48:08 +00:00
|
|
|
bus, statemachine,
|
2014-01-04 21:48:17 +00:00
|
|
|
get_opt("httpinterface", "api_password"))
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2014-01-04 21:48:17 +00:00
|
|
|
add_status("HTTPInterface", True)
|
2013-10-22 05:06:22 +00:00
|
|
|
|
|
|
|
for component, success_init in statusses:
|
|
|
|
status = "initialized" if success_init else "Failed to initialize"
|
|
|
|
|
|
|
|
logger.info("{}: {}".format(component, status))
|
2013-10-13 17:42:22 +00:00
|
|
|
|
2013-11-20 07:48:08 +00:00
|
|
|
ha.start_home_assistant(bus)
|