Пример #1
0
    def test_update_with_missing_json_attrs(self):
        """Test attributes get extracted from a JSON result."""
        data = command_line.CommandSensorData(
            self.hass,
            (
                'echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
             \\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'
            ),
            15,
        )

        self.sensor = command_line.CommandSensor(
            self.hass,
            data,
            "test",
            None,
            None,
            ["key", "another_key", "key_three", "special_key"],
        )
        self.sensor.update()
        assert self.sensor.device_state_attributes["key"] == "some_json_value"
        assert (
            self.sensor.device_state_attributes["another_key"] == "another_json_value"
        )
        assert self.sensor.device_state_attributes["key_three"] == "value_three"
        assert "special_key" not in self.sensor.device_state_attributes
    def test_template_render(self):
        """Ensure command with templates get rendered properly."""
        self.hass.states.set("sensor.test_state", "Works")
        data = command_line.CommandSensorData(
            self.hass, "echo {{ states.sensor.test_state.state }}", 15)
        data.update()

        assert data.value == "Works"
 def test_update_with_json_attrs_not_dict(self, mock_logger):
     """Test attributes get extracted from a JSON result."""
     data = command_line.CommandSensorData(self.hass, "echo [1, 2, 3]", 15)
     self.sensor = command_line.CommandSensor(self.hass, data, "test", None,
                                              None, ["key"])
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
 def test_update_with_json_attrs_no_data(self, mock_logger):
     """Test attributes when no JSON result fetched."""
     data = command_line.CommandSensorData(self.hass, "echo ", 15)
     self.sensor = command_line.CommandSensor(self.hass, data, "test", None,
                                              None, ["key"])
     self.sensor.update()
     assert {} == self.sensor.device_state_attributes
     assert mock_logger.warning.called
Пример #5
0
    def test_template(self):
        """Test command sensor with template."""
        data = command_line.CommandSensorData(self.hass, "echo 50", 15)

        entity = command_line.CommandSensor(
            self.hass,
            data,
            "test",
            "in",
            Template("{{ value | multiply(0.1) }}", self.hass),
            [],
        )

        entity.update()
        assert float(entity.state) == 5
Пример #6
0
    def test_template_render_with_quote(self):
        """Ensure command with templates and quotes get rendered properly."""
        self.hass.states.set("sensor.test_state", "Works 2")
        with patch(
            "homeassistant.components.command_line.subprocess.check_output",
            return_value=b"Works\n",
        ) as check_output:
            data = command_line.CommandSensorData(
                self.hass, 'echo "{{ states.sensor.test_state.state }}" "3 4"', 15,
            )
            data.update()

        assert data.value == "Works"
        check_output.assert_called_once_with(
            'echo "Works 2" "3 4"', shell=True, timeout=15  # nosec # shell by design
        )
Пример #7
0
    def test_update_with_unnecessary_json_attrs(self):
        """Test attributes get extracted from a JSON result."""
        data = command_line.CommandSensorData(
            self.hass,
            ('echo { \\"key\\": \\"some_json_value\\", \\"another_key\\":\
             \\"another_json_value\\", \\"key_three\\": \\"value_three\\" }'),
            15,
        )

        self.sensor = command_line.CommandSensor(self.hass, data, "test", None,
                                                 None, ["key", "another_key"])
        self.sensor.update()
        assert "some_json_value" == self.sensor.device_state_attributes["key"]
        assert ("another_json_value" ==
                self.sensor.device_state_attributes["another_key"])
        assert not ("key_three" in self.sensor.device_state_attributes)
Пример #8
0
    def test_bad_command(self):
        """Test bad command."""
        data = command_line.CommandSensorData(self.hass, "asdfasdf", 15)
        data.update()

        assert data.value is None