Starting and stopping of modules now via start and shutdown events

pull/2/head
Paulus Schoutsen 2013-09-23 00:50:29 -07:00
parent 6fbadc24e4
commit 371b4c7708
4 changed files with 17 additions and 16 deletions

View File

@ -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

View File

@ -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. """

View File

@ -1,3 +1,6 @@
EVENT_START = "start"
EVENT_SHUTDOWN = "shutdown"
def ensure_list(parameter):
return parameter if isinstance(parameter, list) else [parameter]

View File

@ -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. """