Metadata-Version: 2.5
Name: pyeebus
Version: 0.4.0
Summary: EEBUS (SHIP and SPINE) for Python, ported from the enbility Go implementation
Project-URL: Homepage, https://github.com/frane/pyeebus
Author: Frane Bandov
License-Expression: MIT
License-File: LICENSE
Keywords: eebus,energy-management,hems,ship,spine,wallbox
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Framework :: AsyncIO
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Home Automation
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: aiohttp>=3.9
Requires-Dist: cryptography>=42
Requires-Dist: zeroconf>=0.131
Provides-Extra: test
Requires-Dist: pytest-asyncio>=0.23; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# pyeebus

EEBUS for Python: SHIP (transport), SPINE (data model) and the energy manager (CEM) use cases for EV charging. Ported from the Go implementation by [enbility](https://enbility.net/) (ship-go, spine-go, eebus-go, MIT licensed).

> **Status: 0.1.0, early.** SHIP, SPINE and the use cases (EV charging, LPC, MPC) are tested against eebus-go/spine-go in both directions and in use with a real Elli Charger 2 and Solar Manager (through [elli-eebus-proxy](https://github.com/frane/elli-eebus-proxy)). The API may still change.

## Why

EEBUS is how energy managers talk to wallboxes, heat pumps and grid control boxes. Until now, the only complete open-source implementation has been in Go. pyeebus makes EEBUS available to Python projects such as Home Assistant integrations.

## Roadmap

| Layer | Status |
|---|---|
| SHIP: certificates/SKI, TLS websocket, handshake (CMI, hello incl. prolongation, protocol, PIN none, access methods), data exchange, close | ✅ tested against ship-go |
| SHIP: mDNS announce and discovery (`zeroconf`), trust store, auto-reconnect | ✅ |
| SPINE: devices/entities/features, detailed discovery, use case data, subscriptions, bindings, partial updates, heartbeat | ✅ tested against spine-go |
| Use cases (CEM side): EVSECC, EVCC, EVCEM, OPEV, OSCEV, EVSOC | ✅ tested against eebus-go |
| Limitation and monitoring of power consumption: LPC and LPP (Energy Guard), MPC (Monitoring Appliance) | ✅ tested against eebus-go |
| Controllable System side: CS-LPC and CS-LPP (feed-in) with the LPC/LPP state machine (init, limited, failsafe, ...) | ✅ tested against eebus-go |
| Monitored Unit side: MU-MPC (power, energy, currents, voltages, frequency) | ✅ tested against eebus-go |
| Coordinated EV charging (CEVC) | later |

## Try it

```bash
pip install pyeebus   # or: uvx pyeebus ...

pyeebus discover                     # list EEBUS devices on the network
pyeebus connect <SKI of the device>  # pair, connect as energy manager, show what the device offers
pyeebus connect <SKI> --read-all     # ... and print all data the device offers
pyeebus connect <SKI> --raw          # ... and print every SPINE message
pyeebus connect <SKI> --lpc-limit 4200   # test: limit the power to 4200 W for 5 minutes (LPC)
```

`connect` announces pyeebus on the network as an energy manager. Then pair with it on the device. For example, on an Elli wallbox: *Connections → HEMS connection → found EEBUS devices → Pair*. The identity is kept in `~/.config/pyeebus/`.

`pyeebus simulate-evse` runs a simulated wallbox with a pluggable EV for development.

## Library

```python
from pyeebus.service import EebusService
from pyeebus.usecases import EVCC, EVCEM, EVSECC, OPEV, PhaseLimit

service = EebusService.from_directory("/path/to/state", brand="Me", model="HEMS", serial="1")
cem = service.entities[0]                      # the CEM entity

def on_event(ski, entity, event):              # e.g. "EvConnected", "DataUpdateCurrentPerPhase"
    if event == EVCEM.DATA_UPDATE_CURRENT_PER_PHASE:
        print(evcem.current_per_phase(entity))

evsecc, evcc, evcem, opev = (uc(cem, on_event).setup() for uc in (EVSECC, EVCC, EVCEM, OPEV))

await service.start()
service.trust("<SKI of the wallbox>")          # connects as soon as it is found via mDNS

# later, with the EV entity from an event:
await opev.write_load_control_limits(ev, [PhaseLimit(p, 10) for p in "abc"])   # 10 A
```

For devices that offer power limitation (LPC, e.g. wallboxes with §14a EnWG support) use `LPC` and `MPC`: `await lpc.write_consumption_limit(evse, LoadLimit(4200, duration=3600))`, `mpc.power(evse)`, `mpc.energy_consumed(evse)`.

To act as the controllable system yourself (a device behind a §14a EnWG control box), use `CsLPC` (and `CsLPP` for a feed-in limit, the same API with `production_*` names and positive watts; pyeebus puts LPP's negative sign on the wire). Incoming limits and failsafe values go through your approval callbacks; the built-in state machine (LPC/LPP 1.0.0) handles init, heartbeat timeout (failsafe), failsafe duration and limit durations, so you only apply `effective_limit()` (W, `None` = no limit) whenever `STATE_CHANGED` comes. The failsafe limit is required: it applies from the start (init) until the Energy Guard states a limit:

```python
mpc = MuMPC(service.entities[0], power=MonitorPowerConfig("abc"),
            energy=MonitorEnergyConfig(value_source_consumption="measuredValue")).setup()  # before CsLPC
lpc = CsLPC(service.entities[0], on_event, failsafe_limit=4200, failsafe_duration=2 * 3600,
            approve_limit=lambda limit: None).setup()
lpc.set_consumption_nominal_max(11000)
lpc.set_consumption_limit(LoadLimit(11000, is_active=False))
mpc.update(power_total=3200, energy_consumed=1_234_000)   # W, Wh; notifies the monitoring appliance
```

The use cases follow eebus-go: `evcc.charge_state(ev)`, `evcc.communication_standard(ev)`, `evcem.energy_charged(ev)`, `opev.current_limits(ev)` (min/max/default per phase) and so on. Values the device has not sent yet raise `DataNotAvailable`.

Lower levels are usable on their own: `pyeebus.ship` (`ShipNode`, `ShipConnection`) and `pyeebus.spine` (`LocalDevice`, features, subscriptions).

**Limitation of Python's `ssl`:** a server cannot request a client certificate without verifying it. For that reason pyeebus accepts incoming connections only from peers whose certificate it already knows, for example from an earlier outgoing connection. It reaches new peers as a client.

## Development

```bash
python3 -m venv .venv && . .venv/bin/activate
pip install -e ".[test]"
pytest
```

The interop tests against ship-go and eebus-go run when `SHIP_GO_HARNESS` and `EEBUS_GO_HARNESS` point to the built harnesses; see [interop/](interop). CI builds them automatically. `tools/gen_schema.py` regenerates the SPINE list/key metadata from spine-go's model.

*EEBUS is a trademark of EEBus Initiative e.V. This project is not affiliated with it.*
