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_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
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_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
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)