anker-solix-api/README.md

187 lines
10 KiB
Markdown
Raw Permalink Normal View History

<img src="https://public-aiot-fra-prod.s3.dualstack.eu-central-1.amazonaws.com/anker-power/public/product/anker-power/e9478c2d-e665-4d84-95d7-dd4844f82055/20230719-144818.png" alt="Solarbank E1600 Logo" title="Anker Solix Api" align="right" height="100" />
2024-01-30 13:20:01 +00:00
# Anker Solix Api
2024-01-30 11:58:09 +00:00
2024-01-30 13:20:01 +00:00
[![github licence](https://img.shields.io/badge/Licence-MIT-orange)](https://github.com/thomluther/anker-solix-api/blob/main/LICENSE)
![python badge](https://img.shields.io/badge/Made%20with-Python-orange)
2024-01-30 11:58:09 +00:00
This is an experimental Python library for Anker Solix Power devices (Solarbank, Inverter etc).
2024-01-30 13:20:01 +00:00
🚨 This is by no means an official Anker Api. 🚨
2024-01-30 13:57:47 +00:00
🚨 It can break at any time, or Api request can be removed/added/changed and break some of the endpoint methods used in this Api.🚨
2024-01-30 11:58:09 +00:00
# Python Versions
The library is currently supported on
- Python 3.11
- Python 3.12
2024-01-30 11:58:09 +00:00
# Required libraries
2024-02-23 08:37:32 +00:00
This project uses `pipenv` for Python dependency management
2024-01-30 11:58:09 +00:00
```bash
2024-02-23 08:37:32 +00:00
pip install pipenv
pipenv sync -d
pipenv run python [...]
2024-01-30 11:58:09 +00:00
```
# Anker Account Information
Because of the way the Anker Solix Api works, one account with email/password cannot be used for the Anker mobile App and this Api in parallel.
2024-01-30 13:57:47 +00:00
The Anker Cloud allows only one request token per account at any time. Each new authentication request by a client will create a new token and drop a previous token.
Therefore usage of this Api may kick out your account login in the mobile app.
2024-01-30 13:57:47 +00:00
However, starting with Anker mobile app release 2.0, you can share your defined system(s) with 'family members'.
Therefore it is recommended to create a second Anker account with a different email address and share your defined system(s) with the second account.
Attention: A shared account is only a member of the shared system, and as such currently has no permissions to access or query device details of the shared system.
Therefore an Api homepage query will neither display any data for a shared account. However, a shared account can receive Api scene/site details of shared systems (App system = Api site),
2024-01-30 13:57:47 +00:00
which is equivalent to what is displayed in the mobile app on the home screen for the selected system.
2024-01-30 11:58:09 +00:00
# Usage
Everything starts with an:
[aiohttp](https://aiohttp.readthedocs.io/en/stable/) `ClientSession`:
```python
2024-01-31 12:22:35 +00:00
import logging, json
2024-01-30 11:58:09 +00:00
import asyncio
from aiohttp import ClientSession
2024-01-31 12:22:35 +00:00
from api import api, errors
2024-02-23 08:37:32 +00:00
import common
2024-01-30 11:58:09 +00:00
_LOGGER: logging.Logger = logging.getLogger(__name__)
# _LOGGER.setLevel(logging.DEBUG) # enable for detailed Api output
2024-01-30 11:58:09 +00:00
async def main() -> None:
"""Create the aiohttp session and run the example."""
async with ClientSession() as websession:
"""put your code here, example"""
myapi = api.AnkerSolixApi(
2024-02-23 08:37:32 +00:00
common.user(), common.password(), common.country(), websession, _LOGGER
)
2024-01-30 11:58:09 +00:00
await myapi.update_sites()
2024-04-24 10:46:25 +00:00
await myapi.update_site_details()
2024-01-30 11:58:09 +00:00
await myapi.update_device_details()
2024-04-24 10:46:25 +00:00
await myapi.update_device_energy()
2024-01-30 11:58:09 +00:00
print("System Overview:")
print(json.dumps(myapi.sites, indent=2))
print("Device Overview:")
print(json.dumps(myapi.devices, indent=2))
# run async main
if __name__ == "__main__":
2024-01-30 11:58:09 +00:00
try:
asyncio.run(main())
except Exception as err:
print(f"{type(err)}: {err}")
2024-01-30 11:58:09 +00:00
```
2024-04-24 10:46:25 +00:00
The AnkerSolixApi class provides 4 main methods to query data and cache them into internal dictionaries:
- `AnkerSolixApi.update_sites()` to query overview data for all accessible sites and store data in dictionaries `AnkerSolixApi.sites` and `AnkerSolixApi.devices` for quick access.
2024-04-24 10:46:25 +00:00
This method could be run in regular intervals (60sec or more) to fetch new data of the systems. Note that the system devices update the cloud data only once per minute, therefore less than 60 second intervals do not provide much benefit
- `AnkerSolixApi.update_device_details()` to query further settings for the device serials as found in the sites query or for stand alone devices and store data in dictionary `AnkerSolixApi.devices`
This method should be run less frequently since this will mostly fetch various device configuration settings and needs multiple queries.
2024-02-20 13:35:04 +00:00
It currently is developped for Solarbank and Inverter devices only, further device types such as Portable Power Stations or Power Panels
could be added once example data is available.
2024-04-24 10:46:25 +00:00
- `AnkerSolixApi.update_site_details()` to query further settings for the defined site (power system) and store data in dictionary `AnkerSolixApi.sites` for quick access.
This method should be run less frequently since this will mostly fetch various site configuration settings and needs multiple queries.
- `AnkerSolixApi.update_device_energy()` to query further energy statistics for the devices and store data in dictionary `AnkerSolixApi.devices` for quick access.
This method should be run less frequently since this will fetch 2-4 queries per device. Currently only solarbank devices are supported, but it was noticed, that the energy statistics endpoint (maybe each endpoint) is limited to 25-30 queries per minute.
2024-01-30 13:57:47 +00:00
Check out `test_api.py` and other python executable tools that may help to leverage and explore the Api for your Anker power system.
The subfolder [`examples`](https://github.com/thomluther/anker-solix-api/tree/main/examples) contains actual example exports with json files using anonymized responses of the `export_system.py` module giving you an idea of how various Api responses look like. (Note that the Solarbank was switched off when the data were pulled, so some fields may be empty)
Those json files can also be used to develop/debug the Api for system constellations not available to the developper.
2024-01-30 11:58:09 +00:00
# AnkerSolixApi Tools
2024-01-30 11:58:09 +00:00
## test_api.py
2024-02-23 08:37:32 +00:00
```
> pipenv run ./test_api.py
```
Example exec module that can be used to explore and test AnkerSolixApi methods or direct enpoint requests with parameters.
2024-02-20 13:35:04 +00:00
You can modify this module as required and hard code your credentials. Optionally you can put your credentials also in the api/credentials.py file.
None of these is indexed and they are added to gitignore, so your local changes are not tracked for git updates/commits.
2024-01-30 11:58:09 +00:00
## export_system.py
2024-02-23 08:37:32 +00:00
```
> pipenv run ./export_system.py
```
Example exec module to use the Anker Api for export of defined system data and device details.
2024-01-30 11:58:09 +00:00
This module will prompt for the Anker account details if not pre-set in the header.
Upon successfull authentication, you can specify a subfolder for the exported JSON files received as Api query response, defaulting to your nick name
2024-01-30 11:58:09 +00:00
Optionally you can specify whether personalized information in the response data should be randomized in the files, like SNs, Site IDs, Trace IDs etc.
You can review the response files afterwards. They can be used as examples for dedicated data extraction from the devices.
Optionally the AnkerSolixApi class can use the json files for debugging and testing on various system outputs.
2024-01-30 11:58:09 +00:00
2024-04-24 10:46:25 +00:00
**Note**:
You should preferrably run the export_system with the owner account of the site. Otherwise only limited information can be exported by shared accounts due to access permissions.
2024-01-30 11:58:09 +00:00
## solarbank_monitor.py
2024-02-23 08:37:32 +00:00
```
> pipenv run ./solarbank_monitor.py
```
2024-02-20 13:35:04 +00:00
Example exec module to use the Anker Api for continously querying and displaying important solarbank parameters.
This module will can use real time data from your Anker account, or optionally use json files from your local examples or export folder.
When using the real time option, it will prompt for the Anker account details if not pre-set in the header.
2024-01-30 11:58:09 +00:00
Upon successfull authentication, you will see the solarbank parameters displayed and refreshed at reqular interval.
2024-02-20 13:35:04 +00:00
When using monitoring from local json file folder, they values will not change. But this option is useful to validate the api parsing with various system constellations.
2024-01-30 11:58:09 +00:00
Note: When the system owning account is used, more details for the solarbank can be queried and displayed.
2024-02-20 13:35:04 +00:00
**Attention: When executing this module with real time data from your Anker account, the used account cannot be used in the Anker App since it will be kicked out on each refresh.**
2024-01-30 11:58:09 +00:00
## energy_csv.py
2024-02-23 08:37:32 +00:00
```
> pipenv run ./energy_csv.py
```
Example exec module to use the Anker Api for export of daily Solarbank Energy Data.
2024-01-30 11:58:09 +00:00
This method will prompt for the Anker account details if not pre-set in the header.
Then you can specify a start day and the number of days for data extraction from the Anker Cloud.
Note: The Solar production and Solarbank discharge can be queried across the full range. The solarbank
charge however can be queried only as total for an interval (e.g. day). Therefore when solarbank charge
data is also selected for export, an additional Api query per day is required.
2024-01-30 11:58:09 +00:00
The received daily values will be exported into a csv file.
# Contributing
2024-01-30 13:20:01 +00:00
![github contributors](https://img.shields.io/github/contributors/thomluther/anker-solix-api?color=orange)
![last commit](https://img.shields.io/github/last-commit/thomluther/anker-solix-api?color=orange)
[![Community Discussion](https://img.shields.io/badge/Home%20Assistant%20Community-Discussion-orange)](https://community.home-assistant.io/t/feature-request-integration-or-addon-for-anker-solix-e1600-solarbank/641086)
Github is used to host code, to track issues and feature requests, as well as accept pull requests.
Pull requests are the best way to propose changes to the codebase.
2024-01-30 12:58:58 +00:00
1. [Check for open features/bugs](https://github.com/thomluther/anker-solix-api/issues)
or [initiate a discussion on one](https://github.com/thomluther/anker-solix-api/issues/new).
1. [Fork the repository](https://github.com/thomluther/anker-solix-api/fork).
1. Fork the repo and create your branch from `main`.
1. If you've changed something, update the documentation.
1. Test your contribution.
1. Issue that pull request!
2024-01-30 11:58:09 +00:00
2024-01-30 12:58:58 +00:00
# Acknowledgements / Credits
2024-01-30 11:58:09 +00:00
2024-01-30 13:57:47 +00:00
- [python-eufy-security](https://github.com/FuzzyMistborn/python-eufy-security)
- [solix2mqtt](https://github.com/tomquist/solix2mqtt)
2024-01-30 13:20:01 +00:00
# Showing Your Appreciation
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/thomasluthe)
If you like this project, please give it a star on [GitHub](https://github.com/thomluther/anker-solix-api)