How does OctoPrint handle choosing the Printer port, and can I re-use this?

OctoPrint lists possible ports for the printer, allows the user to choose one, and remembers this moving forwards,

I'd like to do the same in a plugin, this time to detect ports for sensors, allow the user to select/map sensors, and remember this moving forwards.

Before attempting to re-invent the wheel, it would be helpful to understand how OctoPrint does this, and if there's anything I can safely reuse.

This will be in the printer/comm layer stuff. If you look here is what it's doing for connection logic.

But the real guts of the autodetection is here.

Don't look to closely at this, these guts will be gutted (ba-dum-tss) for 1.4.1.

In general, I don't think you can really reuse anything there... The only thing that comes to mind is listing serial ports, but that's probably it, and that might find you more than you want due to additional ports defined by the user:

Just sticking to globing the files should be the easiest approach really.

LOL, that's funny.

@foosel, I'm not sure if you are too far along in this rewrite to take suggestions, but would it be possible to have an option to specify the serial port using the "/dev/serial/by-id/ID of Device" path?

The reason I ask this is that I'm running OP on a PC with Hassio, which has a Zwave stick and a Zigbee stick in there. Both those things are accessed by the by-id path in Hass. That makes it so that when the computer reboots, it doesn't matter what ttyACM# gets assigned to each device(which appears to be random). It would be about 10 seconds of extra setup when you first setup OctoPrint, but it will by 100% accurate from then on unless you change printers.

That's actually why I asked this question, I assumed there would be some sort of smart cross-platform way to remember the correct device between restarts and with other devices added, even if the device is physically moved to a different port - this becomes really important when you have a number of sensors plugged in

Out of scope for the "burn it with fire" fixing of the port auto detection that's scheduled for 1.4.1, but definitely something I can still take into consideration for the general comm layer rewrite (the new connection scheme in there is flexible enough for something like this I think).

Looking into this at the moment, it would be super helpful if some of this functionality could be extended for use by plugins.

For my use case, I'd like to:

  1. Let the user see all available COM ports in my plugin settings, with extra info to identify what's plugged in, so that they can select one or more for the plugin to connect to
  2. Store the selected device/port, and use it for collecting sensor data
  3. Check that the device is still present after restart, and safely handle when it isn't
  4. Prevent the user from accidentally selecting the Printer COM port
  5. I'm sure I'll think of more edge cases that should be handled...

Looking at get_connection_options(), it looks like the fundamentals I'd need for this are already in place, and I could override them if needed (for example, if I'm dealing with a sensor that only handles a particular baudrate subset, I could just override that) and leave the rest). I won't touch any of this for now, as you said it's going to burn, but for future consideration this would sure be helpful.

For now, I'm just going to use PySerial list_ports to spit out a list, let the user pick one, and hope it's still there later. It feels like building proper handling of serial devices for one plugin is a bit overkill, there must be other plugins that could make good use of this as common functionality.

What do you think? Could be an official "SensorManager" plugin installed as a dependency by other plugins that use sensors?