timofurrer / w1thermsensor

A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
MIT License
493 stars 113 forks source link

How to unload devices not present anymore in the bus #109

Open 11ange opened 3 years ago

11ange commented 3 years ago

Hello ! How I can unload ou remove devices that are no present anymore in the bus? Everytime I connect a sensor on the bus a new folder is created (at /sys/bus/w1/devices/w1_bus_master1 with the HW-ID of the new sensor), but folders for defective sensors or sensors not present are still there.

Here is part of my code:

sensores = W1ThermSensor.get_available_sensors()
if len(sensores) == 2:
    try:
        sensor1 = W1ThermSensor(Sensor.DS18B20, sensores[0].id)
        dicio_ds["ds0"] = format(sensor1.get_temperature(), "^4.1f")
    except SensorNotReadyError:
        dicio_ds["ds0"] = -126
    try:
        sensor2 = W1ThermSensor(Sensor.DS18B20, sensores[1].id)
        dicio_ds["ds1"] = format(sensor2.get_temperature(), "^4.1f")
    except SensorNotReadyError:
        dicio_ds["ds1"] = -126
else:
    dicio_ds["ds0"] = -126
    dicio_ds["ds1"] = -126

If one of the sensors are defective and I remove it and put a new one, the len(sensores)=3 and it is not desirable, as I have only 2 sensors operational. Is this possible or should I improve my code? Thank you in advance!

bsimmo commented 2 years ago

Can you trigger a refresh sensors ? It could be built into an faulty sensor check too. The ghost/removed sensors would report a faulty code or missing measurement. So trigger a check (button/command/timed and have them removed from your dicio_ds ?