Пример #1
0
 def test_split_entity_string(self):
     """Test splitting of entity string."""
     service.call_from_config(
         self.opp,
         {
             "service": "test_domain.test_service",
             "entity_id": "hello.world, sensor.beer",
         },
     )
     self.opp.block_till_done()
     assert ["hello.world",
             "sensor.beer"] == self.calls[-1].data.get("entity_id")
Пример #2
0
    def test_service_template_service_call(self):
        """Test legacy service_template call with templating."""
        config = {
            "service_template": "{{ 'test_domain.test_service' }}",
            "entity_id": "hello.world",
            "data": {
                "hello": "goodbye"
            },
        }

        service.call_from_config(self.opp, config)
        self.opp.block_till_done()

        assert self.calls[0].data["hello"] == "goodbye"
Пример #3
0
    def test_not_mutate_input(self):
        """Test for immutable input."""
        config = cv.SERVICE_SCHEMA({
            "service": "test_domain.test_service",
            "entity_id": "hello.world, sensor.beer",
            "data": {
                "hello": 1
            },
            "data_template": {
                "nested": {
                    "value": "{{ 1 + 1 }}"
                }
            },
        })
        orig = deepcopy(config)

        # Only change after call is each template getting opp.attached
        template.attach(self.opp, orig)

        service.call_from_config(self.opp, config, validate_config=False)
        assert orig == config
Пример #4
0
    def test_bad_template(self):
        """Test passing bad template."""
        config = {
            "service_template": "{{ var_service }}",
            "entity_id": "hello.world",
            "data_template": {
                "hello": "{{ states + unknown_var }}"
            },
        }

        service.call_from_config(
            self.opp,
            config,
            variables={
                "var_service": "test_domain.test_service",
                "var_data": "goodbye",
            },
        )
        self.opp.block_till_done()

        assert len(self.calls) == 0
Пример #5
0
    def test_passing_variables_to_templates(self):
        """Test passing variables to templates."""
        config = {
            "service_template": "{{ var_service }}",
            "entity_id": "hello.world",
            "data_template": {
                "hello": "{{ var_data }}"
            },
        }

        service.call_from_config(
            self.opp,
            config,
            variables={
                "var_service": "test_domain.test_service",
                "var_data": "goodbye",
            },
        )
        self.opp.block_till_done()

        assert self.calls[0].data["hello"] == "goodbye"
Пример #6
0
    def test_fail_silently_if_no_service(self, mock_log):
        """Test failing if service is missing."""
        service.call_from_config(self.opp, None)
        assert mock_log.call_count == 1

        service.call_from_config(self.opp, {})
        assert mock_log.call_count == 2

        service.call_from_config(self.opp, {"service": "invalid"})
        assert mock_log.call_count == 3
Пример #7
0
    def test_service_call(self):
        """Test service call with templating."""
        config = {
            "service": "{{ 'test_domain.test_service' }}",
            "entity_id": "hello.world",
            "data": {
                "hello": "{{ 'goodbye' }}",
                "effect": {
                    "value": "{{ 'complex' }}",
                    "simple": "simple"
                },
            },
            "data_template": {
                "list": ["{{ 'list' }}", "2"]
            },
            "target": {
                "area_id": "test-area-id",
                "entity_id": "will.be_overridden"
            },
        }

        service.call_from_config(self.opp, config)
        self.opp.block_till_done()

        assert dict(self.calls[0].data) == {
            "hello": "goodbye",
            "effect": {
                "value": "complex",
                "simple": "simple",
            },
            "list": ["list", "2"],
            "entity_id": ["hello.world"],
            "area_id": ["test-area-id"],
        }

        config = {
            "service": "{{ 'test_domain.test_service' }}",
            "target": {
                "area_id": ["area-42", "{{ 'area-51' }}"],
                "device_id": ["abcdef", "{{ 'fedcba' }}"],
                "entity_id": ["light.static", "{{ 'light.dynamic' }}"],
            },
        }

        service.call_from_config(self.opp, config)
        self.opp.block_till_done()

        assert dict(self.calls[1].data) == {
            "area_id": ["area-42", "area-51"],
            "device_id": ["abcdef", "fedcba"],
            "entity_id": ["light.static", "light.dynamic"],
        }

        config = {
            "service": "{{ 'test_domain.test_service' }}",
            "target": "{{ var_target }}",
        }

        service.call_from_config(
            self.opp,
            config,
            variables={
                "var_target": {
                    "entity_id": "light.static",
                    "area_id": ["area-42", "area-51"],
                },
            },
        )

        service.call_from_config(self.opp, config)
        self.opp.block_till_done()

        assert dict(self.calls[2].data) == {
            "area_id": ["area-42", "area-51"],
            "entity_id": ["light.static"],
        }