Use only 1 event listener for event stream.
This commit is contained in:
parent
225a672a92
commit
c7cc045acd
2 changed files with 7 additions and 12 deletions
|
@ -89,6 +89,9 @@ class APIEventStream(HomeAssistantView):
|
|||
if event.event_type == EVENT_TIME_CHANGED:
|
||||
return
|
||||
|
||||
if restrict and event.event_type not in restrict:
|
||||
return
|
||||
|
||||
_LOGGER.debug('STREAM %s FORWARDING %s', id(stop_obj), event)
|
||||
|
||||
if event.event_type == EVENT_HOMEASSISTANT_STOP:
|
||||
|
@ -100,11 +103,7 @@ class APIEventStream(HomeAssistantView):
|
|||
|
||||
def stream():
|
||||
"""Stream events to response."""
|
||||
if restrict:
|
||||
for event_type in restrict:
|
||||
self.hass.bus.listen(event_type, thread_forward_events)
|
||||
else:
|
||||
self.hass.bus.listen(MATCH_ALL, thread_forward_events)
|
||||
self.hass.bus.listen(MATCH_ALL, thread_forward_events)
|
||||
|
||||
_LOGGER.debug('STREAM %s ATTACHED', id(stop_obj))
|
||||
|
||||
|
@ -139,11 +138,7 @@ class APIEventStream(HomeAssistantView):
|
|||
_LOGGER.debug('STREAM %s RESPONSE CLOSED', id(stop_obj))
|
||||
break
|
||||
|
||||
if restrict:
|
||||
for event in restrict:
|
||||
self.hass.bus.remove_listener(event, thread_forward_events)
|
||||
else:
|
||||
self.hass.bus.remove_listener(MATCH_ALL, thread_forward_events)
|
||||
self.hass.bus.remove_listener(MATCH_ALL, thread_forward_events)
|
||||
|
||||
return self.Response(stream(), mimetype='text/event-stream')
|
||||
|
||||
|
|
|
@ -432,7 +432,7 @@ class TestAPI(unittest.TestCase):
|
|||
# with closing(requests.get(_url(const.URL_API_STREAM), timeout=3,
|
||||
# stream=True, headers=HA_HEADERS)) as req:
|
||||
|
||||
# self.assertEqual(listen_count + 2, self._listen_count())
|
||||
# self.assertEqual(listen_count + 1, self._listen_count())
|
||||
|
||||
# hass.bus.fire('test_event')
|
||||
|
||||
|
@ -447,7 +447,7 @@ class TestAPI(unittest.TestCase):
|
|||
# const.URL_API_STREAM))
|
||||
# with closing(requests.get(url, stream=True, timeout=3,
|
||||
# headers=HA_HEADERS)) as req:
|
||||
# self.assertEqual(listen_count + 3, self._listen_count())
|
||||
# self.assertEqual(listen_count + 1, self._listen_count())
|
||||
|
||||
# hass.bus.fire('test_event1')
|
||||
# data = self._stream_next_event(req)
|
||||
|
|
Loading…
Add table
Reference in a new issue