From 371b4c77081710f6b884c90ff7be5ac8348a4c60 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 23 Sep 2013 00:50:29 -0700 Subject: [PATCH] Starting and stopping of modules now via start and shutdown events --- homeassistant/HomeAssistant.py | 17 +++++++---------- homeassistant/HttpInterface.py | 5 ++++- homeassistant/common.py | 3 +++ homeassistant/observer/Timer.py | 8 +++----- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/homeassistant/HomeAssistant.py b/homeassistant/HomeAssistant.py index 987ebb7edd5..0792cbafa40 100644 --- a/homeassistant/HomeAssistant.py +++ b/homeassistant/HomeAssistant.py @@ -2,8 +2,10 @@ import logging from ConfigParser import SafeConfigParser import time +from homeassistant.common import EVENT_START, EVENT_SHUTDOWN + from homeassistant.StateMachine import StateMachine -from homeassistant.EventBus import EventBus +from homeassistant.EventBus import EventBus, Event from homeassistant.HttpInterface import HttpInterface from homeassistant.observer.DeviceTracker import DeviceTracker @@ -100,10 +102,9 @@ class HomeAssistant(object): def start(self): - self.setup_timer().start() - - if self.httpinterface is not None: - self.httpinterface.start() + self.setup_timer() + + self.get_event_bus().fire(Event(EVENT_START)) while True: try: @@ -111,10 +112,6 @@ class HomeAssistant(object): except KeyboardInterrupt: print "" - print "Interrupt received. Wrapping up and quiting.." - self.timer.stop() - - if self.httpinterface is not None: - self.httpinterface.stop() + self.eventbus.fire(Event(EVENT_SHUTDOWN)) break diff --git a/homeassistant/HttpInterface.py b/homeassistant/HttpInterface.py index b97c117f962..23fc601a15e 100644 --- a/homeassistant/HttpInterface.py +++ b/homeassistant/HttpInterface.py @@ -1,10 +1,11 @@ import threading import urlparse import logging +from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import requests -from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from homeassistant.common import EVENT_START, EVENT_SHUTDOWN SERVER_HOST = '127.0.0.1' SERVER_PORT = 8080 @@ -22,6 +23,8 @@ class HttpInterface(threading.Thread): self._stop = threading.Event() + eventbus.listen(EVENT_START, lambda event: self.start()) + eventbus.listen(EVENT_SHUTDOWN, lambda event: self.stop()) def run(self): """ Start the HTTP interface. """ diff --git a/homeassistant/common.py b/homeassistant/common.py index de5ccf46f4c..a77c0ca2d91 100644 --- a/homeassistant/common.py +++ b/homeassistant/common.py @@ -1,3 +1,6 @@ +EVENT_START = "start" +EVENT_SHUTDOWN = "shutdown" + def ensure_list(parameter): return parameter if isinstance(parameter, list) else [parameter] diff --git a/homeassistant/observer/Timer.py b/homeassistant/observer/Timer.py index 3aa5c3c9f69..7e32aecd2a8 100644 --- a/homeassistant/observer/Timer.py +++ b/homeassistant/observer/Timer.py @@ -3,6 +3,7 @@ from datetime import datetime import threading import time +from homeassistant.common import EVENT_START, EVENT_SHUTDOWN from homeassistant.EventBus import Event from homeassistant.util import ensure_list, matcher @@ -24,11 +25,8 @@ class Timer(threading.Thread): self.eventbus = eventbus self._stop = threading.Event() - - def stop(self): - """ Tell the timer to stop. """ - self._stop.set() - + eventbus.listen(EVENT_START, lambda event: self.start()) + eventbus.listen(EVENT_SHUTDOWN, lambda event: self._stop.set()) def run(self): """ Start the timer. """