def init_app(self, app):
        # type: (Flask) -> None
        """Init the Flask-MQTT addon."""
        self.client_id = app.config.get("MQTT_CLIENT_ID", "")

        if isinstance(self.client_id, unicode):
            self.client._client_id = self.client_id.encode('utf-8')
        else:
            self.client._client_id = self.client_id

        self.client._transport = app.config.get("MQTT_TRANSPORT", "tcp").lower()
        self.client._protocol = app.config.get("MQTT_PROTOCOL_VERSION", MQTTv311)

        self.client.on_connect = self._handle_connect
        self.client.on_disconnect = self._handle_disconnect
        self.username = app.config.get("MQTT_USERNAME")
        self.password = app.config.get("MQTT_PASSWORD")
        self.broker_url = app.config.get("MQTT_BROKER_URL", "localhost")
        self.broker_port = app.config.get("MQTT_BROKER_PORT", 1883)
        self.tls_enabled = app.config.get("MQTT_TLS_ENABLED", False)
        self.keepalive = app.config.get("MQTT_KEEPALIVE", 60)
        self.last_will_topic = app.config.get("MQTT_LAST_WILL_TOPIC")
        self.last_will_message = app.config.get("MQTT_LAST_WILL_MESSAGE")
        self.last_will_qos = app.config.get("MQTT_LAST_WILL_QOS", 0)
        self.last_will_retain = app.config.get("MQTT_LAST_WILL_RETAIN", False)

        if self.tls_enabled:
            self.tls_ca_certs = app.config["MQTT_TLS_CA_CERTS"]
            self.tls_certfile = app.config.get("MQTT_TLS_CERTFILE")
            self.tls_keyfile = app.config.get("MQTT_TLS_KEYFILE")
            self.tls_cert_reqs = app.config.get("MQTT_TLS_CERT_REQS",
                                                ssl.CERT_REQUIRED)
            self.tls_version = app.config.get("MQTT_TLS_VERSION",
                                              ssl.PROTOCOL_TLSv1)
            self.tls_ciphers = app.config.get("MQTT_TLS_CIPHERS")
            self.tls_insecure = app.config.get("MQTT_TLS_INSECURE", False)

        # set last will message
        if self.last_will_topic is not None:
            self.client.will_set(
                self.last_will_topic,
                self.last_will_message,
                self.last_will_qos,
                self.last_will_retain,
            )

        self._connect()
def init_app ( self , app ) : self . client_id = app . config . get ( "MQTT_CLIENT_ID" , "" ) if isinstance ( self . client_id , unicode ) : self . client . _client_id = self . client_id . encode ( 'utf-8' ) else : self . client . _client_id = self . client_id self . client . _transport = app . config . get ( "MQTT_TRANSPORT" , "tcp" ) . lower ( ) self . client . _protocol = app . config . get ( "MQTT_PROTOCOL_VERSION" , MQTTv311 ) self . client . on_connect = self . _handle_connect self . client . on_disconnect = self . _handle_disconnect self . username = app . config . get ( "MQTT_USERNAME" ) self . password = app . config . get ( "MQTT_PASSWORD" ) self . broker_url = app . config . get ( "MQTT_BROKER_URL" , "localhost" ) self . broker_port = app . config . get ( "MQTT_BROKER_PORT" , 1883 ) self . tls_enabled = app . config . get ( "MQTT_TLS_ENABLED" , False ) self . keepalive = app . config . get ( "MQTT_KEEPALIVE" , 60 ) self . last_will_topic = app . config . get ( "MQTT_LAST_WILL_TOPIC" ) self . last_will_message = app . config . get ( "MQTT_LAST_WILL_MESSAGE" ) self . last_will_qos = app . config . get ( "MQTT_LAST_WILL_QOS" , 0 ) self . last_will_retain = app . config . get ( "MQTT_LAST_WILL_RETAIN" , False ) if self . tls_enabled : self . tls_ca_certs = app . config [ "MQTT_TLS_CA_CERTS" ] self . tls_certfile = app . config . get ( "MQTT_TLS_CERTFILE" ) self . tls_keyfile = app . config . get ( "MQTT_TLS_KEYFILE" ) self . tls_cert_reqs = app . config . get ( "MQTT_TLS_CERT_REQS" , ssl . CERT_REQUIRED ) self . tls_version = app . config . get ( "MQTT_TLS_VERSION" , ssl . PROTOCOL_TLSv1 ) self . tls_ciphers = app . config . get ( "MQTT_TLS_CIPHERS" ) self . tls_insecure = app . config . get ( "MQTT_TLS_INSECURE" , False ) if self . last_will_topic is not None : self . client . will_set ( self . last_will_topic , self . last_will_message , self . last_will_qos , self . last_will_retain , ) self . _connect ( )
Init the Flask-MQTT addon.