Metadata-Version: 2.4
Name: opalum-receiver
Version: 0.1.1
Summary: Python library for controlling Opalum speakers/amplifiers over their serial (RS232) protocol
Author: Martin Munk Hansen
License-Expression: MIT
Project-URL: Homepage, https://github.com/martinmunk/opalum-receiver
Project-URL: Source, https://github.com/martinmunk/opalum-receiver
Project-URL: Repository, https://github.com/martinmunk/opalum-receiver.git
Project-URL: Issues, https://github.com/martinmunk/opalum-receiver/issues
Keywords: opalum,rs232,serial,amplifier,speaker,home-automation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Home Automation
Classifier: Topic :: System :: Hardware :: Hardware Drivers
Classifier: Typing :: Typed
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pyserial>=3.4
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: ruff>=0.1; extra == "dev"
Dynamic: license-file

# opalum-receiver

A small Python library for controlling **Opalum** speakers / amplifiers over their
serial (RS232) protocol. It is modelled on libraries such as
[`nad_receiver`](https://pypi.org/project/nad-receiver/) and `denonavr`: a single
importable class constructed with a serial port path, with methods for power,
volume, mute, source selection and the auto-standby timeout.

## Installation

```bash
pip install opalum-receiver          # from a package index
pip install -e ".[dev]"              # from a checkout, with test/lint tooling
```

Runtime dependency: [`pyserial`](https://pypi.org/project/pyserial/). On Debian /
Ubuntu it is also available as the apt package `python3-serial`.

## Usage

```python
from opalum_receiver import OpalumReceiver

avr = OpalumReceiver("/dev/ttyUSB0")   # opens the port and powers the device on
try:
    avr.set_source("Bluetooth")        # by name (case-insensitive), or index 0-5
    avr.set_volume(30)                 # 0-100 percent
    avr.volume_up()
    avr.mute()                         # sends vol00
    avr.unmute()                       # restores the pre-mute volume
    avr.set_standby_timeout(False)
finally:
    avr.close()

# Or as a context manager:
with OpalumReceiver("/dev/ttyUSB0") as avr:
    avr.power_off()
```

Every command method returns `True` when the device acknowledged the command and
`False` otherwise.

### Public API

| Method | Wire command | Notes |
| --- | --- | --- |
| `set_power(on)` / `power_on()` / `power_off()` | `pwr1` / `pwr0` | |
| `set_volume(level)` | `volXX` | `level` is 0-100 percent; scaled to the device's 0x00-0x63 range |
| `volume_up()` / `volume_down()` | `vol+` / `vol-` | |
| `mute()` | `vol00` | the protocol has no dedicated mute |
| `unmute()` | `volXX` | restores the remembered pre-mute volume |
| `set_mute(muted)` | | convenience for `mute()` / `unmute()` |
| `set_source(source)` / `set_input(source)` | `srcN` | name (case-insensitive), numeric string, or `int` 0-5 |
| `set_standby_timeout(enabled)` | `sta1` / `sta0` | |
| `get_power()` / `get_volume()` / `get_mute()` / `get_source()` / `get_standby_timeout()` | - | cached last-set value, or `None` if unknown (see below) |
| `available_sources()` | - | `["Toslink1", "Toslink2", "Coaxial", "Bluetooth", "Analog", "Mixer"]` |
| `close()` | - | closes the serial port |

Sources map to protocol indices as: `Toslink1=0`, `Toslink2=1`, `Coaxial=2`,
`Bluetooth=3`, `Analog=4`, `Mixer=5`.

## Protocol notes

- **Serial line:** 115200 baud, 8 data bits, no parity, 1 stop bit, no flow
  control.
- **Framing:** every command is terminated with a carriage return (`\r`). The
  device replies with `OK`, `ER`, or an echo of the command. A command is treated
  as successful when the reply contains `OK` **or** echoes the command, and does
  **not** contain `ER`.
- **Volume encoding:** the 0-100 percentage is scaled to the device's native
  `0x00`-`0x63` (0-99, 100 steps) range with integer truncation
  (`level * 0x63 // 100`) and sent as two lowercase hex digits, e.g. `vol31`.
- **No query commands.** The Opalum protocol only confirms writes; it cannot be
  asked for its current state. The `get_*` methods therefore return the last value
  set through this object, or `None` if it has not been set since construction.
- **Mute** is emulated: there is no mute command, so `mute()` sends `vol00` and
  `unmute()` re-sends the remembered volume.

### Injecting a transport (testing)

The serial transport is separated from the command mapping and can be injected,
which is how the offline test suite runs without hardware:

```python
from opalum_receiver import OpalumReceiver
from opalum_receiver.transport import BaseTransport

class FakeTransport(BaseTransport):
    def __init__(self):
        self.requests = []
    def command(self, request):      # return the raw device reply
        self.requests.append(request)
        return "OK"
    def close(self):
        pass

avr = OpalumReceiver(transport=FakeTransport(), power_on=False)
```

## Development

```bash
pip install -e ".[dev]"
PYTHONPATH=src python3 -m pytest -q
ruff check src tests
```

CI (Forgejo Actions) installs the test dependencies hermetically from apt
(`python3 python3-pytest python3-serial`) and runs the same pytest command.

## Releasing

Releases are cut as `release/x.y.z` tags; the published version is derived from
the tag. See [PUBLISHING.md](PUBLISHING.md) for the private Forgejo registry and
public PyPI (Trusted Publishing) flows.

## Credit

The wire protocol (serial settings, command framing, volume/source encoding and
value ranges) was ported faithfully from the C++ `opalum_serial` backend of the
`alsa-avr-control` project.

## License

MIT - Martin Munk Hansen. See [LICENSE](LICENSE).
