original_docstring: Connect to the IoT broker.
docstring: Connect to the IoT broker.
docstring_tokens: Connect to the IoT broker .
code: def connect(self):
          """Connect to the IoT broker."""
          if self.state != STATE_DISCONNECTED:
              raise RuntimeError('Connect called while not disconnected')
  
          hass = self.cloud.hass
          self.close_requested = False
          self.state = STATE_CONNECTING
          self.tries = 0
  
          @asyncio.coroutine
          def _handle_hass_stop(event):
              """Handle Home Assistant shutting down."""
              nonlocal remove_hass_stop_listener
              remove_hass_stop_listener = None
              yield from self.disconnect()
  
          remove_hass_stop_listener = hass.bus.async_listen_once(
              EVENT_HOMEASSISTANT_STOP, _handle_hass_stop)
  
          while True:
              try:
                  yield from self._handle_connection()
              except Exception:  # pylint: disable=broad-except
                  # Safety net. This should never hit.
                  # Still adding it here to make sure we can always reconnect
                  _LOGGER.exception("Unexpected error")
  
              if self.close_requested:
                  break
  
              self.state = STATE_CONNECTING
              self.tries += 1
  
              try:
                  # Sleep 2^tries seconds between retries
                  self.retry_task = hass.async_add_job(asyncio.sleep(
                      2**min(9, self.tries), loop=hass.loop))
                  yield from self.retry_task
                  self.retry_task = None
              except asyncio.CancelledError:
                  # Happens if disconnect called
                  break
  
          self.state = STATE_DISCONNECTED
          if remove_hass_stop_listener is not None:
              remove_hass_stop_listener()
code_tokens: def connect ( self ) : if self . state != STATE_DISCONNECTED : raise RuntimeError ( 'Connect called while not disconnected' ) hass = self . cloud . hass self . close_requested = False self . state = STATE_CONNECTING self . tries = 0 @ asyncio . coroutine def _handle_hass_stop ( event ) : """Handle Home Assistant shutting down.""" nonlocal remove_hass_stop_listener remove_hass_stop_listener = None yield from self . disconnect ( ) remove_hass_stop_listener = hass . bus . async_listen_once ( EVENT_HOMEASSISTANT_STOP , _handle_hass_stop ) while True : try : yield from self . _handle_connection ( ) except Exception : _LOGGER . exception ( "Unexpected error" ) if self . close_requested : break self . state = STATE_CONNECTING self . tries += 1 try : self . retry_task = hass . async_add_job ( asyncio . sleep ( 2 ** min ( 9 , self . tries ) , loop = hass . loop ) ) yield from self . retry_task self . retry_task = None except asyncio . CancelledError : break self . state = STATE_DISCONNECTED if remove_hass_stop_listener is not None : remove_hass_stop_listener ( )
short_docstring: Connect to the IoT broker.
short_docstring_tokens: Connect to the IoT broker .
comment: """Connect to the IoT broker."""$SEP$"""Handle Home Assistant shutting down."""$SEP$# pylint: disable=broad-except$SEP$# Safety net. This should never hit.$SEP$# Still adding it here to make sure we can always reconnect$SEP$# Sleep 2^tries seconds between retries$SEP$# Happens if disconnect called