Skip to main content

Pythonic bindings for the Handsets Android control CLI

Project description

handsets — Python bindings

A small, Pythonic wrapper around the Handsets CLI (hs). Drives Android devices from Python without reimplementing the subprocess + JSON-parse + exit-code boilerplate every caller used to write by hand.

Install

pip install handsets

You also need the hs binary on $PATH. See the project install instructions.

Usage

from handsets import Session

with Session() as d:                   # `hs use` on enter, `hs drop` on exit
    for node in d.ui():
        print(node.cls, node.text, node.coords)

    d.tap("Continue")                  # text lookup
    d.tap(540, 860)                    # raw coords
    d.fill("EditText", "you@x.com")    # atomic ACTION_SET_TEXT against the selector
    d.type("hello")                    # keystrokes to the focused field
    d.submit()
    d.wait(text="Welcome", timeout="15s")

Errors map to typed exceptions:

from handsets import Session, NotFound, Timeout, Ambiguous

try:
    d.tap("Submit", unique=True, timeout="5s")
except NotFound:
    ...  # exit code 2 — selector matched nothing
except Timeout:
    ...  # exit code 3 — wait budget exhausted
except Ambiguous:
    ...  # exit code 4 — --unique saw multiple matches

Everything else (daemon errors, bad arguments, secure-window blocks) raises a generic HandsetsError whose .code attribute carries the structured ErrCode enum value from the CLI's JSON output.

Batching

For tight loops, Session.batch() opens a warm-socket context that keeps one hs run - subprocess alive across calls. Per-call process startup (~5–10 ms each) collapses into one startup for the whole batch:

with Session() as d:
    with d.batch(timeout="5s", retries=2) as b:
        for label in labels_to_press:
            b.tap(label, visible=True)
        b.wait(text="Done")

batch() covers the action verbs (tap, type, fill, submit, paste, wait, go, swipe). Query verbs (find, ui) still spawn per-call — they emit multiple JSON lines which would desync the batch read loop.

Talking to a specific device

Session(serial="PIXEL6_SERIAL")

Multiple sessions can run side-by-side; each one shells out independently.

Why a thin wrapper?

The CLI already does the hard work: warm daemon, push-mirrored state, millisecond round-trips. The Python layer's job is to make that ergonomic — context managers, typed exceptions, no manual subprocess.run. Future versions may keep an hs run subprocess warm and stream commands over its stdin to amortise per-call process overhead.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

handsets-0.1.15.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

handsets-0.1.15-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file handsets-0.1.15.tar.gz.

File metadata

  • Download URL: handsets-0.1.15.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for handsets-0.1.15.tar.gz
Algorithm Hash digest
SHA256 8367355da5c0a188f25257d202f0b4cef53db4f73d99724f705cfc6c02390031
MD5 f46ef88874d2a2dbd40e9d5052c011dd
BLAKE2b-256 491341c98199dfd296a5bc8c3d4be059a082b34cc912116a2f5ea9b982d6eb03

See more details on using hashes here.

File details

Details for the file handsets-0.1.15-py3-none-any.whl.

File metadata

  • Download URL: handsets-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for handsets-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 b386e53e0e69ec43912a5f484112c6abf7d808f10b6c94296e0a06b6e9c2bc29
MD5 f9f634eea3b3f31c9aa38fbc02a1e9cd
BLAKE2b-256 9c39f9788d8e3a6091e09a0726828f96cf8aa6e466229b1cd0f7d1555754a3a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page