Making the config bootstrap more verbose about what started

This commit is contained in:
Paulus Schoutsen 2013-10-21 22:06:22 -07:00
parent 0d156ecbf0
commit 05472481c5
3 changed files with 76 additions and 16 deletions

View file

@ -193,12 +193,17 @@ class HueLightControl(object):
except ImportError: except ImportError:
logging.getLogger(__name__).error(("HueLightControl:" logging.getLogger(__name__).error(("HueLightControl:"
"Unable to init due to missing dependency phue.")) "Unable to init due to missing dependency phue."))
self.success_init = False
return return
self.bridge = phue.Bridge(host) self.bridge = phue.Bridge(host)
self.lights = self.bridge.get_light_objects() self.lights = self.bridge.get_light_objects()
self.light_ids = [light.light_id for light in self.lights] self.light_ids = [light.light_id for light in self.lights]
self.success_init = True
def is_light_on(self, light_id=None): def is_light_on(self, light_id=None):
""" Returns if specified or all light are on. """ """ Returns if specified or all light are on. """
@ -249,7 +254,7 @@ def setup_file_downloader(eventbus, download_path):
"Download path {} does not exist. File Downloader not active."). "Download path {} does not exist. File Downloader not active.").
format(download_path)) format(download_path))
return return False
def download_file(event): def download_file(event):
""" Downloads file specified in the url. """ """ Downloads file specified in the url. """
@ -303,6 +308,8 @@ def setup_file_downloader(eventbus, download_path):
eventbus.listen(EVENT_DOWNLOAD_FILE, download_file) eventbus.listen(EVENT_DOWNLOAD_FILE, download_file)
return True
def setup_webbrowser(eventbus): def setup_webbrowser(eventbus):
""" Listen for browse_url events and open """ Listen for browse_url events and open
the url in the default webbrowser. """ the url in the default webbrowser. """
@ -312,6 +319,8 @@ def setup_webbrowser(eventbus):
eventbus.listen(EVENT_BROWSE_URL, eventbus.listen(EVENT_BROWSE_URL,
lambda event: webbrowser.open(event.data['url'])) lambda event: webbrowser.open(event.data['url']))
return True
def setup_chromecast(eventbus, host): def setup_chromecast(eventbus, host):
""" Listen for chromecast events. """ """ Listen for chromecast events. """
from homeassistant.packages import pychromecast from homeassistant.packages import pychromecast
@ -325,6 +334,8 @@ def setup_chromecast(eventbus, host):
eventbus.listen(EVENT_CHROMECAST_YOUTUBE_VIDEO, eventbus.listen(EVENT_CHROMECAST_YOUTUBE_VIDEO,
lambda event: pychromecast.play_youtube_video(host, event.data['video'])) lambda event: pychromecast.play_youtube_video(host, event.data['video']))
return True
def setup_media_buttons(eventbus): def setup_media_buttons(eventbus):
""" Listen for keyboard events. """ """ Listen for keyboard events. """
try: try:
@ -332,7 +343,8 @@ def setup_media_buttons(eventbus):
except ImportError: except ImportError:
logging.getLogger(__name__).error(("MediaButtons:" logging.getLogger(__name__).error(("MediaButtons:"
"Unable to setup due to missing dependency PyUserInput.")) "Unable to setup due to missing dependency PyUserInput."))
return
return False
keyboard = pykeyboard.PyKeyboard() keyboard = pykeyboard.PyKeyboard()
keyboard.special_key_assignment() keyboard.special_key_assignment()
@ -348,3 +360,5 @@ def setup_media_buttons(eventbus):
eventbus.listen(EVENT_KEYBOARD_MEDIA_PLAY_PAUSE, eventbus.listen(EVENT_KEYBOARD_MEDIA_PLAY_PAUSE,
lambda event: keyboard.tap_key(keyboard.media_play_pause_key)) lambda event: keyboard.tap_key(keyboard.media_play_pause_key))
return True

View file

