Added shutdown event
This commit is contained in:
parent
483546a31d
commit
ead057cb04
3 changed files with 21 additions and 5 deletions
|
@ -15,7 +15,8 @@ from datetime import datetime
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
ALL_EVENTS = '*'
|
||||
EVENT_START = "start"
|
||||
EVENT_HOMEASSISTANT_START = "homeassistant.start"
|
||||
EVENT_HOMEASSISTANT_STOP = "homeassistant.stop"
|
||||
EVENT_STATE_CHANGED = "state_changed"
|
||||
EVENT_TIME_CHANGED = "time_changed"
|
||||
|
||||
|
@ -31,14 +32,27 @@ DATE_STR_FORMAT = "%H:%M:%S %d-%m-%Y"
|
|||
|
||||
def start_home_assistant(eventbus):
|
||||
""" Start home assistant. """
|
||||
request_shutdown = threading.Event()
|
||||
|
||||
def forge_shutdown_listener(request_shutdown):
|
||||
""" Creates a listener for shutdowns.
|
||||
Local variables cannot be referenced but parameters can. """
|
||||
return lambda event: request_shutdown.set()
|
||||
|
||||
eventbus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||
forge_shutdown_listener(request_shutdown))
|
||||
|
||||
Timer(eventbus)
|
||||
|
||||
eventbus.fire(EVENT_START)
|
||||
eventbus.fire(EVENT_HOMEASSISTANT_START)
|
||||
|
||||
while True:
|
||||
try:
|
||||
time.sleep(1)
|
||||
|
||||
if request_shutdown.isSet():
|
||||
break
|
||||
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
|
||||
|
@ -284,7 +298,8 @@ class Timer(threading.Thread):
|
|||
self.daemon = True
|
||||
self.eventbus = eventbus
|
||||
|
||||
eventbus.listen_once(EVENT_START, lambda event: self.start())
|
||||
eventbus.listen_once(EVENT_HOMEASSISTANT_START,
|
||||
lambda event: self.start())
|
||||
|
||||
def run(self):
|
||||
""" Start the timer. """
|
||||
|
|
|
@ -126,7 +126,8 @@ class HTTPInterface(threading.Thread):
|
|||
self.server.statemachine = statemachine
|
||||
self.server.api_password = api_password
|
||||
|
||||
eventbus.listen_once(ha.EVENT_START, lambda event: self.start())
|
||||
eventbus.listen_once(ha.EVENT_HOMEASSISTANT_START,
|
||||
lambda event: self.start())
|
||||
|
||||
def run(self):
|
||||
""" Start the HTTP interface. """
|
||||
|
|
|
@ -43,7 +43,7 @@ def ensure_homeassistant_started():
|
|||
hah.HTTPInterface(core['eventbus'], core['statemachine'],
|
||||
API_PASSWORD)
|
||||
|
||||
core['eventbus'].fire(ha.EVENT_START)
|
||||
core['eventbus'].fire(ha.EVENT_HOMEASSISTANT_START)
|
||||
|
||||
# Give objects time to startup
|
||||
time.sleep(1)
|
||||
|
|
Loading…
Add table
Reference in a new issue