Metadata-Version: 2.4
Name: aioleafi
Version: 0.0.1
Summary: Asyncio client for the Leafi Nova local TLS-PSK API
Author: Leafi
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/leafihome/aioleafi
Project-URL: Source, https://github.com/leafihome/aioleafi
Project-URL: Issues, https://github.com/leafihome/aioleafi/issues
Keywords: leafi,nova,home-assistant,asyncio,blinds
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: Topic :: Home Automation
Classifier: Typing :: Typed
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cryptography>=42
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-asyncio; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# aioleafi

Asyncio Python client for the Leafi Nova local TCP API (TLS 1.2 PSK, newline-delimited
JSON-RPC). Used by the `leafi` Home Assistant integration. Licensed Apache-2.0.

The library implements version 1 of the Nova local API: the TLS-PSK handshake, the per-Nova
key format and the encrypted key upload. The source in this repository is the reference for
the wire format.

```python
import asyncio
from aioleafi import LeafiClient, parse_key


async def main() -> None:
    client = LeafiClient("192.0.2.42", parse_key("1YFA-8W83-AV53-VA3S-4W0Y-SX51-BFR"))
    client.add_state_listener(print)
    hello = await client.connect()  # TLS-PSK handshake, hello, get_state snapshot
    print(hello)
    await client.open_cover()
    await asyncio.sleep(60)  # events arrive as they happen; nothing is polled
    await client.close()


asyncio.run(main())
```

`client.start()` instead of `connect()` keeps the connection up and reconnects with backoff
(5 s doubling to 20 min; `reconnect_now()` skips the wait). It stops on a wrong key, wrong
Nova or unsupported API version and reports it through `add_error_listener`.

## Behaviour worth knowing

- **No polling.** A sleeping Nova answers at its next DTIM beacon (about 0.7 s). State comes from
  `cover` and `sensors` events; `get_state()` sends a request at most once per 10 s and otherwise
  returns the current values without sending anything.
- A `ping` goes out every 30 minutes; two missed replies mark the connection lost.
- `busy` is retried once after 1 s; other error replies raise `NovaError` subclasses
  (`NotCalibratedError`, `UnknownMethodError`, ...).
- Handshake failure after a successful TCP connect raises `InvalidKeyError`; no TCP connection
  raises `CannotConnectError`; a different DSN in `hello` raises `WrongNovaError`.

## TLS-PSK approach

Python 3.13 added `ssl.SSLContext.set_psk_client_callback` / `set_psk_server_callback`, so no
third-party TLS library is needed. The client context pins TLS 1.2, cipher
`PSK-AES128-GCM-SHA256` and identity `ha`; it needs an OpenSSL with PSK suites (the python.org
Windows build, OpenSSL 3.0.x, and HA's Python 3.13+ images have them). The handshake runs with
`StreamWriter.start_tls` after a plain TCP connect, which lets the client tell "no TCP" from
"handshake rejected". Verified against both the fake Nova here and OpenSSL's server side.

## Keys and uploads

`parse_key`, `format_key`, `key_id` implement the 27-character Crockford base32 form (mod-37 check
character). `parse_key_list` validates a `leafi-ha-keys` list with the spec's per-entry rejection
reasons, and `decrypt_key_upload` / `encrypt_key_upload` implement the AES-128-GCM envelope of
the app's POST (Method 3).

## Testing

`aioleafi.testing.FakeNova` is a TLS-PSK fake Nova built from the interface spec.

```
pip install -e ".[dev]"
pytest
ruff check . && ruff format --check . && mypy src
```