@ -3,16 +3,20 @@ Provides methods to bootstrap a home assistant instance.
""" """
import ConfigParser import ConfigParser
import logging
import homeassistant as ha import homeassistant as ha
import homeassistant.observers as observers import homeassistant.observers as observers
import homeassistant.actors as actors import homeassistant.actors as actors
import homeassistant.httpinterface as httpinterface import homeassistant.httpinterface as httpinterface
# pylint: disable=too-many-branches
def from_config_file(config_path): def from_config_file(config_path):
""" Starts home assistant with all possible functionality """ Starts home assistant with all possible functionality
based on a config file. """ based on a config file. """
statusses = []
# Read config # Read config
config = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
config.read(config_path) config.read(config_path)
@ -34,6 +38,14 @@ def from_config_file(config_path):
config.get('tomato','password'), config.get('tomato','password'),
config.get('tomato','http_id')) config.get('tomato','http_id'))
if device_scanner.success_init:
statusses.append(("Device Scanner - Tomato", True))
else:
statusses.append(("Device Scanner - Tomato", False))
device_scanner = None
else: else:
device_scanner = None device_scanner = None
@ -42,6 +54,9 @@ def from_config_file(config_path):
if device_scanner: if device_scanner:
device_tracker = observers.DeviceTracker(eventbus, statemachine, device_tracker = observers.DeviceTracker(eventbus, statemachine,
device_scanner) device_scanner)
statusses.append(("Device Tracker", True))
else: else:
device_tracker = None device_tracker = None
@ -50,19 +65,21 @@ def from_config_file(config_path):
if config.has_option("common", "latitude") and \ if config.has_option("common", "latitude") and \
config.has_option("common", "longitude"): config.has_option("common", "longitude"):
statusses.append(("Weather - Ephem",
observers.track_sun(eventbus, statemachine, observers.track_sun(eventbus, statemachine,
config.get("common","latitude"), config.get("common","latitude"),
config.get("common","longitude")) config.get("common","longitude"))))
# Init actors # Init actors
# Light control # Light control
if config.has_section("hue"): if config.has_section("hue") and config.has_option("hue", "host"):
if config.has_option("hue", "host"): light_control = actors.HueLightControl(config.get("hue", "host"))
hue_host = config.get("hue", "host")
else:
hue_host = None
light_control = actors.HueLightControl(hue_host) statusses.append(("Light Control - Hue", light_control.success_init))
else:
light_control = None
# Light trigger # Light trigger
@ -70,22 +87,38 @@ def from_config_file(config_path):
actors.LightTrigger(eventbus, statemachine, actors.LightTrigger(eventbus, statemachine,
device_tracker, light_control) device_tracker, light_control)
statusses.append(("Light Trigger", True))
if config.has_option("chromecast", "host"): if config.has_option("chromecast", "host"):
actors.setup_chromecast(eventbus, config.get("chromecast", "host")) statusses.append(("Chromecast", actors.setup_chromecast(eventbus,
config.get("chromecast", "host"))))
if config.has_option("downloader", "download_dir"): if config.has_option("downloader", "download_dir"):
actors.setup_file_downloader(eventbus, result = actors.setup_file_downloader(eventbus,
config.get("downloader", "download_dir")) config.get("downloader", "download_dir"))
actors.setup_webbrowser(eventbus) statusses.append(("Downloader", result))
actors.setup_media_buttons(eventbus)
statusses.append(("Webbrowser", actors.setup_webbrowser(eventbus)))
statusses.append(("Media Buttons", actors.setup_media_buttons(eventbus)))
# Init HTTP interface # Init HTTP interface
if config.has_option("httpinterface", "api_password"): if config.has_option("httpinterface", "api_password"):
httpinterface.HTTPInterface(eventbus, statemachine, httpinterface.HTTPInterface(eventbus, statemachine,
config.get("httpinterface","api_password")) config.get("httpinterface","api_password"))
statusses.append(("HTTPInterface", True))
logger = logging.getLogger(__name__)
for component, success_init in statusses:
status = "initialized" if success_init else "Failed to initialize"
logger.info("{}: {}".format(component, status))
ha.start_home_assistant(eventbus) ha.start_home_assistant(eventbus)

View file

@ -52,7 +52,7 @@ def track_sun(eventbus, statemachine, latitude, longitude):
except ImportError: except ImportError:
logger.error(("TrackSun:" logger.error(("TrackSun:"
"Unable to setup due to missing dependency ephem.")) "Unable to setup due to missing dependency ephem."))
return return False
sun = ephem.Sun() # pylint: disable=no-member sun = ephem.Sun() # pylint: disable=no-member
@ -89,6 +89,7 @@ def track_sun(eventbus, statemachine, latitude, longitude):
update_sun_state(None) update_sun_state(None)
return True
class DeviceTracker(object): class DeviceTracker(object):
""" Class that tracks which devices are home and which are not. """ """ Class that tracks which devices are home and which are not. """
@ -278,6 +279,8 @@ class TomatoDeviceScanner(object):
self.date_updated = None self.date_updated = None
self.last_results = {"wldev": [], "dhcpd_lease": []} self.last_results = {"wldev": [], "dhcpd_lease": []}
self.success_init = self._update_tomato_info()
def scan_devices(self): def scan_devices(self):
""" Scans for new devices and return a """ Scans for new devices and return a
list containing found device ids. """ list containing found device ids. """
@ -331,22 +334,30 @@ class TomatoDeviceScanner(object):
self.date_updated = datetime.now() self.date_updated = datetime.now()
return True
elif response.status_code == 401: elif response.status_code == 401:
# Authentication error # Authentication error
self.logger.exception(("Tomato:Failed to authenticate, " self.logger.exception(("Tomato:Failed to authenticate, "
"please check your username and password")) "please check your username and password"))
return False
except requests.ConnectionError: except requests.ConnectionError:
# We get this if we could not connect to the router or # We get this if we could not connect to the router or
# an invalid http_id was supplied # an invalid http_id was supplied
self.logger.exception(("Tomato:Failed to connect to the router" self.logger.exception(("Tomato:Failed to connect to the router"
"or invalid http_id supplied")) "or invalid http_id supplied"))
return False
except ValueError: except ValueError:
# If json decoder could not parse the response # If json decoder could not parse the response
self.logger.exception(("Tomato:Failed to parse response " self.logger.exception(("Tomato:Failed to parse response "
"from router")) "from router"))
return False
finally: finally:
self.lock.release() self.lock.release()
@ -354,3 +365,5 @@ class TomatoDeviceScanner(object):
# We acquired the lock before the IF check, # We acquired the lock before the IF check,
# release it before we return True # release it before we return True
self.lock.release() self.lock.release()
return True