Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ client.send_telemetry({'temperature': 41.9}, queued=False)
## Using Device APIs

**TBDeviceMqttClient** provides access to Device MQTT APIs of ThingsBoard platform. It allows to publish telemetry and attribute updates, subscribe to attribute changes, send and receive RPC commands, etc. Use **TBHTTPClient** for the Device HTTP API.
#### Proxy configuration
You can configure an MQTT proxy before connecting the client. The arguments are forwarded to the underlying Paho MQTT client.

```python
import socks
from tb_device_mqtt import TBDeviceMqttClient


client = TBDeviceMqttClient("127.0.0.1", username="A1_TEST_TOKEN")
client.proxy_set(proxy_type=socks.HTTP, proxy_addr="proxy.example.com", proxy_port=3128)
client.connect()
```

#### Subscription to attributes
You can subscribe to attribute updates from the server. The following example demonstrates how to subscribe to attribute updates from the server.
##### MQTT
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
long_description_content_type="text/markdown",
python_requires=">=3.9",
packages=["."],
install_requires=['tb-paho-mqtt-client>=2.1.2', 'requests>=2.31.0', 'orjson'])
install_requires=['tb-paho-mqtt-client>=2.1.2', 'requests>=2.31.0', 'orjson', 'PySocks'])
4 changes: 4 additions & 0 deletions tb_device_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ def __request_firmware_info(self):
def is_connected(self):
return self.__is_connected

def proxy_set(self, **kwargs):
"""Configure proxy settings for the underlying MQTT client."""
return self._client.proxy_set(**kwargs)

def connect(self, callback=None, min_reconnect_delay=1, timeout=120, tls=False, ca_certs=None, cert_file=None,
key_file=None, keepalive=120):
"""Connect to ThingsBoard. The callback will be called when the connection is established."""
Expand Down
46 changes: 46 additions & 0 deletions tests/test_mqtt_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2026. ThingsBoard
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from unittest.mock import Mock, patch

from tb_device_mqtt import TBDeviceMqttClient
from tb_gateway_mqtt import TBGatewayMqttClient


class MqttProxyTest(unittest.TestCase):

@patch('tb_device_mqtt.Thread.start')
def test_device_client_proxy_set_delegates_to_paho_client(self, _):
client = TBDeviceMqttClient('localhost', username='token')
client._client.proxy_set = Mock(return_value=None)

client.proxy_set(proxy_type='HTTP', proxy_addr='proxy.local', proxy_port=3128)

client._client.proxy_set.assert_called_once_with(
proxy_type='HTTP', proxy_addr='proxy.local', proxy_port=3128)

@patch('tb_device_mqtt.Thread.start')
def test_gateway_client_inherits_proxy_set(self, _):
client = TBGatewayMqttClient('localhost', username='token')
client._client.proxy_set = Mock(return_value=None)

client.proxy_set(proxy_type='HTTP', proxy_addr='proxy.local', proxy_port=3128)

client._client.proxy_set.assert_called_once_with(
proxy_type='HTTP', proxy_addr='proxy.local', proxy_port=3128)


if __name__ == '__main__':
unittest.main()