Ejemplo n.º 1
0
    def test_state_changed_event_sends_message_and_timestamp(
            self, mock_utcnow, mock_pub):
        """Test the sending of a message and timestamps if event changed."""
        e_id = "another.entity"
        base_topic = "pub"

        # Add the statestream component for publishing state updates
        assert self.add_statestream(base_topic=base_topic,
                                    publish_attributes=None,
                                    publish_timestamps=True)
        self.hass.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_statestream state change on initialization, etc.
        mock_pub.reset_mock()

        # Set a state of an entity
        mock_state_change_event(self.hass, State(e_id, "on"))
        self.hass.block_till_done()

        # Make sure 'on' was published to pub/fake/entity/state
        calls = [
            call.async_publish(self.hass, "pub/another/entity/state", "on", 1,
                               True),
            call.async_publish(self.hass, "pub/another/entity/last_changed",
                               ANY, 1, True),
            call.async_publish(self.hass, "pub/another/entity/last_updated",
                               ANY, 1, True),
        ]

        mock_pub.assert_has_calls(calls, any_order=True)
        assert mock_pub.called
Ejemplo n.º 2
0
    def test_state_changed_attr_sends_message(self, mock_utcnow, mock_pub):
        """"Test the sending of a new message if attribute changed."""
        e_id = 'fake.entity'
        base_topic = 'pub'

        # Add the statestream component for publishing state updates
        assert self.add_statestream(base_topic=base_topic,
                                    publish_attributes=True)
        self.hass.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_statestream state change on initialization, etc.
        mock_pub.reset_mock()

        test_attributes = {"testing": "YES"}

        # Set a state of an entity
        mock_state_change_event(self.hass, State(e_id, 'off',
                                                 attributes=test_attributes))
        self.hass.block_till_done()

        # Make sure 'on' was published to pub/fake/entity/state
        calls = [
            call.async_publish(self.hass, 'pub/fake/entity/state', 'off', 1,
                               True),
            call.async_publish(self.hass, 'pub/fake/entity/testing', 'YES',
                               1, True)
        ]

        mock_pub.assert_has_calls(calls, any_order=True)
        assert mock_pub.called
    def test_state_changed_event_sends_message_and_timestamp(
            self,
            mock_utcnow,
            mock_pub):
        """"Test the sending of a message and timestamps if event changed."""
        e_id = 'another.entity'
        base_topic = 'pub'

        # Add the statestream component for publishing state updates
        assert self.add_statestream(base_topic=base_topic,
                                    publish_attributes=None,
                                    publish_timestamps=True)
        self.hass.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_statestream state change on initialization, etc.
        mock_pub.reset_mock()

        # Set a state of an entity
        mock_state_change_event(self.hass, State(e_id, 'on'))
        self.hass.block_till_done()

        # Make sure 'on' was published to pub/fake/entity/state
        calls = [
            call.async_publish(self.hass, 'pub/another/entity/state', 'on', 1,
                               True),
            call.async_publish(self.hass, 'pub/another/entity/last_changed',
                               ANY, 1, True),
            call.async_publish(self.hass, 'pub/another/entity/last_updated',
                               ANY, 1, True),
        ]

        mock_pub.assert_has_calls(calls, any_order=True)
        assert mock_pub.called
    def test_state_changed_attr_sends_message(self, mock_utcnow, mock_pub):
        """"Test the sending of a new message if attribute changed."""
        e_id = 'fake.entity'
        base_topic = 'pub'

        # Add the statestream component for publishing state updates
        assert self.add_statestream(base_topic=base_topic,
                                    publish_attributes=True)
        self.hass.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_statestream state change on initialization, etc.
        mock_pub.reset_mock()

        test_attributes = {"testing": "YES"}

        # Set a state of an entity
        mock_state_change_event(self.hass,
                                State(e_id, 'off', attributes=test_attributes))
        self.hass.block_till_done()

        # Make sure 'on' was published to pub/fake/entity/state
        calls = [
            call.async_publish(self.hass, 'pub/fake/entity/state', 'off', 1,
                               True),
            call.async_publish(self.hass, 'pub/fake/entity/testing', 'YES', 1,
                               True)
        ]

        mock_pub.assert_has_calls(calls, any_order=True)
        assert mock_pub.called
Ejemplo n.º 5
0
    def test_state_changed_attr_sends_message(self, mock_utcnow, mock_pub):
        """Test the sending of a new message if attribute changed."""
        e_id = "fake.entity"
        base_topic = "pub"

        # Add the statestream component for publishing state updates
        assert self.add_statestream(base_topic=base_topic,
                                    publish_attributes=True)
        self.hass.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_statestream state change on initialization, etc.
        mock_pub.reset_mock()

        test_attributes = {
            "testing": "YES",
            "list": ["a", "b", "c"],
            "bool": False
        }

        # Set a state of an entity
        mock_state_change_event(self.hass,
                                State(e_id, "off", attributes=test_attributes))
        self.hass.block_till_done()

        # Make sure 'on' was published to pub/fake/entity/state
        calls = [
            call.async_publish(self.hass, "pub/fake/entity/state", "off", 1,
                               True),
            call.async_publish(self.hass, "pub/fake/entity/testing", '"YES"',
                               1, True),
            call.async_publish(self.hass, "pub/fake/entity/list",
                               '["a", "b", "c"]', 1, True),
            call.async_publish(self.hass, "pub/fake/entity/bool", "false", 1,
                               True),
        ]

        mock_pub.assert_has_calls(calls, any_order=True)
        assert mock_pub.called