MQTT: Managing binary payloads (#6976)
* Managing binary payloads Hello, background: I wrote a HA camera component that gets the image from a binary payload. I'm testing it with Zanzito (https://play.google.com/store/apps/details?id=it.barbaro.zanzito) and it works apparently well: it gets the image and correctly displays it in the front-end. But I had to make the changes I'm proposing here: the message was being blocked because the utf-8 decoding failed. As far as I know, the utf-8 encoding is required for the topic, not for the payload. What I did here was try the utf-8 decoding, but even if unsuccessful, it dispatches the message anyway. Is there anything else I'm missing? thanks Gianluca * Update __init__.py * Update __init__.py * Update __init__.py * git test - ignore * Should work * minor fixes * updated mqtt/services.yaml * added two tests, modified threaded subscribe * removing polymer * requested changes * requested changes - minor fix * security wrap around payload_file_path * services.yaml updated * removed file publishing * minor fix
This commit is contained in:
1 parent
9a9342ec3f
commit
fa65783f39
3 files changed
+64
-58
No files matched your search
@@ -209,6 +209,31 @@ class TestMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
def test_subscribe_binary_topic(self):
|
||||
"""Test the subscription to a binary topic."""
|
||||
mqtt.subscribe(self.hass, 'test-topic', self.record_calls,
|
||||
0, None)
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 0x9a)
|
||||
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
self.assertEqual('test-topic', self.calls[0][0])
|
||||
self.assertEqual(0x9a, self.calls[0][1])
|
||||
|
||||
def test_receiving_non_utf8_message_gets_logged(self):
|
||||
"""Test receiving a non utf8 encoded message."""
|
||||
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||
|
||||
with self.assertLogs(level='ERROR') as test_handle:
|
||||
fire_mqtt_message(self.hass, 'test-topic', 0x9a)
|
||||
self.hass.block_till_done()
|
||||
self.assertIn(
|
||||
"ERROR:homeassistant.components.mqtt:Illegal payload "
|
||||
"encoding utf-8 from MQTT "
|
||||
"topic: test-topic, Payload: 154",
|
||||
test_handle.output[0])
|
||||
|
||||
|
||||
class TestMQTTCallbacks(unittest.TestCase):
|
||||
"""Test the MQTT callbacks."""
|
||||
@@ -255,7 +280,8 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
last_event = calls[0]
|
||||
self.assertEqual('Hello World!', last_event['payload'])
|
||||
self.assertEqual(bytearray('Hello World!', 'utf-8'),
|
||||
last_event['payload'])
|
||||
self.assertEqual(message.topic, last_event['topic'])
|
||||
self.assertEqual(message.qos, last_event['qos'])
|
||||
|
||||
@@ -298,38 +324,6 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
self.assertRaises(vol.Invalid, mqtt.valid_publish_topic, 'bad+topic')
|
||||
self.assertRaises(vol.Invalid, mqtt.valid_subscribe_topic, 'bad\0one')
|
||||
|
||||
def test_receiving_non_utf8_message_gets_logged(self):
|
||||
"""Test receiving a non utf8 encoded message."""
|
||||
calls = []
|
||||
|
||||
@callback
|
||||
def record(topic, payload, qos):
|
||||
"""Helper to record calls."""
|
||||
data = {
|
||||
'topic': topic,
|
||||
'payload': payload,
|
||||
'qos': qos,
|
||||
}
|
||||
calls.append(data)
|
||||
|
||||
async_dispatcher_connect(
|
||||
self.hass, mqtt.SIGNAL_MQTT_MESSAGE_RECEIVED, record)
|
||||
|
||||
payload = 0x9a
|
||||
topic = 'test_topic'
|
||||
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
||||
message = MQTTMessage(topic, 1, payload)
|
||||
with self.assertLogs(level='ERROR') as test_handle:
|
||||
self.hass.data['mqtt']._mqtt_on_message(
|
||||
None,
|
||||
{'hass': self.hass},
|
||||
message)
|
||||
self.hass.block_till_done()
|
||||
self.assertIn(
|
||||
"ERROR:homeassistant.components.mqtt:Illegal utf-8 unicode "
|
||||
"payload from MQTT topic: %s, Payload: " % topic,
|
||||
test_handle.output[0])
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_setup_embedded_starts_with_no_config(hass):
|
||||
|
||||
Reference in new issue
Block a user