mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Added shutdown event
This commit is contained in:
parent
483546a31d
commit
ead057cb04
@ -15,7 +15,8 @@ from datetime import datetime
|
|||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
ALL_EVENTS = '*'
|
ALL_EVENTS = '*'
|
||||||
EVENT_START = "start"
|
EVENT_HOMEASSISTANT_START = "homeassistant.start"
|
||||||
|
EVENT_HOMEASSISTANT_STOP = "homeassistant.stop"
|
||||||
EVENT_STATE_CHANGED = "state_changed"
|
EVENT_STATE_CHANGED = "state_changed"
|
||||||
EVENT_TIME_CHANGED = "time_changed"
|
EVENT_TIME_CHANGED = "time_changed"
|
||||||
|
|
||||||
@ -31,14 +32,27 @@ DATE_STR_FORMAT = "%H:%M:%S %d-%m-%Y"
|
|||||||
|
|
||||||
def start_home_assistant(eventbus):
|
def start_home_assistant(eventbus):
|
||||||
""" Start home assistant. """
|
""" 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)
|
Timer(eventbus)
|
||||||
|
|
||||||
eventbus.fire(EVENT_START)
|
eventbus.fire(EVENT_HOMEASSISTANT_START)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
if request_shutdown.isSet():
|
||||||
|
break
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -284,7 +298,8 @@ class Timer(threading.Thread):
|
|||||||
self.daemon = True
|
self.daemon = True
|
||||||
self.eventbus = eventbus
|
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):
|
def run(self):
|
||||||
""" Start the timer. """
|
""" Start the timer. """
|
||||||
|
@ -126,7 +126,8 @@ class HTTPInterface(threading.Thread):
|
|||||||
self.server.statemachine = statemachine
|
self.server.statemachine = statemachine
|
||||||
self.server.api_password = api_password
|
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):
|
def run(self):
|
||||||
""" Start the HTTP interface. """
|
""" Start the HTTP interface. """
|
||||||
|
@ -43,7 +43,7 @@ def ensure_homeassistant_started():
|
|||||||
hah.HTTPInterface(core['eventbus'], core['statemachine'],
|
hah.HTTPInterface(core['eventbus'], core['statemachine'],
|
||||||
API_PASSWORD)
|
API_PASSWORD)
|
||||||
|
|
||||||
core['eventbus'].fire(ha.EVENT_START)
|
core['eventbus'].fire(ha.EVENT_HOMEASSISTANT_START)
|
||||||
|
|
||||||
# Give objects time to startup
|
# Give objects time to startup
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user