File: min_example.py

package info (click to toggle)
growattserver 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 288 kB
  • sloc: python: 1,294; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 3,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import growattServer
import datetime
import json
import requests

"""
# Example script controlling a MID/TLX Growatt (MID-30KTL3-XH + APX battery) system using the public growatt API 
# You can obtain an API token from the Growatt API documentation or developer portal.
"""

# 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"  # gitleaks:allow

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")
    plant_id = plants['plants'][0]['plant_id']

    # Devices
    devices = api.device_list(plant_id)

    for device in devices['devices']:
        if device['type'] == 7:  # (MIN/TLX)
            inverter_sn = device['device_sn']
            print(f"Processing inverter: {inverter_sn}")

            # Get device details
            inverter_data = api.min_detail(inverter_sn)
            print("Saving inverter data to inverter_data.json")
            with open('inverter_data.json', 'w') as f:
                json.dump(inverter_data, f, indent=4, sort_keys=True)

            # Get energy data
            energy_data = api.min_energy(device_sn=inverter_sn)
            print("Saving energy data to energy_data.json")
            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.min_energy_history(inverter_sn)
            print("Saving energy history data to energy_history.json")
            with open('energy_history.json', 'w') as f:
                json.dump(energy_history_data['datas'],
                          f, indent=4, sort_keys=True)

            # Get settings
            settings_data = api.min_settings(device_sn=inverter_sn)
            print("Saving settings data to settings_data.json")
            with open('settings_data.json', 'w') as f:
                json.dump(settings_data, f, indent=4, sort_keys=True)

            # Read time segments
            tou = api.min_read_time_segments(inverter_sn, settings_data)
            print(json.dumps(tou, indent=4))

            # Read discharge power
            discharge_power = api.min_read_parameter(
                inverter_sn, 'discharge_power')
            print("Current discharge power:", discharge_power, "%")

            # Settings parameters - Uncomment to test

            # Turn on AC charging
#            api.min_write_parameter(inverter_sn, 'ac_charge', 1)
#            print("AC charging enabled successfully")

            # Enable Load First between 00:00 and 11:59 using time segment 1
#            api.min_write_time_segment(
#                device_sn=inverter_sn,
#                segment_id=1,
#                batt_mode=growattServer.BATT_MODE_BATTERY_FIRST,
#                start_time=datetime.time(0, 0),
#                end_time=datetime.time(00, 59),
#                enabled=True
#            )
#            print("Time segment updated successfully")


except growattServer.GrowattV1ApiError as e:
    print(f"API Error: {e} (Code: {e.error_code}, Message: {e.error_msg})")
except growattServer.GrowattParameterError as e:
    print(f"Parameter Error: {e}")
except requests.exceptions.RequestException as e:
    print(f"Network Error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")