2013-10-13 10:42:22 -07:00
|
|
|
"""
|
|
|
|
Provides methods to bootstrap a home assistant instance.
|
2014-04-24 00:40:45 -07:00
|
|
|
|
|
|
|
Each method will return a tuple (bus, statemachine).
|
|
|
|
|
|
|
|
After bootstrapping you can add your own components or
|
|
|
|
start by calling homeassistant.start_home_assistant(bus)
|
2013-10-13 10:42:22 -07:00
|
|
|
"""
|
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
import importlib
|
2014-04-14 00:10:24 -07:00
|
|
|
import configparser
|
2013-10-21 22:06:22 -07:00
|
|
|
import logging
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
import homeassistant
|
2014-01-23 23:26:00 -08:00
|
|
|
import homeassistant.components as components
|
2013-11-10 16:46:48 -08:00
|
|
|
|
2014-01-23 21:34:08 -08:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
|
2014-04-24 00:40:45 -07:00
|
|
|
def from_config_file(config_path, enable_logging=True):
|
2013-10-13 10:42:22 -07:00
|
|
|
""" Starts home assistant with all possible functionality
|
2014-04-24 00:40:45 -07:00
|
|
|
based on a config file.
|
|
|
|
Will return a tuple (bus, statemachine). """
|
|
|
|
|
|
|
|
if enable_logging:
|
|
|
|
# Setup the logging for home assistant.
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
|
|
# Log errors to a file
|
|
|
|
err_handler = logging.FileHandler("home-assistant.log",
|
|
|
|
mode='w', delay=True)
|
|
|
|
err_handler.setLevel(logging.ERROR)
|
|
|
|
err_handler.setFormatter(
|
|
|
|
logging.Formatter('%(asctime)s %(name)s: %(message)s',
|
|
|
|
datefmt='%H:%M %d-%m-%y'))
|
|
|
|
logging.getLogger('').addHandler(err_handler)
|
2014-01-29 18:44:39 -08:00
|
|
|
|
|
|
|
# Start the actual bootstrapping
|
2014-01-04 13:48:17 -08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-10-21 22:06:22 -07:00
|
|
|
statusses = []
|
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Read config
|
2014-04-14 23:48:00 -07:00
|
|
|
config = configparser.ConfigParser()
|
2013-10-13 10:42:22 -07:00
|
|
|
config.read(config_path)
|
|
|
|
|
|
|
|
# Init core
|
2014-04-24 00:40:45 -07:00
|
|
|
hass = homeassistant.HomeAssistant()
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
has_opt = config.has_option
|
|
|
|
get_opt = config.get
|
|
|
|
has_section = config.has_section
|
|
|
|
add_status = lambda name, result: statusses.append((name, result))
|
2014-01-23 17:46:29 -08:00
|
|
|
load_module = lambda module: importlib.import_module(
|
|
|
|
'homeassistant.components.'+module)
|
2014-01-04 13:48:17 -08:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
def get_opt_safe(section, option, default=None):
|
|
|
|
""" Failure proof option retriever. """
|
|
|
|
try:
|
|
|
|
return config.get(section, option)
|
2014-04-14 00:10:24 -07:00
|
|
|
except (configparser.NoSectionError, configparser.NoOptionError):
|
2014-01-04 17:55:05 -08:00
|
|
|
return default
|
|
|
|
|
2014-04-21 17:58:58 -07:00
|
|
|
def get_hosts(section):
|
|
|
|
""" Helper method to retrieve hosts from config. """
|
|
|
|
if has_opt(section, "hosts"):
|
|
|
|
return get_opt(section, "hosts").split(",")
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Device scanner
|
2014-01-04 13:48:17 -08:00
|
|
|
dev_scan = None
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
try:
|
|
|
|
# For the error message if not all option fields exist
|
|
|
|
opt_fields = "host, username, password"
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
if has_section('device_tracker.tomato'):
|
2014-01-23 17:46:29 -08:00
|
|
|
device_tracker = load_module('device_tracker')
|
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
dev_scan_name = "Tomato"
|
|
|
|
opt_fields += ", http_id"
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
dev_scan = device_tracker.TomatoDeviceScanner(
|
|
|
|
get_opt('device_tracker.tomato', 'host'),
|
|
|
|
get_opt('device_tracker.tomato', 'username'),
|
|
|
|
get_opt('device_tracker.tomato', 'password'),
|
|
|
|
get_opt('device_tracker.tomato', 'http_id'))
|
2013-12-11 20:43:26 -08:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
elif has_section('device_tracker.netgear'):
|
2014-01-23 17:46:29 -08:00
|
|
|
device_tracker = load_module('device_tracker')
|
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
dev_scan_name = "Netgear"
|
2013-12-11 20:43:26 -08:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
dev_scan = device_tracker.NetgearDeviceScanner(
|
|
|
|
get_opt('device_tracker.netgear', 'host'),
|
|
|
|
get_opt('device_tracker.netgear', 'username'),
|
|
|
|
get_opt('device_tracker.netgear', 'password'))
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-04-21 22:51:50 +03:00
|
|
|
elif has_section('device_tracker.luci'):
|
|
|
|
device_tracker = load_module('device_tracker')
|
|
|
|
|
|
|
|
dev_scan_name = "Luci"
|
|
|
|
|
|
|
|
dev_scan = device_tracker.LuciDeviceScanner(
|
|
|
|
get_opt('device_tracker.luci', 'host'),
|
|
|
|
get_opt('device_tracker.luci', 'username'),
|
|
|
|
get_opt('device_tracker.luci', 'password'))
|
|
|
|
|
2014-04-14 00:10:24 -07:00
|
|
|
except configparser.NoOptionError:
|
2014-01-04 13:48:17 -08:00
|
|
|
# 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 10:42:22 -07:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
if not dev_scan.success_init:
|
|
|
|
dev_scan = None
|
2013-12-11 20:43:26 -08:00
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Device Tracker
|
2014-01-04 13:48:17 -08:00
|
|
|
if dev_scan:
|
2014-04-24 00:40:45 -07:00
|
|
|
device_tracker.DeviceTracker(hass, dev_scan)
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
add_status("Device Tracker", True)
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Sun tracker
|
2014-01-04 13:48:17 -08:00
|
|
|
if has_opt("common", "latitude") and \
|
|
|
|
has_opt("common", "longitude"):
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
sun = load_module('sun')
|
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
add_status("Sun",
|
|
|
|
sun.setup(hass,
|
|
|
|
get_opt("common", "latitude"),
|
|
|
|
get_opt("common", "longitude")))
|
2014-01-23 17:46:29 -08:00
|
|
|
else:
|
|
|
|
sun = None
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2013-12-11 00:07:30 -08:00
|
|
|
# Chromecast
|
2014-03-11 22:45:05 -07:00
|
|
|
if has_section("chromecast"):
|
2014-01-23 17:46:29 -08:00
|
|
|
chromecast = load_module('chromecast')
|
|
|
|
|
2014-04-21 17:58:58 -07:00
|
|
|
hosts = get_hosts("chromecast")
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
add_status("Chromecast", chromecast.setup(hass, hosts))
|
2013-12-07 11:42:13 -08:00
|
|
|
|
2014-03-23 12:03:34 -07:00
|
|
|
# WeMo
|
|
|
|
if has_section("wemo"):
|
|
|
|
wemo = load_module('wemo')
|
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
hosts = get_hosts("wemo")
|
|
|
|
|
|
|
|
add_status("WeMo", wemo.setup(hass, hosts))
|
2014-03-23 12:03:34 -07:00
|
|
|
|
2014-04-23 23:55:22 +03:00
|
|
|
# Process tracking
|
|
|
|
if has_section("process"):
|
|
|
|
process = load_module('process')
|
|
|
|
|
2014-04-24 17:13:57 +03:00
|
|
|
processes = dict(config.items('process'))
|
|
|
|
add_status("process", process.setup(hass, processes))
|
2014-04-23 23:55:22 +03:00
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Light control
|
2014-01-04 17:55:05 -08:00
|
|
|
if has_section("light.hue"):
|
2014-01-23 17:46:29 -08:00
|
|
|
light = load_module('light')
|
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
light_control = light.HueLightControl(get_opt_safe("hue", "host"))
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
add_status("Light - Hue", light_control.success_init)
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-04-14 00:10:24 -07:00
|
|
|
if light_control.success_init:
|
2014-04-24 00:40:45 -07:00
|
|
|
light.setup(hass, light_control)
|
2014-04-14 00:10:24 -07:00
|
|
|
else:
|
|
|
|
light_control = None
|
|
|
|
|
2013-10-21 22:06:22 -07:00
|
|
|
else:
|
|
|
|
light_control = None
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
if has_opt("downloader", "download_dir"):
|
2014-01-23 17:46:29 -08:00
|
|
|
downloader = load_module('downloader')
|
|
|
|
|
2014-01-04 13:48:17 -08:00
|
|
|
add_status("Downloader", downloader.setup(
|
2014-04-24 00:40:45 -07:00
|
|
|
hass, get_opt("downloader", "download_dir")))
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
add_status("Core components", components.setup(hass))
|
2013-12-11 00:07:30 -08:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
if has_section('browser'):
|
2014-04-24 00:40:45 -07:00
|
|
|
add_status("Browser", load_module('browser').setup(hass))
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
if has_section('keyboard'):
|
2014-04-24 00:40:45 -07:00
|
|
|
add_status("Keyboard", load_module('keyboard').setup(hass))
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2013-10-13 10:42:22 -07:00
|
|
|
# Init HTTP interface
|
2014-04-29 00:30:31 -07:00
|
|
|
if has_opt("http", "api_password"):
|
|
|
|
http = load_module('http')
|
2014-01-23 17:46:29 -08:00
|
|
|
|
2014-04-29 00:30:31 -07:00
|
|
|
http.setup(hass, get_opt("http", "api_password"))
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-04-29 00:30:31 -07:00
|
|
|
add_status("HTTP", True)
|
2013-10-21 22:06:22 -07:00
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
# Init groups
|
2014-01-23 17:46:29 -08:00
|
|
|
if has_section("group"):
|
|
|
|
group = load_module('group')
|
|
|
|
|
|
|
|
for name, entity_ids in config.items("group"):
|
2014-01-04 17:55:05 -08:00
|
|
|
add_status("Group - {}".format(name),
|
2014-04-24 00:40:45 -07:00
|
|
|
group.setup(hass, name, entity_ids.split(",")))
|
2014-01-04 17:55:05 -08:00
|
|
|
|
|
|
|
# Light trigger
|
2014-01-23 17:46:29 -08:00
|
|
|
if light_control and sun:
|
|
|
|
device_sun_light_trigger = load_module('device_sun_light_trigger')
|
|
|
|
|
2014-01-04 17:55:05 -08:00
|
|
|
light_group = get_opt_safe("device_sun_light_trigger", "light_group")
|
2014-03-26 00:08:50 -07:00
|
|
|
light_profile = get_opt_safe("device_sun_light_trigger",
|
|
|
|
"light_profile")
|
2014-01-04 17:55:05 -08:00
|
|
|
|
2014-01-23 17:46:29 -08:00
|
|
|
add_status("Device Sun Light Trigger",
|
2014-04-24 00:40:45 -07:00
|
|
|
device_sun_light_trigger.setup(hass,
|
2014-03-26 00:08:50 -07:00
|
|
|
light_group, light_profile))
|
2014-01-04 17:55:05 -08:00
|
|
|
|
2013-10-21 22:06:22 -07:00
|
|
|
for component, success_init in statusses:
|
|
|
|
status = "initialized" if success_init else "Failed to initialize"
|
|
|
|
|
|
|
|
logger.info("{}: {}".format(component, status))
|
2013-10-13 10:42:22 -07:00
|
|
|
|
2014-04-24 00:40:45 -07:00
|
|
|
return hass
|