def add_device(self, name, protocol, model=None, **parameters):
device = Device(self.lib.tdAddDevice(), lib=self.lib)<EOL>try:<EOL><INDENT>device.name = name<EOL>device.protocol = protocol<EOL>if model:<EOL><INDENT>device.model = model<EOL><DEDENT>for key, value in parameters.items():<EOL><INDENT>device.set_parameter(key, value)<EOL><DEDENT>return DeviceFactory(device.id, lib=self.lib)<EOL><DEDENT>except Exception:<EOL><INDENT>import sys<EOL>exc_info = sys.exc_info()<EOL>try:<EOL><INDENT>device.remove()<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>if "<STR_LIT>" in dir(Exception):<EOL><INDENT>raise exc_info[<NUM_LIT:0>].with_traceback(exc_info[<NUM_LIT:1>], exc_info[<NUM_LIT:2>])<EOL><DEDENT>else:<EOL><INDENT>exec("<STR_LIT>")<EOL><DEDENT><DEDENT>
Add a new device.

        :return: a :class:`Device` or :class:`DeviceGroup` instance.