A Python script to support the Linux w1_therm kernel driver. It discovers
multiple 1-Wire buses, 1wire temperature sensors, triggers simultaneous conversions on supported buses, and returns temperatures in Fahrenheit by default.
The package reads the driver's sysfs interface directly and has no runtime dependencies.
- Linux with the kernel
w1_thermdriver loaded. - Read/write access to
/sys/bus/w1/devices(or an alternate sysfs root). - Python 3.7 or later.
W1Bus recognizes the common w1_therm temperature families: 28-, 10-,
22-, and 3b-.
python -m pip install w1-therm-apifrom w1_therm_api import W1Bus
bus = W1Bus()
# Send Convert T to each master that exposes therm_bulk_read, then wait for
# the driver's cached results to become available.
if bus.convert():
for sensor_id, temperature_f in bus.read_temperatures():
if temperature_f is None:
print(f"{sensor_id}: unavailable")
else:
print(f"{sensor_id}: {temperature_f:.2f} F")To receive Celsius values, select it when creating the bus:
bus = W1Bus(unit="C")Sensors are discovered when W1Bus is created. To ask each master to search
for new devices and refresh that list:
bus.rescan()
print(bus.sensors)For systems without therm_bulk_read, individual reads still work:
for sensor_id, temperature_f in bus.read_temperatures():
print(sensor_id, temperature_f)When available, convert() writes trigger to each master's
therm_bulk_read entry. It then polls until the driver reports that the
conversion result is ready before reading every sensor's temperature entry.
This lets devices on a bus convert in parallel, including parasite-powered
devices when the driver and adapter support a strong pullup.
read_temperature() first reads the cached temperature value. If that file
is missing, empty, invalid, or reports the DS18x20 power-on value of 85 C, it
falls back to w1_slave and validates its CRC status. Failed reads and
power-on values are returned as (sensor_id, None); valid values are returned
as (sensor_id, temperature_f).
The underlying sysfs entries and their behavior are documented in the Linux
kernel's w1_therm driver ABI. This package does not configure resolution,
EEPROM, alarm, power, or driver feature settings.
W1Bus(
devices_dir="/sys/bus/w1/devices",
known_families=("28-", "10-", "22-", "3b-"),
unit="F",
)find_masters()returns discoveredw1_bus_master*directories.find_sensors()returns recognized sensor directories.rescan(timeout=2.0)requests a bus search and refreshessensors.start_conversion()triggers bulk conversion and returns whether any master was triggered.wait_for_conversion(timeout=2.0)waits for all triggered bulk conversions.convert(timeout=2.0)performs both steps.read_temperature(sensor_path)returns(sensor_id, temperature_f | None).read_temperatures()reads every currently discovered sensor.unitaccepts"F"(default) or"C"; unit names are case-insensitive.
MIT.