Metadata-Version: 2.4
Name: pylabgruppen-ipx
Version: 0.1.0rc3
Summary: Asyncio client for controlling Lab.gruppen IPX amplifiers over the network
Keywords: lab.gruppen,labgruppen,ipx,amplifier
Author: imstevenxyz
Author-email: imstevenxyz <commits@imsteven.xyz>
License-Expression: LGPL-3.0-only
License-File: LICENSE
Classifier: Framework :: AsyncIO
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Topic :: Home Automation
Classifier: Topic :: Multimedia :: Sound/Audio
Classifier: Typing :: Typed
Requires-Python: >=3.11
Project-URL: Homepage, https://gitlab.com/imstevenxyz/pylabgruppen-ipx
Project-URL: Issues, https://gitlab.com/imstevenxyz/pylabgruppen-ipx/-/work_items
Description-Content-Type: text/markdown

# pylabgruppen-ipx

Asyncio Python client for controlling Lab.gruppen IPX amplifiers over the network. It has no dependencies outside the standard library.

Currently tested on the following hardware:

- IPX 1200, Firmware 2.3.5.0 / 2.3.2.0 (20190612)

> [!NOTE]
> An amplifier accepts only one controller at a time, and only broadcasts for discovery while no controller is connected. Disconnect IPX Controller or any other client before using this library or the CLI.

## Instalation and usage

### CLI

Install the package from PyPI:

```sh
uv tool install pylabgruppen-ipx
```

Find amplifiers on the local network. Discovery runs until you press Ctrl+C (or until --timeout).

```
> pylabgruppen-ipx discover
MODEL             HOST              PORT  MAC
----------------  ---------------  -----  -----------------
IPX 1200          192.168.1.50     50168  00:11:22:33:44:55
```

Control an amplifier by its IP address or hostname:

```sh
pylabgruppen-ipx status 192.168.1.50
pylabgruppen-ipx power 192.168.1.50 on
pylabgruppen-ipx recall 192.168.1.50 2
```

| Command                        | Description                                |
| ------------------------------ | ------------------------------------------ |
| `discover [--timeout SECONDS]` | Discover amplifiers on the local network   |
| `status HOST`                  | Show state and details                     |
| `power HOST {on,off}`          | Power on or off (standby)                  |
| `mute HOST OUTPUT {on,off}`    | Mute or unmute an output (numbered from 1) |
| `presets HOST`                 | Show stored presets                        |
| `recall HOST ID`               | Recall (activate) a preset                 |

Use `--help` for all information on all commands and possible arguments.

### Library

Add the package to your project's dependencies, or install it with pip:

```sh
pip install pylabgruppen-ipx
```

#### Discovering amplifiers

```python
import asyncio

from pylabgruppen_ipx import IPXDiscovery

async def main() -> None:
    async with IPXDiscovery() as discovery:
        await asyncio.sleep(5)

    for amp in discovery.amplifiers:
        print(amp.model, amp.host, amp.port, amp.mac)

asyncio.run(main())
```

Or keep scanning in the background and get a callback for each new or changed amplifier:

```python
from pylabgruppen_ipx import DiscoveredIPXAmplifier, IPXDiscovery

def on_found(amp: DiscoveredIPXAmplifier) -> None:
    print(f"Found {amp.model} at {amp.host}")

discovery = IPXDiscovery(on_found=on_found)
await discovery.scan()
...
await discovery.stop()
```

#### Controlling an amplifier

```python
from pylabgruppen_ipx import IPXAmplifier, IPXError

try:
    async with IPXAmplifier("192.168.1.50") as amp:
        ...
except IPXError as err:
    print(f"Error: {err}")
```

The amplifier pushes meter readings and temperature on its own schedule while powered on. Pass `on_frame_received` to receive each unsolicited push:

```python
from pylabgruppen_ipx import IPXAmplifier, IPXFrame, IPXFrameMessageClass
from pylabgruppen_ipx.amp.protocol import ADDR_TEMPERATURE
from pylabgruppen_ipx.amp.utils import u32_at

def print_frame(frame: IPXFrame) -> None:
    if frame.msg_class == IPXFrameMessageClass.GET:
        if frame.payload[:4] == ADDR_TEMPERATURE:
            print(f"Temperature: {u32_at(frame.payload, 4)} °C")
            return

async def main() -> None:
    async with IPXAmplifier("192.168.1.50", on_frame_received=print_frame) as amp:
        ...
```

## Development

Setup development environment:

```sh
git clone https://gitlab.com/iot.imsteven.xyz/pylabgruppen-ipx.git
cd pylabgruppen-ipx
python3 -m venv .venv
source .venv/bin/activate
```

Run the CLI from the working tree:

```sh
PYTHONPATH=src python3 -m pylabgruppen_ipx discover
```

Code is formatted with [Ruff](https://docs.astral.sh/ruff/):

```sh
pip install ruff
ruff format src
```

Project layout:

| Path                        | Contents                                     |
| --------------------------- | -------------------------------------------- |
| `src/pylabgruppen_ipx/amp/` | Protocol, discovery and amplifier connection |
| `src/pylabgruppen_ipx/cli/` | Command line interface                       |
