1wire for Trio: Access to 1wire

This project enables you to access one or more 1wire buses asynchronously.

Prerequisites:

Content, indices and tables

Introduction

… or “how to talk to 1wire in five minutes”[1].

[1]Setting up an owserver <http://owfs.org/uploads/owserver.html> instance not included.

First, create an OWFS instance:

import trio
from trio_owfs import OWFS

async def main():
    async with OWFS() as ow:
        await setup_onewire(ow)
        pass # do whatever you want with 1wire

trio.run(main)

Optionally, start an event monitor:

async def setup_onewire(ow):
    await ow.add_task(monitor, ow)

async def monitor(ow):
    with ow.events as events:
        async for msg in events:
            logger.info("%s", msg)

Connect to one or more owserver instances:

async def setup_onewire(ow):
    # … continued
    s = await ow.add_server("127.0.0.1")

At this point, the server is connected and its bus has been enumerated. The connection to the server is kept alive and will automatically be re-established until you leave the with OWFS() block, or call await s.aclose().

Your event monitor receives a trio_owfs.event.DeviceLocated event for each 1wire slave; in a more complicated program, it should match them with your configuration file.

You can also ask by device ID, and get/set attributes if the device is present:

dev = await ow.get_device("10.DBDB39010800.EF")
# triggers a :class:`trio_owfs.event.DeviceAdded` event if not yet known
if dev.bus is not None:
    # make sure that the data accessor methods are loaded
    await ow.ensure_struct(dev)

    heat = await dev.temperature
    await dev.set_temphigh(99)

Note that all IDs are in FDIDC for (family, dot, ID, dot, checksum).

You can arrange for periodic bus scans, or trigger them yourself:

await ow.scan_now()

Re-scanning will automatically add new buses and devices, relocate moved devices, free buses which have been disconnected, and de-locate (i.e. remove the bus attribute from) devices that can no longer be found.

Trio-OWFS transparently supports the DS2509 <http://owfs.org/uploads/DS2409.html> bus coupler, by creating (and auto-scanning) two buses for its main and aux ports. Don’t change its settings yourself; you’re likely to confuse your owserver.

Future plans

  • support simultaneous temperature or voltage conversion (no bus delay)
  • poll a device in the background
  • auto-poll each bus’s alert subdirectory
  • improve auto-discovery of device attributes by reading /structure

Non-plans

Trio-OWFS will never support

  • Cached bus access. If you want to cache a value, do it in Python.
  • Linked owservers (i.e. one server that forwards to another).

Trio-owfs Reference

Entry point

The base for accessing the 1wire system is an async context:

async with OWFS() as ow:
    pass  # do whatever
async with trio_owfs.OWFS(**kwargs)

Access to an owserver.

class trio_owfs.server.Server(service, host='localhost', port=4304)

Encapsulate one server connection.

get_bus(*path)

Return the bus at this path. Allocate new if not existing.

await drop()

Stop talking and delete yourself

await start()

Start talking. Returns when the connection is established, raises an error if not possible.

TODO: if the connection subsequently drops, it’s re-established transparently.

Buses.

class trio_owfs.bus.Bus(server, *path)

Describes one bus.

delocate()

This bus can no longer be found

await attr_get(*attr)

Read this attribute

await attr_set(*attr, value)

Write this attribute

Devices.

class trio_owfs.device.Device(service, id)

Base class for devices.

A device may or may not have a known location.

locate(bus)

The device has been seen here.

delocate(bus)

The device is no longer located here.

await attr_get(*attr)

Read this attribute

await attr_set(*attr, value)

Write this attribute

classmethod await setup_struct(server)

Read the device’s structural data from OWFS and add methods to access the fields

Events: whatever is happening on the bus

class trio_owfs.event.Event

Base class for all events

class trio_owfs.event.ServerEvent

Base class for all server-related events

class trio_owfs.event.ServerRegistered(server)

A new known server appears. The server is not yet connected!

class trio_owfs.event.ServerConnected(server)

We have connected to a server

class trio_owfs.event.ServerDisconnected(server)

We have disconnected from a server

class trio_owfs.event.ServerDeregistered(server)

This server is no longer known

class trio_owfs.event.BusEvent

Base class for all Bus-related events

class trio_owfs.event.BusAdded(bus)

The Bus has been created. Its location is not yet known!

class trio_owfs.event.BusAdded_Path(*path)

Not an event. Used for storing the bus path for comparisons in tests.

class trio_owfs.event.BusDeleted(bus)

The Bus has been deleted

class trio_owfs.event.DeviceEvent

Base class for all device-related events

class trio_owfs.event.DeviceAdded(device)

The device has been created. Its location is not yet known!

class trio_owfs.event.DeviceDeleted(device)

The device has been deleted

class trio_owfs.event.DeviceLocated(device)

The device has been found

class trio_owfs.event.DeviceNotFound(device)

The device’s location is no longer known

Release history