Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create async API clients #10

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Makefile ;)

dist: clean
@pip3 install --upgrade build twine
python -m build
twine check dist/*

Expand Down
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This modules contains two distinct sets of APIs:
- the original sonnen API (v1)
- the new v2 API

Both are available in a synchronous and an asynchronous version:
- `sonnenbatterie` - SonnenAPI v1, synchronous
- `AsyncSonnenBatterie` - SonnenAPI v1, asynchronous
- `SonnenBatterieV2` - SonnenAPI v2, synchronous
- `AsyncSonnenBatterieV2` - SonnenAPI v2, asynchronous

The major difference between those two is the method to authenticate. With API v1 you need
to login using a username and a password. The newer API requires a token that can be found
in the web UI of your Sonnenbatterie.
Expand All @@ -28,7 +34,9 @@ pip3 install sonnenbatterie

## Usage

### SonnenAPI v1 - sync
``` python
# API v1
from sonnenbatterie import sonnenbatterie

sb_host = '192.168.1.2'
Expand All @@ -44,12 +52,40 @@ print(sb.get_batterysystem()) # retrieve battery system data
print(sb.get_inverter()) # retrieve inverter status
print(sb.get_systemdata()) # retrieve system data
print(sb.get_battery()) # get battery information
```

### SonnenAPI v1 - async
``` python
# API v1
from sonnenbatterie import AsyncSonnenBatterie

sb_host = '192.168.1.2'
sb_user = 'User'
sb_pass = 'Password'

# Init class, establish connection
sb = AsyncSonnenBatterie(sb_host, sb_user, sb_pass)

print(await sb.get_status()) # retrieve general information
print(await sb.get_powermeters()) # retrieive power meter details
print(await sb.get_batterysystem()) # retrieve battery system data
print(await sb.get_inverter()) # retrieve inverter status
print(await sb.get_systemdata()) # retrieve system data
print(await sb.get_battery()) # get battery information

# Async needs to close the connection!
await sb.logout()
```

### SonnenAPI v2 - sync
``` python
# API v2
# can either be access directly, see below, or
# via sb.sb2 (gets initialiazed automatically when creating a V1 object)

from sonnebatterie2 import SonnenBatterieV2

sb_host = '192.168.1.2'
sb_token = 'SeCrEtToKeN' # retrieve via Web UI of SonnenBatterie

sb2 = SonnenBatterieV2(sb_host, sb_token)
Expand All @@ -58,6 +94,29 @@ print(sb2.get_battery_module_data()) # get battery module data
print(sb2.get_inverter_data()) # retrieve inverter data
print(sb2.get_latest_data()) # get latest date from sonnenbatterie
print(sb2_get_powermeter_data()) # get data from power meters
print(sb2.get_status_data()) # get overall status information
print(sb2.get_status()) # get overall status information
print(sb2.get_io_data()) # get io status
```

### SonnenAPI v2 - async
``` python
# API v2
# can either be access directly, see below, or
# via sb.sb2 (gets initialiazed automatically when creating a V1 object)

from sonnebatterie2 import AsyncSonnenBatterieV2

sb_host = '192.168.1.2'
sb_token = 'SeCrEtToKeN' # retrieve via Web UI of SonnenBatterie

sb2 = AsyncSonnenBatterieV2(sb_host, sb_token)
print(await sb2.get_configurations()) # retrieve configuration overview
print(await sb2.get_battery_module_data()) # get battery module data
print(await sb2.get_inverter_data()) # retrieve inverter data
print(await sb2.get_latest_data()) # get latest date from sonnenbatterie
print(await sb2_get_powermeter_data()) # get data from power meters
print(await sb2.get_status()) # get overall status information
print(await sb2.get_io_data()) # get io status
# Async needs to close the connection!
await sb.logout()
```
7 changes: 4 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sonnenbatterie
version = 0.4.0
version = 0.5.0
author = Jan Weltmeyer
description = "Access Sonnenbatterie REST API"
long_description = file: README.md
Expand All @@ -11,7 +11,7 @@ project_urls =
Issue Tracker = https://github.com/weltmeyer/python_sonnenbatterie/issues
classifiers =
Development Status :: 4 - Beta
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.10
Operating System :: OS Independent
Environment :: Console
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Expand All @@ -21,9 +21,10 @@ package_dir =
= .
packages = find:
include_package_data = True
python_requires = >= 3.8
python_requires = >= 3.10
install_requires =
requests
aiohttp

[options.packages.find]
where = .
2 changes: 1 addition & 1 deletion sonnenbatterie/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .sonnenbatterie import sonnenbatterie
from .sonnenbatterie import sonnenbatterie, AsyncSonnenBatterie
14 changes: 7 additions & 7 deletions sonnenbatterie/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEFAULT_BATTERY_LOGIN_TIMEOUT=120
DEFAULT_CONNECT_TO_BATTERY_TIMEOUT=60
DEFAULT_READ_FROM_BATTERY_TIMEOUT=60
DEFAULT_BATTERY_LOGIN_TIMEOUT=10
DEFAULT_CONNECT_TO_BATTERY_TIMEOUT=6
DEFAULT_READ_FROM_BATTERY_TIMEOUT=6

SONNEN_OPERATING_MODE_UNKNOWN_NAME="Unknown Operating Mode"
SONNEN_OPERATING_MODE_UNKNOWN="0"
Expand All @@ -9,10 +9,10 @@
SONNEN_OPERATING_MODE_BATTERY_MODULE_EXTENSION_30_PERCENT_NAME="Battery-Module-Extension (30%)"
SONNEN_OPERATING_MODE_TIME_OF_USE_NAME="Time-Of-Use"
SONNEN_OPERATING_MODE_NAMES_TO_OPERATING_MODES = {
SONNEN_OPERATING_MODE_MANUAL_NAME :"1",
SONNEN_OPERATING_MODE_AUTOMATIC_SELF_CONSUMPTION_NAME:"2",
SONNEN_OPERATING_MODE_BATTERY_MODULE_EXTENSION_30_PERCENT_NAME:"6",
SONNEN_OPERATING_MODE_TIME_OF_USE_NAME:"10"
SONNEN_OPERATING_MODE_MANUAL_NAME : 1,
SONNEN_OPERATING_MODE_AUTOMATIC_SELF_CONSUMPTION_NAME: 2,
SONNEN_OPERATING_MODE_BATTERY_MODULE_EXTENSION_30_PERCENT_NAME: 6,
SONNEN_OPERATING_MODE_TIME_OF_USE_NAME: 10
}
SONNEN_OPERATING_MODES_TO_OPERATING_MODE_NAMES = {v:k for k,v in SONNEN_OPERATING_MODE_NAMES_TO_OPERATING_MODES.items()}

Expand Down
Loading
Loading