original_docstring:  Provide a streaming interface for the event bus. 
docstring: Provide a streaming interface for the event bus.
docstring_tokens: Provide a streaming interface for the event bus .
code: def _handle_get_api_stream(handler, path_match, data):
      """ Provide a streaming interface for the event bus. """
      gracefully_closed = False
      hass = handler.server.hass
      wfile = handler.wfile
      write_lock = threading.Lock()
      block = threading.Event()
  
      restrict = data.get('restrict')
      if restrict:
          restrict = restrict.split(',')
  
      def write_message(payload):
          """ Writes a message to the output. """
          with write_lock:
              msg = "data: {}\n\n".format(payload)
  
              try:
                  wfile.write(msg.encode("UTF-8"))
                  wfile.flush()
              except IOError:
                  block.set()
  
      def forward_events(event):
          """ Forwards events to the open request. """
          nonlocal gracefully_closed
  
          if block.is_set() or event.event_type == EVENT_TIME_CHANGED or \
             restrict and event.event_type not in restrict:
              return
          elif event.event_type == EVENT_HOMEASSISTANT_STOP:
              gracefully_closed = True
              block.set()
              return
  
          write_message(json.dumps(event, cls=rem.JSONEncoder))
  
      handler.send_response(HTTP_OK)
      handler.send_header('Content-type', 'text/event-stream')
      handler.end_headers()
  
      hass.bus.listen(MATCH_ALL, forward_events)
  
      while True:
          write_message(STREAM_PING_PAYLOAD)
  
          block.wait(STREAM_PING_INTERVAL)
  
          if block.is_set():
              break
  
      if not gracefully_closed:
          _LOGGER.info("Found broken event stream to %s, cleaning up",
                       handler.client_address[0])
  
      hass.bus.remove_listener(MATCH_ALL, forward_events)
code_tokens: def _handle_get_api_stream ( handler , path_match , data ) : gracefully_closed = False hass = handler . server . hass wfile = handler . wfile write_lock = threading . Lock ( ) block = threading . Event ( ) restrict = data . get ( 'restrict' ) if restrict : restrict = restrict . split ( ',' ) def write_message ( payload ) : """ Writes a message to the output. """ with write_lock : msg = "data: {}\n\n" . format ( payload ) try : wfile . write ( msg . encode ( "UTF-8" ) ) wfile . flush ( ) except IOError : block . set ( ) def forward_events ( event ) : """ Forwards events to the open request. """ nonlocal gracefully_closed if block . is_set ( ) or event . event_type == EVENT_TIME_CHANGED or restrict and event . event_type not in restrict : return elif event . event_type == EVENT_HOMEASSISTANT_STOP : gracefully_closed = True block . set ( ) return write_message ( json . dumps ( event , cls = rem . JSONEncoder ) ) handler . send_response ( HTTP_OK ) handler . send_header ( 'Content-type' , 'text/event-stream' ) handler . end_headers ( ) hass . bus . listen ( MATCH_ALL , forward_events ) while True : write_message ( STREAM_PING_PAYLOAD ) block . wait ( STREAM_PING_INTERVAL ) if block . is_set ( ) : break if not gracefully_closed : _LOGGER . info ( "Found broken event stream to %s, cleaning up" , handler . client_address [ 0 ] ) hass . bus . remove_listener ( MATCH_ALL , forward_events )
short_docstring: Provide a streaming interface for the event bus.
short_docstring_tokens: Provide a streaming interface for the event bus .
comment: """ Provide a streaming interface for the event bus. """$SEP$""" Writes a message to the output. """$SEP$""" Forwards events to the open request. """