1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Some code cleanup

This commit is contained in:
Paulus Schoutsen
2014-01-04 13:48:17 -08:00
parent 328b9a84c0
commit 367433acb2
6 changed files with 117 additions and 114 deletions

View File

@@ -12,11 +12,13 @@ from homeassistant.components import (general, chromecast,
browser, httpinterface)
# pylint: disable=too-many-branches
# 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. """
logger = logging.getLogger(__name__)
statusses = []
# Read config
@@ -27,104 +29,113 @@ def from_config_file(config_path):
bus = ha.Bus()
statemachine = ha.StateMachine(bus)
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
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'):
dev_scan = None
device_scanner = device.TomatoDeviceScanner(
config.get('tomato', 'host'),
config.get('tomato', 'username'),
config.get('tomato', 'password'),
config.get('tomato', 'http_id'))
try:
# For the error message if not all option fields exist
opt_fields = "host, username, password"
statusses.append(("Device Scanner - Tomato",
device_scanner.success_init))
if has_section('tomato'):
dev_scan_name = "Tomato"
opt_fields += ", http_id"
elif config.has_option('netgear', 'host') and \
config.has_option('netgear', 'username') and \
config.has_option('netgear', 'password'):
dev_scan = device.TomatoDeviceScanner(
get_opt('tomato', 'host'),
get_opt('tomato', 'username'),
get_opt('tomato', 'password'),
get_opt('tomato', 'http_id'))
device_scanner = device.NetgearDeviceScanner(
config.get('netgear', 'host'),
config.get('netgear', 'username'),
config.get('netgear', 'password'))
elif has_section('netgear'):
dev_scan_name = "Netgear"
statusses.append(("Device Scanner - Netgear",
device_scanner.success_init))
dev_scan = device.NetgearDeviceScanner(
get_opt('netgear', 'host'),
get_opt('netgear', 'username'),
get_opt('netgear', 'password'))
else:
device_scanner = None
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)))
if device_scanner and not device_scanner.success_init:
device_scanner = None
add_status("Device Scanner - {}".format(dev_scan_name), False)
if dev_scan:
add_status("Device Scanner - {}".format(dev_scan_name),
dev_scan.success_init)
if not dev_scan.success_init:
dev_scan = None
# Device Tracker
if device_scanner:
device.DeviceTracker(bus, statemachine, device_scanner)
if dev_scan:
device.DeviceTracker(bus, statemachine, dev_scan)
statusses.append(("Device Tracker", True))
add_status("Device Tracker", True)
# Sun tracker
if config.has_option("common", "latitude") and \
config.has_option("common", "longitude"):
if has_opt("common", "latitude") and \
has_opt("common", "longitude"):
statusses.append(("Weather - Ephem",
sun.setup(
bus, statemachine,
config.get("common", "latitude"),
config.get("common", "longitude"))))
add_status("Weather - Ephem",
sun.setup(
bus, statemachine,
get_opt("common", "latitude"),
get_opt("common", "longitude")))
# Chromecast
if config.has_option("chromecast", "host"):
if has_opt("chromecast", "host"):
chromecast_started = chromecast.setup(bus, statemachine,
config.get("chromecast", "host"))
get_opt("chromecast", "host"))
statusses.append(("Chromecast", chromecast_started))
add_status("Chromecast", chromecast_started)
else:
chromecast_started = False
# Light control
if config.has_section("hue"):
if config.has_option("hue", "host"):
light_control = light.HueLightControl(config.get("hue", "host"))
if has_section("hue"):
if has_opt("hue", "host"):
light_control = light.HueLightControl(get_opt("hue", "host"))
else:
light_control = light.HueLightControl()
statusses.append(("Light Control - Hue", light_control.success_init))
add_status("Light Control - Hue", light_control.success_init)
light.setup(bus, statemachine, light_control)
else:
light_control = None
# Light trigger
if light_control:
light.setup(bus, statemachine, light_control)
add_status("Light Trigger",
device_sun_light_trigger.setup(bus, statemachine))
statusses.append(("Light Trigger", device_sun_light_trigger.setup(
bus, statemachine)))
if config.has_option("downloader", "download_dir"):
statusses.append(("Downloader", downloader.setup(
bus, config.get("downloader", "download_dir"))))
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:
statusses.append(("General", general.setup(bus, statemachine)))
add_status("General", general.setup(bus, statemachine))
statusses.append(("Browser", browser.setup(bus)))
add_status("Browser", browser.setup(bus))
statusses.append(("Media Buttons", keyboard.setup(bus)))
add_status("Media Buttons", keyboard.setup(bus))
# Init HTTP interface
if config.has_option("httpinterface", "api_password"):
if has_opt("httpinterface", "api_password"):
httpinterface.HTTPInterface(
bus, statemachine,
config.get("httpinterface", "api_password"))
get_opt("httpinterface", "api_password"))
statusses.append(("HTTPInterface", True))
logger = logging.getLogger(__name__)
add_status("HTTPInterface", True)
for component, success_init in statusses:
status = "initialized" if success_init else "Failed to initialize"