forked from indykoning/PyPi_GrowattServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for SPH inverters using V1 API #11
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ace52f3
Add V1 API support for SPH devices
johanzander f1c771c
Update README and OpenAPI documentation to clarify support for MIN an…
johanzander 288e28c
Adress review comments
johanzander baace18
Enhance SPH read methods with global settings and optional device_sn
johanzander 8b9a67c
docs and API: clarify SPH helper methods, harmonize with MIN V1, impr…
johanzander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # SPH Inverter Settings | ||
|
|
||
| This is part of the [OpenAPI V1 doc](../openapiv1.md). | ||
|
|
||
| For SPH (hybrid inverter) systems, the public V1 API provides methods to read and write inverter settings. SPH inverters have different time period configurations compared to MIN inverters: | ||
|
|
||
| * **Read Parameter** | ||
| * function: `api.sph_read_parameter` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `parameter_id`: Parameter ID to read (e.g., "discharge_power") | ||
| * `start_address`, `end_address`: Optional, for reading registers by address | ||
|
|
||
| * **Write Parameter** | ||
| * function: `api.sph_write_parameter` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `parameter_id`: Parameter ID to write (e.g., "ac_charge") | ||
| * `parameter_values`: Value to set (single value, list, or dictionary) | ||
|
|
||
| * **AC Charge Time Periods** | ||
| * function: `api.sph_write_ac_charge_times` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `charge_power`: Charging power percentage (0-100) | ||
| * `charge_stop_soc`: Stop charging at this SOC percentage (0-100) | ||
| * `mains_enabled`: Boolean to enable/disable grid charging | ||
| * `periods`: List of 3 period dicts, each with: | ||
| * `start_time`: datetime.time object for period start | ||
| * `end_time`: datetime.time object for period end | ||
| * `enabled`: Boolean to enable/disable period | ||
|
|
||
| * **AC Discharge Time Periods** | ||
| * function: `api.sph_write_ac_discharge_times` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `discharge_power`: Discharge power percentage (0-100) | ||
| * `discharge_stop_soc`: Stop discharging at this SOC percentage (0-100) | ||
| * `periods`: List of 3 period dicts, each with: | ||
| * `start_time`: datetime.time object for period start | ||
| * `end_time`: datetime.time object for period end | ||
| * `enabled`: Boolean to enable/disable period | ||
|
|
||
| * **Read AC Charge Time Periods** | ||
| * function: `api.sph_read_ac_charge_times` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `settings_data`: Optional settings data to avoid redundant API calls | ||
|
|
||
| * **Read AC Discharge Time Periods** | ||
| * function: `api.sph_read_ac_discharge_times` | ||
| * parameters: | ||
| * `device_sn`: The device serial number | ||
| * `settings_data`: Optional settings data to avoid redundant API calls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """ | ||
| Example script for SPH devices using the OpenAPI V1. | ||
|
|
||
| This script demonstrates controlling SPH interface devices (device type 5) | ||
| such as hybrid inverter systems. | ||
| You can obtain an API token from the Growatt API documentation or developer portal. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import requests | ||
|
|
||
| import growattServer | ||
|
|
||
| # Get the API token from user input or environment variable | ||
| # api_token = os.environ.get("GROWATT_API_TOKEN") or input("Enter your Growatt API token: ") | ||
|
|
||
| # test token from official API docs https://www.showdoc.com.cn/262556420217021/1494053950115877 | ||
| api_token = "6eb6f069523055a339d71e5b1f6c88cc" # noqa: S105 | ||
|
|
||
| try: | ||
| # Initialize the API with token instead of using login | ||
| api = growattServer.OpenApiV1(token=api_token) | ||
|
|
||
| # Plant info | ||
| plants = api.plant_list() | ||
| print(f"Plants: Found {plants['count']} plants") # noqa: T201 | ||
| plant_id = plants["plants"][0]["plant_id"] | ||
|
|
||
| # Devices | ||
| devices = api.device_list(plant_id) | ||
|
|
||
| for device in devices["devices"]: | ||
| print(device) # noqa: T201 | ||
| if device["type"] == growattServer.DeviceType.SPH.value: | ||
| inverter_sn = device["device_sn"] | ||
| print(f"Processing SPH device: {inverter_sn}") # noqa: T201 | ||
|
|
||
| # Get device details | ||
| inverter_data = api.sph_detail( | ||
| device_sn=inverter_sn, | ||
| ) | ||
| print("Saving inverter data to inverter_data.json") # noqa: T201 | ||
| with open("inverter_data.json", "w") as f: | ||
| json.dump(inverter_data, f, indent=4, sort_keys=True) | ||
|
|
||
| # Get energy data | ||
| energy_data = api.sph_energy( | ||
| device_sn=inverter_sn, | ||
| ) | ||
| print("Saving energy data to energy_data.json") # noqa: T201 | ||
| with open("energy_data.json", "w") as f: | ||
| json.dump(energy_data, f, indent=4, sort_keys=True) | ||
|
|
||
| # Get energy history | ||
| energy_history_data = api.sph_energy_history( | ||
| device_sn=inverter_sn, | ||
| ) | ||
| print("Saving energy history data to energy_history.json") # noqa: T201 | ||
| with open("energy_history.json", "w") as f: | ||
| json.dump( | ||
| energy_history_data.get("datas", []), | ||
| f, | ||
| indent=4, | ||
| sort_keys=True, | ||
| ) | ||
|
|
||
| # Get details (includes settings data) | ||
| detail_data = api.sph_detail( | ||
| device_sn=inverter_sn, | ||
| ) | ||
| print("Saving detail data to settings_data.json") # noqa: T201 | ||
| with open("settings_data.json", "w") as f: | ||
| json.dump(detail_data, f, indent=4, sort_keys=True) | ||
|
|
||
| # Read AC charge time periods | ||
| charge_times = api.sph_read_ac_charge_times( | ||
| device_sn=inverter_sn, | ||
| settings_data=detail_data, | ||
| ) | ||
| print("AC Charge Time Periods:") # noqa: T201 | ||
| print(json.dumps(charge_times, indent=4)) # noqa: T201 | ||
|
|
||
| # Read AC discharge time periods | ||
| discharge_times = api.sph_read_ac_discharge_times( | ||
| device_sn=inverter_sn, | ||
| settings_data=detail_data, | ||
| ) | ||
| print("AC Discharge Time Periods:") # noqa: T201 | ||
| print(json.dumps(discharge_times, indent=4)) # noqa: T201 | ||
|
|
||
| # Read discharge power | ||
| discharge_power = api.sph_read_parameter( | ||
| device_sn=inverter_sn, | ||
| parameter_id="discharge_power", | ||
| ) | ||
| print(f"Current discharge power: {discharge_power}%") # noqa: T201 | ||
|
|
||
| # Write examples - Uncomment to test | ||
|
|
||
| # Set AC charge time periods: charge at 50% power to 95% SOC | ||
| # api.sph_write_ac_charge_times( | ||
| # device_sn=inverter_sn, | ||
| # charge_power=50, # 50% charging power | ||
| # charge_stop_soc=95, # Stop at 95% SOC | ||
| # mains_enabled=True, # Enable grid charging | ||
| # periods=[ | ||
| # {"start_time": datetime.time(0, 0), "end_time": datetime.time(6, 0), "enabled": True}, | ||
| # {"start_time": datetime.time(0, 0), "end_time": datetime.time(0, 0), "enabled": False}, | ||
| # {"start_time": datetime.time(0, 0), "end_time": datetime.time(0, 0), "enabled": False}, | ||
| # ] | ||
| # ) | ||
| # print("AC charge periods updated successfully") | ||
|
|
||
| # Set AC discharge time periods: discharge at 100% power to 20% SOC | ||
| # api.sph_write_ac_discharge_times( | ||
| # device_sn=inverter_sn, | ||
| # discharge_power=100, # 100% discharge power | ||
| # discharge_stop_soc=20, # Stop at 20% SOC | ||
| # periods=[ | ||
| # {"start_time": datetime.time(17, 0), "end_time": datetime.time(22, 0), "enabled": True}, | ||
| # {"start_time": datetime.time(0, 0), "end_time": datetime.time(0, 0), "enabled": False}, | ||
| # {"start_time": datetime.time(0, 0), "end_time": datetime.time(0, 0), "enabled": False}, | ||
| # ] | ||
| # ) | ||
| # print("AC discharge periods updated successfully") | ||
|
|
||
| except growattServer.GrowattV1ApiError as e: | ||
| print(f"API Error: {e} (Code: {e.error_code}, Message: {e.error_msg})") # noqa: T201 | ||
| except growattServer.GrowattParameterError as e: | ||
| print(f"Parameter Error: {e}") # noqa: T201 | ||
| except requests.exceptions.RequestException as e: | ||
| print(f"Network Error: {e}") # noqa: T201 | ||
| except Exception as e: # noqa: BLE001 | ||
| print(f"Unexpected error: {e}") # noqa: T201 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.