def __init__(self, device=None, manufacturer=None, model=None, device_manager=None): """ Instantiates a Scale object. If no arguments are passed, it wraps itself around whatever device calling `device_manager.find` returns and automatically fills in its `manufacturer` and `model` properties according to what `device_manager.get_manufacturer` and `device_manager.get_model` return, respectively. If a `device` argument is passed, the Scale object wraps it and treats it like a PyUSB device. If a `device` argument is passed and `manufacturer` and/or `model` are omitted, the Scale object will query the `device_manager` object to discover and populate the Scale's `manufacturer` and `model` properties. If `manufacturer`, and/or `model` are passed and `device` is omitted, Scale will wrap the device returned when those arguments are passed to `device_manager.find`. If `manufacturer`, and/or `model`, and `device` are all passed, then the Scale object wraps `device` and sets its own `manufacturer`, `model`, and `name` properties accordingly, regardless of `device`'s *actual* manufacturer and model. If a `device_manager` argument is passed, it uses that object instead of a ScaleManager instance to find the attached scale. """ if not device_manager: device_manager = ScaleManager() device = device_manager.find(manufacturer=manufacturer, model=model) if not manufacturer and device: manufacturer = device_manager.get_manufacturer(device) if not model and device: model = device_manager.get_model(device) self._device = device self._model = model self._manufacturer = manufacturer self._manager = device_manager self._endpoint = None self._last_reading = None # Initialize the USB connection to the scale. if self.device: self.connect()
class TestScaleManagerFind(unittest.TestCase): def setUp(self): self.manager = ScaleManager( lookup=mocks.usb_ids.USB_IDS, usb_lib=mocks.usb_lib.MockUSBLib() ) def test_find_empty(self): """Make sure it finds a scale by default.""" device = self.manager.find() self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_vendor(self): """Make sure it can find a manufacturer by name.""" device = self.manager.find(manufacturer="Fake Vendor") self.assertEqual(device.idVendor, mocks.usb_ids.FAKE_VDR) def test_find_both_scale(self): """Make sure it can find a scale when both arguments are supplied.""" device = self.manager.find( manufacturer="Faux Manufacturer", model="Faux Scale" ) self.assertEqual(device.idVendor, mocks.usb_ids.FAUX_MFR) self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_both_other(self): """Make sure it find anything when both arguments are supplied.""" device = self.manager.find( manufacturer="Fake Vendor", model="Fake Device of Some Other Stripe" ) self.assertEqual(device.idVendor, mocks.usb_ids.FAKE_VDR) self.assertEqual(device.idProduct, mocks.usb_ids.OTHER) def test_find_scale(self): """Make sure it can find a scale when only the model is supplied.""" device = self.manager.find(model="Fake Scale") self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_neither(self): """Make sure it finds nothing when nothing matches exactly.""" device = self.manager.find( manufacturer="Fake Vendor", model="DNE" ) self.assertEqual(device, None)
class TestScaleManagerFind(unittest.TestCase): def setUp(self): self.manager = ScaleManager(lookup=mocks.usb_ids.USB_IDS, usb_lib=mocks.usb_lib.MockUSBLib()) def test_find_empty(self): """Make sure it finds a scale by default.""" device = self.manager.find() self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_vendor(self): """Make sure it can find a manufacturer by name.""" device = self.manager.find(manufacturer="Fake Vendor") self.assertEqual(device.idVendor, mocks.usb_ids.FAKE_VDR) def test_find_both_scale(self): """Make sure it can find a scale when both arguments are supplied.""" device = self.manager.find(manufacturer="Faux Manufacturer", model="Faux Scale") self.assertEqual(device.idVendor, mocks.usb_ids.FAUX_MFR) self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_both_other(self): """Make sure it find anything when both arguments are supplied.""" device = self.manager.find(manufacturer="Fake Vendor", model="Fake Device of Some Other Stripe") self.assertEqual(device.idVendor, mocks.usb_ids.FAKE_VDR) self.assertEqual(device.idProduct, mocks.usb_ids.OTHER) def test_find_scale(self): """Make sure it can find a scale when only the model is supplied.""" device = self.manager.find(model="Fake Scale") self.assertEqual(device.idProduct, mocks.usb_ids.SCALE) def test_find_neither(self): """Make sure it finds nothing when nothing matches exactly.""" device = self.manager.find(manufacturer="Fake Vendor", model="DNE") self.assertEqual(device, None)
def setUp(self): self.manager = ScaleManager(lookup=mocks.usb_ids.USB_IDS, usb_lib=mocks.usb_lib.MockUSBLib()) self.endpoint = mocks.usb_lib.MockEndpoint(0, 0)
def setUp(self): self.manager = ScaleManager(lookup=mocks.usb_ids.USB_IDS, usb_lib=mocks.usb_lib.MockUSBLib())