コード例 #1
0
    def test_restore_all_active_subscriptions_on_reconnect(self):
        """Test active subscriptions are restored correctly on reconnect."""
        self.opp.data["mqtt"]._mqttc.subscribe.side_effect = (
            (0, 1),
            (0, 2),
            (0, 3),
            (0, 4),
        )

        unsub = mqtt.subscribe(self.opp, "test/state", None, qos=2)
        mqtt.subscribe(self.opp, "test/state", None)
        mqtt.subscribe(self.opp, "test/state", None, qos=1)
        self.opp.block_till_done()

        expected = [
            mock.call("test/state", 2),
            mock.call("test/state", 0),
            mock.call("test/state", 1),
        ]
        assert self.opp.data["mqtt"]._mqttc.subscribe.mock_calls == expected

        unsub()
        self.opp.block_till_done()
        assert self.opp.data["mqtt"]._mqttc.unsubscribe.call_count == 0

        self.opp.data["mqtt"]._mqtt_on_disconnect(None, None, 0)
        self.opp.data["mqtt"]._mqtt_on_connect(None, None, None, 0)
        self.opp.block_till_done()

        expected.append(mock.call("test/state", 1))
        assert self.opp.data["mqtt"]._mqttc.subscribe.mock_calls == expected
コード例 #2
0
    def test_subscribe_topic_level_wildcard_and_wildcard_no_match(self):
        """Test the subscription of wildcard topics."""
        mqtt.subscribe(self.opp, "+/test-topic/#", self.record_calls)

        fire_mqtt_message(self.opp, "hi/another-test-topic", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 0
コード例 #3
0
    def test_subscribe_topic_level_wildcard_no_subtree_match(self):
        """Test the subscription of wildcard topics."""
        mqtt.subscribe(self.opp, "test-topic/+/on", self.record_calls)

        fire_mqtt_message(self.opp, "test-topic/bier", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 0
コード例 #4
0
    def test_subscribe_topic_not_match(self):
        """Test if subscribed topic is not a match."""
        mqtt.subscribe(self.opp, "test-topic", self.record_calls)

        fire_mqtt_message(self.opp, "another-test-topic", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 0
コード例 #5
0
    def test_restore_subscriptions_on_reconnect(self):
        """Test subscriptions are restored on reconnect."""
        mqtt.subscribe(self.opp, "test/state", None)
        self.opp.block_till_done()
        assert self.opp.data["mqtt"]._mqttc.subscribe.call_count == 1

        self.opp.data["mqtt"]._mqtt_on_disconnect(None, None, 0)
        self.opp.data["mqtt"]._mqtt_on_connect(None, None, None, 0)
        self.opp.block_till_done()
        assert self.opp.data["mqtt"]._mqttc.subscribe.call_count == 2
コード例 #6
0
    def test_not_calling_unsubscribe_with_active_subscribers(self):
        """Test not calling unsubscribe() when other subscribers are active."""
        unsub = mqtt.subscribe(self.opp, "test/state", None)
        mqtt.subscribe(self.opp, "test/state", None)
        self.opp.block_till_done()
        assert self.opp.data["mqtt"]._mqttc.subscribe.called

        unsub()
        self.opp.block_till_done()
        assert not self.opp.data["mqtt"]._mqttc.unsubscribe.called
コード例 #7
0
    def test_subscribe_topic_sys_root_and_wildcard_topic(self):
        """Test the subscription of $ root and wildcard topics."""
        mqtt.subscribe(self.opp, "$test-topic/#", self.record_calls)

        fire_mqtt_message(self.opp, "$test-topic/some-topic", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == "$test-topic/some-topic"
        assert self.calls[0][0].payload == "test-payload"
コード例 #8
0
    def test_subscribe_topic_sys_root(self):
        """Test the subscription of $ root topics."""
        mqtt.subscribe(self.opp, "$test-topic/subtree/on", self.record_calls)

        fire_mqtt_message(self.opp, "$test-topic/subtree/on", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == "$test-topic/subtree/on"
        assert self.calls[0][0].payload == "test-payload"
コード例 #9
0
    def test_subscribe_topic_level_wildcard_and_wildcard_subtree_topic(self):
        """Test the subscription of wildcard topics."""
        mqtt.subscribe(self.opp, "+/test-topic/#", self.record_calls)

        fire_mqtt_message(self.opp, "hi/test-topic/here-iam", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == "hi/test-topic/here-iam"
        assert self.calls[0][0].payload == "test-payload"
コード例 #10
0
    def test_subscribe_topic_level_wildcard(self):
        """Test the subscription of wildcard topics."""
        mqtt.subscribe(self.opp, "test-topic/+/on", self.record_calls)

        fire_mqtt_message(self.opp, "test-topic/bier/on", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == "test-topic/bier/on"
        assert self.calls[0][0].payload == "test-payload"
コード例 #11
0
    def test_subscribe_special_characters(self):
        """Test the subscription to topics with special characters."""
        topic = "/test-topic/$(.)[^]{-}"
        payload = "p4y.l[]a|> ?"

        mqtt.subscribe(self.opp, topic, self.record_calls)

        fire_mqtt_message(self.opp, topic, payload)
        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == topic
        assert self.calls[0][0].payload == payload
コード例 #12
0
    def test_all_subscriptions_run_when_decode_fails(self):
        """Test all other subscriptions still run when decode fails for one."""
        mqtt.subscribe(self.opp,
                       "test-topic",
                       self.record_calls,
                       encoding="ascii")
        mqtt.subscribe(self.opp, "test-topic", self.record_calls)

        fire_mqtt_message(self.opp, "test-topic", "°C")

        self.opp.block_till_done()
        assert len(self.calls) == 1
コード例 #13
0
    def test_receiving_non_utf8_message_gets_logged(self):
        """Test receiving a non utf8 encoded message."""
        mqtt.subscribe(self.opp, "test-topic", self.record_calls)

        with self.assertLogs(level="WARNING") as test_handle:
            fire_mqtt_message(self.opp, "test-topic", b"\x9a")

            self.opp.block_till_done()
            assert (
                "WARNING:openpeerpower.components.mqtt:Can't decode payload "
                "b'\\x9a' on test-topic with encoding utf-8"
                in test_handle.output[0])
コード例 #14
0
    def test_retained_message_on_subscribe_received(self):
        """Test every subscriber receives retained message on subscribe."""
        def side_effect(*args):
            async_fire_mqtt_message(self.opp, "test/state", "online")
            return 0, 0

        self.opp.data["mqtt"]._mqttc.subscribe.side_effect = side_effect

        calls_a = mock.MagicMock()
        mqtt.subscribe(self.opp, "test/state", calls_a)
        self.opp.block_till_done()
        assert calls_a.called

        calls_b = mock.MagicMock()
        mqtt.subscribe(self.opp, "test/state", calls_b)
        self.opp.block_till_done()
        assert calls_b.called
コード例 #15
0
    def test_subscribe_topic(self):
        """Test the subscription of a topic."""
        unsub = mqtt.subscribe(self.opp, "test-topic", self.record_calls)

        fire_mqtt_message(self.opp, "test-topic", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1
        assert self.calls[0][0].topic == "test-topic"
        assert self.calls[0][0].payload == "test-payload"

        unsub()

        fire_mqtt_message(self.opp, "test-topic", "test-payload")

        self.opp.block_till_done()
        assert len(self.calls) == 1