Metadata-Version: 2.5
Name: python-steronix
Version: 0.1.0
Summary: Async Python client of the Steronix integrations API: read a Steronix device and open its relays
Project-URL: Homepage, https://github.com/gate-control-dev/python-steronix
Project-URL: Issues, https://github.com/gate-control-dev/python-steronix/issues
Project-URL: Changelog, https://github.com/gate-control-dev/python-steronix/blob/main/CHANGELOG.md
Author: EVO Lab
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: asyncio,gate,home-assistant,relay,steronix
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Home Automation
Classifier: Typing :: Typed
Requires-Python: >=3.13
Requires-Dist: aiohttp>=3.10
Requires-Dist: yarl>=1.9
Description-Content-Type: text/markdown

# python-steronix

Async Python client of the Steronix integrations API (`integrations-v1`,
contract version 1.1.0). It reads one Steronix device and opens its relays
with an integration token. It is the library behind the Steronix Home
Assistant integration.

- Typed models: `TokenInfo`, `Device` (relays and inputs), `OpenResult`.
- One error class per answer of the API.
- Opens with an idempotency key per button press; never retried on their own.
- Long polling for live changes, with plain polling as the fallback.
- HTTPS only: the client refuses an `http://` base URL and never follows a
  redirect, so the token is never sent in clear.

## Install

```bash
pip install python-steronix
```

Python 3.13 or newer; the only dependency is `aiohttp`.

## The token

The device's owner creates a token on the portal, **Controls →
Integrations → Create token**. It is shown once. A token is for one device
and the relays chosen when it was made. It ends when it expires, when it is
revoked, when the device changes owner, or when its creator's account is
deleted. Treat it as an opaque string and keep it secret.

## Use

```python
import asyncio

import aiohttp

from steronix import Actor, ActorType, SteronixClient


async def main() -> None:
    async with aiohttp.ClientSession() as session:
        client = SteronixClient(session, "stx_…")

        token = await client.get_token()
        print(token.name, token.relays, token.expires_at)

        result = await client.get_device()
        device = result.device
        print(device.name, device.online, [i.active for i in device.inputs])

        opened = await client.open_relay(1, Actor(ActorType.USER, name="Ivan", login="ivan"))
        print("opened, log event", opened.open_number)


asyncio.run(main())
```

The default base URL is production, `https://evo-lab.cloud/api/integrations/v1`.
Pass `base_url=` for another server.

### Following the device

`DeviceWatcher` yields the device's state at start and after every change.
It asks `GET /device?wait=30` with the last `ETag`; the server answers as
soon as the state changes, else after 30 s with `304`, and it asks again at
once. When long polling fails it polls every 10 s and tries long polling
again after 5 minutes.

```python
from steronix import DeviceWatcher, SteronixAuthenticationError, SteronixError

watcher = DeviceWatcher(client)
try:
    async for update in watcher.updates():
        if isinstance(update, SteronixError):
            ...  # the API cannot be reached now; show the device as unavailable
        else:
            ...  # a new state of the device
except SteronixAuthenticationError:
    ...  # the token no longer works: ask for a new one
```

For one wait yourself: `await client.wait_for_change(etag, wait=30)` returns
the new state, or `None` when nothing changed.

## Errors

Every error is a `SteronixError`.

| Error | When |
|---|---|
| `SteronixConnectionError` | The API cannot be reached: network, TLS or timeout |
| `SteronixInvalidResponseError` | An answer the contract does not describe |
| `SteronixBadRequestError` | 400: the request does not match the contract |
| `SteronixAuthenticationError` | 401: the token is missing, unknown, expired or revoked, or its grant ended |
| `SteronixRelayNotAllowedError` | 403: the token may not open this relay |
| `SteronixDeviceOfflineError` | 409 `DEVICE_OFFLINE`: nothing was sent to the device |
| `SteronixRelayBlockedError` | 409 `RELAY_BLOCKED`: the schedule or the manual lock holds the relay |
| `SteronixRateLimitError` | 429: too many requests; wait `retry_after` seconds |
| `SteronixUnavailableError` | 503: integrations are off or the token cannot be checked now |
| `SteronixNoAnswerError` | 504: the open was sent, the device did not answer; whether it opened is unknown |
| `SteronixResponseError` | Any other error status; the base class of the ones above |

A response error has `status`, `code`, `problem` (the RFC 9457 body, or
`None`) and `retry_after` (from `Retry-After`, or `None`). A 429 from the
edge's flood brake has no body; it is the same `SteronixRateLimitError`.

## Limits

The server limits each token: one open per relay every 2 seconds, 60 opens
an hour, a few immediate reads a minute and at most 2 waiting reads at once.
Over a limit the answer is 429 with `Retry-After`.

## Development

```bash
uv sync
uv run pytest
uv run ruff check . && uv run ruff format --check . && uv run mypy src
uv build
```

The tests run a fake API over real HTTPS. `contract/` holds the pinned
contract the tests check against. `examples/smoke.py` tries a real token:

```bash
STX_TOKEN=stx_… uv run python examples/smoke.py --seconds 60
```

## Licence

Apache-2.0
