From 173d14c0c153dcd59625229b94bf79a37a9ff2e5 Mon Sep 17 00:00:00 2001 From: lejeune quentin Date: Mon, 13 Dec 2021 14:37:06 +0000 Subject: [PATCH] [FIX] hw_drivers: Add printer manually to IoT Actually it is not possible to add manually a printer. With this commit we loop into all printer recorded in cups to check if a printer is not automatically added. If a printer does not match with a identifier automaticcaly generated, it is added to the list of usable printer. closes odoo/odoo#95709 X-original-commit: de03cbefd537503cd46cbecf1622bd2a547b4615 Signed-off-by: Masereel Pierre --- .../interfaces/PrinterInterface.py | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface.py b/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface.py index ca601dbce04..371773b899e 100644 --- a/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface.py +++ b/addons/hw_drivers/iot_handlers/interfaces/PrinterInterface.py @@ -20,18 +20,29 @@ class PrinterInterface(Interface): with cups_lock: printers = conn.getPrinters() devices = conn.getDevices() - for printer in printers: - path = printers.get(printer).get('device-uri', False) - if path and path in devices: - devices.get(path).update({'supported': True}) # these printers are automatically supported - for path in devices: - if 'uuid=' in path: - identifier = sub('[^a-zA-Z0-9_]', '', path.split('uuid=')[1]) - elif 'serial=' in path: - identifier = sub('[^a-zA-Z0-9_]', '', path.split('serial=')[1]) - else: - identifier = sub('[^a-zA-Z0-9_]', '', path) - devices[path]['identifier'] = identifier - devices[path]['url'] = path - printer_devices[identifier] = devices[path] + for printer_name, printer in printers.items(): + path = printer.get('device-uri', False) + if printer_name != self.get_identifier(path): + printer.update({'supported': True}) # these printers are automatically supported + device_class = 'network' + if 'usb' in printer.get('device-uri'): + device_class = 'direct' + printer.update({'device-class': device_class}) + printer.update({'device-make-and-model': printer_name}) # give name setted in Cups + printer.update({'device-id': ''}) + devices.update({printer_name: printer}) + for path, device in devices.items(): + identifier = self.get_identifier(path) + device.update({'identifier': identifier}) + device.update({'url': path}) + printer_devices.update({identifier: device}) return printer_devices + + def get_identifier(self, path): + if 'uuid=' in path: + identifier = sub('[^a-zA-Z0-9_]', '', path.split('uuid=')[1]) + elif 'serial=' in path: + identifier = sub('[^a-zA-Z0-9_]', '', path.split('serial=')[1]) + else: + identifier = sub('[^a-zA-Z0-9_]', '', path) + return identifier