From 98731d75795ddb66df92ceb0b83b26dc5a55448a Mon Sep 17 00:00:00 2001 From: johanzander Date: Sat, 28 Sep 2024 17:45:39 +0200 Subject: [PATCH 01/13] Added support for TLX hybrid inverters --- README.md | 42 ++- examples/tlx_example.py | 95 +++++++ growattServer/__init__.py | 523 ++++++++++++++++++++++++++++++++------ 3 files changed, 575 insertions(+), 85 deletions(-) create mode 100644 examples/tlx_example.py diff --git a/README.md b/README.md index dc01c8b..e6e99d8 100755 --- a/README.md +++ b/README.md @@ -42,6 +42,16 @@ Any methods that may be useful. `api.tlx_detail(tlx_id)` Get detailed data on a tlx type inverter. +`api.tlx_params(tlx_id)` Get parameters for the tlx type inverter. + +`api.tlx_get_all_settings(tlx_id)` Get all possible settings for the tlx type inverter. + +`api.tlx_get_enabled_settings(tlx_id)` Get all enabled settings for the tlx type inverter. + +`api.get_battery_info(serial_num)` Get battery info. Uses the tlx API, so might only work for tlx type inverter. + +`api.get_battery_info_detailed(serial_num)` Get detailed battery info. Uses the tlx API, so might only work for tlx type inverter. + `api.mix_info(mix_id, plant_id=None)` Get high level information about the Mix system including daily and overall totals. NOTE: `plant_id` is an optional parameter, it does not appear to be used by the remote API, but is used by the mobile app these calls were reverse-engineered from. `api.mix_totals(mix_id, plant_id)` Get daily and overall total information for the Mix system (duplicates some of the information from `mix_info`). @@ -68,6 +78,10 @@ Any methods that may be useful. `api.update_plant_settings(plant_id, changed_settings, current_settings)` Update the settings for a plant to the values specified in the dictionary, if the `current_settings` are not provided it will look them up automatically using the `get_plant_settings` function - See 'Plant settings' below for more information +`api.update_tlx_inverter_setting(serial_number, setting_type, parameter)` Applies the provided parameter for the specified setting on the specified tlx inverter; see 'Inverter settings' below for more information. + +`api.update_tlx_inverter_time_segment(serial_number, segment_id, batt_mode, start_time, end_time, enabled)` Updates one of the 9 time segments with the specified battery mode (load, battery, grid first); see 'Inverter settings' below for more information. + `api.update_mix_inverter_setting(serial_number, setting_type, parameters)` Applies the provided parameters (dictionary or array) for the specified setting on the specified mix inverter; see 'Inverter settings' below for more information `api.update_ac_inverter_setting(serial_number, setting_type, parameters)` Applies the provided parameters (dictionary or array) for the specified setting on the specified AC-coupled inverter; see 'Inverter settings' below for more information @@ -137,7 +151,7 @@ The plant settings function(s) allow you to re-configure the settings for a spec The function `update_plant_settings` allows you to provide a python dictionary of any/all of the above settings and change their value. ## Inverter Settings -NOTE: The inverter settings function appears to only work with 'mix' systems based on the API call that it makes being specific to 'mix' inverters +NOTE: The inverter settings function appears to only work with 'mix' and 'tlx' systems based on the API call that it makes being specific to those inverter types The inverter settings function(s) allow you to change individual values on your inverter e.g. time, charging period etc. From what has been reverse engineered from the api, each setting has a `setting_type` and a set of `parameters` that are relevant to it. @@ -191,8 +205,30 @@ Known working settings & parameters are as follows (all parameter values are str * `param15`: Schedule 3 - End time - Hour e.g. "02" (2am) * `param16`: Schedule 3 - End time - Minute e.g. "00" (0 minutes) * `param17`: Schedule 3 - Enabled/Disabled (0 = Disabled, 1 = Enabled) - -The three functions `update_mix_inverter_setting`, `update_ac_inverter_setting`, and `update_inverter_setting` take either a dictionary or an array. If an array is passed it will automatically generate the `paramN` key based on array index since all params for settings seem to used the same numbering scheme. +* **TLX inverter settings** + * function: `api.update_tlx_inverter_setting` + * type: `charge_power` + * param1: Charging power % (value between 0 and 100) + * type: `charge_stop_soc` + * param1: Charge Stop SOC + * type: `discharge_power` + * param1: Discharging power % (value between 0 and 100) + * type: `discharge_stop_soc` + * param1: Discharge Stop SOC + * type: `ac_charge` + * param1: Allow AC (grid) charging (0 = Disabled, 1 = Enabled) + * type: `pf_sys_year` + * param1: datetime in format: `YYYY-MM-DD HH:MM:SS` + * function: `api.update_tlx_inverter_time_segment` + * segment_id: The segment to update (1-9) + * batt_mode: Battery Mode for the segment: 0=Load First(Self-Consumption), 1=Battery First, 2=Grid First + * start_time: timedate object with start time of segment with format HH:MM + * end_time: timedate object with end time of segment with format HH:MM + * enabled: time segment enabled, boolean: True (Enabled), False (Disabled) + +The four functions `update_tlx_inverter_setting`, `update_mix_inverter_setting`, `update_ac_inverter_setting`, and `update_inverter_setting` take either a dictionary or an array. If an array is passed it will automatically generate the `paramN` key based on array index since all params for settings seem to used the same numbering scheme. + +Only the settings described above have been tested with `update_tlx_inverter_setting` and they all take only one single parameter. It is very likely that the function works with all settings returned by `tlx_get_enabled_settings`, but this has not been tested. A helper function `update_tlx_inverter_time_segment` is provided for the settings that require more than one parameter. ## Noah Settings The noah settings function allow you to change individual values on your noah system e.g. system default output power, battery management, operation mode and currency diff --git a/examples/tlx_example.py b/examples/tlx_example.py new file mode 100644 index 0000000..9f01c5e --- /dev/null +++ b/examples/tlx_example.py @@ -0,0 +1,95 @@ +import growattServer +import datetime +import getpass +import json + +""" +# Example script controlling a Growatt MID-30KTL3-XH + APX battery hybrid system by emulating the ShinePhone iOS app. +# The same API calls are used by the ShinePhone Android app as well. Traffic intercepted using HTTP Toolkit. +# +# The plant / energy / device APIs seem to be generic for all Growatt systems, while the inverter and battery APIs use the TLX APIs. +# +# The available settings under the 'Control' tab in ShinePhone are created by combining the results from two function calls: +# tlx_get_all_settings() seem to returns the sum of all settings for all systems while tlx_get_enabled_settings() tells +# which of these settings are valid for the TLX system. +# +# Settings that takes a single parameter can be set using update_tlx_inverter_setting(). A helper function, update_tlx_inverter_time_segment() +# is provided for updating time segments which take several parameters. The inverter is picky and time intervals can't be overlapping, +# even if they are disabled. +# +# The set functions are commented out in the example, uncomment to test, and use at your own risk. Most likely all settings returned in +# tlx_get_enabled_settings() can be set using update_tlx_inverter_setting(), but has not been tested. +# +""" + +#Prompt user for username +username=input("Enter username:") + +#Prompt user to input password +user_pass=getpass.getpass("Enter password:") + +user_agent = 'ShinePhone/8.1.17 (iPhone; iOS 15.6.1; Scale/2.00)' +api = growattServer.GrowattApi(agent_identifier=user_agent) + +login_response = api.login(username, user_pass) +user_id = login_response['user']['id'] +print("Login successful, user_id:", user_id) + +# Plant info +plant_list = api.plant_list_two() +plant_id = plant_list[0]['id'] +plant_info = api.plant_info(plant_id) +print("Plant info:", json.dumps(plant_info, indent=4, sort_keys=True)) + +# Energy data +energy_data = api.get_energy_data(plant_id) +print("Energy data", json.dumps(energy_data, indent=4, sort_keys=True)) + +# Devices +devices = api.get_all_devices(plant_id) +print("Devices:", json.dumps(devices, indent=4, sort_keys=True)) + +for device in devices['deviceList']: + if device['deviceType'] == 'tlx': + # Inverter info + inverter_sn = device['deviceSn'] + inverter_info = api.tlx_params(inverter_sn) + print("TLX inverter info:", json.dumps(inverter_info, indent=4, sort_keys=True)) + + # Data + data = api.tlx_data(inverter_sn, datetime.datetime.now()) + print("TLX data:", json.dumps(data, indent=4, sort_keys=True)) + + # Settings + all_settings = api.tlx_get_all_settings(inverter_sn) + enabled_settings = api.tlx_get_enabled_settings(inverter_sn) + enabled_keys = enabled_settings['enable'].keys() + available_settings = {k: v for k, v in all_settings.items() if k in enabled_keys} + print("Settings:", json.dumps(available_settings, indent=4, sort_keys=True)) + + elif device['deviceType'] == 'bat': + # Battery info + batt_info = api.get_battery_info(device['deviceSn']) + print("Battery info:", json.dumps(batt_info, indent=4, sort_keys=True)) + batt_info_detailed = api.get_battery_info_detailed(plant_id, device['deviceSn']) + print("Battery info: detailed", json.dumps(batt_info_detailed, indent=4, sort_keys=True)) + + +# Examples of updating settings, uncomment to use + +# Set charging power to 95% +#res = api.update_tlx_inverter_setting(inverter_sn, 'charge_power', 96) +#print(res) + +# Turn on AC charging +#res = api.update_tlx_inverter_setting(inverter_sn, 'ac_charge', 1) +#print(res) + +# Enable Load First between 00:01 and 11:59 using time segment 1 +#res = api.update_tlx_inverter_time_segment(serial_number = inverter_sn, +# segment_id = 1, +# batt_mode = growattServer.BATT_MODE_LOAD_FIRST, +# start_time = datetime.time(00, 1), +# end_time = datetime.time(11, 59), +# enabled=True) +#print(res) \ No newline at end of file diff --git a/growattServer/__init__.py b/growattServer/__init__.py index bb8a685..ef8101d 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -8,6 +8,10 @@ import warnings from random import randint +BATT_MODE_LOAD_FIRST = 0 +BATT_MODE_BATTERY_FIRST = 1 +BATT_MODE_GRID_FIRST = 2 + def hash_password(password): """ Normal MD5, except add c if a byte of the digest is less than 10. @@ -59,9 +63,9 @@ def __get_date_string(self, timespan=None, date=None): return date_str - def get_url(self, page): + def __get_url(self, page): """ - Simple helper function to get the page url/ + Simple helper function to get the page URL. """ return self.server_url + page @@ -128,106 +132,350 @@ def login(self, username, password, is_password_hashed=False): if not is_password_hashed: password = hash_password(password) - response = self.session.post(self.get_url('newTwoLoginAPI.do'), data={ + response = self.session.post(self.__get_url('newTwoLoginAPI.do'), data={ 'userName': username, 'password': password }) - data = json.loads(response.content.decode('utf-8'))['back'] - if data['success']: - data.update({ - 'userId': data['user']['id'], - 'userLevel': data['user']['rightlevel'] - }) - return data + + if response.status_code == 200: + data = response.json()['back'] + if data['success']: + data.update({ + 'userId': data['user']['id'], + 'userLevel': data['user']['rightlevel'] + }) + return data + else: + raise Exception('Login failed') def plant_list(self, user_id): """ Get a list of plants connected to this account. - """ - response = self.session.get(self.get_url('PlantListAPI.do'), - params={'userId': user_id}, - allow_redirects=False) - data = json.loads(response.content.decode('utf-8')) - return data['back'] + Args: + user_id (str): The ID of the user. + + Returns: + list: A list of plants connected to the account. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get( + self.__get_url('PlantListAPI.do'), + params={'userId': user_id}, + allow_redirects=False + ) + + if response.status_code == 200: + data = response.json() + return data.get('back', []) + else: + raise Exception('Failed to retrieve plant list') def plant_detail(self, plant_id, timespan, date=None): """ Get plant details for specified timespan. + + Args: + plant_id (str): The ID of the plant. + timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the plant details. + + Raises: + ValueError: If the timespan is not a valid Timespan. + Exception: If the request to the server fails. """ + if not isinstance(timespan, Timespan): + raise ValueError("Invalid timespan value") + date_str = self.__get_date_string(timespan, date) - response = self.session.get(self.get_url('PlantDetailAPI.do'), params={ + response = self.session.get(self.__get_url('PlantDetailAPI.do'), params={ 'plantId': plant_id, 'type': timespan.value, 'date': date_str }) - data = json.loads(response.content.decode('utf-8')) - return data['back'] + + if response.status_code == 200: + return response.json().get('back', {}) + else: + raise Exception('Failed to retrieve plant details') + + def plant_list_two(self): + """ + Get a list of all plants with detailed information. + + Returns: + list: A list of plants with detailed information. + """ + response = self.session.post( + self.__get_url('newTwoPlantAPI.do'), + params={'op': 'getAllPlantListTwo'}, + data={ + 'language': '1', + 'nominalPower': '', + 'order': '1', + 'pageSize': '15', + 'plantName': '', + 'plantStatus': '', + 'toPageNum': '1' + } + ) + + if response.status_code == 200: + data = response.json() + return data.get('PlantList', []) + else: + raise Exception('Failed to retrieve plant list') def inverter_data(self, inverter_id, date=None): """ Get inverter data for specified date or today. + + Args: + inverter_id (str): The ID of the inverter. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the inverter data. + + Raises: + Exception: If the request to the server fails. """ date_str = self.__get_date_string(date=date) - response = self.session.get(self.get_url('newInverterAPI.do'), params={ + response = self.session.get(self.__get_url('newInverterAPI.do'), params={ 'op': 'getInverterData', 'id': inverter_id, 'type': 1, 'date': date_str }) - data = json.loads(response.content.decode('utf-8')) - return data + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve inverter data') def inverter_detail(self, inverter_id): """ - Get "All parameters" from PV inverter. + Get detailed data from PV inverter. + + Args: + inverter_id (str): The ID of the inverter. + + Returns: + dict: A dictionary containing the inverter details. + + Raises: + Exception: If the request to the server fails. """ - response = self.session.get(self.get_url('newInverterAPI.do'), params={ + response = self.session.get(self.__get_url('newInverterAPI.do'), params={ 'op': 'getInverterDetailData', 'inverterId': inverter_id }) - data = json.loads(response.content.decode('utf-8')) - return data + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve inverter details') def inverter_detail_two(self, inverter_id): """ - Get "All parameters" from PV inverter. + Get detailed data from PV inverter (alternative endpoint). + + Args: + inverter_id (str): The ID of the inverter. + + Returns: + dict: A dictionary containing the inverter details. + + Raises: + Exception: If the request to the server fails. """ - response = self.session.get(self.get_url('newInverterAPI.do'), params={ + response = self.session.get(self.__get_url('newInverterAPI.do'), params={ 'op': 'getInverterDetailData_two', 'inverterId': inverter_id }) - data = json.loads(response.content.decode('utf-8')) - return data + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve inverter details') def tlx_data(self, tlx_id, date=None): """ - Get inverter data for specified date or today. + Get TLX inverter data for specified date or today. + + Args: + tlx_id (str): The ID of the TLX inverter. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the TLX inverter data. + + Raises: + Exception: If the request to the server fails. """ date_str = self.__get_date_string(date=date) - response = self.session.get(self.get_url('newTlxApi.do'), params={ + response = self.session.get(self.__get_url('newTlxApi.do'), params={ 'op': 'getTlxData', 'id': tlx_id, 'type': 1, 'date': date_str }) - data = json.loads(response.content.decode('utf-8')) - return data + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve TLX inverter data') def tlx_detail(self, tlx_id): """ - Get "All parameters" from PV inverter. + Get detailed data from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the detailed TLX inverter data. + + Raises: + Exception: If the request to the server fails. """ - response = self.session.get(self.get_url('newTlxApi.do'), params={ + response = self.session.get(self.__get_url('newTlxApi.do'), params={ 'op': 'getTlxDetailData', 'id': tlx_id }) - data = json.loads(response.content.decode('utf-8')) - return data + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve detailed TLX inverter data') + + def tlx_params(self, tlx_id): + """ + Get parameters for TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the TLX inverter parameters. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get(self.__get_url('newTlxApi.do'), params={ + 'op': 'getTlxParams', + 'id': tlx_id + }) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve TLX inverter parameters') + + def tlx_get_all_settings(self, tlx_id): + """ + Get all possible settings from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing all possible settings for the TLX inverter. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post(self.__get_url('newTlxApi.do'), params={ + 'op': 'getTlxSetData' + }, data={ + 'serialNum': tlx_id + }) + + if response.status_code == 200: + data = response.json() + return data['obj']['tlxSetBean'] + else: + raise Exception('Failed to retrieve TLX inverter settings') + + def tlx_get_enabled_settings(self, tlx_id): + """ + Get "Enabled settings" from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the enabled settings. + + Raises: + Exception: If the request to the server fails. + """ + string_time = datetime.datetime.now().strftime('%Y-%m-%d') + response = self.session.post( + self.__get_url('newLoginAPI.do'), + params={'op': 'getSetPass'}, + data={'deviceSn': tlx_id, 'stringTime': string_time, 'type': '5'} + ) + + if response.status_code == 200: + return response.json().get('obj', {}) + else: + raise Exception('Failed to retrieve enabled inverter settings') + + def get_battery_info(self, serial_num): + """ + Get battery information. + + Args: + serial_num (str): The serial number of the battery. + + Returns: + dict: A dictionary containing the battery information. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.__get_url('newTlxApi.do'), + params={'op': 'getBatInfo'}, + data={'lan': 1, 'serialNum': serial_num} + ) + + if response.status_code == 200: + return response.json().get('obj', {}) + else: + raise Exception('Failed to retrieve battery info') + + def get_battery_info_detailed(self, plant_id, serial_num): + """ + Get detailed battery information. + + Args: + plant_id (str): The ID of the plant. + serial_num (str): The serial number of the battery. + + Returns: + dict: A dictionary containing the detailed battery information. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.__get_url('newTlxApi.do'), + params={'op': 'getBatDetailData'}, + data={'lan': 1, 'plantId': plant_id, 'id': serial_num} + ) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to retrieve detailed battery info') def mix_info(self, mix_id, plant_id = None): """ @@ -268,9 +516,9 @@ def mix_info(self, mix_id, plant_id = None): if (plant_id): request_params['plantId'] = plant_id - response = self.session.get(self.get_url('newMixApi.do'), params=request_params) + response = self.session.get(self.__get_url('newMixApi.do'), params=request_params) - data = json.loads(response.content.decode('utf-8')) + data = response.json() return data['obj'] def mix_totals(self, mix_id, plant_id): @@ -296,13 +544,13 @@ def mix_totals(self, mix_id, plant_id): 'photovoltaicRevenueTotal' -- Revenue earned from PV total (all time) in 'unit' currency 'unit' -- Unit of currency for 'Revenue' """ - response = self.session.post(self.get_url('newMixApi.do'), params={ + response = self.session.post(self.__get_url('newMixApi.do'), params={ 'op': 'getEnergyOverview', 'mixId': mix_id, 'plantId': plant_id }) - data = json.loads(response.content.decode('utf-8')) + data = response.json() return data['obj'] def mix_system_status(self, mix_id, plant_id): @@ -339,13 +587,13 @@ def mix_system_status(self, mix_id, plant_id): 'vac1' -- Grid voltage in V (same as vAc1) 'wBatteryType' -- ??? 1 """ - response = self.session.post(self.get_url('newMixApi.do'), params={ + response = self.session.post(self.__get_url('newMixApi.do'), params={ 'op': 'getSystemStatus_KW', 'mixId': mix_id, 'plantId': plant_id }) - data = json.loads(response.content.decode('utf-8')) + data = response.json() return data['obj'] def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): @@ -399,15 +647,14 @@ def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): """ date_str = self.__get_date_string(timespan, date) - response = self.session.post(self.get_url('newMixApi.do'), params={ + response = self.session.post(self.__get_url('newMixApi.do'), params={ 'op': 'getEnergyProdAndCons_KW', 'plantId': plant_id, 'mixId': mix_id, 'type': timespan.value, 'date': date_str }) - data = json.loads(response.content.decode('utf-8')) - + data = response.json() return data['obj'] def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): @@ -458,50 +705,47 @@ def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): """ date_str = self.__get_date_string(timespan, date) - response = self.session.post(self.get_url('newPlantAPI.do'), params={ + response = self.session.post(self.__get_url('newPlantAPI.do'), params={ 'action': "getEnergyStorageData", 'date': date_str, 'type': timespan.value, 'plantId': plant_id }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() def storage_detail(self, storage_id): """ Get "All parameters" from battery storage. """ - response = self.session.get(self.get_url('newStorageAPI.do'), params={ + response = self.session.get(self.__get_url('newStorageAPI.do'), params={ 'op': 'getStorageInfo_sacolar', 'storageId': storage_id }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() def storage_params(self, storage_id): """ Get much more detail from battery storage. """ - response = self.session.get(self.get_url('newStorageAPI.do'), params={ + response = self.session.get(self.__get_url('newStorageAPI.do'), params={ 'op': 'getStorageParams_sacolar', 'storageId': storage_id }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() def storage_energy_overview(self, plant_id, storage_id): """ Get some energy/generation overview data. """ - response = self.session.post(self.get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ + response = self.session.post(self.__get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ 'plantId': plant_id, 'storageSn': storage_id }) - data = json.loads(response.content.decode('utf-8')) + data = response.json() return data['obj'] def inverter_list(self, plant_id): @@ -521,15 +765,45 @@ def plant_info(self, plant_id): """ Get basic plant information with device list. """ - response = self.session.get(self.get_url('newTwoPlantAPI.do'), params={ + response = self.session.get(self.__get_url('newTwoPlantAPI.do'), params={ 'op': 'getAllDeviceListTwo', 'plantId': plant_id, 'pageNum': 1, 'pageSize': 1 }) - data = json.loads(response.content.decode('utf-8')) - return data + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to get plant info') + + def get_all_devices(self, plant_id): + """ + Get basic plant information with device list. + """ + response = self.session.get(self.__get_url('newTwoPlantAPI.do'), + params={'op': 'getAllDeviceList', + 'plantId': plant_id, + 'language': 1}) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to get device data') + + def get_energy_data(self, plant_id): + """ + Get energy data + """ + response = self.session.post(self.__get_url('newTwoPlantAPI.do'), + params={'op': 'getUserCenterEnertyDataByPlantid'}, + data={ 'language': 1, + 'plantId': plant_id}) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to energy data') def get_plant_settings(self, plant_id): """ @@ -541,12 +815,16 @@ def get_plant_settings(self, plant_id): Returns: A python dictionary containing the settings for the specified plant """ - response = self.session.get(self.get_url('newPlantAPI.do'), params={ + response = self.session.get(self.__get_url('newPlantAPI.do'), params={ 'op': 'getPlant', 'plantId': plant_id }) - data = json.loads(response.content.decode('utf-8')) - return data + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to get plant settings') + def is_plant_noah_system(self, plant_id): """ @@ -565,11 +843,11 @@ def is_plant_noah_system(self, plant_id): 'deviceSn' -- Serial number of the configured noah device 'plantName' -- Friendly name of the plant """ - response = self.session.post(self.get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ + response = self.session.post(self.__get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ 'plantId': plant_id }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() + def noah_system_status(self, serial_number): """ @@ -599,11 +877,11 @@ def noah_system_status(self, serial_number): 'moneyUnit' -- Unit of currency e.g. '€' 'status' -- Is the noah device online (True or False) """ - response = self.session.post(self.get_url('noahDeviceApi/noah/getSystemStatus'), data={ + response = self.session.post(self.__get_url('noahDeviceApi/noah/getSystemStatus'), data={ 'deviceSn': serial_number }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() + def noah_info(self, serial_number): """ @@ -645,11 +923,11 @@ def noah_info(self, serial_number): 'plantImgName' -- Friendly name of the plant Image 'plantName' -- Friendly name of the plant """ - response = self.session.post(self.get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ + response = self.session.post(self.__get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ 'deviceSn': serial_number }) - data = json.loads(response.content.decode('utf-8')) - return data + return response.json() + def update_plant_settings(self, plant_id, changed_settings, current_settings = None): """ @@ -694,9 +972,12 @@ def update_plant_settings(self, plant_id, changed_settings, current_settings = N for setting, value in changed_settings.items(): form_settings[setting] = (None, str(value)) - response = self.session.post(self.get_url('newTwoPlantAPI.do?op=updatePlant'), files = form_settings) - data = json.loads(response.content.decode('utf-8')) - return data + response = self.session.post(self.__get_url('newTwoPlantAPI.do?op=updatePlant'), files = form_settings) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to update plant settings') def update_inverter_setting(self, serial_number, setting_type, default_parameters, parameters): @@ -724,10 +1005,13 @@ def update_inverter_setting(self, serial_number, setting_type, settings_parameters = {**default_parameters, **settings_parameters} - response = self.session.post(self.get_url('newTcpsetAPI.do'), + response = self.session.post(self.__get_url('newTcpsetAPI.do'), params=settings_parameters) - data = json.loads(response.content.decode('utf-8')) - return data + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to update inverter settings') def update_mix_inverter_setting(self, serial_number, setting_type, parameters): """ @@ -773,6 +1057,78 @@ def update_ac_inverter_setting(self, serial_number, setting_type, parameters): return self.update_inverter_setting(serial_number, setting_type, default_parameters, parameters) + def update_tlx_inverter_time_segment(self, serial_number, segment_id, batt_mode, start_time, end_time, enabled): + """ + Updates the time segment settings for a TLX hybrid inverter. + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + segment_id -- ID of the time segment to be updated (int) + batt_mode -- Battery mode (int) + start_time -- Start time of the segment (datetime.time) + end_time -- End time of the segment (datetime.time) + enabled -- Whether the segment is enabled (bool) + + Returns: + JSON response from the server whether the configuration was successful + """ + params = { + 'op': 'tlxSet' + } + data = { + 'serialNum': serial_number, + 'type': f'time_segment{segment_id}', + 'param1': batt_mode, + 'param2': start_time.strftime('%H'), + 'param3': start_time.strftime('%M'), + 'param4': end_time.strftime('%H'), + 'param5': end_time.strftime('%M'), + 'param6': '1' if enabled else '0' + } + + print(f"Params: {params}") + print(f"Data: {data}") + + response = self.session.post(self.__get_url('newTcpsetAPI.do'), params=params, data=data) + result = response.json() + + print(f"Response: {result}") + + if not result.get('success', False): + raise Exception(f"Failed to update TLX inverter time segment: {result.get('msg', 'Unknown error')}") + + return result + + def update_tlx_inverter_setting(self, serial_number, setting_type, parameter): + """ + Alias for setting parameters on a tlx hybrid inverter + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + setting_type -- Setting to be configured (str) + parameter -- Parameter(s) to be sent to the system (str, dict, list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + default_parameters = { + 'op': 'tlxSet', + 'serialNum': serial_number, + 'type': setting_type + } + + # If parameter is a single value, convert it to a dictionary + if not isinstance(parameter, (dict, list)): + parameter = {'param1': parameter} + elif isinstance(parameter, list): + parameter = {f'param{index+1}': param for index, param in enumerate(parameter)} + + return self.update_inverter_setting(serial_number, setting_type, + default_parameters, parameter) + + def update_noah_settings(self, serial_number, setting_type, parameters): """ Applies settings for specified noah device based on serial number @@ -801,7 +1157,10 @@ def update_noah_settings(self, serial_number, setting_type, parameters): settings_parameters = {**default_parameters, **settings_parameters} - response = self.session.post(self.get_url('noahDeviceApi/noah/set'), + response = self.session.post(self.__get_url('noahDeviceApi/noah/set'), data=settings_parameters) - data = json.loads(response.content.decode('utf-8')) - return data \ No newline at end of file + + return response.json() + + + From 2c5a0ead53e2c734f223ecd9dd6ff5c77a481106 Mon Sep 17 00:00:00 2001 From: johanzander Date: Sun, 29 Sep 2024 01:00:35 +0200 Subject: [PATCH 02/13] updated version to 1.6.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f58534a..1452ae7 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setuptools.setup( name="growattServer", - version="1.5.0", + version="1.6.0", author="IndyKoning", author_email="indykoningnl@gmail.com", description="A package to talk to growatt server", From 5bd07fec4cdad0d7e46106b6ecdcd32224269421 Mon Sep 17 00:00:00 2001 From: johanzander Date: Sun, 29 Sep 2024 23:11:36 +0200 Subject: [PATCH 03/13] Added more functions: get_energy_data(), tlx_get_system_status(), tlx_get_energy_overview(), tlx_get_energy_prod_con() --- README.md | 8 +++ examples/tlx_example.py | 30 ++++++--- growattServer/__init__.py | 136 +++++++++++++++++++++++++++++++------- 3 files changed, 142 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index e6e99d8..056617b 100755 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ Any methods that may be useful. `api.plant_detail(plant_id, timespan<1=day, 2=month>, date)` Get details of a specific plant. +`api.get_energy_data(plant_id)` Get energy data for the specified plant. + `api.inverter_list(plant_id)` Get a list of inverters in specified plant. (May be deprecated in the future, since it gets all devices. Use `device_list` instead). `api.device_list(plant_id)` Get a list of devices in specified plant. @@ -38,6 +40,12 @@ Any methods that may be useful. `api.inverter_detail(inverter_id)` Get detailed data on inverter. +`api.tlx_get_system_status(plant_id, tlx_id)` Get system status. + +`api.tlx_get_energy_overview(plant_id, tlx_id)` Get energy overview of the system. + +`api.tlx_get_energy_prod_cons(plant_id, tlx_id)` Get energy production and consumption for the system. + `api.tlx_data(tlx_id, date)` Get some basic data of a specific date for the tlx type inverter. `api.tlx_detail(tlx_id)` Get detailed data on a tlx type inverter. diff --git a/examples/tlx_example.py b/examples/tlx_example.py index 9f01c5e..a8939ef 100644 --- a/examples/tlx_example.py +++ b/examples/tlx_example.py @@ -41,31 +41,43 @@ plant_info = api.plant_info(plant_id) print("Plant info:", json.dumps(plant_info, indent=4, sort_keys=True)) -# Energy data +# Energy data (used in the 'Plant' Tab) energy_data = api.get_energy_data(plant_id) -print("Energy data", json.dumps(energy_data, indent=4, sort_keys=True)) +print("Plant Energy data", json.dumps(energy_data, indent=4, sort_keys=True)) # Devices devices = api.get_all_devices(plant_id) print("Devices:", json.dumps(devices, indent=4, sort_keys=True)) -for device in devices['deviceList']: +for device in devices: if device['deviceType'] == 'tlx': - # Inverter info + # Inverter info (used in inverter view) inverter_sn = device['deviceSn'] inverter_info = api.tlx_params(inverter_sn) - print("TLX inverter info:", json.dumps(inverter_info, indent=4, sort_keys=True)) + print("Inverter info:", json.dumps(inverter_info, indent=4, sort_keys=True)) - # Data + # PV production data data = api.tlx_data(inverter_sn, datetime.datetime.now()) - print("TLX data:", json.dumps(data, indent=4, sort_keys=True)) + print("PV production data:", json.dumps(data, indent=4, sort_keys=True)) - # Settings + # System settings all_settings = api.tlx_get_all_settings(inverter_sn) enabled_settings = api.tlx_get_enabled_settings(inverter_sn) enabled_keys = enabled_settings['enable'].keys() available_settings = {k: v for k, v in all_settings.items() if k in enabled_keys} - print("Settings:", json.dumps(available_settings, indent=4, sort_keys=True)) + print("System settings:", json.dumps(available_settings, indent=4, sort_keys=True)) + + # System status + data = api.tlx_get_system_status(plant_id, inverter_sn) + print("System status:", json.dumps(data, indent=4, sort_keys=True)) + + # Energy overview + data = api.tlx_get_energy_overview(plant_id, inverter_sn) + print("Energy overview:", json.dumps(data, indent=4, sort_keys=True)) + + # Energy production & consumption + data = api.tlx_get_energy_prod_cons(plant_id, inverter_sn) + print("Energy production & consumption:", json.dumps(data, indent=4, sort_keys=True)) elif device['deviceType'] == 'bat': # Battery info diff --git a/growattServer/__init__.py b/growattServer/__init__.py index ef8101d..aa6b8d9 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -305,6 +305,95 @@ def inverter_detail_two(self, inverter_id): else: raise Exception('Failed to retrieve inverter details') + def tlx_get_system_status(self, plant_id, tlx_id): + """ + Get status of the system + + Args: + plant_id (str): The ID of the plant. + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing system status. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.__get_url("newTlxApi.do"), + params={"op": "getSystemStatus_KW"}, + data={"plantId": plant_id, + "id": tlx_id} + ) + + if response.status_code == 200: + return response.json()['obj'] + else: + raise Exception("Failed to retrieve system status") + + def tlx_get_energy_overview(self, plant_id, tlx_id): + """ + Get energy overview + + Args: + plant_id (str): The ID of the plant. + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing energy data. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.__get_url("newTlxApi.do"), + params={"op": "getEnergyOverview"}, + data={"plantId": plant_id, + "id": tlx_id} + ) + + if response.status_code == 200: + return response.json()['obj'] + else: + raise Exception("Failed to retrieve energy data") + + def tlx_get_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): + """ + Get energy production and consumption (KW) + + Args: + tlx_id (str): The ID of the TLX inverter. + timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. + date (datetime): The date you are interested in. + + Returns: + dict: A dictionary containing energy data. + + Raises: + ValueError: If the timespan is not a valid Timespan. + Exception: If the request to the server fails. + """ + if not isinstance(timespan, Timespan): + raise ValueError("Invalid timespan value") + + if date is None: + date = datetime.datetime.now().strftime("%Y-%m-%d") + + response = self.session.post( + self.__get_url("newTlxApi.do"), + params={"op": "getEnergyProdAndCons_KW"}, + data={'date': date, + "plantId": plant_id, + "language": "1", + "id": tlx_id, + "type": timespan.value} + ) + + if response.status_code == 200: + return response.json()['obj'] + else: + raise Exception("Failed to retrieve energy production/consumption") + def tlx_data(self, tlx_id, date=None): """ Get TLX inverter data for specified date or today. @@ -702,6 +791,8 @@ def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): 'ratio4' -- % of 'Load consumption' that is imported from the grid e.g '50.2%' (not accurate for Mix systems) 'ratio5' -- % of Self consumption that is from batteries e.g. '92.1%' (not accurate for Mix systems) 'ratio6' -- % of Self consumption that is directly from Solar e.g. '7.9%' (not accurate for Mix systems) + + NOTE: Does not return any data for a tlx system. Use get_energy_data() instead. """ date_str = self.__get_date_string(timespan, date) @@ -714,6 +805,26 @@ def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): return response.json() + def get_plant_settings(self, plant_id): + """ + Returns a dictionary containing the settings for the specified plant + + Keyword arguments: + plant_id -- The id of the plant you want the settings of + + Returns: + A python dictionary containing the settings for the specified plant + """ + response = self.session.get(self.__get_url('newPlantAPI.do'), params={ + 'op': 'getPlant', + 'plantId': plant_id + }) + + if response.status_code == 200: + return response.json() + else: + raise Exception('Failed to get plant settings') + def storage_detail(self, storage_id): """ Get "All parameters" from battery storage. @@ -787,13 +898,13 @@ def get_all_devices(self, plant_id): 'language': 1}) if response.status_code == 200: - return response.json() + return response.json()['deviceList'] else: raise Exception('Failed to get device data') def get_energy_data(self, plant_id): """ - Get energy data + Get the energy data used in the 'Plant' tab in the phone """ response = self.session.post(self.__get_url('newTwoPlantAPI.do'), params={'op': 'getUserCenterEnertyDataByPlantid'}, @@ -804,27 +915,6 @@ def get_energy_data(self, plant_id): return response.json() else: raise Exception('Failed to energy data') - - def get_plant_settings(self, plant_id): - """ - Returns a dictionary containing the settings for the specified plant - - Keyword arguments: - plant_id -- The id of the plant you want the settings of - - Returns: - A python dictionary containing the settings for the specified plant - """ - response = self.session.get(self.__get_url('newPlantAPI.do'), params={ - 'op': 'getPlant', - 'plantId': plant_id - }) - - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to get plant settings') - def is_plant_noah_system(self, plant_id): """ From 7679e01ac318082cbed6105281576b7849ea632b Mon Sep 17 00:00:00 2001 From: johanzander Date: Tue, 5 Nov 2024 23:36:35 +0100 Subject: [PATCH 04/13] Applied suggestions from code review Co-authored-by: indykoning <15870933+indykoning@users.noreply.github.com> --- growattServer/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/growattServer/__init__.py b/growattServer/__init__.py index aa6b8d9..229abe1 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -63,7 +63,7 @@ def __get_date_string(self, timespan=None, date=None): return date_str - def __get_url(self, page): + def get_url(self, page): """ Simple helper function to get the page URL. """ @@ -305,7 +305,7 @@ def inverter_detail_two(self, inverter_id): else: raise Exception('Failed to retrieve inverter details') - def tlx_get_system_status(self, plant_id, tlx_id): + def tlx_system_status(self, tlx_id, plant_id): """ Get status of the system @@ -331,7 +331,7 @@ def tlx_get_system_status(self, plant_id, tlx_id): else: raise Exception("Failed to retrieve system status") - def tlx_get_energy_overview(self, plant_id, tlx_id): + def tlx_energy_overview(self, plant_id, tlx_id): """ Get energy overview @@ -357,7 +357,7 @@ def tlx_get_energy_overview(self, plant_id, tlx_id): else: raise Exception("Failed to retrieve energy data") - def tlx_get_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): + def tlx_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): """ Get energy production and consumption (KW) @@ -517,7 +517,7 @@ def tlx_get_enabled_settings(self, tlx_id): else: raise Exception('Failed to retrieve enabled inverter settings') - def get_battery_info(self, serial_num): + def tlx_battery_info(self, serial_num): """ Get battery information. @@ -541,7 +541,7 @@ def get_battery_info(self, serial_num): else: raise Exception('Failed to retrieve battery info') - def get_battery_info_detailed(self, plant_id, serial_num): + def tlx_battery_details(self, plant_id, serial_num): """ Get detailed battery information. @@ -902,7 +902,7 @@ def get_all_devices(self, plant_id): else: raise Exception('Failed to get device data') - def get_energy_data(self, plant_id): + def plant_energy_data(self, plant_id): """ Get the energy data used in the 'Plant' tab in the phone """ From 46413f591b7d47f6e13bb04a7fc76baab6bcf3c8 Mon Sep 17 00:00:00 2001 From: johanzander Date: Thu, 7 Nov 2024 21:22:52 +0100 Subject: [PATCH 05/13] Updates after review comments Updates after review comments --- README.md | 22 +-- examples/tlx_example.py | 27 ++-- growattServer/__init__.py | 301 ++++++++++++++------------------------ 3 files changed, 136 insertions(+), 214 deletions(-) diff --git a/README.md b/README.md index 056617b..f6e1e3c 100755 --- a/README.md +++ b/README.md @@ -28,9 +28,11 @@ Any methods that may be useful. `api.plant_info(plant_id)` Get info for specified plant. +`api.plant_settings(plant_id)` Get the current settings for the specified plant + `api.plant_detail(plant_id, timespan<1=day, 2=month>, date)` Get details of a specific plant. -`api.get_energy_data(plant_id)` Get energy data for the specified plant. +`api.plant_energy_data(plant_id)` Get energy data for the specified plant. `api.inverter_list(plant_id)` Get a list of inverters in specified plant. (May be deprecated in the future, since it gets all devices. Use `device_list` instead). @@ -40,11 +42,11 @@ Any methods that may be useful. `api.inverter_detail(inverter_id)` Get detailed data on inverter. -`api.tlx_get_system_status(plant_id, tlx_id)` Get system status. +`api.tlx_system_status(plant_id, tlx_id)` Get system status. -`api.tlx_get_energy_overview(plant_id, tlx_id)` Get energy overview of the system. +`api.tlx_energy_overview(plant_id, tlx_id)` Get energy overview of the system. -`api.tlx_get_energy_prod_cons(plant_id, tlx_id)` Get energy production and consumption for the system. +`api.tlx_energy_prod_cons(plant_id, tlx_id)` Get energy production and consumption for the system. `api.tlx_data(tlx_id, date)` Get some basic data of a specific date for the tlx type inverter. @@ -56,9 +58,9 @@ Any methods that may be useful. `api.tlx_get_enabled_settings(tlx_id)` Get all enabled settings for the tlx type inverter. -`api.get_battery_info(serial_num)` Get battery info. Uses the tlx API, so might only work for tlx type inverter. +`api.tlx_battery_info(serial_num)` Get battery info for tlx systems. -`api.get_battery_info_detailed(serial_num)` Get detailed battery info. Uses the tlx API, so might only work for tlx type inverter. +`api.tlx_battery_info_detailed(serial_num)` Get detailed battery info. `api.mix_info(mix_id, plant_id=None)` Get high level information about the Mix system including daily and overall totals. NOTE: `plant_id` is an optional parameter, it does not appear to be used by the remote API, but is used by the mobile app these calls were reverse-engineered from. @@ -76,8 +78,6 @@ Any methods that may be useful. `api.storage_energy_overview(plant_id, storage_id)` Get the information you see in the "Generation overview". -`api.get_plant_settings(plant_id)` Get the current settings for the specified plant - `api.is_plant_noah_system(plant_id)` Get the Information if noah devices are configured for the specified plant `api.noah_system_status(serial_number)` Get the current status for the specified noah device e.g. workMode, soc, chargePower, disChargePower, current import/export etc. @@ -221,11 +221,13 @@ Known working settings & parameters are as follows (all parameter values are str * param1: Charge Stop SOC * type: `discharge_power` * param1: Discharging power % (value between 0 and 100) + * type: `on_grid_discharge_stop_soc` + * param1: On-grid discharge Stop SOC * type: `discharge_stop_soc` - * param1: Discharge Stop SOC + * param1: Off-grid discharge Stop SOC * type: `ac_charge` * param1: Allow AC (grid) charging (0 = Disabled, 1 = Enabled) - * type: `pf_sys_year` + * type: `pf_sys_year` * param1: datetime in format: `YYYY-MM-DD HH:MM:SS` * function: `api.update_tlx_inverter_time_segment` * segment_id: The segment to update (1-9) diff --git a/examples/tlx_example.py b/examples/tlx_example.py index a8939ef..6488cee 100644 --- a/examples/tlx_example.py +++ b/examples/tlx_example.py @@ -22,10 +22,10 @@ # """ -#Prompt user for username +# Prompt user for username username=input("Enter username:") -#Prompt user to input password +# Prompt user to input password user_pass=getpass.getpass("Enter password:") user_agent = 'ShinePhone/8.1.17 (iPhone; iOS 15.6.1; Scale/2.00)' @@ -42,11 +42,11 @@ print("Plant info:", json.dumps(plant_info, indent=4, sort_keys=True)) # Energy data (used in the 'Plant' Tab) -energy_data = api.get_energy_data(plant_id) +energy_data = api.plant_energy_data(plant_id) print("Plant Energy data", json.dumps(energy_data, indent=4, sort_keys=True)) # Devices -devices = api.get_all_devices(plant_id) +devices = api.device_list(plant_id) print("Devices:", json.dumps(devices, indent=4, sort_keys=True)) for device in devices: @@ -61,36 +61,39 @@ print("PV production data:", json.dumps(data, indent=4, sort_keys=True)) # System settings - all_settings = api.tlx_get_all_settings(inverter_sn) - enabled_settings = api.tlx_get_enabled_settings(inverter_sn) + all_settings = api.tlx_all_settings(inverter_sn) + enabled_settings = api.tlx_enabled_settings(inverter_sn) + # 'on_grid_discharge_stop_soc' is present in web UI, but for some reason not + # returned in enabled settings so we enable it manually here instead + enabled_settings['enable']['on_grid_discharge_stop_soc'] = '1' enabled_keys = enabled_settings['enable'].keys() available_settings = {k: v for k, v in all_settings.items() if k in enabled_keys} print("System settings:", json.dumps(available_settings, indent=4, sort_keys=True)) # System status - data = api.tlx_get_system_status(plant_id, inverter_sn) + data = api.tlx_system_status(plant_id, inverter_sn) print("System status:", json.dumps(data, indent=4, sort_keys=True)) # Energy overview - data = api.tlx_get_energy_overview(plant_id, inverter_sn) + data = api.tlx_energy_overview(plant_id, inverter_sn) print("Energy overview:", json.dumps(data, indent=4, sort_keys=True)) # Energy production & consumption - data = api.tlx_get_energy_prod_cons(plant_id, inverter_sn) + data = api.tlx_energy_prod_cons(plant_id, inverter_sn) print("Energy production & consumption:", json.dumps(data, indent=4, sort_keys=True)) elif device['deviceType'] == 'bat': # Battery info - batt_info = api.get_battery_info(device['deviceSn']) + batt_info = api.tlx_battery_info(device['deviceSn']) print("Battery info:", json.dumps(batt_info, indent=4, sort_keys=True)) - batt_info_detailed = api.get_battery_info_detailed(plant_id, device['deviceSn']) + batt_info_detailed = api.tlx_battery_info_detailed(plant_id, device['deviceSn']) print("Battery info: detailed", json.dumps(batt_info_detailed, indent=4, sort_keys=True)) # Examples of updating settings, uncomment to use # Set charging power to 95% -#res = api.update_tlx_inverter_setting(inverter_sn, 'charge_power', 96) +#res = api.update_tlx_inverter_setting(inverter_sn, 'charge_power', 95) #print(res) # Turn on AC charging diff --git a/growattServer/__init__.py b/growattServer/__init__.py index aa6b8d9..cc53527 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1,12 +1,11 @@ -name = "growattServer" - import datetime from enum import IntEnum -import hashlib -import json import requests -import warnings from random import randint +import warnings +import hashlib + +name = "growattServer" BATT_MODE_LOAD_FIRST = 0 BATT_MODE_BATTERY_FIRST = 1 @@ -50,10 +49,10 @@ def __init__(self, add_random_user_id=False, agent_identifier=None): def __get_date_string(self, timespan=None, date=None): if timespan is not None: - assert timespan in Timespan + assert timespan in Timespan if date is None: - date = datetime.datetime.now() + date = datetime.datetime.now() date_str="" if timespan == Timespan.month: @@ -63,7 +62,7 @@ def __get_date_string(self, timespan=None, date=None): return date_str - def __get_url(self, page): + def get_url(self, page): """ Simple helper function to get the page URL. """ @@ -132,21 +131,18 @@ def login(self, username, password, is_password_hashed=False): if not is_password_hashed: password = hash_password(password) - response = self.session.post(self.__get_url('newTwoLoginAPI.do'), data={ + response = self.session.post(self.get_url('newTwoLoginAPI.do'), data={ 'userName': username, 'password': password }) - if response.status_code == 200: - data = response.json()['back'] - if data['success']: - data.update({ - 'userId': data['user']['id'], - 'userLevel': data['user']['rightlevel'] - }) - return data - else: - raise Exception('Login failed') + data = response.json()['back'] + if data['success']: + data.update({ + 'userId': data['user']['id'], + 'userLevel': data['user']['rightlevel'] + }) + return data def plant_list(self, user_id): """ @@ -162,16 +158,12 @@ def plant_list(self, user_id): Exception: If the request to the server fails. """ response = self.session.get( - self.__get_url('PlantListAPI.do'), + self.get_url('PlantListAPI.do'), params={'userId': user_id}, allow_redirects=False ) - if response.status_code == 200: - data = response.json() - return data.get('back', []) - else: - raise Exception('Failed to retrieve plant list') + return response.json().get('back', []) def plant_detail(self, plant_id, timespan, date=None): """ @@ -186,24 +178,17 @@ def plant_detail(self, plant_id, timespan, date=None): dict: A dictionary containing the plant details. Raises: - ValueError: If the timespan is not a valid Timespan. Exception: If the request to the server fails. """ - if not isinstance(timespan, Timespan): - raise ValueError("Invalid timespan value") - date_str = self.__get_date_string(timespan, date) - response = self.session.get(self.__get_url('PlantDetailAPI.do'), params={ + response = self.session.get(self.get_url('PlantDetailAPI.do'), params={ 'plantId': plant_id, 'type': timespan.value, 'date': date_str }) - if response.status_code == 200: - return response.json().get('back', {}) - else: - raise Exception('Failed to retrieve plant details') + return response.json().get('back', {}) def plant_list_two(self): """ @@ -213,7 +198,7 @@ def plant_list_two(self): list: A list of plants with detailed information. """ response = self.session.post( - self.__get_url('newTwoPlantAPI.do'), + self.get_url('newTwoPlantAPI.do'), params={'op': 'getAllPlantListTwo'}, data={ 'language': '1', @@ -226,11 +211,7 @@ def plant_list_two(self): } ) - if response.status_code == 200: - data = response.json() - return data.get('PlantList', []) - else: - raise Exception('Failed to retrieve plant list') + return response.json().get('PlantList', []) def inverter_data(self, inverter_id, date=None): """ @@ -247,17 +228,14 @@ def inverter_data(self, inverter_id, date=None): Exception: If the request to the server fails. """ date_str = self.__get_date_string(date=date) - response = self.session.get(self.__get_url('newInverterAPI.do'), params={ + response = self.session.get(self.get_url('newInverterAPI.do'), params={ 'op': 'getInverterData', 'id': inverter_id, 'type': 1, 'date': date_str }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve inverter data') + return response.json() def inverter_detail(self, inverter_id): """ @@ -272,15 +250,12 @@ def inverter_detail(self, inverter_id): Raises: Exception: If the request to the server fails. """ - response = self.session.get(self.__get_url('newInverterAPI.do'), params={ + response = self.session.get(self.get_url('newInverterAPI.do'), params={ 'op': 'getInverterDetailData', 'inverterId': inverter_id }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve inverter details') + return response.json() def inverter_detail_two(self, inverter_id): """ @@ -295,17 +270,14 @@ def inverter_detail_two(self, inverter_id): Raises: Exception: If the request to the server fails. """ - response = self.session.get(self.__get_url('newInverterAPI.do'), params={ + response = self.session.get(self.get_url('newInverterAPI.do'), params={ 'op': 'getInverterDetailData_two', 'inverterId': inverter_id }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve inverter details') + return response.json() - def tlx_get_system_status(self, plant_id, tlx_id): + def tlx_system_status(self, plant_id, tlx_id): """ Get status of the system @@ -320,18 +292,15 @@ def tlx_get_system_status(self, plant_id, tlx_id): Exception: If the request to the server fails. """ response = self.session.post( - self.__get_url("newTlxApi.do"), + self.get_url("newTlxApi.do"), params={"op": "getSystemStatus_KW"}, data={"plantId": plant_id, "id": tlx_id} ) - if response.status_code == 200: - return response.json()['obj'] - else: - raise Exception("Failed to retrieve system status") + return response.json().get('obj', {}) - def tlx_get_energy_overview(self, plant_id, tlx_id): + def tlx_energy_overview(self, plant_id, tlx_id): """ Get energy overview @@ -346,18 +315,15 @@ def tlx_get_energy_overview(self, plant_id, tlx_id): Exception: If the request to the server fails. """ response = self.session.post( - self.__get_url("newTlxApi.do"), + self.get_url("newTlxApi.do"), params={"op": "getEnergyOverview"}, data={"plantId": plant_id, "id": tlx_id} ) - if response.status_code == 200: - return response.json()['obj'] - else: - raise Exception("Failed to retrieve energy data") + return response.json().get('obj', {}) - def tlx_get_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): + def tlx_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): """ Get energy production and consumption (KW) @@ -370,29 +336,22 @@ def tlx_get_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, dat dict: A dictionary containing energy data. Raises: - ValueError: If the timespan is not a valid Timespan. Exception: If the request to the server fails. """ - if not isinstance(timespan, Timespan): - raise ValueError("Invalid timespan value") - if date is None: - date = datetime.datetime.now().strftime("%Y-%m-%d") + date_str = self.__get_date_string(timespan, date) response = self.session.post( - self.__get_url("newTlxApi.do"), + self.get_url("newTlxApi.do"), params={"op": "getEnergyProdAndCons_KW"}, - data={'date': date, + data={'date': date_str, "plantId": plant_id, "language": "1", "id": tlx_id, "type": timespan.value} ) - if response.status_code == 200: - return response.json()['obj'] - else: - raise Exception("Failed to retrieve energy production/consumption") + return response.json().get('obj', {}) def tlx_data(self, tlx_id, date=None): """ @@ -409,17 +368,14 @@ def tlx_data(self, tlx_id, date=None): Exception: If the request to the server fails. """ date_str = self.__get_date_string(date=date) - response = self.session.get(self.__get_url('newTlxApi.do'), params={ + response = self.session.get(self.get_url('newTlxApi.do'), params={ 'op': 'getTlxData', 'id': tlx_id, 'type': 1, 'date': date_str }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve TLX inverter data') + return response.json() def tlx_detail(self, tlx_id): """ @@ -434,15 +390,12 @@ def tlx_detail(self, tlx_id): Raises: Exception: If the request to the server fails. """ - response = self.session.get(self.__get_url('newTlxApi.do'), params={ + response = self.session.get(self.get_url('newTlxApi.do'), params={ 'op': 'getTlxDetailData', 'id': tlx_id }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve detailed TLX inverter data') + return response.json() def tlx_params(self, tlx_id): """ @@ -457,17 +410,14 @@ def tlx_params(self, tlx_id): Raises: Exception: If the request to the server fails. """ - response = self.session.get(self.__get_url('newTlxApi.do'), params={ + response = self.session.get(self.get_url('newTlxApi.do'), params={ 'op': 'getTlxParams', 'id': tlx_id }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve TLX inverter parameters') + return response.json() - def tlx_get_all_settings(self, tlx_id): + def tlx_all_settings(self, tlx_id): """ Get all possible settings from TLX inverter. @@ -480,19 +430,15 @@ def tlx_get_all_settings(self, tlx_id): Raises: Exception: If the request to the server fails. """ - response = self.session.post(self.__get_url('newTlxApi.do'), params={ + response = self.session.post(self.get_url('newTlxApi.do'), params={ 'op': 'getTlxSetData' }, data={ 'serialNum': tlx_id }) - if response.status_code == 200: - data = response.json() - return data['obj']['tlxSetBean'] - else: - raise Exception('Failed to retrieve TLX inverter settings') + return response.json().get('obj', {}).get('tlxSetBean') - def tlx_get_enabled_settings(self, tlx_id): + def tlx_enabled_settings(self, tlx_id): """ Get "Enabled settings" from TLX inverter. @@ -507,17 +453,14 @@ def tlx_get_enabled_settings(self, tlx_id): """ string_time = datetime.datetime.now().strftime('%Y-%m-%d') response = self.session.post( - self.__get_url('newLoginAPI.do'), + self.get_url('newLoginAPI.do'), params={'op': 'getSetPass'}, data={'deviceSn': tlx_id, 'stringTime': string_time, 'type': '5'} ) - if response.status_code == 200: - return response.json().get('obj', {}) - else: - raise Exception('Failed to retrieve enabled inverter settings') + return response.json().get('obj', {}) - def get_battery_info(self, serial_num): + def tlx_battery_info(self, serial_num): """ Get battery information. @@ -531,17 +474,14 @@ def get_battery_info(self, serial_num): Exception: If the request to the server fails. """ response = self.session.post( - self.__get_url('newTlxApi.do'), + self.get_url('newTlxApi.do'), params={'op': 'getBatInfo'}, data={'lan': 1, 'serialNum': serial_num} ) - if response.status_code == 200: - return response.json().get('obj', {}) - else: - raise Exception('Failed to retrieve battery info') + return response.json().get('obj', {}) - def get_battery_info_detailed(self, plant_id, serial_num): + def tlx_battery_info_detailed(self, plant_id, serial_num): """ Get detailed battery information. @@ -556,15 +496,12 @@ def get_battery_info_detailed(self, plant_id, serial_num): Exception: If the request to the server fails. """ response = self.session.post( - self.__get_url('newTlxApi.do'), + self.get_url('newTlxApi.do'), params={'op': 'getBatDetailData'}, data={'lan': 1, 'plantId': plant_id, 'id': serial_num} ) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to retrieve detailed battery info') + return response.json() def mix_info(self, mix_id, plant_id = None): """ @@ -605,10 +542,9 @@ def mix_info(self, mix_id, plant_id = None): if (plant_id): request_params['plantId'] = plant_id - response = self.session.get(self.__get_url('newMixApi.do'), params=request_params) + response = self.session.get(self.get_url('newMixApi.do'), params=request_params) - data = response.json() - return data['obj'] + return response.json().get('obj', {}) def mix_totals(self, mix_id, plant_id): """ @@ -633,14 +569,13 @@ def mix_totals(self, mix_id, plant_id): 'photovoltaicRevenueTotal' -- Revenue earned from PV total (all time) in 'unit' currency 'unit' -- Unit of currency for 'Revenue' """ - response = self.session.post(self.__get_url('newMixApi.do'), params={ + response = self.session.post(self.get_url('newMixApi.do'), params={ 'op': 'getEnergyOverview', 'mixId': mix_id, 'plantId': plant_id }) - data = response.json() - return data['obj'] + return response.json().get('obj', {}) def mix_system_status(self, mix_id, plant_id): """ @@ -676,14 +611,13 @@ def mix_system_status(self, mix_id, plant_id): 'vac1' -- Grid voltage in V (same as vAc1) 'wBatteryType' -- ??? 1 """ - response = self.session.post(self.__get_url('newMixApi.do'), params={ + response = self.session.post(self.get_url('newMixApi.do'), params={ 'op': 'getSystemStatus_KW', 'mixId': mix_id, 'plantId': plant_id }) - data = response.json() - return data['obj'] + return response.json().get('obj', {}) def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): """ @@ -736,15 +670,15 @@ def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): """ date_str = self.__get_date_string(timespan, date) - response = self.session.post(self.__get_url('newMixApi.do'), params={ + response = self.session.post(self.get_url('newMixApi.do'), params={ 'op': 'getEnergyProdAndCons_KW', 'plantId': plant_id, 'mixId': mix_id, 'type': timespan.value, 'date': date_str }) - data = response.json() - return data['obj'] + + return response.json().get('obj', {}) def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): """ @@ -792,11 +726,11 @@ def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): 'ratio5' -- % of Self consumption that is from batteries e.g. '92.1%' (not accurate for Mix systems) 'ratio6' -- % of Self consumption that is directly from Solar e.g. '7.9%' (not accurate for Mix systems) - NOTE: Does not return any data for a tlx system. Use get_energy_data() instead. + NOTE: Does not return any data for a tlx system. Use plant_energy_data() instead. """ date_str = self.__get_date_string(timespan, date) - response = self.session.post(self.__get_url('newPlantAPI.do'), params={ + response = self.session.post(self.get_url('newPlantAPI.do'), params={ 'action': "getEnergyStorageData", 'date': date_str, 'type': timespan.value, @@ -805,7 +739,7 @@ def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): return response.json() - def get_plant_settings(self, plant_id): + def plant_settings(self, plant_id): """ Returns a dictionary containing the settings for the specified plant @@ -815,21 +749,18 @@ def get_plant_settings(self, plant_id): Returns: A python dictionary containing the settings for the specified plant """ - response = self.session.get(self.__get_url('newPlantAPI.do'), params={ + response = self.session.get(self.get_url('newPlantAPI.do'), params={ 'op': 'getPlant', 'plantId': plant_id }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to get plant settings') + return response.json() def storage_detail(self, storage_id): """ Get "All parameters" from battery storage. """ - response = self.session.get(self.__get_url('newStorageAPI.do'), params={ + response = self.session.get(self.get_url('newStorageAPI.do'), params={ 'op': 'getStorageInfo_sacolar', 'storageId': storage_id }) @@ -840,7 +771,7 @@ def storage_params(self, storage_id): """ Get much more detail from battery storage. """ - response = self.session.get(self.__get_url('newStorageAPI.do'), params={ + response = self.session.get(self.get_url('newStorageAPI.do'), params={ 'op': 'getStorageParams_sacolar', 'storageId': storage_id }) @@ -851,13 +782,12 @@ def storage_energy_overview(self, plant_id, storage_id): """ Get some energy/generation overview data. """ - response = self.session.post(self.__get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ + response = self.session.post(self.get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ 'plantId': plant_id, 'storageSn': storage_id }) - data = response.json() - return data['obj'] + return response.json().get('obj', {}) def inverter_list(self, plant_id): """ @@ -866,55 +796,53 @@ def inverter_list(self, plant_id): warnings.warn("This function may be deprecated in the future because naming is not correct, use device_list instead", DeprecationWarning) return self.device_list(plant_id) + def __get_all_devices(self, plant_id): + """ + Get basic plant information with device list. + """ + response = self.session.get(self.get_url('newTwoPlantAPI.do'), + params={'op': 'getAllDeviceList', + 'plantId': plant_id, + 'language': 1}) + + return response.json().get('deviceList', {}) + def device_list(self, plant_id): """ Get a list of all devices connected to plant. """ - return self.plant_info(plant_id)['deviceList'] + + device_list = self.plant_info(plant_id).get('deviceList', []) + + if not device_list: + # for tlx systems, the device_list in plant is empty, so use __get_all_devices() instead + device_list = self.__get_all_devices(plant_id) + + return device_list def plant_info(self, plant_id): """ Get basic plant information with device list. """ - response = self.session.get(self.__get_url('newTwoPlantAPI.do'), params={ + response = self.session.get(self.get_url('newTwoPlantAPI.do'), params={ 'op': 'getAllDeviceListTwo', 'plantId': plant_id, 'pageNum': 1, 'pageSize': 1 }) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to get plant info') - - def get_all_devices(self, plant_id): - """ - Get basic plant information with device list. - """ - response = self.session.get(self.__get_url('newTwoPlantAPI.do'), - params={'op': 'getAllDeviceList', - 'plantId': plant_id, - 'language': 1}) - - if response.status_code == 200: - return response.json()['deviceList'] - else: - raise Exception('Failed to get device data') + return response.json() - def get_energy_data(self, plant_id): + def plant_energy_data(self, plant_id): """ Get the energy data used in the 'Plant' tab in the phone """ - response = self.session.post(self.__get_url('newTwoPlantAPI.do'), + response = self.session.post(self.get_url('newTwoPlantAPI.do'), params={'op': 'getUserCenterEnertyDataByPlantid'}, data={ 'language': 1, 'plantId': plant_id}) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to energy data') + return response.json() def is_plant_noah_system(self, plant_id): """ @@ -933,7 +861,7 @@ def is_plant_noah_system(self, plant_id): 'deviceSn' -- Serial number of the configured noah device 'plantName' -- Friendly name of the plant """ - response = self.session.post(self.__get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ + response = self.session.post(self.get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ 'plantId': plant_id }) return response.json() @@ -967,7 +895,7 @@ def noah_system_status(self, serial_number): 'moneyUnit' -- Unit of currency e.g. '€' 'status' -- Is the noah device online (True or False) """ - response = self.session.post(self.__get_url('noahDeviceApi/noah/getSystemStatus'), data={ + response = self.session.post(self.get_url('noahDeviceApi/noah/getSystemStatus'), data={ 'deviceSn': serial_number }) return response.json() @@ -1013,7 +941,7 @@ def noah_info(self, serial_number): 'plantImgName' -- Friendly name of the plant Image 'plantName' -- Friendly name of the plant """ - response = self.session.post(self.__get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ + response = self.session.post(self.get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ 'deviceSn': serial_number }) return response.json() @@ -1027,14 +955,14 @@ def update_plant_settings(self, plant_id, changed_settings, current_settings = N Keyword arguments: plant_id -- The id of the plant you wish to update the settings for changed_settings -- A python dictionary containing the settings to be changed and their value - current_settings -- A python dictionary containing the current settings of the plant (use the response from get_plant_settings), if None - fetched for you + current_settings -- A python dictionary containing the current settings of the plant (use the response from plant_settings), if None - fetched for you Returns: A response from the server stating whether the configuration was successful or not """ #If no existing settings have been provided then get them from the growatt server if current_settings == None: - current_settings = self.get_plant_settings(plant_id) + current_settings = self.plant_settings(plant_id) #These are the parameters that the form requires, without these an error is thrown. Pre-populate their values with the current values form_settings = { @@ -1062,12 +990,9 @@ def update_plant_settings(self, plant_id, changed_settings, current_settings = N for setting, value in changed_settings.items(): form_settings[setting] = (None, str(value)) - response = self.session.post(self.__get_url('newTwoPlantAPI.do?op=updatePlant'), files = form_settings) + response = self.session.post(self.get_url('newTwoPlantAPI.do?op=updatePlant'), files = form_settings) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to update plant settings') + return response.json() def update_inverter_setting(self, serial_number, setting_type, default_parameters, parameters): @@ -1095,13 +1020,10 @@ def update_inverter_setting(self, serial_number, setting_type, settings_parameters = {**default_parameters, **settings_parameters} - response = self.session.post(self.__get_url('newTcpsetAPI.do'), + response = self.session.post(self.get_url('newTcpsetAPI.do'), params=settings_parameters) - if response.status_code == 200: - return response.json() - else: - raise Exception('Failed to update inverter settings') + return response.json() def update_mix_inverter_setting(self, serial_number, setting_type, parameters): """ @@ -1175,15 +1097,10 @@ def update_tlx_inverter_time_segment(self, serial_number, segment_id, batt_mode, 'param5': end_time.strftime('%M'), 'param6': '1' if enabled else '0' } - - print(f"Params: {params}") - print(f"Data: {data}") - response = self.session.post(self.__get_url('newTcpsetAPI.do'), params=params, data=data) + response = self.session.post(self.get_url('newTcpsetAPI.do'), params=params, data=data) result = response.json() - print(f"Response: {result}") - if not result.get('success', False): raise Exception(f"Failed to update TLX inverter time segment: {result.get('msg', 'Unknown error')}") @@ -1247,7 +1164,7 @@ def update_noah_settings(self, serial_number, setting_type, parameters): settings_parameters = {**default_parameters, **settings_parameters} - response = self.session.post(self.__get_url('noahDeviceApi/noah/set'), + response = self.session.post(self.get_url('noahDeviceApi/noah/set'), data=settings_parameters) return response.json() From c6aacf20f485e9437a4e4a7f295aab7517afe538 Mon Sep 17 00:00:00 2001 From: johanzander Date: Sat, 9 Nov 2024 00:43:58 +0100 Subject: [PATCH 06/13] added tlx example showing key energy and power metrics. --- examples/tlx_example_dashboard.py | 127 ++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 examples/tlx_example_dashboard.py diff --git a/examples/tlx_example_dashboard.py b/examples/tlx_example_dashboard.py new file mode 100644 index 0000000..1c93528 --- /dev/null +++ b/examples/tlx_example_dashboard.py @@ -0,0 +1,127 @@ + +import growattServer +import getpass + +# Example script fetching key power and today+total energy metrics from a Growatt MID-30KTL3-XH (TLX) + APX battery hybrid system +# +# There is a lot of overlap in what the various Growatt APIs returns. +# tlx_detail() contains the bulk of the needed data, but some info is missing and is fetched from +# tlx_system_status(), tlx_energy_overview() and tlx_battery_info_detailed() instead + + +# Prompt user for username +username=input("Enter username:") + +# Prompt user to input password +user_pass=getpass.getpass("Enter password:") + +# Login, emulating the Growatt app +user_agent = 'ShinePhone/8.1.17 (iPhone; iOS 15.6.1; Scale/2.00)' +api = growattServer.GrowattApi(agent_identifier=user_agent) +login_response = api.login(username, user_pass) +if not login_response['success']: + print(f"Failed to log in, msg: {login_response['msg']}, error: {login_response['error']}") + exit() + +# Get plant(s) +plant_list = api.plant_list_two() +plant_id = plant_list[0]['id'] + +# Get devices in plant +devices = api.device_list(plant_id) + +# Iterate over all devices. Here we are interested in data from 'tlx' inverters and 'bat' devices +batteries_info = [] +for device in devices: + if device['deviceType'] == 'tlx': + inverter_sn = device['deviceSn'] + + # Inverter detail, contains the bulk of energy and power values + inverter_detail = api.tlx_detail(inverter_sn).get('data') + + # Energy overview is used to retrieve "epvToday" which is not present in tlx_detail() for some reason + energy_overview = api.tlx_energy_overview(plant_id, inverter_sn) + + # System status, contains power values, not available in inverter_detail() + system_status = api.tlx_system_status(plant_id, inverter_sn) + + if device['deviceType'] == 'bat': + batt_info = api.tlx_battery_info(device['deviceSn']) + if batt_info.get('lost'): + # Disconnected batteries are listed with 'old' power/energy/SOC data + # Therefore we check it it's 'lost' and skip it in that case. + print("'Lost' battery found, skipping") + continue + + # Battery info + batt_info = api.tlx_battery_info_detailed(plant_id, device['deviceSn']).get('data') + + if float(batt_info['chargeOrDisPower']) > 0: + bdcChargePower = float(batt_info['chargeOrDisPower']) + bdcDischargePower = 0 + else: + bdcChargePower = 0 + bdcDischargePower = float(batt_info['chargeOrDisPower']) + bdcDischargePower = -bdcDischargePower + + battery_data = { + 'serialNum': device['deviceSn'], + 'bdcChargePower': bdcChargePower, + 'bdcDischargePower': bdcDischargePower, + 'dischargeTotal': batt_info['dischargeTotal'], + 'soc': batt_info['soc'] + } + batteries_info.append(battery_data) + + +solar_production = f'{float(energy_overview["epvToday"]):.1f}/{float(energy_overview["epvTotal"]):.1f}' +solar_production_pv1 = f'{float(inverter_detail["epv1Today"]):.1f}/{float(inverter_detail["epv1Total"]):.1f}' +solar_production_pv2 = f'{float(inverter_detail["epv2Today"]):.1f}/{float(inverter_detail["epv2Total"]):.1f}' +energy_output = f'{float(inverter_detail["eacToday"]):.1f}/{float(inverter_detail["eacTotal"]):.1f}' +system_production = f'{float(inverter_detail["esystemToday"]):.1f}/{float(inverter_detail["esystemTotal"]):.1f}' +battery_charged = f'{float(inverter_detail["echargeToday"]):.1f}/{float(inverter_detail["echargeTotal"]):.1f}' +battery_grid_charge = f'{float(inverter_detail["eacChargeToday"]):.1f}/{float(inverter_detail["eacChargeTotal"]):.1f}' +battery_discharged = f'{float(inverter_detail["edischargeToday"]):.1f}/{float(inverter_detail["edischargeTotal"]):.1f}' +exported_to_grid = f'{float(inverter_detail["etoGridToday"]):.1f}/{float(inverter_detail["etoGridTotal"]):.1f}' +imported_from_grid = f'{float(inverter_detail["etoUserToday"]):.1f}/{float(inverter_detail["etoUserTotal"]):.1f}' +load_consumption = f'{float(inverter_detail["elocalLoadToday"]):.1f}/{float(inverter_detail["elocalLoadTotal"]):.1f}' +self_consumption = f'{float(inverter_detail["eselfToday"]):.1f}/{float(inverter_detail["eselfTotal"]):.1f}' +battery_charged = f'{float(inverter_detail["echargeToday"]):.1f}/{float(inverter_detail["echargeTotal"]):.1f}' + +print("\nGeneration overview Today/Total(kWh)") +print(f'Solar production {solar_production:>22}') +print(f' Solar production, PV1 {solar_production_pv1:>22}') +print(f' Solar production, PV2 {solar_production_pv2:>22}') +print(f'Energy Output {energy_output:>22}') +print(f'System production {system_production:>22}') +print(f'Self consumption {self_consumption:>22}') +print(f'Load consumption {load_consumption:>22}') +print(f'Battery Charged {battery_charged:>22}') +print(f' Charged from grid {battery_grid_charge:>22}') +print(f'Battery Discharged {battery_discharged:>22}') +print(f'Import from grid {imported_from_grid:>22}') +print(f'Export to grid {exported_to_grid:>22}') + +print("\nPower overview (Watts)") +print(f'AC Power {float(inverter_detail["pac"]):>22.1f}') +print(f'Self power {float(inverter_detail["pself"]):>22.1f}') +print(f'Export power {float(inverter_detail["pacToGridTotal"]):>22.1f}') +print(f'Import power {float(inverter_detail["pacToUserTotal"]):>22.1f}') +print(f'Local load power {float(inverter_detail["pacToLocalLoad"]):>22.1f}') +print(f'PV power {float(inverter_detail["psystem"]):>22.1f}') +print(f'PV #1 power {float(inverter_detail["ppv1"]):>22.1f}') +print(f'PV #2 power {float(inverter_detail["ppv2"]):>22.1f}') +print(f'Battery charge power {float(system_status["chargePower"])*1000:>22.1f}') +if len(batteries_info) > 0: + print(f'Batt #1 charge power {float(batteries_info[0]["bdcChargePower"]):>22.1f}') +if len(batteries_info) > 1: + print(f'Batt #2 charge power {float(batteries_info[1]["bdcChargePower"]):>22.1f}') +print(f'Battery discharge power {float(system_status["pdisCharge"])*1000:>18.1f}') +if len(batteries_info) > 0: + print(f'Batt #1 discharge power {float(batteries_info[0]["bdcDischargePower"]):>22.1f}') +if len(batteries_info) > 1: + print(f'Batt #2 discharge power {float(batteries_info[1]["bdcDischargePower"]):>22.1f}') +if len(batteries_info) > 0: + print(f'Batt #1 SOC {int(batteries_info[0]["soc"]):>21}%') +if len(batteries_info) > 1: + print(f'Batt #2 SOC {int(batteries_info[1]["soc"]):>21}%') From a85818dd64357dd6bc43a6bab8867349c9a2e4ac Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Fri, 14 Mar 2025 17:01:42 +0000 Subject: [PATCH 07/13] Added support and examples for min/tlx inverters using the public growatt v1 API --- README.md | 43 ++- examples/min_example.py | 93 ++++++ examples/min_example_dashboard.py | 93 ++++++ examples/tlx_example_dashboard.py | 2 +- growattServer/__init__.py | 532 +++++++++++++++++++++++++++++- setup.py | 2 +- 6 files changed, 751 insertions(+), 14 deletions(-) create mode 100644 examples/min_example.py create mode 100644 examples/min_example_dashboard.py diff --git a/README.md b/README.md index f6e1e3c..ef70dd3 100755 --- a/README.md +++ b/README.md @@ -5,8 +5,14 @@ Package to retrieve PV information from the growatt server. Special thanks to [Sjoerd Langkemper](https://github.com/Sjord) who has provided a strong base to start off from https://github.com/Sjord/growatt_api_client These projects may merge in the future since they are simmilar in code and function. +In addition to the reverse-engineered APIs used by ShinePhone this library now also support the public Growatt API (v1) for MIN systems (TLX are identified as MIN system in the public API). Certain endpoints are not supported anymore by openapi.growatt.com. For example `api.min_write_parameter()` should be used instead of old `api.update_tlx_inverter_setting()`. + ## Usage +# Classic API + +Uses username/password basic authentication + ```python import growattServer @@ -16,16 +22,32 @@ login_response = api.login(, ) print(api.plant_list(login_response['user']['id'])) ``` +# Public API + +The public v1 API requires token based authentication + +```python +import growattServer + +api = growattServer.GrowattApi(token="YOUR_API_TOKEN") +#Get a list of growatt plants. +plants = api.plant_list_v1() +print(plants) +``` + + ## Methods and Variables ### Methods Any methods that may be useful. -`api.login(username, password)` Log into the growatt API. This must be done before making any request. After this you will be logged in. You will want to capture the response to get the `userId` variable. +`api.login(username, password)` Log into the growatt API. This must be done before making any request. After this you will be logged in. You will want to capture the response to get the `userId` variable. Should not be used for public v1 APIs. `api.plant_list(user_id)` Get a list of plants registered to your account. +`api.plant_list_v1()` Get a list of plants registered to your account, using public v1 API. + `api.plant_info(plant_id)` Get info for specified plant. `api.plant_settings(plant_id)` Get the current settings for the specified plant @@ -38,6 +60,8 @@ Any methods that may be useful. `api.device_list(plant_id)` Get a list of devices in specified plant. +`api.device_list_v1(plant_id)` Get a list of devices in specified plant using the public v1 API. + `api.inverter_data(inverter_id, date)` Get some basic data of a specific date for the inverter. `api.inverter_detail(inverter_id)` Get detailed data on inverter. @@ -96,6 +120,23 @@ Any methods that may be useful. `api.update_noah_settings(serial_number, setting_type, parameters)` Applies the provided parameters (dictionary or array) for the specified setting on the specified noah device; see 'Noah settings' below for more information +`api.min_energy(device_sn)` Get current energy data for a min inverter, including power and energy values. + +`api.min_detail(device_sn)` Get detailed data for a min inverter. + +`api.min_energy_history(device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None)` Get energy history data for a min inverter (7-day max range). + +`api.min_settings(device_sn)` Get all settings for a min inverter. + +`api.min_read_parameter(device_sn, parameter_id, start_address=None, end_address=None)` Read a specific setting for a min inverter. + +`api.min_write_parameter(device_sn, parameter_id, parameter_values)` Set parameters on a min inverter. Parameter values can be a single value, a list, or a dictionary. + +`api.min_write_time_segment(device_sn, segment_id, batt_mode, start_time, end_time, enabled=True)` Update a specific time segment for a min inverter. + +`api.min_read_time_segments(device_sn, settings_data=None)` Read all time segments from a MIN inverter. Optionally pass settings_data to avoid redundant API calls. + + ### Variables Some variables you may want to set. diff --git a/examples/min_example.py b/examples/min_example.py new file mode 100644 index 0000000..d18beee --- /dev/null +++ b/examples/min_example.py @@ -0,0 +1,93 @@ +import growattServer +import datetime +import json +import os + +""" +# 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 + +# Initialize the API with token instead of using login +api = growattServer.GrowattApi(token=api_token) + +# Plant info +plant_list = api.plant_list_v1() # Use V1 endpoint +if plant_list['error_code'] != 0: + print(f"Failed to get plant list, error: {plant_list['error_msg']}") + exit() + +print(f"Plants: Found {plant_list['data']['count']} plants") +plant_id = plant_list['data']['plants'][0]['plant_id'] + +# Devices +devices_response = api.device_list_v1(plant_id) +if devices_response['error_code'] != 0: + print(f"Failed to get devices, error: {plant_list['error_msg']}") + exit() + +for device in devices_response['data']['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] + + # Get device details using v1 API + inverter_detail_response = api.min_detail(inverter_sn) + if inverter_detail_response['error_code'] == 0: + inverter_data = inverter_detail_response['data'] + 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 using v1 API + energy_response = api.min_energy(device_sn=inverter_sn) + if energy_response['error_code'] == 0: + energy_data = energy_response['data'] + 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 details from v1 API + energy_history_response = api.min_energy_history(inverter_sn) + if energy_history_response['error_code'] == 0: + energy_history_data = energy_history_response['data']['datas'] + print("Saving energy history data to energy_history.json") + with open('energy_history.json', 'w') as f: + json.dump(energy_history_data, f, indent=4, sort_keys=True) + + # Get settings using v1 API + settings_response = api.min_settings(device_sn=inverter_sn) + if settings_response['error_code'] == 0: + settings_data = settings_response['data'] + with open('settings_data.json', 'w') as f: + json.dump(settings_data, f, indent=4, sort_keys=True) + + tou = api.min_read_time_segments(inverter_sn, settings_response) + print(json.dumps(tou, indent=4)) + + # Example of reading individual parameter + res = api.min_read_parameter(inverter_sn, 'discharge_power') + print("Current discharge power: ", res) + + ## Settings parameters. Uncomment to test + + # Turn on AC charging +# res = api.min_write_parameter(inverter_sn, 'ac_charge', 1) +# print("AC charging enabled: ", res) + + # Enable Load First between 00:00 and 11:59 using time segment 1 +# res = api.min_write_time_segment( +# device_sn=inverter_sn, +# segment_id=1, +# batt_mode=growattServer.BATT_MODE_LOAD_FIRST, +# start_time=datetime.time(0, 0), +# end_time=datetime.time(00, 59), +# enabled=True +# ) +# print(res) + diff --git a/examples/min_example_dashboard.py b/examples/min_example_dashboard.py new file mode 100644 index 0000000..1cf6413 --- /dev/null +++ b/examples/min_example_dashboard.py @@ -0,0 +1,93 @@ +import growattServer +import json + +""" +Example script fetching key power and today+total energy metrics from a Growatt MID-30KTL3-XH (TLX) + APX battery hybrid system +using the V1 API with token-based authentication. +""" + +# 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 + +# Initialize the API with token +api = growattServer.GrowattApi(token=api_token) + +# Get plant list using V1 API +plant_response = api.plant_list_v1() +if plant_response['error_code'] != 0: + print(f"Failed to get plants, error: {plant_response['error_msg']}") + exit() + +plant_id = plant_response['data']['plants'][0]['plant_id'] + +# Get devices in plant using V1 API +devices_response = api.device_list_v1(plant_id) +if devices_response['error_code'] != 0: + print(f"Failed to get devices, error: {devices_response['error_msg']}") + exit() + +# Iterate over all devices +energy_data = None +for device in devices_response['data']['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] + + # Get energy data using new API + energy_response = api.min_energy(device_sn=inverter_sn) + if energy_response['error_code'] == 0: + energy_data = energy_response['data'] + with open('energy_data.json', 'w') as f: + json.dump(energy_data, f, indent=4, sort_keys=True) + +# Ensure we have the needed data +if not energy_data: + print("Could not retrieve necessary data from the inverter") + exit() + +#energy data does not contain epvToday for some reason, so we need to calculate it +epv_today = energy_data["epv1Today"] + energy_data["epv2Today"] + +solar_production = f'{float(epv_today):.1f}/{float(energy_data["epvTotal"]):.1f}' +solar_production_pv1 = f'{float(energy_data["epv1Today"]):.1f}/{float(energy_data["epv1Total"]):.1f}' +solar_production_pv2 = f'{float(energy_data["epv2Today"]):.1f}/{float(energy_data["epv2Total"]):.1f}' +energy_output = f'{float(energy_data["eacToday"]):.1f}/{float(energy_data["eacTotal"]):.1f}' +system_production = f'{float(energy_data["esystemToday"]):.1f}/{float(energy_data["esystemTotal"]):.1f}' +battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' +battery_grid_charge = f'{float(energy_data["eacChargeToday"]):.1f}/{float(energy_data["eacChargeTotal"]):.1f}' +battery_discharged = f'{float(energy_data["edischargeToday"]):.1f}/{float(energy_data["edischargeTotal"]):.1f}' +exported_to_grid = f'{float(energy_data["etoGridToday"]):.1f}/{float(energy_data["etoGridTotal"]):.1f}' +imported_from_grid = f'{float(energy_data["etoUserToday"]):.1f}/{float(energy_data["etoUserTotal"]):.1f}' +load_consumption = f'{float(energy_data["elocalLoadToday"]):.1f}/{float(energy_data["elocalLoadTotal"]):.1f}' +self_consumption = f'{float(energy_data["eselfToday"]):.1f}/{float(energy_data["eselfTotal"]):.1f}' +battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' + +# Output the dashboard +print("\nGeneration overview Today/Total(kWh)") +print(f'Solar production {solar_production:>22}') +print(f' Solar production, PV1 {solar_production_pv1:>22}') +print(f' Solar production, PV2 {solar_production_pv2:>22}') +print(f'Energy Output {energy_output:>22}') +print(f'System production {system_production:>22}') +print(f'Self consumption {self_consumption:>22}') +print(f'Load consumption {load_consumption:>22}') +print(f'Battery Charged {battery_charged:>22}') +print(f' Charged from grid {battery_grid_charge:>22}') +print(f'Battery Discharged {battery_discharged:>22}') +print(f'Import from grid {imported_from_grid:>22}') +print(f'Export to grid {exported_to_grid:>22}') + +print("\nPower overview (Watts)") +print(f'AC Power {float(energy_data["pac"]):>22.1f}') +print(f'Self power {float(energy_data["pself"]):>22.1f}') +print(f'Export power {float(energy_data["pacToGridTotal"]):>22.1f}') +print(f'Import power {float(energy_data["pacToUserTotal"]):>22.1f}') +print(f'Local load power {float(energy_data["pacToLocalLoad"]):>22.1f}') +print(f'PV power {float(energy_data["ppv"]):>22.1f}') +print(f'PV #1 power {float(energy_data["ppv1"]):>22.1f}') +print(f'PV #2 power {float(energy_data["ppv2"]):>22.1f}') +print(f'Battery charge power {float(energy_data["bdc1ChargePower"]):>22.1f}') +print(f'Battery discharge power {float(energy_data["bdc1DischargePower"]):>22.1f}') +print(f'Battery SOC {int(energy_data["soc1"]):>21}%') diff --git a/examples/tlx_example_dashboard.py b/examples/tlx_example_dashboard.py index 1c93528..14a7962 100644 --- a/examples/tlx_example_dashboard.py +++ b/examples/tlx_example_dashboard.py @@ -108,7 +108,7 @@ print(f'Export power {float(inverter_detail["pacToGridTotal"]):>22.1f}') print(f'Import power {float(inverter_detail["pacToUserTotal"]):>22.1f}') print(f'Local load power {float(inverter_detail["pacToLocalLoad"]):>22.1f}') -print(f'PV power {float(inverter_detail["psystem"]):>22.1f}') +print(f'PV power {float(inverter_detail["ppv"]):>22.1f}') print(f'PV #1 power {float(inverter_detail["ppv1"]):>22.1f}') print(f'PV #2 power {float(inverter_detail["ppv2"]):>22.1f}') print(f'Battery charge power {float(system_status["chargePower"])*1000:>22.1f}') diff --git a/growattServer/__init__.py b/growattServer/__init__.py index cc53527..5b40a04 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1,4 +1,5 @@ import datetime +from datetime import date, timedelta from enum import IntEnum import requests from random import randint @@ -30,22 +31,42 @@ class GrowattApi: server_url = 'https://openapi.growatt.com/' agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)" - def __init__(self, add_random_user_id=False, agent_identifier=None): - if (agent_identifier != None): - self.agent_identifier = agent_identifier - - #If a random user id is required, generate a 5 digit number and add it to the user agent - if (add_random_user_id): - random_number = ''.join(["{}".format(randint(0,9)) for num in range(0,5)]) - self.agent_identifier += " - " + random_number - + def __init__(self, add_random_user_id=False, agent_identifier=None, token=None): + """ + Initialize the Growatt API client. + + Args: + add_random_user_id (bool): Add a random user ID to the agent identifier. + agent_identifier (str): Override the default agent identifier. + token (str): API token for authentication (use this for V1 API access). + username (str): Username for login-based authentication. + password (str): Password for login-based authentication. + is_password_hashed (bool): Whether the provided password is already hashed. + """ + self.api_url = f"{self.server_url}v1/" + self.token = token + self.v1_api_enabled = token is not None self.session = requests.Session() self.session.hooks = { 'response': lambda response, *args, **kwargs: response.raise_for_status() } - headers = {'User-Agent': self.agent_identifier} - self.session.headers.update(headers) + # Set up authentication + if token: + print("Using token-based authentication") + # Use token-based auth for V1 API + self.session.headers.update({"token": token}) + else: + print("Using password-based authentication") + if agent_identifier is not None: + self.agent_identifier = agent_identifier + headers = {'User-Agent': self.agent_identifier} + self.session.headers.update(headers) + # If a random user id is required, generate a 5 digit number and add it to the user agent + if add_random_user_id: + random_number = ''.join(["{}".format(randint(0, 9)) for num in range(0, 5)]) + self.agent_identifier += " - " + random_number + def __get_date_string(self, timespan=None, date=None): if timespan is not None: @@ -68,6 +89,12 @@ def get_url(self, page): """ return self.server_url + page + def get_v1_url(self, page): + """ + Simple helper function to get the page URL for v1 API. + """ + return self.api_url + page + def login(self, username, password, is_password_hashed=False): """ Log the user in. @@ -1170,4 +1197,487 @@ def update_noah_settings(self, serial_number, setting_type, parameters): return response.json() + def plant_list_v1(self): + """ + Get a list of all plants with detailed information. + + Returns: + list: A list of plants with detailed information. + """ + response = self.session.get( + url=self.get_v1_url('plant/list'), + data={ + 'page': '', + 'perpage': '', + 'search_type': '', + 'search_keyword': '' + } + ) + + return response.json() + + def device_list_v1(self, plant_id): + """ + Get devices associated with plant. + + Note: + returned "device_type" mappings: + 1: inverter (including MAX) + 2: storage + 3: other + 4: max (single MAX) + 5: sph + 6: spa + 7: min (including TLX) + 8: pcs + 9: hps + 10: pbd + + Args: + plant_id (int): Power Station ID + + Returns: + DeviceList + e.g. + { + "data": { + "count": 3, + "devices": [ + { + "device_sn": "ZT00100001", + "last_update_time": "2018-12-13 11:03:52", + "model": "A0B0D0T0PFU1M3S4", + "lost": True, + "status": 0, + "manufacturer": "Growatt", + "device_id": 116, + "datalogger_sn": "CRAZT00001", + "type": 1 + }, + ] + }, + "error_code": 0, + "error_msg": "" + } + """ + response = self.session.get( + url=self.get_v1_url("device/list"), + params={ + "plant_id": plant_id, + "page": "", + "perpage": "", + }, + ) + return response.json() + + + def min_detail(self, device_sn): + """ + Get detailed data for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter details. + + Raises: + Exception: If the request to the server fails. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + response = self.session.get( + self.get_v1_url('device/tlx/tlx_data_info'), + params={ + 'device_sn': device_sn + } + ) + + return response.json() + + def min_energy(self, device_sn): + """ + Get energy data for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter energy data. + + Raises: + Exception: If the request to the server fails. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + response = self.session.post( + url=self.get_v1_url("device/tlx/tlx_last_data"), + data={ + "tlx_sn": device_sn, + }, + ) + + return response.json() + + def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None): + """ + Get MIN inverter data history. + + Args: + device_sn (str): The ID of the MIN inverter. + start_date (date, optional): Start date. Defaults to today. + end_date (date, optional): End date. Defaults to today. + timezone (str, optional): Timezone ID. + page (int, optional): Page number. + limit (int, optional): Results per page. + + Returns: + dict: A dictionary containing the MIN inverter history data. + + Raises: + Exception: If the request to the server fails. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + if start_date is None and end_date is None: + start_date = date.today() + end_date = date.today() + elif start_date is None: + start_date = end_date + elif end_date is None: + end_date = start_date + + # check interval validity + if end_date - start_date > timedelta(days=7): + raise ValueError("date interval must not exceed 7 days") + + response = self.session.post( + url=self.get_v1_url('device/tlx/tlx_data'), + data={ + "tlx_sn": device_sn, + "start_date": start_date.strftime("%Y-%m-%d"), + "end_date": end_date.strftime("%Y-%m-%d"), + "timezone_id": timezone, + "page": page, + "perpage": limit, + } + ) + return response.json() + + def min_settings(self, device_sn): + """ + Get settings for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter settings. + + Raises: + Exception: If the request to the server fails. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + response = self.session.get( + self.get_v1_url('device/tlx/tlx_set_info'), + params={ + 'device_sn': device_sn + } + ) + + return response.json() + + def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_address=None): + """ + Read setting from MIN inverter. + + Args: + device_sn (str): The ID of the TLX inverter. + parameter_id (str): Parameter ID to read. Don't use start_address and end_address if this is set. + start_address (int, optional): Register start address (for set_any_reg). Don't use parameter_id if this is set. + end_address (int, optional): Register end address (for set_any_reg). Don't use parameter_id if this is set. + + Returns: + dict: A dictionary containing the setting value. + + Raises: + Exception: If the request to the server fails. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return None + + if parameter_id is None and start_address is None: + raise ValueError("specify either parameter_id or start_address/end_address") + elif parameter_id is not None and start_address is not None: + raise ValueError( + "specify either parameter_id or start_address/end_address - not both." + ) + elif parameter_id is not None: + # named parameter + start_address = 0 + end_address = 0 + else: + # using register-number mode + parameter_id = "set_any_reg" + if start_address is None: + start_address = end_address + if end_address is None: + end_address = start_address + + + response = self.session.post( + self.get_v1_url('readMinParam'), + data = { + "device_sn": device_sn, + "paramId": parameter_id, + "startAddr": start_address, + "endAddr": end_address, + } + ) + + return response.json() + + def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): + """ + Set parameters on a MIN inverter. + + Args: + device_sn (str): Serial number of the inverter + parameter_id (str): Setting type to be configured + parameter_values: Parameter values to be sent to the system. + Can be a single string (for param1 only), + a list of strings (for sequential params), + or a dictionary mapping param positions to values + + Returns: + dict: JSON response from the server + + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + # Initialize all parameters as empty strings + parameters = {i: "" for i in range(1, 20)} + + # Process parameter values based on type + if parameter_values is not None: + if isinstance(parameter_values, (str, int, float, bool)): + # Single value goes to param1 + parameters[1] = str(parameter_values) + elif isinstance(parameter_values, list): + # List of values go to sequential params + for i, value in enumerate(parameter_values, 1): + if i <= 19: # Only use up to 19 parameters + parameters[i] = str(value) + elif isinstance(parameter_values, dict): + # Dict maps param positions to values + for pos, value in parameter_values.items(): + pos = int(pos) if not isinstance(pos, int) else pos + if 1 <= pos <= 19: # Validate parameter positions + parameters[pos] = str(value) + + # IMPORTANT: Create a data dictionary with ALL parameters explicitly included + request_data = { + "tlx_sn": device_sn, + "type": parameter_id + } + + # Add all 19 parameters to the request + for i in range(1, 20): + request_data[f"param{i}"] = str(parameters[i]) + + # Send the request + response = self.session.post( + self.get_v1_url('tlxSet'), + data=request_data + ) + + return response.json() + + def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, end_time, enabled=True): + """ + Set a time segment for a MIN inverter. + + Args: + device_sn (str): The serial number of the inverter. + segment_id (int): Time segment ID (1-9). + batt_mode (int): 0=load priority, 1=battery priority, 2=grid priority. + start_time (datetime.time): Start time for the segment. + end_time (datetime.time): End time for the segment. + enabled (bool): Whether this segment is enabled. + + Returns: + dict: The server response. + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + if not 1 <= segment_id <= 9: + raise ValueError("segment_id must be between 1 and 9") + + if not 0 <= batt_mode <= 2: + raise ValueError("batt_mode must be between 0 and 2") + + # Initialize ALL 19 parameters as empty strings, not just the ones we need + all_params = { + "tlx_sn": device_sn, + "type": f"time_segment{segment_id}" + } + + # Add param1 through param19, setting the values we need + all_params["param1"] = str(batt_mode) + all_params["param2"] = str(start_time.hour) + all_params["param3"] = str(start_time.minute) + all_params["param4"] = str(end_time.hour) + all_params["param5"] = str(end_time.minute) + all_params["param6"] = "1" if enabled else "0" + + # Add empty strings for all unused parameters + for i in range(7, 20): + all_params[f"param{i}"] = "" + + # Send the request + response = self.session.post( + self.get_v1_url('tlxSet'), + data=all_params + ) + + return response.json() + + def min_read_time_segments(self, device_sn, settings_data=None): + """ + Read Time-of-Use (TOU) settings from a Growatt MIN/TLX inverter. + + Retrieves all 9 time segments from a Growatt MIN/TLX inverter and + parses them into a structured format. + + Args: + device_sn (str): The device serial number of the inverter + settings_data (dict, optional): Settings data from min_settings call to avoid repeated API calls. + Can be either the complete response or just the data portion. + + Returns: + list: A list of dictionaries, each containing details for one time segment: + - segment_id (int): The segment number (1-9) + - batt_mode (int): 0=Load First, 1=Battery First, 2=Grid First + - mode_name (str): String representation of the mode + - start_time (str): Start time in format "HH:MM" + - end_time (str): End time in format "HH:MM" + - enabled (bool): Whether the segment is enabled + + Example: + api = GrowattApi(token="your_api_token") + + # Option 1: Make a single call + tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER") + + # Option 2: Reuse existing settings data + settings_response = api.min_settings("DEVICE_SERIAL_NUMBER") + tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER", settings_response) + + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return [] + + # Process the settings data + if settings_data is None: + # Fetch settings if not provided + settings_response = self.min_settings(device_sn=device_sn) + if settings_response.get('error_code', 1) != 0: + print(f"Failed to get settings, error: {settings_response.get('error_msg', 'Unknown error')}") + return [] + settings_data = settings_response.get('data', {}) + else: + # Check if we were given the full API response or just the data portion + if 'error_code' in settings_data and 'data' in settings_data: + # This is the full API response + if settings_data['error_code'] != 0: + print(f"Settings data contains an error: {settings_data.get('error_msg', 'Unknown error')}") + return [] + settings_data = settings_data.get('data', {}) + # If it's just the data portion, use it directly (nothing to do) + + # Define mode names + mode_names = { + 0: "Load First", + 1: "Battery First", + 2: "Grid First" + } + + segments = [] + + # Process each time segment + for i in range(1, 10): # Segments 1-9 + # Get raw time values + start_time_raw = settings_data.get(f'forcedTimeStart{i}', "0:0") + end_time_raw = settings_data.get(f'forcedTimeStop{i}', "0:0") + + # Handle 'null' string values + if start_time_raw == 'null' or not start_time_raw: + start_time_raw = "0:0" + if end_time_raw == 'null' or not end_time_raw: + end_time_raw = "0:0" + + # Format times with leading zeros (HH:MM) + try: + start_parts = start_time_raw.split(":") + start_hour = int(start_parts[0]) + start_min = int(start_parts[1]) + start_time = f"{start_hour:02d}:{start_min:02d}" + except (ValueError, IndexError): + start_time = "00:00" + + try: + end_parts = end_time_raw.split(":") + end_hour = int(end_parts[0]) + end_min = int(end_parts[1]) + end_time = f"{end_hour:02d}:{end_min:02d}" + except (ValueError, IndexError): + end_time = "00:00" + + # Get the mode value safely + mode_raw = settings_data.get(f'time{i}Mode') + if mode_raw == 'null' or mode_raw is None: + batt_mode = None + else: + try: + batt_mode = int(mode_raw) + except (ValueError, TypeError): + batt_mode = None + + # Get the enabled status safely + enabled_raw = settings_data.get(f'forcedStopSwitch{i}', 0) + if enabled_raw == 'null' or enabled_raw is None: + enabled = False + else: + try: + enabled = int(enabled_raw) == 1 + except (ValueError, TypeError): + enabled = False + + segment = { + 'segment_id': i, + 'batt_mode': batt_mode, + 'mode_name': mode_names.get(batt_mode, "Unknown"), + 'start_time': start_time, + 'end_time': end_time, + 'enabled': enabled + } + + segments.append(segment) + + return segments \ No newline at end of file diff --git a/setup.py b/setup.py index 1452ae7..9643566 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setuptools.setup( name="growattServer", - version="1.6.0", + version="1.7.0", author="IndyKoning", author_email="indykoningnl@gmail.com", description="A package to talk to growatt server", From a80aaa439f33d175c892fe45fa40ac7490583e94 Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Sat, 15 Mar 2025 13:02:14 +0100 Subject: [PATCH 08/13] Adds plant endpoints using public v1 API --- README.md | 6 +++ growattServer/__init__.py | 99 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/README.md b/README.md index ef70dd3..d59157d 100755 --- a/README.md +++ b/README.md @@ -48,6 +48,12 @@ Any methods that may be useful. `api.plant_list_v1()` Get a list of plants registered to your account, using public v1 API. +`api.plant_details_v1(plant_id)` Get detailed information about a power station, using public v1 API. + +`api.plant_energy_overview_v1(plant_id)` Get energy overview data for a plant, using public v1 API. + +`api.plant_energy_history_v1(plant_id, start_date, end_date, time_unit, page, perpage)` Get historical energy data for a plant for multiple days/months/years, using public v1 API. + `api.plant_info(plant_id)` Get info for specified plant. `api.plant_settings(plant_id)` Get the current settings for the specified plant diff --git a/growattServer/__init__.py b/growattServer/__init__.py index 5b40a04..42809db 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1216,6 +1216,105 @@ def plant_list_v1(self): return response.json() + def plant_details_v1(self, plant_id): + """ + Get basic information about a power station. + + Args: + plant_id (int): Power Station ID + + Returns: + dict: A dictionary containing the plant details. + + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + response = self.session.get( + self.get_v1_url('plant/details'), + params={'plant_id': plant_id} + ) + + return response.json() + + def plant_energy_overview_v1(self, plant_id): + """ + Get an overview of a plant's energy data. + + Args: + plant_id (int): Power Station ID + + Returns: + dict: A dictionary containing the plant energy overview. + + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + response = self.session.get( + self.get_v1_url('plant/data'), + params={'plant_id': plant_id} + ) + + return response.json() + + def plant_energy_history_v1(self, plant_id, start_date=None, end_date=None, time_unit="day", page=None, perpage=None): + """ + Retrieve plant energy data for multiple days/months/years. + + Args: + plant_id (int): Power Station ID + start_date (date, optional): Start Date - defaults to today + end_date (date, optional): End Date - defaults to today + time_unit (str, optional): Time unit ('day', 'month', 'year') - defaults to 'day' + page (int, optional): Page number - defaults to 1 + perpage (int, optional): Number of items per page - defaults to 20, max 100 + + Returns: + dict: A dictionary containing the plant energy history. + + Notes: + - When time_unit is 'day', date interval cannot exceed 7 days + - When time_unit is 'month', start date must be within same or previous year + - When time_unit is 'year', date interval must not exceed 20 years + + """ + if not self.v1_api_enabled: + warnings.warn("V1 API is not enabled. This method requires an API token.", RuntimeWarning) + return {"error_code": 1, "error_msg": "API token required", "data": None} + + if start_date is None and end_date is None: + start_date = date.today() + end_date = date.today() + elif start_date is None: + start_date = end_date + elif end_date is None: + end_date = start_date + + # Validate date ranges based on time_unit + if time_unit == "day" and (end_date - start_date).days > 7: + warnings.warn("Date interval must not exceed 7 days in 'day' mode.", RuntimeWarning) + elif time_unit == "month" and (end_date.year - start_date.year > 1): + warnings.warn("Start date must be within same or previous year in 'month' mode.", RuntimeWarning) + elif time_unit == "year" and (end_date.year - start_date.year > 20): + warnings.warn("Date interval must not exceed 20 years in 'year' mode.", RuntimeWarning) + + response = self.session.get( + self.get_v1_url('plant/energy'), + params={ + 'plant_id': plant_id, + 'start_date': start_date.strftime("%Y-%m-%d"), + 'end_date': end_date.strftime("%Y-%m-%d"), + 'time_unit': time_unit, + 'page': page, + 'perpage': perpage + } + ) + + return response.json() + def device_list_v1(self, plant_id): """ Get devices associated with plant. From e5e9e239fb80098b8748c1208835b962e4a82a84 Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Sun, 16 Mar 2025 19:58:57 +0000 Subject: [PATCH 09/13] fixes battery SOC --- examples/min_example_dashboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/min_example_dashboard.py b/examples/min_example_dashboard.py index 1cf6413..5506424 100644 --- a/examples/min_example_dashboard.py +++ b/examples/min_example_dashboard.py @@ -90,4 +90,4 @@ print(f'PV #2 power {float(energy_data["ppv2"]):>22.1f}') print(f'Battery charge power {float(energy_data["bdc1ChargePower"]):>22.1f}') print(f'Battery discharge power {float(energy_data["bdc1DischargePower"]):>22.1f}') -print(f'Battery SOC {int(energy_data["soc1"]):>21}%') +print(f'Battery SOC {int(energy_data["bdc1Soc"]):>21}%') From a701ca14f01385f24be7bac0ef3e48df73ba74bc Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Tue, 15 Apr 2025 05:26:30 +0000 Subject: [PATCH 10/13] Adds Growatt API V1 client implementation, while preserving 100% backward compatibility --- README.md | 95 ++- examples/min_example_dashboard.py | 97 +++ growattServer/__init__.py | 1177 +--------------------------- growattServer/base_api.py | 1178 +++++++++++++++++++++++++++++ growattServer/v1_api.py | 605 +++++++++++++++ 5 files changed, 1979 insertions(+), 1173 deletions(-) create mode 100644 examples/min_example_dashboard.py create mode 100644 growattServer/base_api.py create mode 100644 growattServer/v1_api.py diff --git a/README.md b/README.md index f6e1e3c..08dae00 100755 --- a/README.md +++ b/README.md @@ -5,8 +5,14 @@ Package to retrieve PV information from the growatt server. Special thanks to [Sjoerd Langkemper](https://github.com/Sjord) who has provided a strong base to start off from https://github.com/Sjord/growatt_api_client These projects may merge in the future since they are simmilar in code and function. +This library now supports both the legacy password-based authentication and the V1 API with token-based authentication for MIN systems (TLX are identified as MIN system in the public API). Certain endpoints are not supported anymore by openapi.growatt.com. For example `api.min_write_parameter()` should be used instead of old `api.update_tlx_inverter_setting()`. + ## Usage +### Legacy API + +Uses username/password basic authentication + ```python import growattServer @@ -16,13 +22,28 @@ login_response = api.login(, ) print(api.plant_list(login_response['user']['id'])) ``` +### V1 API + +The public v1 API requires token-based authentication + +```python +import growattServer + +api = growattServer.GrowattApiV1(token="YOUR_API_TOKEN") +#Get a list of growatt plants. +plants = api.plant_list_v1() +print(plants) +``` + ## Methods and Variables ### Methods Any methods that may be useful. -`api.login(username, password)` Log into the growatt API. This must be done before making any request. After this you will be logged in. You will want to capture the response to get the `userId` variable. +#### Legacy API Methods + +`api.login(username, password)` Log into the growatt API. This must be done before making any request. After this you will be logged in. You will want to capture the response to get the `userId` variable. Should not be used for public v1 APIs. `api.plant_list(user_id)` Get a list of plants registered to your account. @@ -96,6 +117,34 @@ Any methods that may be useful. `api.update_noah_settings(serial_number, setting_type, parameters)` Applies the provided parameters (dictionary or array) for the specified setting on the specified noah device; see 'Noah settings' below for more information +#### V1 API Methods + +`api.plant_list_v1()` Get a list of plants registered to your account, using public v1 API. + +`api.plant_details_v1(plant_id)` Get detailed information about a power station, using public v1 API. + +`api.plant_energy_overview_v1(plant_id)` Get energy overview data for a plant, using public v1 API. + +`api.plant_energy_history_v1(plant_id, start_date, end_date, time_unit, page, perpage)` Get historical energy data for a plant for multiple days/months/years, using public v1 API. + +`api.device_list_v1(plant_id)` Get a list of devices in specified plant using the public v1 API. + +`api.min_energy(device_sn)` Get current energy data for a min inverter, including power and energy values. + +`api.min_detail(device_sn)` Get detailed data for a min inverter. + +`api.min_energy_history(device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None)` Get energy history data for a min inverter (7-day max range). + +`api.min_settings(device_sn)` Get all settings for a min inverter. + +`api.min_read_parameter(device_sn, parameter_id, start_address=None, end_address=None)` Read a specific setting for a min inverter. + +`api.min_write_parameter(device_sn, parameter_id, parameter_values)` Set parameters on a min inverter. Parameter values can be a single value, a list, or a dictionary. + +`api.min_write_time_segment(device_sn, segment_id, batt_mode, start_time, end_time, enabled=True)` Update a specific time segment for a min inverter. + +`api.min_read_time_segments(device_sn, settings_data=None)` Read all time segments from a MIN inverter. Optionally pass settings_data to avoid redundant API calls. + ### Variables Some variables you may want to set. @@ -118,6 +167,8 @@ The library can be initialised to introduce randomness into the User Agent field This has been added since the Growatt servers started checking for the presence of a `User-Agent` field in the headers that are sent. +#### Legacy API Initialization + By default the library will use a pre-set `User-Agent` value which identifies this library while also appearing like an Android device. However, it is also possible to pass in parameters to the intialisation of the library to override this entirely, or just add a random ID to the value. e.g. ```python @@ -128,6 +179,12 @@ api = growattServer.GrowattApi(True) # Adds a randomly generated User ID to the api = growattServer.GrowattApi(False, "my_user_agent_value") # Overrides the default and uses "my_user_agent_value" in the User-Agent header ``` +#### V1 API Initialization + +```python +api = growattServer.GrowattApiV1(token="YOUR_API_TOKEN") # Initialize with your API token +``` + Please see the `user_agent_options.py` example in the `examples` directory if you wish to investigate further. ## Examples @@ -240,6 +297,40 @@ The four functions `update_tlx_inverter_setting`, `update_mix_inverter_setting`, Only the settings described above have been tested with `update_tlx_inverter_setting` and they all take only one single parameter. It is very likely that the function works with all settings returned by `tlx_get_enabled_settings`, but this has not been tested. A helper function `update_tlx_inverter_time_segment` is provided for the settings that require more than one parameter. +## MIN/TLX Inverter Settings Using V1 API + +For MIN/TLX systems, the public V1 API provides a more robust way to read and write inverter settings: + +* **Read Parameter** + * function: `api.min_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.min_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) + +* **Time Segments** + * function: `api.min_write_time_segment` + * parameters: + * `device_sn`: The device serial number + * `segment_id`: Segment number (1-9) + * `batt_mode`: Battery mode (0=Load First, 1=Battery First, 2=Grid First) + * `start_time`: Datetime.time object for segment start + * `end_time`: Datetime.time object for segment end + * `enabled`: Boolean to enable/disable segment + +* **Read Time Segments** + * function: `api.min_read_time_segments` + * parameters: + * `device_sn`: The device serial number + * `settings_data`: Optional settings data to avoid redundant API calls + ## Noah Settings The noah settings function allow you to change individual values on your noah system e.g. system default output power, battery management, operation mode and currency From what has been reverse engineered from the api, each setting has a `setting_type` and a set of `parameters` that are relevant to it. @@ -287,4 +378,4 @@ The library contains functions that allow you to modify the configuration of you To the best of our knowledge only the `settings` functions perform modifications to your system and all other operations are read only. Regardless of the operation: -***The library is used entirely at your own risk.*** +***The library is used entirely at your own risk.*** \ No newline at end of file diff --git a/examples/min_example_dashboard.py b/examples/min_example_dashboard.py new file mode 100644 index 0000000..270aafc --- /dev/null +++ b/examples/min_example_dashboard.py @@ -0,0 +1,97 @@ +import growattServer +import json + +""" +Example script fetching key power and today+total energy metrics from a Growatt MID-30KTL3-XH (TLX) + APX battery hybrid system +using the V1 API with token-based authentication. +""" + +# 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 + +# Initialize the API with token +api = growattServer.GrowattApiV1(token=api_token) + +# Get plant list using V1 API +plant_response = api.plant_list_v1() +if plant_response['error_code'] != 0: + print(f"Failed to get plants, error: {plant_response['error_msg']}") + exit() + +plant_id = plant_response['data']['plants'][0]['plant_id'] + +# Get devices in plant using V1 API +devices_response = api.device_list_v1(plant_id) +if devices_response['error_code'] != 0: + print(f"Failed to get devices, error: {devices_response['error_msg']}") + exit() + +# Iterate over all devices +energy_data = None +for device in devices_response['data']['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] + + # Get energy data using new API + energy_response = api.min_energy(device_sn=inverter_sn) + if energy_response['error_code'] != 0: + print( + f"Failed to get energy data, error: {energy_response['error_msg']}") + exit() + + energy_data = energy_response['data'] + with open('energy_data.json', 'w') as f: + json.dump(energy_data, f, indent=4, sort_keys=True) + +# energy data does not contain epvToday for some reason, so we need to calculate it +epv_today = energy_data["epv1Today"] + energy_data["epv2Today"] + +solar_production = f'{float(epv_today):.1f}/{float(energy_data["epvTotal"]):.1f}' +solar_production_pv1 = f'{float(energy_data["epv1Today"]):.1f}/{float(energy_data["epv1Total"]):.1f}' +solar_production_pv2 = f'{float(energy_data["epv2Today"]):.1f}/{float(energy_data["epv2Total"]):.1f}' +energy_output = f'{float(energy_data["eacToday"]):.1f}/{float(energy_data["eacTotal"]):.1f}' +system_production = f'{float(energy_data["esystemToday"]):.1f}/{float(energy_data["esystemTotal"]):.1f}' +battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' +battery_grid_charge = f'{float(energy_data["eacChargeToday"]):.1f}/{float(energy_data["eacChargeTotal"]):.1f}' +battery_discharged = f'{float(energy_data["edischargeToday"]):.1f}/{float(energy_data["edischargeTotal"]):.1f}' +exported_to_grid = f'{float(energy_data["etoGridToday"]):.1f}/{float(energy_data["etoGridTotal"]):.1f}' +imported_from_grid = f'{float(energy_data["etoUserToday"]):.1f}/{float(energy_data["etoUserTotal"]):.1f}' +load_consumption = f'{float(energy_data["elocalLoadToday"]):.1f}/{float(energy_data["elocalLoadTotal"]):.1f}' +self_consumption = f'{float(energy_data["eselfToday"]):.1f}/{float(energy_data["eselfTotal"]):.1f}' +battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' + +# Output the dashboard +print("\nGeneration overview Today/Total(kWh)") +print(f'Solar production {solar_production:>22}') +print(f' Solar production, PV1 {solar_production_pv1:>22}') +print(f' Solar production, PV2 {solar_production_pv2:>22}') +print(f'Energy Output {energy_output:>22}') +print(f'System production {system_production:>22}') +print(f'Self consumption {self_consumption:>22}') +print(f'Load consumption {load_consumption:>22}') +print(f'Battery Charged {battery_charged:>22}') +print(f' Charged from grid {battery_grid_charge:>22}') +print(f'Battery Discharged {battery_discharged:>22}') +print(f'Import from grid {imported_from_grid:>22}') +print(f'Export to grid {exported_to_grid:>22}') + +print("\nPower overview (Watts)") +print(f'AC Power {float(energy_data["pac"]):>22.1f}') +print(f'Self power {float(energy_data["pself"]):>22.1f}') +print( + f'Export power {float(energy_data["pacToGridTotal"]):>22.1f}') +print( + f'Import power {float(energy_data["pacToUserTotal"]):>22.1f}') +print( + f'Local load power {float(energy_data["pacToLocalLoad"]):>22.1f}') +print(f'PV power {float(energy_data["ppv"]):>22.1f}') +print(f'PV #1 power {float(energy_data["ppv1"]):>22.1f}') +print(f'PV #2 power {float(energy_data["ppv2"]):>22.1f}') +print( + f'Battery charge power {float(energy_data["bdc1ChargePower"]):>22.1f}') +print( + f'Battery discharge power {float(energy_data["bdc1DischargePower"]):>22.1f}') +print(f'Battery SOC {int(energy_data["bdc1Soc"]):>21}%') diff --git a/growattServer/__init__.py b/growattServer/__init__.py index cc53527..dc1085b 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1,1173 +1,8 @@ -import datetime -from enum import IntEnum -import requests -from random import randint -import warnings -import hashlib - -name = "growattServer" - -BATT_MODE_LOAD_FIRST = 0 -BATT_MODE_BATTERY_FIRST = 1 -BATT_MODE_GRID_FIRST = 2 - -def hash_password(password): - """ - Normal MD5, except add c if a byte of the digest is less than 10. - """ - password_md5 = hashlib.md5(password.encode('utf-8')).hexdigest() - for i in range(0, len(password_md5), 2): - if password_md5[i] == '0': - password_md5 = password_md5[0:i] + 'c' + password_md5[i + 1:] - return password_md5 - -class Timespan(IntEnum): - hour = 0 - day = 1 - month = 2 - -class GrowattApi: - server_url = 'https://openapi.growatt.com/' - agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)" - - def __init__(self, add_random_user_id=False, agent_identifier=None): - if (agent_identifier != None): - self.agent_identifier = agent_identifier - - #If a random user id is required, generate a 5 digit number and add it to the user agent - if (add_random_user_id): - random_number = ''.join(["{}".format(randint(0,9)) for num in range(0,5)]) - self.agent_identifier += " - " + random_number - - self.session = requests.Session() - self.session.hooks = { - 'response': lambda response, *args, **kwargs: response.raise_for_status() - } - - headers = {'User-Agent': self.agent_identifier} - self.session.headers.update(headers) - - def __get_date_string(self, timespan=None, date=None): - if timespan is not None: - assert timespan in Timespan - - if date is None: - date = datetime.datetime.now() - - date_str="" - if timespan == Timespan.month: - date_str = date.strftime('%Y-%m') - else: - date_str = date.strftime('%Y-%m-%d') - - return date_str - - def get_url(self, page): - """ - Simple helper function to get the page URL. - """ - return self.server_url + page - - def login(self, username, password, is_password_hashed=False): - """ - Log the user in. - - Returns - 'data' -- A List containing Objects containing the folowing - 'plantName' -- Friendly name of the plant - 'plantId' -- The ID of the plant - 'service' - 'quality' - 'isOpenSmartFamily' - 'totalData' -- An Object - 'success' -- True or False - 'msg' - 'app_code' - 'user' -- An Object containing a lot of user information - 'uid' - 'userLanguage' - 'inverterGroup' -- A List - 'timeZone' -- A Number - 'lat' - 'lng' - 'dataAcqList' -- A List - 'type' - 'accountName' -- The username - 'password' -- The password hash of the user - 'isValiPhone' - 'kind' - 'mailNotice' -- True or False - 'id' - 'lasLoginIp' - 'lastLoginTime' - 'userDeviceType' - 'phoneNum' - 'approved' -- True or False - 'area' -- Continent of the user - 'smsNotice' -- True or False - 'isAgent' - 'token' - 'nickName' - 'parentUserId' - 'customerCode' - 'country' - 'isPhoneNumReg' - 'createDate' - 'rightlevel' - 'appType' - 'serverUrl' - 'roleId' - 'enabled' -- True or False - 'agentCode' - 'inverterList' -- A list - 'email' - 'company' - 'activeName' - 'codeIndex' - 'appAlias' - 'isBigCustomer' - 'noticeType' - """ - if not is_password_hashed: - password = hash_password(password) - - response = self.session.post(self.get_url('newTwoLoginAPI.do'), data={ - 'userName': username, - 'password': password - }) - - data = response.json()['back'] - if data['success']: - data.update({ - 'userId': data['user']['id'], - 'userLevel': data['user']['rightlevel'] - }) - return data - - def plant_list(self, user_id): - """ - Get a list of plants connected to this account. - - Args: - user_id (str): The ID of the user. - - Returns: - list: A list of plants connected to the account. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.get( - self.get_url('PlantListAPI.do'), - params={'userId': user_id}, - allow_redirects=False - ) - - return response.json().get('back', []) - - def plant_detail(self, plant_id, timespan, date=None): - """ - Get plant details for specified timespan. - - Args: - plant_id (str): The ID of the plant. - timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. - date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). - - Returns: - dict: A dictionary containing the plant details. - - Raises: - Exception: If the request to the server fails. - """ - date_str = self.__get_date_string(timespan, date) - - response = self.session.get(self.get_url('PlantDetailAPI.do'), params={ - 'plantId': plant_id, - 'type': timespan.value, - 'date': date_str - }) - - return response.json().get('back', {}) - - def plant_list_two(self): - """ - Get a list of all plants with detailed information. - - Returns: - list: A list of plants with detailed information. - """ - response = self.session.post( - self.get_url('newTwoPlantAPI.do'), - params={'op': 'getAllPlantListTwo'}, - data={ - 'language': '1', - 'nominalPower': '', - 'order': '1', - 'pageSize': '15', - 'plantName': '', - 'plantStatus': '', - 'toPageNum': '1' - } - ) - - return response.json().get('PlantList', []) - - def inverter_data(self, inverter_id, date=None): - """ - Get inverter data for specified date or today. - - Args: - inverter_id (str): The ID of the inverter. - date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). - - Returns: - dict: A dictionary containing the inverter data. - - Raises: - Exception: If the request to the server fails. - """ - date_str = self.__get_date_string(date=date) - response = self.session.get(self.get_url('newInverterAPI.do'), params={ - 'op': 'getInverterData', - 'id': inverter_id, - 'type': 1, - 'date': date_str - }) - - return response.json() - - def inverter_detail(self, inverter_id): - """ - Get detailed data from PV inverter. - - Args: - inverter_id (str): The ID of the inverter. - - Returns: - dict: A dictionary containing the inverter details. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.get(self.get_url('newInverterAPI.do'), params={ - 'op': 'getInverterDetailData', - 'inverterId': inverter_id - }) - - return response.json() - - def inverter_detail_two(self, inverter_id): - """ - Get detailed data from PV inverter (alternative endpoint). - - Args: - inverter_id (str): The ID of the inverter. - - Returns: - dict: A dictionary containing the inverter details. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.get(self.get_url('newInverterAPI.do'), params={ - 'op': 'getInverterDetailData_two', - 'inverterId': inverter_id - }) - - return response.json() - - def tlx_system_status(self, plant_id, tlx_id): - """ - Get status of the system - - Args: - plant_id (str): The ID of the plant. - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing system status. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.post( - self.get_url("newTlxApi.do"), - params={"op": "getSystemStatus_KW"}, - data={"plantId": plant_id, - "id": tlx_id} - ) - - return response.json().get('obj', {}) - - def tlx_energy_overview(self, plant_id, tlx_id): - """ - Get energy overview - - Args: - plant_id (str): The ID of the plant. - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing energy data. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.post( - self.get_url("newTlxApi.do"), - params={"op": "getEnergyOverview"}, - data={"plantId": plant_id, - "id": tlx_id} - ) - - return response.json().get('obj', {}) - - def tlx_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): - """ - Get energy production and consumption (KW) - - Args: - tlx_id (str): The ID of the TLX inverter. - timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. - date (datetime): The date you are interested in. - - Returns: - dict: A dictionary containing energy data. - - Raises: - Exception: If the request to the server fails. - """ - - date_str = self.__get_date_string(timespan, date) - - response = self.session.post( - self.get_url("newTlxApi.do"), - params={"op": "getEnergyProdAndCons_KW"}, - data={'date': date_str, - "plantId": plant_id, - "language": "1", - "id": tlx_id, - "type": timespan.value} - ) - - return response.json().get('obj', {}) - - def tlx_data(self, tlx_id, date=None): - """ - Get TLX inverter data for specified date or today. - - Args: - tlx_id (str): The ID of the TLX inverter. - date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). - - Returns: - dict: A dictionary containing the TLX inverter data. - - Raises: - Exception: If the request to the server fails. - """ - date_str = self.__get_date_string(date=date) - response = self.session.get(self.get_url('newTlxApi.do'), params={ - 'op': 'getTlxData', - 'id': tlx_id, - 'type': 1, - 'date': date_str - }) - - return response.json() - - def tlx_detail(self, tlx_id): - """ - Get detailed data from TLX inverter. - - Args: - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing the detailed TLX inverter data. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.get(self.get_url('newTlxApi.do'), params={ - 'op': 'getTlxDetailData', - 'id': tlx_id - }) - - return response.json() - - def tlx_params(self, tlx_id): - """ - Get parameters for TLX inverter. - - Args: - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing the TLX inverter parameters. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.get(self.get_url('newTlxApi.do'), params={ - 'op': 'getTlxParams', - 'id': tlx_id - }) - - return response.json() - - def tlx_all_settings(self, tlx_id): - """ - Get all possible settings from TLX inverter. - - Args: - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing all possible settings for the TLX inverter. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.post(self.get_url('newTlxApi.do'), params={ - 'op': 'getTlxSetData' - }, data={ - 'serialNum': tlx_id - }) - - return response.json().get('obj', {}).get('tlxSetBean') - - def tlx_enabled_settings(self, tlx_id): - """ - Get "Enabled settings" from TLX inverter. - - Args: - tlx_id (str): The ID of the TLX inverter. - - Returns: - dict: A dictionary containing the enabled settings. - - Raises: - Exception: If the request to the server fails. - """ - string_time = datetime.datetime.now().strftime('%Y-%m-%d') - response = self.session.post( - self.get_url('newLoginAPI.do'), - params={'op': 'getSetPass'}, - data={'deviceSn': tlx_id, 'stringTime': string_time, 'type': '5'} - ) - - return response.json().get('obj', {}) - - def tlx_battery_info(self, serial_num): - """ - Get battery information. - - Args: - serial_num (str): The serial number of the battery. - - Returns: - dict: A dictionary containing the battery information. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.post( - self.get_url('newTlxApi.do'), - params={'op': 'getBatInfo'}, - data={'lan': 1, 'serialNum': serial_num} - ) - - return response.json().get('obj', {}) - - def tlx_battery_info_detailed(self, plant_id, serial_num): - """ - Get detailed battery information. - - Args: - plant_id (str): The ID of the plant. - serial_num (str): The serial number of the battery. - - Returns: - dict: A dictionary containing the detailed battery information. - - Raises: - Exception: If the request to the server fails. - """ - response = self.session.post( - self.get_url('newTlxApi.do'), - params={'op': 'getBatDetailData'}, - data={'lan': 1, 'plantId': plant_id, 'id': serial_num} - ) - - return response.json() - - def mix_info(self, mix_id, plant_id = None): - """ - Returns high level values from Mix device - - Keyword arguments: - mix_id -- The serial number (device_sn) of the inverter - plant_id -- The ID of the plant (the mobile app uses this but it does not appear to be necessary) (default None) - - Returns: - 'acChargeEnergyToday' -- ??? 2.7 - 'acChargeEnergyTotal' -- ??? 25.3 - 'acChargePower' -- ??? 0 - 'capacity': '45' -- The current remaining capacity of the batteries (same as soc but without the % sign) - 'eBatChargeToday' -- Battery charged today in kWh - 'eBatChargeTotal' -- Battery charged total (all time) in kWh - 'eBatDisChargeToday' -- Battery discharged today in kWh - 'eBatDisChargeTotal' -- Battery discharged total (all time) in kWh - 'epvToday' -- Energy generated from PVs today in kWh - 'epvTotal' -- Energy generated from PVs total (all time) in kWh - 'isCharge'-- ??? 0 - Possible a 0/1 based on whether or not the battery is charging - 'pCharge1' -- ??? 0 - 'pDischarge1' -- Battery discharging rate in W - 'soc' -- Statement of charge including % symbol - 'upsPac1' -- ??? 0 - 'upsPac2' -- ??? 0 - 'upsPac3' -- ??? 0 - 'vbat' -- Battery Voltage - 'vbatdsp' -- ??? 51.8 - 'vpv1' -- Voltage PV1 - 'vpv2' -- Voltage PV2 - """ - request_params={ - 'op': 'getMixInfo', - 'mixId': mix_id - } - - if (plant_id): - request_params['plantId'] = plant_id - - response = self.session.get(self.get_url('newMixApi.do'), params=request_params) - - return response.json().get('obj', {}) - - def mix_totals(self, mix_id, plant_id): - """ - Returns "Totals" values from Mix device - - Keyword arguments: - mix_id -- The serial number (device_sn) of the inverter - plant_id -- The ID of the plant - - Returns: - 'echargetoday' -- Battery charged today in kWh (same as eBatChargeToday from mix_info) - 'echargetotal' -- Battery charged total (all time) in kWh (same as eBatChargeTotal from mix_info) - 'edischarge1Today' -- Battery discharged today in kWh (same as eBatDisChargeToday from mix_info) - 'edischarge1Total' -- Battery discharged total (all time) in kWh (same as eBatDisChargeTotal from mix_info) - 'elocalLoadToday' -- Load consumption today in kWh - 'elocalLoadTotal' -- Load consumption total (all time) in kWh - 'epvToday' -- Energy generated from PVs today in kWh (same as epvToday from mix_info) - 'epvTotal' -- Energy generated from PVs total (all time) in kWh (same as epvTotal from mix_info) - 'etoGridToday' -- Energy exported to the grid today in kWh - 'etogridTotal' -- Energy exported to the grid total (all time) in kWh - 'photovoltaicRevenueToday' -- Revenue earned from PV today in 'unit' currency - 'photovoltaicRevenueTotal' -- Revenue earned from PV total (all time) in 'unit' currency - 'unit' -- Unit of currency for 'Revenue' - """ - response = self.session.post(self.get_url('newMixApi.do'), params={ - 'op': 'getEnergyOverview', - 'mixId': mix_id, - 'plantId': plant_id - }) - - return response.json().get('obj', {}) - - def mix_system_status(self, mix_id, plant_id): - """ - Returns current "Status" from Mix device - - Keyword arguments: - mix_id -- The serial number (device_sn) of the inverter - plant_id -- The ID of the plant - - Returns: - 'SOC' -- Statement of charge (remaining battery %) - 'chargePower' -- Battery charging rate in kw - 'fAc' -- Frequency (Hz) - 'lost' -- System status e.g. 'mix.status.normal' - 'pLocalLoad' -- Load conumption in kW - 'pPv1' -- PV1 Wattage in W - 'pPv2' -- PV2 Wattage in W - 'pactogrid' -- Export to grid rate in kW - 'pactouser' -- Import from grid rate in kW - 'pdisCharge1' -- Discharging batteries rate in kW - 'pmax' -- ??? 6 ??? PV Maximum kW ?? - 'ppv' -- PV combined Wattage in kW - 'priorityChoose' -- Priority setting - 0=Local load - 'status' -- System statue - ENUM - Unknown values - 'unit' -- Unit of measurement e.g. 'kW' - 'upsFac' -- ??? 0 - 'upsVac1' -- ??? 0 - 'uwSysWorkMode' -- ??? 6 - 'vAc1' -- Grid voltage in V - 'vBat' -- Battery voltage in V - 'vPv1' -- PV1 voltage in V - 'vPv2' -- PV2 voltage in V - 'vac1' -- Grid voltage in V (same as vAc1) - 'wBatteryType' -- ??? 1 - """ - response = self.session.post(self.get_url('newMixApi.do'), params={ - 'op': 'getSystemStatus_KW', - 'mixId': mix_id, - 'plantId': plant_id - }) - - return response.json().get('obj', {}) - - def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): - """ - Get Mix details for specified timespan - - Keyword arguments: - mix_id -- The serial number (device_sn) of the inverter - plant_id -- The ID of the plant - timespan -- The ENUM value conforming to the time window you want e.g. hours from today, days, or months (Default Timespan.hour) - date -- The date you are interested in (Default datetime.datetime.now()) - - Returns: - A chartData object where each entry is for a specific 5 minute window e.g. 00:05 and 00:10 respectively (below) - 'chartData': { '00:05': { 'pacToGrid' -- Export rate to grid in kW - 'pacToUser' -- Import rate from grid in kW - 'pdischarge' -- Battery discharge in kW - 'ppv' -- Solar generation in kW - 'sysOut' -- Load consumption in kW - }, - '00:10': { 'pacToGrid': '0', - 'pacToUser': '0.93', - 'pdischarge': '0', - 'ppv': '0', - 'sysOut': '0.93'}, - ...... - } - 'eAcCharge' -- Exported to grid in kWh - 'eCharge' -- System production in kWh = Self-consumption + Exported to Grid - 'eChargeToday' -- Load consumption from solar in kWh - 'eChargeToday1' -- Self-consumption in kWh - 'eChargeToday2' -- Self-consumption in kWh (eChargeToday + echarge1) - 'echarge1' -- Load consumption from battery in kWh - 'echargeToat' -- Total battery discharged (all time) in kWh - 'elocalLoad' -- Load consumption in kW (battery + solar + imported) - 'etouser' -- Load consumption imported from grid in kWh - 'photovoltaic' -- Load consumption from solar in kWh (same as eChargeToday) - 'ratio1' -- % of system production that is self-consumed - 'ratio2' -- % of system production that is exported - 'ratio3' -- % of Load consumption that is "self consumption" - 'ratio4' -- % of Load consumption that is "imported from grid" - 'ratio5' -- % of Self consumption that is directly from Solar - 'ratio6' -- % of Self consumption that is from batteries - 'unit' -- Unit of measurement e.g kWh - 'unit2' -- Unit of measurement e.g kW - - - NOTE - It is possible to calculate the PV generation that went into charging the batteries by performing the following calculation: - Solar to Battery = Solar Generation - Export to Grid - Load consumption from solar - epvToday (from mix_info) - eAcCharge - eChargeToday - """ - date_str = self.__get_date_string(timespan, date) - - response = self.session.post(self.get_url('newMixApi.do'), params={ - 'op': 'getEnergyProdAndCons_KW', - 'plantId': plant_id, - 'mixId': mix_id, - 'type': timespan.value, - 'date': date_str - }) - - return response.json().get('obj', {}) - - def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): - """ - Get 'dashboard' data for specified timespan - NOTE - All numerical values returned by this api call include units e.g. kWh or % - - Many of the 'total' values that are returned for a Mix system are inaccurate on the system this was tested against. - However, the statistics that are correct are not available on any other interface, plus these values may be accurate for - non-mix types of system. Where the values have been proven to be inaccurate they are commented below. - - Keyword arguments: - plant_id -- The ID of the plant - timespan -- The ENUM value conforming to the time window you want e.g. hours from today, days, or months (Default Timespan.hour) - date -- The date you are interested in (Default datetime.datetime.now()) - - Returns: - A chartData object where each entry is for a specific 5 minute window e.g. 00:05 and 00:10 respectively (below) - NOTE: The keys are interpreted differently, the examples below describe what they are used for in a 'Mix' system - 'chartData': { '00:05': { 'pacToUser' -- Power from battery in kW - 'ppv' -- Solar generation in kW - 'sysOut' -- Load consumption in kW - 'userLoad' -- Export in kW - }, - '00:10': { 'pacToUser': '0', - 'ppv': '0', - 'sysOut': '0.7', - 'userLoad': '0'}, - ...... - } - 'chartDataUnit' -- Unit of measurement e.g. 'kW', - 'eAcCharge' -- Energy exported to the grid in kWh e.g. '20.5kWh' (not accurate for Mix systems) - 'eCharge' -- System production in kWh = Self-consumption + Exported to Grid e.g '23.1kWh' (not accurate for Mix systems - actually showing the total 'load consumption' - 'eChargeToday1' -- Self-consumption of PPV (possibly including excess diverted to batteries) in kWh e.g. '2.6kWh' (not accurate for Mix systems) - 'eChargeToday2' -- Total self-consumption (PPV consumption(eChargeToday2Echarge1) + Battery Consumption(echarge1)) e.g. '10.1kWh' (not accurate for Mix systems) - 'eChargeToday2Echarge1' -- Self-consumption of PPV only e.g. '0.8kWh' (not accurate for Mix systems) - 'echarge1' -- Self-consumption from Battery only e.g. '9.3kWh' - 'echargeToat' -- Not used on Dashboard view, likely to be total battery discharged e.g. '152.1kWh' - 'elocalLoad' -- Total load consumption (etouser + eChargeToday2) e.g. '20.3kWh', (not accurate for Mix systems) - 'etouser'-- Energy imported from grid today (includes both directly used by load and AC battery charging e.g. '10.2kWh' - 'keyNames' -- Keys to be used for the graph data e.g. ['Solar', 'Load Consumption', 'Export To Grid', 'From Battery'] - 'photovoltaic' -- Same as eChargeToday2Echarge1 e.g. '0.8kWh' - 'ratio1' -- % of 'Solar production' that is self-consumed e.g. '11.3%' (not accurate for Mix systems) - 'ratio2' -- % of 'Solar production' that is exported e.g. '88.7%' (not accurate for Mix systems) - 'ratio3' -- % of 'Load consumption' that is self consumption e.g. '49.8%' (not accurate for Mix systems) - 'ratio4' -- % of 'Load consumption' that is imported from the grid e.g '50.2%' (not accurate for Mix systems) - 'ratio5' -- % of Self consumption that is from batteries e.g. '92.1%' (not accurate for Mix systems) - 'ratio6' -- % of Self consumption that is directly from Solar e.g. '7.9%' (not accurate for Mix systems) - - NOTE: Does not return any data for a tlx system. Use plant_energy_data() instead. - """ - date_str = self.__get_date_string(timespan, date) - - response = self.session.post(self.get_url('newPlantAPI.do'), params={ - 'action': "getEnergyStorageData", - 'date': date_str, - 'type': timespan.value, - 'plantId': plant_id - }) - - return response.json() - - def plant_settings(self, plant_id): - """ - Returns a dictionary containing the settings for the specified plant - - Keyword arguments: - plant_id -- The id of the plant you want the settings of - - Returns: - A python dictionary containing the settings for the specified plant - """ - response = self.session.get(self.get_url('newPlantAPI.do'), params={ - 'op': 'getPlant', - 'plantId': plant_id - }) - - return response.json() - - def storage_detail(self, storage_id): - """ - Get "All parameters" from battery storage. - """ - response = self.session.get(self.get_url('newStorageAPI.do'), params={ - 'op': 'getStorageInfo_sacolar', - 'storageId': storage_id - }) - - return response.json() - - def storage_params(self, storage_id): - """ - Get much more detail from battery storage. - """ - response = self.session.get(self.get_url('newStorageAPI.do'), params={ - 'op': 'getStorageParams_sacolar', - 'storageId': storage_id - }) - - return response.json() - - def storage_energy_overview(self, plant_id, storage_id): - """ - Get some energy/generation overview data. - """ - response = self.session.post(self.get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ - 'plantId': plant_id, - 'storageSn': storage_id - }) - - return response.json().get('obj', {}) - - def inverter_list(self, plant_id): - """ - Use device_list, it's more descriptive since the list contains more than inverters. - """ - warnings.warn("This function may be deprecated in the future because naming is not correct, use device_list instead", DeprecationWarning) - return self.device_list(plant_id) - - def __get_all_devices(self, plant_id): - """ - Get basic plant information with device list. - """ - response = self.session.get(self.get_url('newTwoPlantAPI.do'), - params={'op': 'getAllDeviceList', - 'plantId': plant_id, - 'language': 1}) - - return response.json().get('deviceList', {}) - - def device_list(self, plant_id): - """ - Get a list of all devices connected to plant. - """ - - device_list = self.plant_info(plant_id).get('deviceList', []) - - if not device_list: - # for tlx systems, the device_list in plant is empty, so use __get_all_devices() instead - device_list = self.__get_all_devices(plant_id) - - return device_list - - def plant_info(self, plant_id): - """ - Get basic plant information with device list. - """ - response = self.session.get(self.get_url('newTwoPlantAPI.do'), params={ - 'op': 'getAllDeviceListTwo', - 'plantId': plant_id, - 'pageNum': 1, - 'pageSize': 1 - }) - - return response.json() - - def plant_energy_data(self, plant_id): - """ - Get the energy data used in the 'Plant' tab in the phone - """ - response = self.session.post(self.get_url('newTwoPlantAPI.do'), - params={'op': 'getUserCenterEnertyDataByPlantid'}, - data={ 'language': 1, - 'plantId': plant_id}) - - return response.json() - - def is_plant_noah_system(self, plant_id): - """ - Returns a dictionary containing if noah devices are configured for the specified plant - - Keyword arguments: - plant_id -- The id of the plant you want the noah devices of (str) - - Returns - 'msg' - 'result' -- True or False - 'obj' -- An Object containing if noah devices are configured - 'isPlantNoahSystem' -- Is the specified plant a noah system (True or False) - 'plantId' -- The ID of the plant - 'isPlantHaveNoah' -- Are noah devices configured in the specified plant (True or False) - 'deviceSn' -- Serial number of the configured noah device - 'plantName' -- Friendly name of the plant - """ - response = self.session.post(self.get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ - 'plantId': plant_id - }) - return response.json() - - - def noah_system_status(self, serial_number): - """ - Returns a dictionary containing the status for the specified Noah Device - - Keyword arguments: - serial_number -- The Serial number of the noah device you want the status of (str) - - Returns - 'msg' - 'result' -- True or False - 'obj' -- An Object containing the noah device status - 'chargePower' -- Battery charging rate in watt e.g. '200Watt' - 'workMode' -- Workingmode of the battery (0 = Load First, 1 = Battery First) - 'soc' -- Statement of charge (remaining battery %) - 'associatedInvSn' -- ??? - 'batteryNum' -- Numbers of batterys - 'profitToday' -- Today generated profit through noah device - 'plantId' -- The ID of the plant - 'disChargePower' -- Battery discharging rate in watt e.g. '200Watt' - 'eacTotal' -- Total energy exported to the grid in kWh e.g. '20.5kWh' - 'eacToday' -- Today energy exported to the grid in kWh e.g. '20.5kWh' - 'pac' -- Export to grid rate in watt e.g. '200Watt' - 'ppv' -- Solar generation in watt e.g. '200Watt' - 'alias' -- Friendly name of the noah device - 'profitTotal' -- Total generated profit through noah device - 'moneyUnit' -- Unit of currency e.g. '€' - 'status' -- Is the noah device online (True or False) - """ - response = self.session.post(self.get_url('noahDeviceApi/noah/getSystemStatus'), data={ - 'deviceSn': serial_number - }) - return response.json() - - - def noah_info(self, serial_number): - """ - Returns a dictionary containing the informations for the specified Noah Device - - Keyword arguments: - serial_number -- The Serial number of the noah device you want the informations of (str) - - Returns - 'msg' - 'result' -- True or False - 'obj' -- An Object containing the noah device informations - 'neoList' -- A List containing Objects - 'unitList' -- A Object containing currency units e.g. "Euro": "euro", "DOLLAR": "dollar" - 'noah' -- A Object containing the folowing - 'time_segment' -- A List containing Objects with configured "Operation Mode" - NOTE: The keys are generated numerical, the values are generated with folowing syntax "[workingmode (0 = Load First, 1 = Battery First)]_[starttime]_[endtime]_[output power]" - 'time_segment': { - 'time_segment1': "0_0:0_8:0_150", ([Load First]_[00:00]_[08:00]_[150 watt]) - 'time_segment2': "1_8:0_18:0_0", ([Battery First]_[08:00]_[18:00]_[0 watt]) - .... - } - 'batSns' -- A List containing all battery Serial Numbers - 'associatedInvSn' -- ??? - 'plantId' -- The ID of the plant - 'chargingSocHighLimit' -- Configured "Battery Management" charging upper limit - 'chargingSocLowLimit' -- Configured "Battery Management" charging lower limit - 'defaultPower' -- Configured "System Default Output Power" - 'version' -- The Firmware Version of the noah device - 'deviceSn' -- The Serial number of the noah device - 'formulaMoney' -- Configured "Select Currency" energy cost per kWh e.g. '0.22' - 'alias' -- Friendly name of the noah device - 'model' -- Model Name of the noah device - 'plantName' -- Friendly name of the plant - 'tempType' -- ??? - 'moneyUnitText' -- Configured "Select Currency" (Value from the unitList) e.G. "euro" - 'plantList' -- A List containing Objects containing the folowing - 'plantId' -- The ID of the plant - 'plantImgName' -- Friendly name of the plant Image - 'plantName' -- Friendly name of the plant - """ - response = self.session.post(self.get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ - 'deviceSn': serial_number - }) - return response.json() - - - def update_plant_settings(self, plant_id, changed_settings, current_settings = None): - """ - Applies settings to the plant e.g. ID, Location, Timezone - See README for all possible settings options - - Keyword arguments: - plant_id -- The id of the plant you wish to update the settings for - changed_settings -- A python dictionary containing the settings to be changed and their value - current_settings -- A python dictionary containing the current settings of the plant (use the response from plant_settings), if None - fetched for you - - Returns: - A response from the server stating whether the configuration was successful or not - """ - #If no existing settings have been provided then get them from the growatt server - if current_settings == None: - current_settings = self.plant_settings(plant_id) - - #These are the parameters that the form requires, without these an error is thrown. Pre-populate their values with the current values - form_settings = { - 'plantCoal': (None, str(current_settings['formulaCoal'])), - 'plantSo2': (None, str(current_settings['formulaSo2'])), - 'accountName': (None, str(current_settings['userAccount'])), - 'plantID': (None, str(current_settings['id'])), - 'plantFirm': (None, '0'), #Hardcoded to 0 as I can't work out what value it should have - 'plantCountry': (None, str(current_settings['country'])), - 'plantType': (None, str(current_settings['plantType'])), - 'plantIncome': (None, str(current_settings['formulaMoneyStr'])), - 'plantAddress': (None, str(current_settings['plantAddress'])), - 'plantTimezone': (None, str(current_settings['timezone'])), - 'plantLng': (None, str(current_settings['plant_lng'])), - 'plantCity': (None, str(current_settings['city'])), - 'plantCo2': (None, str(current_settings['formulaCo2'])), - 'plantMoney': (None, str(current_settings['formulaMoneyUnitId'])), - 'plantPower': (None, str(current_settings['nominalPower'])), - 'plantLat': (None, str(current_settings['plant_lat'])), - 'plantDate': (None, str(current_settings['createDateText'])), - 'plantName': (None, str(current_settings['plantName'])), - } - - #Overwrite the current value of the setting with the new value - for setting, value in changed_settings.items(): - form_settings[setting] = (None, str(value)) - - response = self.session.post(self.get_url('newTwoPlantAPI.do?op=updatePlant'), files = form_settings) - - return response.json() - - def update_inverter_setting(self, serial_number, setting_type, - default_parameters, parameters): - """ - Applies settings for specified system based on serial number - See README for known working settings - - Arguments: - serial_number -- Serial number (device_sn) of the inverter (str) - setting_type -- Setting to be configured (str) - default_params -- Default set of parameters for the setting call (dict) - parameters -- Parameters to be sent to the system (dict or list of str) - (array which will be converted to a dictionary) - - Returns: - JSON response from the server whether the configuration was successful - """ - settings_parameters = parameters - - #If we've been passed an array then convert it into a dictionary - if isinstance(parameters, list): - settings_parameters = {} - for index, param in enumerate(parameters, start=1): - settings_parameters['param' + str(index)] = param - - settings_parameters = {**default_parameters, **settings_parameters} - - response = self.session.post(self.get_url('newTcpsetAPI.do'), - params=settings_parameters) - - return response.json() - - def update_mix_inverter_setting(self, serial_number, setting_type, parameters): - """ - Alias for setting inverter parameters on a mix inverter - See README for known working settings - - Arguments: - serial_number -- Serial number (device_sn) of the inverter (str) - setting_type -- Setting to be configured (str) - parameters -- Parameters to be sent to the system (dict or list of str) - (array which will be converted to a dictionary) - - Returns: - JSON response from the server whether the configuration was successful - """ - default_parameters = { - 'op': 'mixSetApiNew', - 'serialNum': serial_number, - 'type': setting_type - } - return self.update_inverter_setting(serial_number, setting_type, - default_parameters, parameters) - - def update_ac_inverter_setting(self, serial_number, setting_type, parameters): - """ - Alias for setting inverter parameters on an AC-coupled inverter - See README for known working settings - - Arguments: - serial_number -- Serial number (device_sn) of the inverter (str) - setting_type -- Setting to be configured (str) - parameters -- Parameters to be sent to the system (dict or list of str) - (array which will be converted to a dictionary) - - Returns: - JSON response from the server whether the configuration was successful - """ - default_parameters = { - 'op': 'spaSetApi', - 'serialNum': serial_number, - 'type': setting_type - } - return self.update_inverter_setting(serial_number, setting_type, - default_parameters, parameters) - - def update_tlx_inverter_time_segment(self, serial_number, segment_id, batt_mode, start_time, end_time, enabled): - """ - Updates the time segment settings for a TLX hybrid inverter. - - Arguments: - serial_number -- Serial number (device_sn) of the inverter (str) - segment_id -- ID of the time segment to be updated (int) - batt_mode -- Battery mode (int) - start_time -- Start time of the segment (datetime.time) - end_time -- End time of the segment (datetime.time) - enabled -- Whether the segment is enabled (bool) - - Returns: - JSON response from the server whether the configuration was successful - """ - params = { - 'op': 'tlxSet' - } - data = { - 'serialNum': serial_number, - 'type': f'time_segment{segment_id}', - 'param1': batt_mode, - 'param2': start_time.strftime('%H'), - 'param3': start_time.strftime('%M'), - 'param4': end_time.strftime('%H'), - 'param5': end_time.strftime('%M'), - 'param6': '1' if enabled else '0' - } - - response = self.session.post(self.get_url('newTcpsetAPI.do'), params=params, data=data) - result = response.json() - - if not result.get('success', False): - raise Exception(f"Failed to update TLX inverter time segment: {result.get('msg', 'Unknown error')}") - - return result - - def update_tlx_inverter_setting(self, serial_number, setting_type, parameter): - """ - Alias for setting parameters on a tlx hybrid inverter - See README for known working settings - - Arguments: - serial_number -- Serial number (device_sn) of the inverter (str) - setting_type -- Setting to be configured (str) - parameter -- Parameter(s) to be sent to the system (str, dict, list of str) - (array which will be converted to a dictionary) - - Returns: - JSON response from the server whether the configuration was successful - """ - default_parameters = { - 'op': 'tlxSet', - 'serialNum': serial_number, - 'type': setting_type - } - - # If parameter is a single value, convert it to a dictionary - if not isinstance(parameter, (dict, list)): - parameter = {'param1': parameter} - elif isinstance(parameter, list): - parameter = {f'param{index+1}': param for index, param in enumerate(parameter)} - - return self.update_inverter_setting(serial_number, setting_type, - default_parameters, parameter) - - - def update_noah_settings(self, serial_number, setting_type, parameters): - """ - Applies settings for specified noah device based on serial number - See README for known working settings - - Arguments: - serial_number -- Serial number (device_sn) of the noah (str) - setting_type -- Setting to be configured (str) - parameters -- Parameters to be sent to the system (dict or list of str) - (array which will be converted to a dictionary) - - Returns: - JSON response from the server whether the configuration was successful - """ - default_parameters = { - 'serialNum': serial_number, - 'type': setting_type - } - settings_parameters = parameters - - #If we've been passed an array then convert it into a dictionary - if isinstance(parameters, list): - settings_parameters = {} - for index, param in enumerate(parameters, start=1): - settings_parameters['param' + str(index)] = param - - settings_parameters = {**default_parameters, **settings_parameters} - - response = self.session.post(self.get_url('noahDeviceApi/noah/set'), - data=settings_parameters) - - return response.json() - +# Import everything from base_api to ensure backward compatibility +from .base_api import * +# Import the V1 API class +from .v1_api import GrowattApiV1 +# Define the name of the package +name = "growattServer" diff --git a/growattServer/base_api.py b/growattServer/base_api.py new file mode 100644 index 0000000..0f74fff --- /dev/null +++ b/growattServer/base_api.py @@ -0,0 +1,1178 @@ +import datetime +from enum import IntEnum +import requests +from random import randint +import warnings +import hashlib + +name = "growattServer" + +BATT_MODE_LOAD_FIRST = 0 +BATT_MODE_BATTERY_FIRST = 1 +BATT_MODE_GRID_FIRST = 2 + + +def hash_password(password): + """ + Normal MD5, except add c if a byte of the digest is less than 10. + """ + password_md5 = hashlib.md5(password.encode('utf-8')).hexdigest() + for i in range(0, len(password_md5), 2): + if password_md5[i] == '0': + password_md5 = password_md5[0:i] + 'c' + password_md5[i + 1:] + return password_md5 + + +class Timespan(IntEnum): + hour = 0 + day = 1 + month = 2 + + +class GrowattApi: + server_url = 'https://openapi.growatt.com/' + agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)" + + def __init__(self, add_random_user_id=False, agent_identifier=None): + if (agent_identifier != None): + self.agent_identifier = agent_identifier + + # If a random user id is required, generate a 5 digit number and add it to the user agent + if (add_random_user_id): + random_number = ''.join(["{}".format(randint(0, 9)) + for num in range(0, 5)]) + self.agent_identifier += " - " + random_number + + self.session = requests.Session() + self.session.hooks = { + 'response': lambda response, *args, **kwargs: response.raise_for_status() + } + + headers = {'User-Agent': self.agent_identifier} + self.session.headers.update(headers) + + def __get_date_string(self, timespan=None, date=None): + if timespan is not None: + assert timespan in Timespan + + if date is None: + date = datetime.datetime.now() + + date_str = "" + if timespan == Timespan.month: + date_str = date.strftime('%Y-%m') + else: + date_str = date.strftime('%Y-%m-%d') + + return date_str + + def get_url(self, page): + """ + Simple helper function to get the page URL. + """ + return self.server_url + page + + def login(self, username, password, is_password_hashed=False): + """ + Log the user in. + + Returns + 'data' -- A List containing Objects containing the folowing + 'plantName' -- Friendly name of the plant + 'plantId' -- The ID of the plant + 'service' + 'quality' + 'isOpenSmartFamily' + 'totalData' -- An Object + 'success' -- True or False + 'msg' + 'app_code' + 'user' -- An Object containing a lot of user information + 'uid' + 'userLanguage' + 'inverterGroup' -- A List + 'timeZone' -- A Number + 'lat' + 'lng' + 'dataAcqList' -- A List + 'type' + 'accountName' -- The username + 'password' -- The password hash of the user + 'isValiPhone' + 'kind' + 'mailNotice' -- True or False + 'id' + 'lasLoginIp' + 'lastLoginTime' + 'userDeviceType' + 'phoneNum' + 'approved' -- True or False + 'area' -- Continent of the user + 'smsNotice' -- True or False + 'isAgent' + 'token' + 'nickName' + 'parentUserId' + 'customerCode' + 'country' + 'isPhoneNumReg' + 'createDate' + 'rightlevel' + 'appType' + 'serverUrl' + 'roleId' + 'enabled' -- True or False + 'agentCode' + 'inverterList' -- A list + 'email' + 'company' + 'activeName' + 'codeIndex' + 'appAlias' + 'isBigCustomer' + 'noticeType' + """ + if not is_password_hashed: + password = hash_password(password) + + response = self.session.post(self.get_url('newTwoLoginAPI.do'), data={ + 'userName': username, + 'password': password + }) + + data = response.json()['back'] + if data['success']: + data.update({ + 'userId': data['user']['id'], + 'userLevel': data['user']['rightlevel'] + }) + return data + + def plant_list(self, user_id): + """ + Get a list of plants connected to this account. + + Args: + user_id (str): The ID of the user. + + Returns: + list: A list of plants connected to the account. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get( + self.get_url('PlantListAPI.do'), + params={'userId': user_id}, + allow_redirects=False + ) + + return response.json().get('back', []) + + def plant_detail(self, plant_id, timespan, date=None): + """ + Get plant details for specified timespan. + + Args: + plant_id (str): The ID of the plant. + timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the plant details. + + Raises: + Exception: If the request to the server fails. + """ + date_str = self.__get_date_string(timespan, date) + + response = self.session.get(self.get_url('PlantDetailAPI.do'), params={ + 'plantId': plant_id, + 'type': timespan.value, + 'date': date_str + }) + + return response.json().get('back', {}) + + def plant_list_two(self): + """ + Get a list of all plants with detailed information. + + Returns: + list: A list of plants with detailed information. + """ + response = self.session.post( + self.get_url('newTwoPlantAPI.do'), + params={'op': 'getAllPlantListTwo'}, + data={ + 'language': '1', + 'nominalPower': '', + 'order': '1', + 'pageSize': '15', + 'plantName': '', + 'plantStatus': '', + 'toPageNum': '1' + } + ) + + return response.json().get('PlantList', []) + + def inverter_data(self, inverter_id, date=None): + """ + Get inverter data for specified date or today. + + Args: + inverter_id (str): The ID of the inverter. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the inverter data. + + Raises: + Exception: If the request to the server fails. + """ + date_str = self.__get_date_string(date=date) + response = self.session.get(self.get_url('newInverterAPI.do'), params={ + 'op': 'getInverterData', + 'id': inverter_id, + 'type': 1, + 'date': date_str + }) + + return response.json() + + def inverter_detail(self, inverter_id): + """ + Get detailed data from PV inverter. + + Args: + inverter_id (str): The ID of the inverter. + + Returns: + dict: A dictionary containing the inverter details. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get(self.get_url('newInverterAPI.do'), params={ + 'op': 'getInverterDetailData', + 'inverterId': inverter_id + }) + + return response.json() + + def inverter_detail_two(self, inverter_id): + """ + Get detailed data from PV inverter (alternative endpoint). + + Args: + inverter_id (str): The ID of the inverter. + + Returns: + dict: A dictionary containing the inverter details. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get(self.get_url('newInverterAPI.do'), params={ + 'op': 'getInverterDetailData_two', + 'inverterId': inverter_id + }) + + return response.json() + + def tlx_system_status(self, plant_id, tlx_id): + """ + Get status of the system + + Args: + plant_id (str): The ID of the plant. + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing system status. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.get_url("newTlxApi.do"), + params={"op": "getSystemStatus_KW"}, + data={"plantId": plant_id, + "id": tlx_id} + ) + + return response.json().get('obj', {}) + + def tlx_energy_overview(self, plant_id, tlx_id): + """ + Get energy overview + + Args: + plant_id (str): The ID of the plant. + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing energy data. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.get_url("newTlxApi.do"), + params={"op": "getEnergyOverview"}, + data={"plantId": plant_id, + "id": tlx_id} + ) + + return response.json().get('obj', {}) + + def tlx_energy_prod_cons(self, plant_id, tlx_id, timespan=Timespan.hour, date=None): + """ + Get energy production and consumption (KW) + + Args: + tlx_id (str): The ID of the TLX inverter. + timespan (Timespan): The ENUM value conforming to the time window you want e.g. hours from today, days, or months. + date (datetime): The date you are interested in. + + Returns: + dict: A dictionary containing energy data. + + Raises: + Exception: If the request to the server fails. + """ + + date_str = self.__get_date_string(timespan, date) + + response = self.session.post( + self.get_url("newTlxApi.do"), + params={"op": "getEnergyProdAndCons_KW"}, + data={'date': date_str, + "plantId": plant_id, + "language": "1", + "id": tlx_id, + "type": timespan.value} + ) + + return response.json().get('obj', {}) + + def tlx_data(self, tlx_id, date=None): + """ + Get TLX inverter data for specified date or today. + + Args: + tlx_id (str): The ID of the TLX inverter. + date (datetime, optional): The date you are interested in. Defaults to datetime.datetime.now(). + + Returns: + dict: A dictionary containing the TLX inverter data. + + Raises: + Exception: If the request to the server fails. + """ + date_str = self.__get_date_string(date=date) + response = self.session.get(self.get_url('newTlxApi.do'), params={ + 'op': 'getTlxData', + 'id': tlx_id, + 'type': 1, + 'date': date_str + }) + + return response.json() + + def tlx_detail(self, tlx_id): + """ + Get detailed data from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the detailed TLX inverter data. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get(self.get_url('newTlxApi.do'), params={ + 'op': 'getTlxDetailData', + 'id': tlx_id + }) + + return response.json() + + def tlx_params(self, tlx_id): + """ + Get parameters for TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the TLX inverter parameters. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.get(self.get_url('newTlxApi.do'), params={ + 'op': 'getTlxParams', + 'id': tlx_id + }) + + return response.json() + + def tlx_all_settings(self, tlx_id): + """ + Get all possible settings from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing all possible settings for the TLX inverter. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post(self.get_url('newTlxApi.do'), params={ + 'op': 'getTlxSetData' + }, data={ + 'serialNum': tlx_id + }) + + return response.json().get('obj', {}).get('tlxSetBean') + + def tlx_enabled_settings(self, tlx_id): + """ + Get "Enabled settings" from TLX inverter. + + Args: + tlx_id (str): The ID of the TLX inverter. + + Returns: + dict: A dictionary containing the enabled settings. + + Raises: + Exception: If the request to the server fails. + """ + string_time = datetime.datetime.now().strftime('%Y-%m-%d') + response = self.session.post( + self.get_url('newLoginAPI.do'), + params={'op': 'getSetPass'}, + data={'deviceSn': tlx_id, 'stringTime': string_time, 'type': '5'} + ) + + return response.json().get('obj', {}) + + def tlx_battery_info(self, serial_num): + """ + Get battery information. + + Args: + serial_num (str): The serial number of the battery. + + Returns: + dict: A dictionary containing the battery information. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.get_url('newTlxApi.do'), + params={'op': 'getBatInfo'}, + data={'lan': 1, 'serialNum': serial_num} + ) + + return response.json().get('obj', {}) + + def tlx_battery_info_detailed(self, plant_id, serial_num): + """ + Get detailed battery information. + + Args: + plant_id (str): The ID of the plant. + serial_num (str): The serial number of the battery. + + Returns: + dict: A dictionary containing the detailed battery information. + + Raises: + Exception: If the request to the server fails. + """ + response = self.session.post( + self.get_url('newTlxApi.do'), + params={'op': 'getBatDetailData'}, + data={'lan': 1, 'plantId': plant_id, 'id': serial_num} + ) + + return response.json() + + def mix_info(self, mix_id, plant_id=None): + """ + Returns high level values from Mix device + + Keyword arguments: + mix_id -- The serial number (device_sn) of the inverter + plant_id -- The ID of the plant (the mobile app uses this but it does not appear to be necessary) (default None) + + Returns: + 'acChargeEnergyToday' -- ??? 2.7 + 'acChargeEnergyTotal' -- ??? 25.3 + 'acChargePower' -- ??? 0 + 'capacity': '45' -- The current remaining capacity of the batteries (same as soc but without the % sign) + 'eBatChargeToday' -- Battery charged today in kWh + 'eBatChargeTotal' -- Battery charged total (all time) in kWh + 'eBatDisChargeToday' -- Battery discharged today in kWh + 'eBatDisChargeTotal' -- Battery discharged total (all time) in kWh + 'epvToday' -- Energy generated from PVs today in kWh + 'epvTotal' -- Energy generated from PVs total (all time) in kWh + 'isCharge'-- ??? 0 - Possible a 0/1 based on whether or not the battery is charging + 'pCharge1' -- ??? 0 + 'pDischarge1' -- Battery discharging rate in W + 'soc' -- Statement of charge including % symbol + 'upsPac1' -- ??? 0 + 'upsPac2' -- ??? 0 + 'upsPac3' -- ??? 0 + 'vbat' -- Battery Voltage + 'vbatdsp' -- ??? 51.8 + 'vpv1' -- Voltage PV1 + 'vpv2' -- Voltage PV2 + """ + request_params = { + 'op': 'getMixInfo', + 'mixId': mix_id + } + + if (plant_id): + request_params['plantId'] = plant_id + + response = self.session.get(self.get_url( + 'newMixApi.do'), params=request_params) + + return response.json().get('obj', {}) + + def mix_totals(self, mix_id, plant_id): + """ + Returns "Totals" values from Mix device + + Keyword arguments: + mix_id -- The serial number (device_sn) of the inverter + plant_id -- The ID of the plant + + Returns: + 'echargetoday' -- Battery charged today in kWh (same as eBatChargeToday from mix_info) + 'echargetotal' -- Battery charged total (all time) in kWh (same as eBatChargeTotal from mix_info) + 'edischarge1Today' -- Battery discharged today in kWh (same as eBatDisChargeToday from mix_info) + 'edischarge1Total' -- Battery discharged total (all time) in kWh (same as eBatDisChargeTotal from mix_info) + 'elocalLoadToday' -- Load consumption today in kWh + 'elocalLoadTotal' -- Load consumption total (all time) in kWh + 'epvToday' -- Energy generated from PVs today in kWh (same as epvToday from mix_info) + 'epvTotal' -- Energy generated from PVs total (all time) in kWh (same as epvTotal from mix_info) + 'etoGridToday' -- Energy exported to the grid today in kWh + 'etogridTotal' -- Energy exported to the grid total (all time) in kWh + 'photovoltaicRevenueToday' -- Revenue earned from PV today in 'unit' currency + 'photovoltaicRevenueTotal' -- Revenue earned from PV total (all time) in 'unit' currency + 'unit' -- Unit of currency for 'Revenue' + """ + response = self.session.post(self.get_url('newMixApi.do'), params={ + 'op': 'getEnergyOverview', + 'mixId': mix_id, + 'plantId': plant_id + }) + + return response.json().get('obj', {}) + + def mix_system_status(self, mix_id, plant_id): + """ + Returns current "Status" from Mix device + + Keyword arguments: + mix_id -- The serial number (device_sn) of the inverter + plant_id -- The ID of the plant + + Returns: + 'SOC' -- Statement of charge (remaining battery %) + 'chargePower' -- Battery charging rate in kw + 'fAc' -- Frequency (Hz) + 'lost' -- System status e.g. 'mix.status.normal' + 'pLocalLoad' -- Load conumption in kW + 'pPv1' -- PV1 Wattage in W + 'pPv2' -- PV2 Wattage in W + 'pactogrid' -- Export to grid rate in kW + 'pactouser' -- Import from grid rate in kW + 'pdisCharge1' -- Discharging batteries rate in kW + 'pmax' -- ??? 6 ??? PV Maximum kW ?? + 'ppv' -- PV combined Wattage in kW + 'priorityChoose' -- Priority setting - 0=Local load + 'status' -- System statue - ENUM - Unknown values + 'unit' -- Unit of measurement e.g. 'kW' + 'upsFac' -- ??? 0 + 'upsVac1' -- ??? 0 + 'uwSysWorkMode' -- ??? 6 + 'vAc1' -- Grid voltage in V + 'vBat' -- Battery voltage in V + 'vPv1' -- PV1 voltage in V + 'vPv2' -- PV2 voltage in V + 'vac1' -- Grid voltage in V (same as vAc1) + 'wBatteryType' -- ??? 1 + """ + response = self.session.post(self.get_url('newMixApi.do'), params={ + 'op': 'getSystemStatus_KW', + 'mixId': mix_id, + 'plantId': plant_id + }) + + return response.json().get('obj', {}) + + def mix_detail(self, mix_id, plant_id, timespan=Timespan.hour, date=None): + """ + Get Mix details for specified timespan + + Keyword arguments: + mix_id -- The serial number (device_sn) of the inverter + plant_id -- The ID of the plant + timespan -- The ENUM value conforming to the time window you want e.g. hours from today, days, or months (Default Timespan.hour) + date -- The date you are interested in (Default datetime.datetime.now()) + + Returns: + A chartData object where each entry is for a specific 5 minute window e.g. 00:05 and 00:10 respectively (below) + 'chartData': { '00:05': { 'pacToGrid' -- Export rate to grid in kW + 'pacToUser' -- Import rate from grid in kW + 'pdischarge' -- Battery discharge in kW + 'ppv' -- Solar generation in kW + 'sysOut' -- Load consumption in kW + }, + '00:10': { 'pacToGrid': '0', + 'pacToUser': '0.93', + 'pdischarge': '0', + 'ppv': '0', + 'sysOut': '0.93'}, + ...... + } + 'eAcCharge' -- Exported to grid in kWh + 'eCharge' -- System production in kWh = Self-consumption + Exported to Grid + 'eChargeToday' -- Load consumption from solar in kWh + 'eChargeToday1' -- Self-consumption in kWh + 'eChargeToday2' -- Self-consumption in kWh (eChargeToday + echarge1) + 'echarge1' -- Load consumption from battery in kWh + 'echargeToat' -- Total battery discharged (all time) in kWh + 'elocalLoad' -- Load consumption in kW (battery + solar + imported) + 'etouser' -- Load consumption imported from grid in kWh + 'photovoltaic' -- Load consumption from solar in kWh (same as eChargeToday) + 'ratio1' -- % of system production that is self-consumed + 'ratio2' -- % of system production that is exported + 'ratio3' -- % of Load consumption that is "self consumption" + 'ratio4' -- % of Load consumption that is "imported from grid" + 'ratio5' -- % of Self consumption that is directly from Solar + 'ratio6' -- % of Self consumption that is from batteries + 'unit' -- Unit of measurement e.g kWh + 'unit2' -- Unit of measurement e.g kW + + + NOTE - It is possible to calculate the PV generation that went into charging the batteries by performing the following calculation: + Solar to Battery = Solar Generation - Export to Grid - Load consumption from solar + epvToday (from mix_info) - eAcCharge - eChargeToday + """ + date_str = self.__get_date_string(timespan, date) + + response = self.session.post(self.get_url('newMixApi.do'), params={ + 'op': 'getEnergyProdAndCons_KW', + 'plantId': plant_id, + 'mixId': mix_id, + 'type': timespan.value, + 'date': date_str + }) + + return response.json().get('obj', {}) + + def dashboard_data(self, plant_id, timespan=Timespan.hour, date=None): + """ + Get 'dashboard' data for specified timespan + NOTE - All numerical values returned by this api call include units e.g. kWh or % + - Many of the 'total' values that are returned for a Mix system are inaccurate on the system this was tested against. + However, the statistics that are correct are not available on any other interface, plus these values may be accurate for + non-mix types of system. Where the values have been proven to be inaccurate they are commented below. + + Keyword arguments: + plant_id -- The ID of the plant + timespan -- The ENUM value conforming to the time window you want e.g. hours from today, days, or months (Default Timespan.hour) + date -- The date you are interested in (Default datetime.datetime.now()) + + Returns: + A chartData object where each entry is for a specific 5 minute window e.g. 00:05 and 00:10 respectively (below) + NOTE: The keys are interpreted differently, the examples below describe what they are used for in a 'Mix' system + 'chartData': { '00:05': { 'pacToUser' -- Power from battery in kW + 'ppv' -- Solar generation in kW + 'sysOut' -- Load consumption in kW + 'userLoad' -- Export in kW + }, + '00:10': { 'pacToUser': '0', + 'ppv': '0', + 'sysOut': '0.7', + 'userLoad': '0'}, + ...... + } + 'chartDataUnit' -- Unit of measurement e.g. 'kW', + 'eAcCharge' -- Energy exported to the grid in kWh e.g. '20.5kWh' (not accurate for Mix systems) + 'eCharge' -- System production in kWh = Self-consumption + Exported to Grid e.g '23.1kWh' (not accurate for Mix systems - actually showing the total 'load consumption' + 'eChargeToday1' -- Self-consumption of PPV (possibly including excess diverted to batteries) in kWh e.g. '2.6kWh' (not accurate for Mix systems) + 'eChargeToday2' -- Total self-consumption (PPV consumption(eChargeToday2Echarge1) + Battery Consumption(echarge1)) e.g. '10.1kWh' (not accurate for Mix systems) + 'eChargeToday2Echarge1' -- Self-consumption of PPV only e.g. '0.8kWh' (not accurate for Mix systems) + 'echarge1' -- Self-consumption from Battery only e.g. '9.3kWh' + 'echargeToat' -- Not used on Dashboard view, likely to be total battery discharged e.g. '152.1kWh' + 'elocalLoad' -- Total load consumption (etouser + eChargeToday2) e.g. '20.3kWh', (not accurate for Mix systems) + 'etouser'-- Energy imported from grid today (includes both directly used by load and AC battery charging e.g. '10.2kWh' + 'keyNames' -- Keys to be used for the graph data e.g. ['Solar', 'Load Consumption', 'Export To Grid', 'From Battery'] + 'photovoltaic' -- Same as eChargeToday2Echarge1 e.g. '0.8kWh' + 'ratio1' -- % of 'Solar production' that is self-consumed e.g. '11.3%' (not accurate for Mix systems) + 'ratio2' -- % of 'Solar production' that is exported e.g. '88.7%' (not accurate for Mix systems) + 'ratio3' -- % of 'Load consumption' that is self consumption e.g. '49.8%' (not accurate for Mix systems) + 'ratio4' -- % of 'Load consumption' that is imported from the grid e.g '50.2%' (not accurate for Mix systems) + 'ratio5' -- % of Self consumption that is from batteries e.g. '92.1%' (not accurate for Mix systems) + 'ratio6' -- % of Self consumption that is directly from Solar e.g. '7.9%' (not accurate for Mix systems) + + NOTE: Does not return any data for a tlx system. Use plant_energy_data() instead. + """ + date_str = self.__get_date_string(timespan, date) + + response = self.session.post(self.get_url('newPlantAPI.do'), params={ + 'action': "getEnergyStorageData", + 'date': date_str, + 'type': timespan.value, + 'plantId': plant_id + }) + + return response.json() + + def plant_settings(self, plant_id): + """ + Returns a dictionary containing the settings for the specified plant + + Keyword arguments: + plant_id -- The id of the plant you want the settings of + + Returns: + A python dictionary containing the settings for the specified plant + """ + response = self.session.get(self.get_url('newPlantAPI.do'), params={ + 'op': 'getPlant', + 'plantId': plant_id + }) + + return response.json() + + def storage_detail(self, storage_id): + """ + Get "All parameters" from battery storage. + """ + response = self.session.get(self.get_url('newStorageAPI.do'), params={ + 'op': 'getStorageInfo_sacolar', + 'storageId': storage_id + }) + + return response.json() + + def storage_params(self, storage_id): + """ + Get much more detail from battery storage. + """ + response = self.session.get(self.get_url('newStorageAPI.do'), params={ + 'op': 'getStorageParams_sacolar', + 'storageId': storage_id + }) + + return response.json() + + def storage_energy_overview(self, plant_id, storage_id): + """ + Get some energy/generation overview data. + """ + response = self.session.post(self.get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={ + 'plantId': plant_id, + 'storageSn': storage_id + }) + + return response.json().get('obj', {}) + + def inverter_list(self, plant_id): + """ + Use device_list, it's more descriptive since the list contains more than inverters. + """ + warnings.warn( + "This function may be deprecated in the future because naming is not correct, use device_list instead", DeprecationWarning) + return self.device_list(plant_id) + + def __get_all_devices(self, plant_id): + """ + Get basic plant information with device list. + """ + response = self.session.get(self.get_url('newTwoPlantAPI.do'), + params={'op': 'getAllDeviceList', + 'plantId': plant_id, + 'language': 1}) + + return response.json().get('deviceList', {}) + + def device_list(self, plant_id): + """ + Get a list of all devices connected to plant. + """ + + device_list = self.plant_info(plant_id).get('deviceList', []) + + if not device_list: + # for tlx systems, the device_list in plant is empty, so use __get_all_devices() instead + device_list = self.__get_all_devices(plant_id) + + return device_list + + def plant_info(self, plant_id): + """ + Get basic plant information with device list. + """ + response = self.session.get(self.get_url('newTwoPlantAPI.do'), params={ + 'op': 'getAllDeviceListTwo', + 'plantId': plant_id, + 'pageNum': 1, + 'pageSize': 1 + }) + + return response.json() + + def plant_energy_data(self, plant_id): + """ + Get the energy data used in the 'Plant' tab in the phone + """ + response = self.session.post(self.get_url('newTwoPlantAPI.do'), + params={ + 'op': 'getUserCenterEnertyDataByPlantid'}, + data={'language': 1, + 'plantId': plant_id}) + + return response.json() + + def is_plant_noah_system(self, plant_id): + """ + Returns a dictionary containing if noah devices are configured for the specified plant + + Keyword arguments: + plant_id -- The id of the plant you want the noah devices of (str) + + Returns + 'msg' + 'result' -- True or False + 'obj' -- An Object containing if noah devices are configured + 'isPlantNoahSystem' -- Is the specified plant a noah system (True or False) + 'plantId' -- The ID of the plant + 'isPlantHaveNoah' -- Are noah devices configured in the specified plant (True or False) + 'deviceSn' -- Serial number of the configured noah device + 'plantName' -- Friendly name of the plant + """ + response = self.session.post(self.get_url('noahDeviceApi/noah/isPlantNoahSystem'), data={ + 'plantId': plant_id + }) + return response.json() + + def noah_system_status(self, serial_number): + """ + Returns a dictionary containing the status for the specified Noah Device + + Keyword arguments: + serial_number -- The Serial number of the noah device you want the status of (str) + + Returns + 'msg' + 'result' -- True or False + 'obj' -- An Object containing the noah device status + 'chargePower' -- Battery charging rate in watt e.g. '200Watt' + 'workMode' -- Workingmode of the battery (0 = Load First, 1 = Battery First) + 'soc' -- Statement of charge (remaining battery %) + 'associatedInvSn' -- ??? + 'batteryNum' -- Numbers of batterys + 'profitToday' -- Today generated profit through noah device + 'plantId' -- The ID of the plant + 'disChargePower' -- Battery discharging rate in watt e.g. '200Watt' + 'eacTotal' -- Total energy exported to the grid in kWh e.g. '20.5kWh' + 'eacToday' -- Today energy exported to the grid in kWh e.g. '20.5kWh' + 'pac' -- Export to grid rate in watt e.g. '200Watt' + 'ppv' -- Solar generation in watt e.g. '200Watt' + 'alias' -- Friendly name of the noah device + 'profitTotal' -- Total generated profit through noah device + 'moneyUnit' -- Unit of currency e.g. '€' + 'status' -- Is the noah device online (True or False) + """ + response = self.session.post(self.get_url('noahDeviceApi/noah/getSystemStatus'), data={ + 'deviceSn': serial_number + }) + return response.json() + + def noah_info(self, serial_number): + """ + Returns a dictionary containing the informations for the specified Noah Device + + Keyword arguments: + serial_number -- The Serial number of the noah device you want the informations of (str) + + Returns + 'msg' + 'result' -- True or False + 'obj' -- An Object containing the noah device informations + 'neoList' -- A List containing Objects + 'unitList' -- A Object containing currency units e.g. "Euro": "euro", "DOLLAR": "dollar" + 'noah' -- A Object containing the folowing + 'time_segment' -- A List containing Objects with configured "Operation Mode" + NOTE: The keys are generated numerical, the values are generated with folowing syntax "[workingmode (0 = Load First, 1 = Battery First)]_[starttime]_[endtime]_[output power]" + 'time_segment': { + 'time_segment1': "0_0:0_8:0_150", ([Load First]_[00:00]_[08:00]_[150 watt]) + 'time_segment2': "1_8:0_18:0_0", ([Battery First]_[08:00]_[18:00]_[0 watt]) + .... + } + 'batSns' -- A List containing all battery Serial Numbers + 'associatedInvSn' -- ??? + 'plantId' -- The ID of the plant + 'chargingSocHighLimit' -- Configured "Battery Management" charging upper limit + 'chargingSocLowLimit' -- Configured "Battery Management" charging lower limit + 'defaultPower' -- Configured "System Default Output Power" + 'version' -- The Firmware Version of the noah device + 'deviceSn' -- The Serial number of the noah device + 'formulaMoney' -- Configured "Select Currency" energy cost per kWh e.g. '0.22' + 'alias' -- Friendly name of the noah device + 'model' -- Model Name of the noah device + 'plantName' -- Friendly name of the plant + 'tempType' -- ??? + 'moneyUnitText' -- Configured "Select Currency" (Value from the unitList) e.G. "euro" + 'plantList' -- A List containing Objects containing the folowing + 'plantId' -- The ID of the plant + 'plantImgName' -- Friendly name of the plant Image + 'plantName' -- Friendly name of the plant + """ + response = self.session.post(self.get_url('noahDeviceApi/noah/getNoahInfoBySn'), data={ + 'deviceSn': serial_number + }) + return response.json() + + def update_plant_settings(self, plant_id, changed_settings, current_settings=None): + """ + Applies settings to the plant e.g. ID, Location, Timezone + See README for all possible settings options + + Keyword arguments: + plant_id -- The id of the plant you wish to update the settings for + changed_settings -- A python dictionary containing the settings to be changed and their value + current_settings -- A python dictionary containing the current settings of the plant (use the response from plant_settings), if None - fetched for you + + Returns: + A response from the server stating whether the configuration was successful or not + """ + # If no existing settings have been provided then get them from the growatt server + if current_settings == None: + current_settings = self.plant_settings(plant_id) + + # These are the parameters that the form requires, without these an error is thrown. Pre-populate their values with the current values + form_settings = { + 'plantCoal': (None, str(current_settings['formulaCoal'])), + 'plantSo2': (None, str(current_settings['formulaSo2'])), + 'accountName': (None, str(current_settings['userAccount'])), + 'plantID': (None, str(current_settings['id'])), + # Hardcoded to 0 as I can't work out what value it should have + 'plantFirm': (None, '0'), + 'plantCountry': (None, str(current_settings['country'])), + 'plantType': (None, str(current_settings['plantType'])), + 'plantIncome': (None, str(current_settings['formulaMoneyStr'])), + 'plantAddress': (None, str(current_settings['plantAddress'])), + 'plantTimezone': (None, str(current_settings['timezone'])), + 'plantLng': (None, str(current_settings['plant_lng'])), + 'plantCity': (None, str(current_settings['city'])), + 'plantCo2': (None, str(current_settings['formulaCo2'])), + 'plantMoney': (None, str(current_settings['formulaMoneyUnitId'])), + 'plantPower': (None, str(current_settings['nominalPower'])), + 'plantLat': (None, str(current_settings['plant_lat'])), + 'plantDate': (None, str(current_settings['createDateText'])), + 'plantName': (None, str(current_settings['plantName'])), + } + + # Overwrite the current value of the setting with the new value + for setting, value in changed_settings.items(): + form_settings[setting] = (None, str(value)) + + response = self.session.post(self.get_url( + 'newTwoPlantAPI.do?op=updatePlant'), files=form_settings) + + return response.json() + + def update_inverter_setting(self, serial_number, setting_type, + default_parameters, parameters): + """ + Applies settings for specified system based on serial number + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + setting_type -- Setting to be configured (str) + default_params -- Default set of parameters for the setting call (dict) + parameters -- Parameters to be sent to the system (dict or list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + settings_parameters = parameters + + # If we've been passed an array then convert it into a dictionary + if isinstance(parameters, list): + settings_parameters = {} + for index, param in enumerate(parameters, start=1): + settings_parameters['param' + str(index)] = param + + settings_parameters = {**default_parameters, **settings_parameters} + + response = self.session.post(self.get_url('newTcpsetAPI.do'), + params=settings_parameters) + + return response.json() + + def update_mix_inverter_setting(self, serial_number, setting_type, parameters): + """ + Alias for setting inverter parameters on a mix inverter + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + setting_type -- Setting to be configured (str) + parameters -- Parameters to be sent to the system (dict or list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + default_parameters = { + 'op': 'mixSetApiNew', + 'serialNum': serial_number, + 'type': setting_type + } + return self.update_inverter_setting(serial_number, setting_type, + default_parameters, parameters) + + def update_ac_inverter_setting(self, serial_number, setting_type, parameters): + """ + Alias for setting inverter parameters on an AC-coupled inverter + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + setting_type -- Setting to be configured (str) + parameters -- Parameters to be sent to the system (dict or list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + default_parameters = { + 'op': 'spaSetApi', + 'serialNum': serial_number, + 'type': setting_type + } + return self.update_inverter_setting(serial_number, setting_type, + default_parameters, parameters) + + def update_tlx_inverter_time_segment(self, serial_number, segment_id, batt_mode, start_time, end_time, enabled): + """ + Updates the time segment settings for a TLX hybrid inverter. + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + segment_id -- ID of the time segment to be updated (int) + batt_mode -- Battery mode (int) + start_time -- Start time of the segment (datetime.time) + end_time -- End time of the segment (datetime.time) + enabled -- Whether the segment is enabled (bool) + + Returns: + JSON response from the server whether the configuration was successful + """ + params = { + 'op': 'tlxSet' + } + data = { + 'serialNum': serial_number, + 'type': f'time_segment{segment_id}', + 'param1': batt_mode, + 'param2': start_time.strftime('%H'), + 'param3': start_time.strftime('%M'), + 'param4': end_time.strftime('%H'), + 'param5': end_time.strftime('%M'), + 'param6': '1' if enabled else '0' + } + + response = self.session.post(self.get_url( + 'newTcpsetAPI.do'), params=params, data=data) + result = response.json() + + if not result.get('success', False): + raise Exception( + f"Failed to update TLX inverter time segment: {result.get('msg', 'Unknown error')}") + + return result + + def update_tlx_inverter_setting(self, serial_number, setting_type, parameter): + """ + Alias for setting parameters on a tlx hybrid inverter + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the inverter (str) + setting_type -- Setting to be configured (str) + parameter -- Parameter(s) to be sent to the system (str, dict, list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + default_parameters = { + 'op': 'tlxSet', + 'serialNum': serial_number, + 'type': setting_type + } + + # If parameter is a single value, convert it to a dictionary + if not isinstance(parameter, (dict, list)): + parameter = {'param1': parameter} + elif isinstance(parameter, list): + parameter = {f'param{index+1}': param for index, + param in enumerate(parameter)} + + return self.update_inverter_setting(serial_number, setting_type, + default_parameters, parameter) + + def update_noah_settings(self, serial_number, setting_type, parameters): + """ + Applies settings for specified noah device based on serial number + See README for known working settings + + Arguments: + serial_number -- Serial number (device_sn) of the noah (str) + setting_type -- Setting to be configured (str) + parameters -- Parameters to be sent to the system (dict or list of str) + (array which will be converted to a dictionary) + + Returns: + JSON response from the server whether the configuration was successful + """ + default_parameters = { + 'serialNum': serial_number, + 'type': setting_type + } + settings_parameters = parameters + + # If we've been passed an array then convert it into a dictionary + if isinstance(parameters, list): + settings_parameters = {} + for index, param in enumerate(parameters, start=1): + settings_parameters['param' + str(index)] = param + + settings_parameters = {**default_parameters, **settings_parameters} + + response = self.session.post(self.get_url('noahDeviceApi/noah/set'), + data=settings_parameters) + + return response.json() diff --git a/growattServer/v1_api.py b/growattServer/v1_api.py new file mode 100644 index 0000000..8de4f20 --- /dev/null +++ b/growattServer/v1_api.py @@ -0,0 +1,605 @@ +import warnings +from datetime import date, timedelta +from . import GrowattApi +import platform + + +class GrowattApiV1(GrowattApi): + """ + Extended Growatt API client with V1 API support. + This class extends the base GrowattApi class with methods for the V1 API. + """ + + def _create_user_agent(self): + python_version = platform.python_version() + system = platform.system() + release = platform.release() + machine = platform.machine() + + user_agent = f"Python/{python_version} ({system} {release}; {machine})" + return user_agent + + def __init__(self, token): + """ + Initialize the Growatt API client with V1 API support. + + Args: + token (str): API token for authentication (required for V1 API access). + """ + # Initialize the base class + super().__init__(agent_identifier=self._create_user_agent()) + + # Add V1 API specific properties + self.api_url = f"{self.server_url}v1/" + + # Set up authentication for V1 API using the provided token + print("Using token-based authentication") + # Use token-based auth for V1 API + self.session.headers.update({"token": token}) + + def get_v1_url(self, page): + """ + Simple helper function to get the page URL for v1 API. + """ + return self.api_url + page + + def plant_list_v1(self): + """ + Get a list of all plants with detailed information. + + Returns: + list: A list of plants with detailed information. + """ + # Prepare request data + request_data = { + 'page': '', + 'perpage': '', + 'search_type': '', + 'search_keyword': '' + } + + # Make the request + response = self.session.get( + url=self.get_v1_url('plant/list'), + data=request_data + ) + + return response.json() + + def plant_details_v1(self, plant_id): + """ + Get basic information about a power station. + + Args: + plant_id (int): Power Station ID + + Returns: + dict: A dictionary containing the plant details. + + """ + + response = self.session.get( + self.get_v1_url('plant/details'), + params={'plant_id': plant_id} + ) + + return response.json() + + def plant_energy_overview_v1(self, plant_id): + """ + Get an overview of a plant's energy data. + + Args: + plant_id (int): Power Station ID + + Returns: + dict: A dictionary containing the plant energy overview. + + """ + + response = self.session.get( + self.get_v1_url('plant/data'), + params={'plant_id': plant_id} + ) + + return response.json() + + def plant_energy_history_v1(self, plant_id, start_date=None, end_date=None, time_unit="day", page=None, perpage=None): + """ + Retrieve plant energy data for multiple days/months/years. + + Args: + plant_id (int): Power Station ID + start_date (date, optional): Start Date - defaults to today + end_date (date, optional): End Date - defaults to today + time_unit (str, optional): Time unit ('day', 'month', 'year') - defaults to 'day' + page (int, optional): Page number - defaults to 1 + perpage (int, optional): Number of items per page - defaults to 20, max 100 + + Returns: + dict: A dictionary containing the plant energy history. + + Notes: + - When time_unit is 'day', date interval cannot exceed 7 days + - When time_unit is 'month', start date must be within same or previous year + - When time_unit is 'year', date interval must not exceed 20 years + + """ + + if start_date is None and end_date is None: + start_date = date.today() + end_date = date.today() + elif start_date is None: + start_date = end_date + elif end_date is None: + end_date = start_date + + # Validate date ranges based on time_unit + if time_unit == "day" and (end_date - start_date).days > 7: + warnings.warn( + "Date interval must not exceed 7 days in 'day' mode.", RuntimeWarning) + elif time_unit == "month" and (end_date.year - start_date.year > 1): + warnings.warn( + "Start date must be within same or previous year in 'month' mode.", RuntimeWarning) + elif time_unit == "year" and (end_date.year - start_date.year > 20): + warnings.warn( + "Date interval must not exceed 20 years in 'year' mode.", RuntimeWarning) + + response = self.session.get( + self.get_v1_url('plant/energy'), + params={ + 'plant_id': plant_id, + 'start_date': start_date.strftime("%Y-%m-%d"), + 'end_date': end_date.strftime("%Y-%m-%d"), + 'time_unit': time_unit, + 'page': page, + 'perpage': perpage + } + ) + + return response.json() + + def device_list_v1(self, plant_id): + """ + Get devices associated with plant. + + Note: + returned "device_type" mappings: + 1: inverter (including MAX) + 2: storage + 3: other + 4: max (single MAX) + 5: sph + 6: spa + 7: min (including TLX) + 8: pcs + 9: hps + 10: pbd + + Args: + plant_id (int): Power Station ID + + Returns: + DeviceList + e.g. + { + "data": { + "count": 3, + "devices": [ + { + "device_sn": "ZT00100001", + "last_update_time": "2018-12-13 11:03:52", + "model": "A0B0D0T0PFU1M3S4", + "lost": True, + "status": 0, + "manufacturer": "Growatt", + "device_id": 116, + "datalogger_sn": "CRAZT00001", + "type": 1 + }, + ] + }, + "error_code": 0, + "error_msg": "" + } + """ + response = self.session.get( + url=self.get_v1_url("device/list"), + params={ + "plant_id": plant_id, + "page": "", + "perpage": "", + }, + ) + return response.json() + + def min_detail(self, device_sn): + """ + Get detailed data for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter details. + + Raises: + Exception: If the request to the server fails. + """ + + response = self.session.get( + self.get_v1_url('device/tlx/tlx_data_info'), + params={ + 'device_sn': device_sn + } + ) + + return response.json() + + def min_energy(self, device_sn): + """ + Get energy data for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter energy data. + + Raises: + Exception: If the request to the server fails. + """ + + response = self.session.post( + url=self.get_v1_url("device/tlx/tlx_last_data"), + data={ + "tlx_sn": device_sn, + }, + ) + + return response.json() + + def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None): + """ + Get MIN inverter data history. + + Args: + device_sn (str): The ID of the MIN inverter. + start_date (date, optional): Start date. Defaults to today. + end_date (date, optional): End date. Defaults to today. + timezone (str, optional): Timezone ID. + page (int, optional): Page number. + limit (int, optional): Results per page. + + Returns: + dict: A dictionary containing the MIN inverter history data. + + Raises: + Exception: If the request to the server fails. + """ + + if start_date is None and end_date is None: + start_date = date.today() + end_date = date.today() + elif start_date is None: + start_date = end_date + elif end_date is None: + end_date = start_date + + # check interval validity + if end_date - start_date > timedelta(days=7): + raise ValueError("date interval must not exceed 7 days") + + response = self.session.post( + url=self.get_v1_url('device/tlx/tlx_data'), + data={ + "tlx_sn": device_sn, + "start_date": start_date.strftime("%Y-%m-%d"), + "end_date": end_date.strftime("%Y-%m-%d"), + "timezone_id": timezone, + "page": page, + "perpage": limit, + } + ) + + return response.json() + + def min_settings(self, device_sn): + """ + Get settings for a MIN inverter. + + Args: + device_sn (str): The serial number of the MIN inverter. + + Returns: + dict: A dictionary containing the MIN inverter settings. + + Raises: + Exception: If the request to the server fails. + """ + + response = self.session.get( + self.get_v1_url('device/tlx/tlx_set_info'), + params={ + 'device_sn': device_sn + } + ) + + return response.json() + + def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_address=None): + """ + Read setting from MIN inverter. + + Args: + device_sn (str): The ID of the TLX inverter. + parameter_id (str): Parameter ID to read. Don't use start_address and end_address if this is set. + start_address (int, optional): Register start address (for set_any_reg). Don't use parameter_id if this is set. + end_address (int, optional): Register end address (for set_any_reg). Don't use parameter_id if this is set. + + Returns: + dict: A dictionary containing the setting value. + + Raises: + Exception: If the request to the server fails. + """ + + if parameter_id is None and start_address is None: + raise ValueError( + "specify either parameter_id or start_address/end_address") + elif parameter_id is not None and start_address is not None: + raise ValueError( + "specify either parameter_id or start_address/end_address - not both." + ) + elif parameter_id is not None: + # named parameter + start_address = 0 + end_address = 0 + else: + # using register-number mode + parameter_id = "set_any_reg" + if start_address is None: + start_address = end_address + if end_address is None: + end_address = start_address + + response = self.session.post( + self.get_v1_url('readMinParam'), + data={ + "device_sn": device_sn, + "paramId": parameter_id, + "startAddr": start_address, + "endAddr": end_address, + } + ) + + return response.json() + + def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): + """ + Set parameters on a MIN inverter. + + Args: + device_sn (str): Serial number of the inverter + parameter_id (str): Setting type to be configured + parameter_values: Parameter values to be sent to the system. + Can be a single string (for param1 only), + a list of strings (for sequential params), + or a dictionary mapping param positions to values + + Returns: + dict: JSON response from the server + + """ + + # Initialize all parameters as empty strings + parameters = {i: "" for i in range(1, 20)} + + # Process parameter values based on type + if parameter_values is not None: + if isinstance(parameter_values, (str, int, float, bool)): + # Single value goes to param1 + parameters[1] = str(parameter_values) + elif isinstance(parameter_values, list): + # List of values go to sequential params + for i, value in enumerate(parameter_values, 1): + if i <= 19: # Only use up to 19 parameters + parameters[i] = str(value) + elif isinstance(parameter_values, dict): + # Dict maps param positions to values + for pos, value in parameter_values.items(): + pos = int(pos) if not isinstance(pos, int) else pos + if 1 <= pos <= 19: # Validate parameter positions + parameters[pos] = str(value) + + # IMPORTANT: Create a data dictionary with ALL parameters explicitly included + request_data = { + "tlx_sn": device_sn, + "type": parameter_id + } + + # Add all 19 parameters to the request + for i in range(1, 20): + request_data[f"param{i}"] = str(parameters[i]) + + # Send the request + response = self.session.post( + self.get_v1_url('tlxSet'), + data=request_data + ) + + return response.json() + + def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, end_time, enabled=True): + """ + Set a time segment for a MIN inverter. + + Args: + device_sn (str): The serial number of the inverter. + segment_id (int): Time segment ID (1-9). + batt_mode (int): 0=load priority, 1=battery priority, 2=grid priority. + start_time (datetime.time): Start time for the segment. + end_time (datetime.time): End time for the segment. + enabled (bool): Whether this segment is enabled. + + Returns: + dict: The server response. + """ + + if not 1 <= segment_id <= 9: + raise ValueError("segment_id must be between 1 and 9") + + if not 0 <= batt_mode <= 2: + raise ValueError("batt_mode must be between 0 and 2") + + # Initialize ALL 19 parameters as empty strings, not just the ones we need + all_params = { + "tlx_sn": device_sn, + "type": f"time_segment{segment_id}" + } + + # Add param1 through param19, setting the values we need + all_params["param1"] = str(batt_mode) + all_params["param2"] = str(start_time.hour) + all_params["param3"] = str(start_time.minute) + all_params["param4"] = str(end_time.hour) + all_params["param5"] = str(end_time.minute) + all_params["param6"] = "1" if enabled else "0" + + # Add empty strings for all unused parameters + for i in range(7, 20): + all_params[f"param{i}"] = "" + + # Send the request + response = self.session.post( + self.get_v1_url('tlxSet'), + data=all_params + ) + + return response.json() + + def min_read_time_segments(self, device_sn, settings_data=None): + """ + Read Time-of-Use (TOU) settings from a Growatt MIN/TLX inverter. + + Retrieves all 9 time segments from a Growatt MIN/TLX inverter and + parses them into a structured format. + + Note that this function uses min_settings() internally to get the data, so same endpoint rate limit applies + + Args: + device_sn (str): The device serial number of the inverter + settings_data (dict, optional): Settings data from min_settings call to avoid repeated API calls. + Can be either the complete response or just the data portion. + + Returns: + list: A list of dictionaries, each containing details for one time segment: + - segment_id (int): The segment number (1-9) + - batt_mode (int): 0=Load First, 1=Battery First, 2=Grid First + - mode_name (str): String representation of the mode + - start_time (str): Start time in format "HH:MM" + - end_time (str): End time in format "HH:MM" + - enabled (bool): Whether the segment is enabled + + Example: + api = GrowattApi(token="your_api_token") + + # Option 1: Make a single call + tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER") + + # Option 2: Reuse existing settings data + settings_response = api.min_settings("DEVICE_SERIAL_NUMBER") + tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER", settings_response) + + """ + + # Process the settings data + if settings_data is None: + # Fetch settings if not provided + settings_response = self.min_settings(device_sn=device_sn) + if settings_response.get('error_code', 1) != 0: + print( + f"Failed to get settings, error: {settings_response.get('error_msg', 'Unknown error')}") + return [] + settings_data = settings_response.get('data', {}) + else: + # Check if we were given the full API response or just the data portion + if 'error_code' in settings_data and 'data' in settings_data: + # This is the full API response + if settings_data['error_code'] != 0: + print( + f"Settings data contains an error: {settings_data.get('error_msg', 'Unknown error')}") + return [] + settings_data = settings_data.get('data', {}) + # If it's just the data portion, use it directly (nothing to do) + + # Define mode names + mode_names = { + 0: "Load First", + 1: "Battery First", + 2: "Grid First" + } + + segments = [] + + # Process each time segment + for i in range(1, 10): # Segments 1-9 + # Get raw time values + start_time_raw = settings_data.get(f'forcedTimeStart{i}', "0:0") + end_time_raw = settings_data.get(f'forcedTimeStop{i}', "0:0") + + # Handle 'null' string values + if start_time_raw == 'null' or not start_time_raw: + start_time_raw = "0:0" + if end_time_raw == 'null' or not end_time_raw: + end_time_raw = "0:0" + + # Format times with leading zeros (HH:MM) + try: + start_parts = start_time_raw.split(":") + start_hour = int(start_parts[0]) + start_min = int(start_parts[1]) + start_time = f"{start_hour:02d}:{start_min:02d}" + except (ValueError, IndexError): + start_time = "00:00" + + try: + end_parts = end_time_raw.split(":") + end_hour = int(end_parts[0]) + end_min = int(end_parts[1]) + end_time = f"{end_hour:02d}:{end_min:02d}" + except (ValueError, IndexError): + end_time = "00:00" + + # Get the mode value safely + mode_raw = settings_data.get(f'time{i}Mode') + if mode_raw == 'null' or mode_raw is None: + batt_mode = None + else: + try: + batt_mode = int(mode_raw) + except (ValueError, TypeError): + batt_mode = None + + # Get the enabled status safely + enabled_raw = settings_data.get(f'forcedStopSwitch{i}', 0) + if enabled_raw == 'null' or enabled_raw is None: + enabled = False + else: + try: + enabled = int(enabled_raw) == 1 + except (ValueError, TypeError): + enabled = False + + segment = { + 'segment_id': i, + 'batt_mode': batt_mode, + 'mode_name': mode_names.get(batt_mode, "Unknown"), + 'start_time': start_time, + 'end_time': end_time, + 'enabled': enabled + } + + segments.append(segment) + + return segments From 56ef2b0cbf8df070f805a1f18ee1bc90278b6bd7 Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Tue, 15 Apr 2025 19:14:01 +0000 Subject: [PATCH 11/13] Rename V1 API client to OpenApiV1 and update method names example scripts for consistency --- README.md | 12 +-- examples/min_example.py | 95 +++++++++++++++++++++ examples/min_example_dashboard.py | 6 +- growattServer/__init__.py | 2 +- growattServer/{v1_api.py => open_api_v1.py} | 53 ++++++------ 5 files changed, 131 insertions(+), 37 deletions(-) create mode 100644 examples/min_example.py rename growattServer/{v1_api.py => open_api_v1.py} (94%) diff --git a/README.md b/README.md index 08dae00..323b818 100755 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The public v1 API requires token-based authentication ```python import growattServer -api = growattServer.GrowattApiV1(token="YOUR_API_TOKEN") +api = growattServer.OpenApiV1(token="YOUR_API_TOKEN") #Get a list of growatt plants. plants = api.plant_list_v1() print(plants) @@ -119,15 +119,15 @@ Any methods that may be useful. #### V1 API Methods -`api.plant_list_v1()` Get a list of plants registered to your account, using public v1 API. +`api.plant_list()` Get a list of plants registered to your account, using public v1 API. -`api.plant_details_v1(plant_id)` Get detailed information about a power station, using public v1 API. +`api.plant_details(plant_id)` Get detailed information about a power station, using public v1 API. -`api.plant_energy_overview_v1(plant_id)` Get energy overview data for a plant, using public v1 API. +`api.plant_energy_overview(plant_id)` Get energy overview data for a plant, using public v1 API. -`api.plant_energy_history_v1(plant_id, start_date, end_date, time_unit, page, perpage)` Get historical energy data for a plant for multiple days/months/years, using public v1 API. +`api.plant_energy_history(plant_id, start_date, end_date, time_unit, page, perpage)` Get historical energy data for a plant for multiple days/months/years, using public v1 API. -`api.device_list_v1(plant_id)` Get a list of devices in specified plant using the public v1 API. +`api.device_list(plant_id)` Get a list of devices in specified plant using the public v1 API. `api.min_energy(device_sn)` Get current energy data for a min inverter, including power and energy values. diff --git a/examples/min_example.py b/examples/min_example.py new file mode 100644 index 0000000..d5bd3fa --- /dev/null +++ b/examples/min_example.py @@ -0,0 +1,95 @@ +import growattServer +import datetime +import json + +""" +# 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 + +# Initialize the API with token instead of using login +api = growattServer.OpenApiV1(token=api_token) + +# Plant info +plant_list = api.plant_list() # Use V1 endpoint +if plant_list['error_code'] != 0: + print(f"Failed to get plant list, error: {plant_list['error_msg']}") + exit() + +print(f"Plants: Found {plant_list['data']['count']} plants") +plant_id = plant_list['data']['plants'][0]['plant_id'] + +# Devices +devices_response = api.device_list(plant_id) +if devices_response['error_code'] != 0: + print(f"Failed to get devices, error: {plant_list['error_msg']}") + exit() + +for device in devices_response['data']['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] + + # Get device details using v1 API + inverter_detail_response = api.min_detail(inverter_sn) + if inverter_detail_response['error_code'] == 0: + inverter_data = inverter_detail_response['data'] + 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 using v1 API + energy_response = api.min_energy(device_sn=inverter_sn) + if energy_response['error_code'] == 0: + energy_data = energy_response['data'] + 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 details from v1 API + energy_history_response = api.min_energy_history(inverter_sn) + if energy_history_response['error_code'] == 0: + energy_history_data = energy_history_response['data']['datas'] + print("Saving energy history data to energy_history.json") + with open('energy_history.json', 'w') as f: + json.dump(energy_history_data, f, indent=4, sort_keys=True) + + # Get settings using v1 API + settings_response = api.min_settings(device_sn=inverter_sn) + if settings_response['error_code'] == 0: + print("Saving settings data to settings_data.json") + settings_data = settings_response['data'] + with open('settings_data.json', 'w') as f: + json.dump(settings_data, f, indent=4, sort_keys=True) + + tou = api.min_read_time_segments(inverter_sn, settings_response) + print(json.dumps(tou, indent=4)) + + # Example of reading individual parameter + res = api.min_read_parameter(inverter_sn, 'discharge_power') + if res['error_code'] != 0: + print(f"Failed to read parameters, error: {res['error_msg']}") + exit() + print("Current discharge power:", res['data'], "%") + + # Settings parameters. Uncomment to test + + # Turn on AC charging +# res = api.min_write_parameter(inverter_sn, 'ac_charge', 1) +# print("AC charging enabled: ", res) + + # Enable Load First between 00:00 and 11:59 using time segment 1 +# res = api.min_write_time_segment( +# device_sn=inverter_sn, +# segment_id=1, +# batt_mode=growattServer.BATT_MODE_LOAD_FIRST, +# start_time=datetime.time(0, 0), +# end_time=datetime.time(00, 59), +# enabled=True +# ) +# print(res) diff --git a/examples/min_example_dashboard.py b/examples/min_example_dashboard.py index 270aafc..e2b8586 100644 --- a/examples/min_example_dashboard.py +++ b/examples/min_example_dashboard.py @@ -13,10 +13,10 @@ api_token = "6eb6f069523055a339d71e5b1f6c88cc" # gitleaks:allow # Initialize the API with token -api = growattServer.GrowattApiV1(token=api_token) +api = growattServer.OpenApiV1(token=api_token) # Get plant list using V1 API -plant_response = api.plant_list_v1() +plant_response = api.plant_list() if plant_response['error_code'] != 0: print(f"Failed to get plants, error: {plant_response['error_msg']}") exit() @@ -24,7 +24,7 @@ plant_id = plant_response['data']['plants'][0]['plant_id'] # Get devices in plant using V1 API -devices_response = api.device_list_v1(plant_id) +devices_response = api.device_list(plant_id) if devices_response['error_code'] != 0: print(f"Failed to get devices, error: {devices_response['error_msg']}") exit() diff --git a/growattServer/__init__.py b/growattServer/__init__.py index dc1085b..45340b4 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -2,7 +2,7 @@ # Import everything from base_api to ensure backward compatibility from .base_api import * # Import the V1 API class -from .v1_api import GrowattApiV1 +from .open_api_v1 import OpenApiV1 # Define the name of the package name = "growattServer" diff --git a/growattServer/v1_api.py b/growattServer/open_api_v1.py similarity index 94% rename from growattServer/v1_api.py rename to growattServer/open_api_v1.py index 8de4f20..1406a84 100644 --- a/growattServer/v1_api.py +++ b/growattServer/open_api_v1.py @@ -4,10 +4,11 @@ import platform -class GrowattApiV1(GrowattApi): +class OpenApiV1(GrowattApi): """ Extended Growatt API client with V1 API support. - This class extends the base GrowattApi class with methods for the V1 API. + This class extends the base GrowattApi class with methods for MIN inverters using + the public V1 API described here: https://www.showdoc.com.cn/262556420217021/0 """ def _create_user_agent(self): @@ -33,17 +34,15 @@ def __init__(self, token): self.api_url = f"{self.server_url}v1/" # Set up authentication for V1 API using the provided token - print("Using token-based authentication") - # Use token-based auth for V1 API self.session.headers.update({"token": token}) - def get_v1_url(self, page): + def _get_url(self, page): """ Simple helper function to get the page URL for v1 API. """ return self.api_url + page - def plant_list_v1(self): + def plant_list(self): """ Get a list of all plants with detailed information. @@ -60,13 +59,13 @@ def plant_list_v1(self): # Make the request response = self.session.get( - url=self.get_v1_url('plant/list'), + url=self._get_url('plant/list'), data=request_data ) return response.json() - def plant_details_v1(self, plant_id): + def plant_details(self, plant_id): """ Get basic information about a power station. @@ -79,13 +78,13 @@ def plant_details_v1(self, plant_id): """ response = self.session.get( - self.get_v1_url('plant/details'), + self._get_url('plant/details'), params={'plant_id': plant_id} ) return response.json() - def plant_energy_overview_v1(self, plant_id): + def plant_energy_overview(self, plant_id): """ Get an overview of a plant's energy data. @@ -98,13 +97,13 @@ def plant_energy_overview_v1(self, plant_id): """ response = self.session.get( - self.get_v1_url('plant/data'), + self._get_url('plant/data'), params={'plant_id': plant_id} ) return response.json() - def plant_energy_history_v1(self, plant_id, start_date=None, end_date=None, time_unit="day", page=None, perpage=None): + def plant_energy_history(self, plant_id, start_date=None, end_date=None, time_unit="day", page=None, perpage=None): """ Retrieve plant energy data for multiple days/months/years. @@ -146,7 +145,7 @@ def plant_energy_history_v1(self, plant_id, start_date=None, end_date=None, time "Date interval must not exceed 20 years in 'year' mode.", RuntimeWarning) response = self.session.get( - self.get_v1_url('plant/energy'), + self._get_url('plant/energy'), params={ 'plant_id': plant_id, 'start_date': start_date.strftime("%Y-%m-%d"), @@ -159,7 +158,7 @@ def plant_energy_history_v1(self, plant_id, start_date=None, end_date=None, time return response.json() - def device_list_v1(self, plant_id): + def device_list(self, plant_id): """ Get devices associated with plant. @@ -204,7 +203,7 @@ def device_list_v1(self, plant_id): } """ response = self.session.get( - url=self.get_v1_url("device/list"), + url=self._get_url("device/list"), params={ "plant_id": plant_id, "page": "", @@ -228,7 +227,7 @@ def min_detail(self, device_sn): """ response = self.session.get( - self.get_v1_url('device/tlx/tlx_data_info'), + self._get_url('device/tlx/tlx_data_info'), params={ 'device_sn': device_sn } @@ -251,7 +250,7 @@ def min_energy(self, device_sn): """ response = self.session.post( - url=self.get_v1_url("device/tlx/tlx_last_data"), + url=self._get_url("device/tlx/tlx_last_data"), data={ "tlx_sn": device_sn, }, @@ -291,7 +290,7 @@ def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone raise ValueError("date interval must not exceed 7 days") response = self.session.post( - url=self.get_v1_url('device/tlx/tlx_data'), + url=self._get_url('device/tlx/tlx_data'), data={ "tlx_sn": device_sn, "start_date": start_date.strftime("%Y-%m-%d"), @@ -319,7 +318,7 @@ def min_settings(self, device_sn): """ response = self.session.get( - self.get_v1_url('device/tlx/tlx_set_info'), + self._get_url('device/tlx/tlx_set_info'), params={ 'device_sn': device_sn } @@ -364,7 +363,7 @@ def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_ad end_address = start_address response = self.session.post( - self.get_v1_url('readMinParam'), + self._get_url('readMinParam'), data={ "device_sn": device_sn, "paramId": parameter_id, @@ -424,7 +423,7 @@ def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): # Send the request response = self.session.post( - self.get_v1_url('tlxSet'), + self._get_url('tlxSet'), data=request_data ) @@ -472,7 +471,7 @@ def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, e # Send the request response = self.session.post( - self.get_v1_url('tlxSet'), + self._get_url('tlxSet'), data=all_params ) @@ -485,7 +484,9 @@ def min_read_time_segments(self, device_sn, settings_data=None): Retrieves all 9 time segments from a Growatt MIN/TLX inverter and parses them into a structured format. - Note that this function uses min_settings() internally to get the data, so same endpoint rate limit applies + Note that this function uses min_settings() internally to get the settings data, + To avoid endpoint rate limit, you can pass the settings_data parameter + with the data returned from min_settings(). Args: device_sn (str): The device serial number of the inverter @@ -518,18 +519,16 @@ def min_read_time_segments(self, device_sn, settings_data=None): # Fetch settings if not provided settings_response = self.min_settings(device_sn=device_sn) if settings_response.get('error_code', 1) != 0: - print( + raise Exception( f"Failed to get settings, error: {settings_response.get('error_msg', 'Unknown error')}") - return [] settings_data = settings_response.get('data', {}) else: # Check if we were given the full API response or just the data portion if 'error_code' in settings_data and 'data' in settings_data: # This is the full API response if settings_data['error_code'] != 0: - print( + raise Exception( f"Settings data contains an error: {settings_data.get('error_msg', 'Unknown error')}") - return [] settings_data = settings_data.get('data', {}) # If it's just the data portion, use it directly (nothing to do) From 32b371f5a192dd755800b68c1eac08a002d95c17 Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Sun, 20 Apr 2025 09:08:39 +0000 Subject: [PATCH 12/13] Adds proper exception handling with custom GrowattApi exceptions. --- examples/min_example.py | 120 ++++++++++++------------ examples/min_example_dashboard.py | 146 +++++++++++++++--------------- growattServer/__init__.py | 3 +- growattServer/exceptions.py | 33 +++++++ growattServer/open_api_v1.py | 124 ++++++++++++++++--------- 5 files changed, 248 insertions(+), 178 deletions(-) create mode 100644 growattServer/exceptions.py diff --git a/examples/min_example.py b/examples/min_example.py index d5bd3fa..3594fd7 100644 --- a/examples/min_example.py +++ b/examples/min_example.py @@ -1,6 +1,7 @@ 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 @@ -13,83 +14,80 @@ # test token from official API docs https://www.showdoc.com.cn/262556420217021/1494053950115877 api_token = "6eb6f069523055a339d71e5b1f6c88cc" # gitleaks:allow -# Initialize the API with token instead of using login -api = growattServer.OpenApiV1(token=api_token) +try: + # Initialize the API with token instead of using login + api = growattServer.OpenApiV1(token=api_token) -# Plant info -plant_list = api.plant_list() # Use V1 endpoint -if plant_list['error_code'] != 0: - print(f"Failed to get plant list, error: {plant_list['error_msg']}") - exit() + # Plant info + plants = api.plant_list() + print(f"Plants: Found {plants['count']} plants") + plant_id = plants['plants'][0]['plant_id'] -print(f"Plants: Found {plant_list['data']['count']} plants") -plant_id = plant_list['data']['plants'][0]['plant_id'] + # Devices + devices = api.device_list(plant_id) -# Devices -devices_response = api.device_list(plant_id) -if devices_response['error_code'] != 0: - print(f"Failed to get devices, error: {plant_list['error_msg']}") - exit() + for device in devices['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] + print(f"Processing inverter: {inverter_sn}") -for device in devices_response['data']['devices']: - if device['type'] == 7: # (MIN/TLX) - inverter_sn = device['device_sn'] - - # Get device details using v1 API - inverter_detail_response = api.min_detail(inverter_sn) - if inverter_detail_response['error_code'] == 0: - inverter_data = inverter_detail_response['data'] + # 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 using v1 API - energy_response = api.min_energy(device_sn=inverter_sn) - if energy_response['error_code'] == 0: - energy_data = energy_response['data'] + # 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 details from v1 API - energy_history_response = api.min_energy_history(inverter_sn) - if energy_history_response['error_code'] == 0: - energy_history_data = energy_history_response['data']['datas'] + # 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, f, indent=4, sort_keys=True) + json.dump(energy_history_data['datas'], + f, indent=4, sort_keys=True) - # Get settings using v1 API - settings_response = api.min_settings(device_sn=inverter_sn) - if settings_response['error_code'] == 0: + # Get settings + settings_data = api.min_settings(device_sn=inverter_sn) print("Saving settings data to settings_data.json") - settings_data = settings_response['data'] with open('settings_data.json', 'w') as f: json.dump(settings_data, f, indent=4, sort_keys=True) - tou = api.min_read_time_segments(inverter_sn, settings_response) - print(json.dumps(tou, indent=4)) - - # Example of reading individual parameter - res = api.min_read_parameter(inverter_sn, 'discharge_power') - if res['error_code'] != 0: - print(f"Failed to read parameters, error: {res['error_msg']}") - exit() - print("Current discharge power:", res['data'], "%") - - # Settings parameters. Uncomment to test - - # Turn on AC charging -# res = api.min_write_parameter(inverter_sn, 'ac_charge', 1) -# print("AC charging enabled: ", res) - - # Enable Load First between 00:00 and 11:59 using time segment 1 -# res = api.min_write_time_segment( -# device_sn=inverter_sn, -# segment_id=1, -# batt_mode=growattServer.BATT_MODE_LOAD_FIRST, -# start_time=datetime.time(0, 0), -# end_time=datetime.time(00, 59), -# enabled=True -# ) -# print(res) + # 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}") diff --git a/examples/min_example_dashboard.py b/examples/min_example_dashboard.py index e2b8586..fac0082 100644 --- a/examples/min_example_dashboard.py +++ b/examples/min_example_dashboard.py @@ -1,5 +1,6 @@ import growattServer import json +import requests """ Example script fetching key power and today+total energy metrics from a Growatt MID-30KTL3-XH (TLX) + APX battery hybrid system @@ -12,86 +13,83 @@ # test token from official API docs https://www.showdoc.com.cn/262556420217021/1494053950115877 api_token = "6eb6f069523055a339d71e5b1f6c88cc" # gitleaks:allow -# Initialize the API with token -api = growattServer.OpenApiV1(token=api_token) +try: + # Initialize the API with token + api = growattServer.OpenApiV1(token=api_token) -# Get plant list using V1 API -plant_response = api.plant_list() -if plant_response['error_code'] != 0: - print(f"Failed to get plants, error: {plant_response['error_msg']}") - exit() + # Get plant list using V1 API + plants = api.plant_list() + plant_id = plants['plants'][0]['plant_id'] -plant_id = plant_response['data']['plants'][0]['plant_id'] + # Get devices in plant + devices = api.device_list(plant_id) -# Get devices in plant using V1 API -devices_response = api.device_list(plant_id) -if devices_response['error_code'] != 0: - print(f"Failed to get devices, error: {devices_response['error_msg']}") - exit() + # Iterate over all devices + energy_data = None + for device in devices['devices']: + if device['type'] == 7: # (MIN/TLX) + inverter_sn = device['device_sn'] -# Iterate over all devices -energy_data = None -for device in devices_response['data']['devices']: - if device['type'] == 7: # (MIN/TLX) - inverter_sn = device['device_sn'] + # Get energy data + energy_data = api.min_energy(device_sn=inverter_sn) + with open('energy_data.json', 'w') as f: + json.dump(energy_data, f, indent=4, sort_keys=True) - # Get energy data using new API - energy_response = api.min_energy(device_sn=inverter_sn) - if energy_response['error_code'] != 0: - print( - f"Failed to get energy data, error: {energy_response['error_msg']}") - exit() + # energy data does not contain epvToday for some reason, so we need to calculate it + epv_today = energy_data["epv1Today"] + energy_data["epv2Today"] - energy_data = energy_response['data'] - with open('energy_data.json', 'w') as f: - json.dump(energy_data, f, indent=4, sort_keys=True) + solar_production = f'{float(epv_today):.1f}/{float(energy_data["epvTotal"]):.1f}' + solar_production_pv1 = f'{float(energy_data["epv1Today"]):.1f}/{float(energy_data["epv1Total"]):.1f}' + solar_production_pv2 = f'{float(energy_data["epv2Today"]):.1f}/{float(energy_data["epv2Total"]):.1f}' + energy_output = f'{float(energy_data["eacToday"]):.1f}/{float(energy_data["eacTotal"]):.1f}' + system_production = f'{float(energy_data["esystemToday"]):.1f}/{float(energy_data["esystemTotal"]):.1f}' + battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' + battery_grid_charge = f'{float(energy_data["eacChargeToday"]):.1f}/{float(energy_data["eacChargeTotal"]):.1f}' + battery_discharged = f'{float(energy_data["edischargeToday"]):.1f}/{float(energy_data["edischargeTotal"]):.1f}' + exported_to_grid = f'{float(energy_data["etoGridToday"]):.1f}/{float(energy_data["etoGridTotal"]):.1f}' + imported_from_grid = f'{float(energy_data["etoUserToday"]):.1f}/{float(energy_data["etoUserTotal"]):.1f}' + load_consumption = f'{float(energy_data["elocalLoadToday"]):.1f}/{float(energy_data["elocalLoadTotal"]):.1f}' + self_consumption = f'{float(energy_data["eselfToday"]):.1f}/{float(energy_data["eselfTotal"]):.1f}' + battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' -# energy data does not contain epvToday for some reason, so we need to calculate it -epv_today = energy_data["epv1Today"] + energy_data["epv2Today"] + # Output the dashboard + print("\nGeneration overview Today/Total(kWh)") + print(f'Solar production {solar_production:>22}') + print(f' Solar production, PV1 {solar_production_pv1:>22}') + print(f' Solar production, PV2 {solar_production_pv2:>22}') + print(f'Energy Output {energy_output:>22}') + print(f'System production {system_production:>22}') + print(f'Self consumption {self_consumption:>22}') + print(f'Load consumption {load_consumption:>22}') + print(f'Battery Charged {battery_charged:>22}') + print(f' Charged from grid {battery_grid_charge:>22}') + print(f'Battery Discharged {battery_discharged:>22}') + print(f'Import from grid {imported_from_grid:>22}') + print(f'Export to grid {exported_to_grid:>22}') -solar_production = f'{float(epv_today):.1f}/{float(energy_data["epvTotal"]):.1f}' -solar_production_pv1 = f'{float(energy_data["epv1Today"]):.1f}/{float(energy_data["epv1Total"]):.1f}' -solar_production_pv2 = f'{float(energy_data["epv2Today"]):.1f}/{float(energy_data["epv2Total"]):.1f}' -energy_output = f'{float(energy_data["eacToday"]):.1f}/{float(energy_data["eacTotal"]):.1f}' -system_production = f'{float(energy_data["esystemToday"]):.1f}/{float(energy_data["esystemTotal"]):.1f}' -battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' -battery_grid_charge = f'{float(energy_data["eacChargeToday"]):.1f}/{float(energy_data["eacChargeTotal"]):.1f}' -battery_discharged = f'{float(energy_data["edischargeToday"]):.1f}/{float(energy_data["edischargeTotal"]):.1f}' -exported_to_grid = f'{float(energy_data["etoGridToday"]):.1f}/{float(energy_data["etoGridTotal"]):.1f}' -imported_from_grid = f'{float(energy_data["etoUserToday"]):.1f}/{float(energy_data["etoUserTotal"]):.1f}' -load_consumption = f'{float(energy_data["elocalLoadToday"]):.1f}/{float(energy_data["elocalLoadTotal"]):.1f}' -self_consumption = f'{float(energy_data["eselfToday"]):.1f}/{float(energy_data["eselfTotal"]):.1f}' -battery_charged = f'{float(energy_data["echargeToday"]):.1f}/{float(energy_data["echargeTotal"]):.1f}' + print("\nPower overview (Watts)") + print(f'AC Power {float(energy_data["pac"]):>22.1f}') + print(f'Self power {float(energy_data["pself"]):>22.1f}') + print( + f'Export power {float(energy_data["pacToGridTotal"]):>22.1f}') + print( + f'Import power {float(energy_data["pacToUserTotal"]):>22.1f}') + print( + f'Local load power {float(energy_data["pacToLocalLoad"]):>22.1f}') + print(f'PV power {float(energy_data["ppv"]):>22.1f}') + print(f'PV #1 power {float(energy_data["ppv1"]):>22.1f}') + print(f'PV #2 power {float(energy_data["ppv2"]):>22.1f}') + print( + f'Battery charge power {float(energy_data["bdc1ChargePower"]):>22.1f}') + print( + f'Battery discharge power {float(energy_data["bdc1DischargePower"]):>22.1f}') + print(f'Battery SOC {int(energy_data["bdc1Soc"]):>21}%') -# Output the dashboard -print("\nGeneration overview Today/Total(kWh)") -print(f'Solar production {solar_production:>22}') -print(f' Solar production, PV1 {solar_production_pv1:>22}') -print(f' Solar production, PV2 {solar_production_pv2:>22}') -print(f'Energy Output {energy_output:>22}') -print(f'System production {system_production:>22}') -print(f'Self consumption {self_consumption:>22}') -print(f'Load consumption {load_consumption:>22}') -print(f'Battery Charged {battery_charged:>22}') -print(f' Charged from grid {battery_grid_charge:>22}') -print(f'Battery Discharged {battery_discharged:>22}') -print(f'Import from grid {imported_from_grid:>22}') -print(f'Export to grid {exported_to_grid:>22}') - -print("\nPower overview (Watts)") -print(f'AC Power {float(energy_data["pac"]):>22.1f}') -print(f'Self power {float(energy_data["pself"]):>22.1f}') -print( - f'Export power {float(energy_data["pacToGridTotal"]):>22.1f}') -print( - f'Import power {float(energy_data["pacToUserTotal"]):>22.1f}') -print( - f'Local load power {float(energy_data["pacToLocalLoad"]):>22.1f}') -print(f'PV power {float(energy_data["ppv"]):>22.1f}') -print(f'PV #1 power {float(energy_data["ppv1"]):>22.1f}') -print(f'PV #2 power {float(energy_data["ppv2"]):>22.1f}') -print( - f'Battery charge power {float(energy_data["bdc1ChargePower"]):>22.1f}') -print( - f'Battery discharge power {float(energy_data["bdc1DischargePower"]):>22.1f}') -print(f'Battery SOC {int(energy_data["bdc1Soc"]):>21}%') +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}") diff --git a/growattServer/__init__.py b/growattServer/__init__.py index 45340b4..927f010 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1,8 +1,9 @@ - # Import everything from base_api to ensure backward compatibility from .base_api import * # Import the V1 API class from .open_api_v1 import OpenApiV1 +# Import exceptions +from .exceptions import GrowattError, GrowattParameterError, GrowattV1ApiError # Define the name of the package name = "growattServer" diff --git a/growattServer/exceptions.py b/growattServer/exceptions.py new file mode 100644 index 0000000..ff67a98 --- /dev/null +++ b/growattServer/exceptions.py @@ -0,0 +1,33 @@ +""" +Exception classes for the growattServer library. + +Note that in addition to these custom exceptions, methods may also raise exceptions +from the underlying requests library (requests.exceptions.RequestException and its +subclasses) when network or HTTP errors occur. These are not wrapped and are passed +through directly to the caller. + +Common requests exceptions to handle: +- requests.exceptions.HTTPError: For HTTP error responses (4XX, 5XX) +- requests.exceptions.ConnectionError: For network connection issues +- requests.exceptions.Timeout: For request timeouts +- requests.exceptions.RequestException: The base exception for all requests exceptions +""" + + +class GrowattError(Exception): + """Base exception class for all Growatt API related errors.""" + pass + + +class GrowattParameterError(GrowattError): + """Raised when invalid parameters are provided to API methods.""" + pass + + +class GrowattV1ApiError(GrowattError): + """Raised when a Growatt V1 API request fails or returns an error.""" + + def __init__(self, message, error_code=None, error_msg=None): + super().__init__(message) + self.error_code = error_code + self.error_msg = error_msg diff --git a/growattServer/open_api_v1.py b/growattServer/open_api_v1.py index 1406a84..19af885 100644 --- a/growattServer/open_api_v1.py +++ b/growattServer/open_api_v1.py @@ -2,6 +2,7 @@ from datetime import date, timedelta from . import GrowattApi import platform +from .exceptions import GrowattParameterError, GrowattV1ApiError class OpenApiV1(GrowattApi): @@ -36,6 +37,28 @@ def __init__(self, token): # Set up authentication for V1 API using the provided token self.session.headers.update({"token": token}) + def _process_response(self, response, operation_name="API operation"): + """ + Process API response and handle errors. + + Args: + response (dict): The JSON response from the API + operation_name (str): Name of the operation for error messages + + Returns: + dict: The 'data' field from the response + + Raises: + GrowattV1ApiError: If the API returns an error response + """ + if response.get('error_code', 1) != 0: + raise GrowattV1ApiError( + f"Error during {operation_name}", + error_code=response.get('error_code'), + error_msg=response.get('error_msg', 'Unknown error') + ) + return response.get('data') + def _get_url(self, page): """ Simple helper function to get the page URL for v1 API. @@ -47,7 +70,11 @@ def plant_list(self): Get a list of all plants with detailed information. Returns: - list: A list of plants with detailed information. + dict: A dictionary containing plants information with 'count' and 'plants' keys. + + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ # Prepare request data request_data = { @@ -63,7 +90,7 @@ def plant_list(self): data=request_data ) - return response.json() + return self._process_response(response.json(), "getting plant list") def plant_details(self, plant_id): """ @@ -75,6 +102,9 @@ def plant_details(self, plant_id): Returns: dict: A dictionary containing the plant details. + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ response = self.session.get( @@ -82,7 +112,7 @@ def plant_details(self, plant_id): params={'plant_id': plant_id} ) - return response.json() + return self._process_response(response.json(), "getting plant details") def plant_energy_overview(self, plant_id): """ @@ -94,6 +124,9 @@ def plant_energy_overview(self, plant_id): Returns: dict: A dictionary containing the plant energy overview. + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ response = self.session.get( @@ -101,7 +134,7 @@ def plant_energy_overview(self, plant_id): params={'plant_id': plant_id} ) - return response.json() + return self._process_response(response.json(), "getting plant energy overview") def plant_energy_history(self, plant_id, start_date=None, end_date=None, time_unit="day", page=None, perpage=None): """ @@ -123,6 +156,10 @@ def plant_energy_history(self, plant_id, start_date=None, end_date=None, time_un - When time_unit is 'month', start date must be within same or previous year - When time_unit is 'year', date interval must not exceed 20 years + Raises: + GrowattParameterError: If date parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ if start_date is None and end_date is None: @@ -156,7 +193,7 @@ def plant_energy_history(self, plant_id, start_date=None, end_date=None, time_un } ) - return response.json() + return self._process_response(response.json(), "getting plant energy history") def device_list(self, plant_id): """ @@ -210,7 +247,7 @@ def device_list(self, plant_id): "perpage": "", }, ) - return response.json() + return self._process_response(response.json(), "getting device list") def min_detail(self, device_sn): """ @@ -223,7 +260,8 @@ def min_detail(self, device_sn): dict: A dictionary containing the MIN inverter details. Raises: - Exception: If the request to the server fails. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ response = self.session.get( @@ -233,7 +271,7 @@ def min_detail(self, device_sn): } ) - return response.json() + return self._process_response(response.json(), "getting MIN inverter details") def min_energy(self, device_sn): """ @@ -246,7 +284,8 @@ def min_energy(self, device_sn): dict: A dictionary containing the MIN inverter energy data. Raises: - Exception: If the request to the server fails. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ response = self.session.post( @@ -256,7 +295,7 @@ def min_energy(self, device_sn): }, ) - return response.json() + return self._process_response(response.json(), "getting MIN inverter energy data") def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None): """ @@ -274,7 +313,9 @@ def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone dict: A dictionary containing the MIN inverter history data. Raises: - Exception: If the request to the server fails. + GrowattParameterError: If date interval is invalid (exceeds 7 days). + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ if start_date is None and end_date is None: @@ -287,7 +328,7 @@ def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone # check interval validity if end_date - start_date > timedelta(days=7): - raise ValueError("date interval must not exceed 7 days") + raise GrowattParameterError("date interval must not exceed 7 days") response = self.session.post( url=self._get_url('device/tlx/tlx_data'), @@ -301,7 +342,7 @@ def min_energy_history(self, device_sn, start_date=None, end_date=None, timezone } ) - return response.json() + return self._process_response(response.json(), "getting MIN inverter energy history") def min_settings(self, device_sn): """ @@ -314,7 +355,8 @@ def min_settings(self, device_sn): dict: A dictionary containing the MIN inverter settings. Raises: - Exception: If the request to the server fails. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ response = self.session.get( @@ -324,7 +366,7 @@ def min_settings(self, device_sn): } ) - return response.json() + return self._process_response(response.json(), "getting MIN inverter settings") def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_address=None): """ @@ -340,14 +382,16 @@ def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_ad dict: A dictionary containing the setting value. Raises: - Exception: If the request to the server fails. + GrowattParameterError: If parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ if parameter_id is None and start_address is None: - raise ValueError( + raise GrowattParameterError( "specify either parameter_id or start_address/end_address") elif parameter_id is not None and start_address is not None: - raise ValueError( + raise GrowattParameterError( "specify either parameter_id or start_address/end_address - not both." ) elif parameter_id is not None: @@ -372,7 +416,7 @@ def min_read_parameter(self, device_sn, parameter_id, start_address=None, end_ad } ) - return response.json() + return self._process_response(response.json(), f"reading parameter {parameter_id}") def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): """ @@ -389,6 +433,9 @@ def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): Returns: dict: JSON response from the server + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ # Initialize all parameters as empty strings @@ -427,7 +474,7 @@ def min_write_parameter(self, device_sn, parameter_id, parameter_values=None): data=request_data ) - return response.json() + return self._process_response(response.json(), f"writing parameter {parameter_id}") def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, end_time, enabled=True): """ @@ -443,13 +490,18 @@ def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, e Returns: dict: The server response. + + Raises: + GrowattParameterError: If parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ if not 1 <= segment_id <= 9: - raise ValueError("segment_id must be between 1 and 9") + raise GrowattParameterError("segment_id must be between 1 and 9") if not 0 <= batt_mode <= 2: - raise ValueError("batt_mode must be between 0 and 2") + raise GrowattParameterError("batt_mode must be between 0 and 2") # Initialize ALL 19 parameters as empty strings, not just the ones we need all_params = { @@ -475,7 +527,7 @@ def min_write_time_segment(self, device_sn, segment_id, batt_mode, start_time, e data=all_params ) - return response.json() + return self._process_response(response.json(), f"writing time segment {segment_id}") def min_read_time_segments(self, device_sn, settings_data=None): """ @@ -491,7 +543,7 @@ def min_read_time_segments(self, device_sn, settings_data=None): Args: device_sn (str): The device serial number of the inverter settings_data (dict, optional): Settings data from min_settings call to avoid repeated API calls. - Can be either the complete response or just the data portion. + Can be either the complete response or just the data portion. Returns: list: A list of dictionaries, each containing details for one time segment: @@ -503,34 +555,22 @@ def min_read_time_segments(self, device_sn, settings_data=None): - enabled (bool): Whether the segment is enabled Example: - api = GrowattApi(token="your_api_token") - # Option 1: Make a single call - tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER") + tou_settings = api.min_read_time_segments("DEVICE_SERIAL_NUMBER") # Option 2: Reuse existing settings data settings_response = api.min_settings("DEVICE_SERIAL_NUMBER") - tou_settings = api.min_read_tou_settings("DEVICE_SERIAL_NUMBER", settings_response) + tou_settings = api.min_read_time_segments("DEVICE_SERIAL_NUMBER", settings_response) + Raises: + GrowattV1ApiError: If the API request fails + requests.exceptions.RequestException: If there is an issue with the HTTP request. """ # Process the settings data if settings_data is None: # Fetch settings if not provided - settings_response = self.min_settings(device_sn=device_sn) - if settings_response.get('error_code', 1) != 0: - raise Exception( - f"Failed to get settings, error: {settings_response.get('error_msg', 'Unknown error')}") - settings_data = settings_response.get('data', {}) - else: - # Check if we were given the full API response or just the data portion - if 'error_code' in settings_data and 'data' in settings_data: - # This is the full API response - if settings_data['error_code'] != 0: - raise Exception( - f"Settings data contains an error: {settings_data.get('error_msg', 'Unknown error')}") - settings_data = settings_data.get('data', {}) - # If it's just the data portion, use it directly (nothing to do) + settings_data = self.min_settings(device_sn=device_sn) # Define mode names mode_names = { From 5977de7015d8e397ad8f4366ecb6c26a97e3fb46 Mon Sep 17 00:00:00 2001 From: Johan Zander Date: Thu, 8 Jan 2026 21:25:18 +0100 Subject: [PATCH 13/13] Add V1 API support for SPH devices Implements complete V1 API support for SPH (type 5) hybrid inverters, parallel to existing MIN device support. Changes: - Add DeviceType enum to distinguish device types - Implement 10 SPH methods in OpenApiV1: * sph_detail() - Get device details * sph_energy() - Get current energy data * sph_energy_history() - Get historical data (7-day max) * sph_settings() - Get all inverter settings * sph_read_parameter() - Read specific parameters * sph_write_parameter() - Write parameters * sph_write_ac_charge_time() - Configure AC charge periods (1-3) * sph_write_ac_discharge_time() - Configure AC discharge periods (1-3) * sph_read_ac_charge_times() - Read all charge periods * sph_read_ac_discharge_times() - Read all discharge periods - Add comprehensive documentation to README - Include working example script (examples/sph_example.py) Technical details: - SPH devices use 'mix' endpoints internally (device/mix/*) - AC charge/discharge periods support 3 time windows each - Methods follow same patterns as existing MIN implementation - All endpoints match official Growatt V1 API documentation --- README.md | 79 ++++- examples/sph_example.py | 135 +++++++++ growattServer/__init__.py | 4 +- growattServer/open_api_v1.py | 553 ++++++++++++++++++++++++++++++++++- 4 files changed, 766 insertions(+), 5 deletions(-) create mode 100644 examples/sph_example.py diff --git a/README.md b/README.md index 8116c4f..c27619a 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Package to retrieve PV information from the growatt server. Special thanks to [Sjoerd Langkemper](https://github.com/Sjord) who has provided a strong base to start off from https://github.com/Sjord/growatt_api_client These projects may merge in the future since they are simmilar in code and function. -This library now supports both the legacy password-based authentication and the V1 API with token-based authentication for MIN systems (TLX are identified as MIN system in the public API). Certain endpoints are not supported anymore by openapi.growatt.com. For example `api.min_write_parameter()` should be used instead of old `api.update_tlx_inverter_setting()`. +This library supports both the classic password-based API and the token-based V1 API, officially supported by Growatt. Currently, the V1 API implementation supports MIN and SPH devices, where MIN broadly corresponds to classic TLX and SPH to classic MIX. If your inverter supports the V1 API, it is encouraged to use this over the classic API, as it offers better security, more features, and more relaxed rate limiting. ## Usage @@ -155,6 +155,26 @@ Any methods that may be useful. `api.min_read_time_segments(device_sn, settings_data=None)` Read all time segments from a MIN inverter. Optionally pass settings_data to avoid redundant API calls. +`api.sph_detail(device_sn)` Get detailed data for an SPH hybrid inverter. + +`api.sph_energy(device_sn)` Get current energy data for an SPH inverter, including power and energy values. + +`api.sph_energy_history(device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None)` Get energy history data for an SPH inverter (7-day max range). + +`api.sph_settings(device_sn)` Get all settings for an SPH inverter. + +`api.sph_read_parameter(device_sn, parameter_id, start_address=None, end_address=None)` Read a specific setting for an SPH inverter. + +`api.sph_write_parameter(device_sn, parameter_id, parameter_values)` Set parameters on an SPH inverter. Parameter values can be a single value, a list, or a dictionary. + +`api.sph_write_ac_charge_time(device_sn, period_id, charge_power, charge_stop_soc, start_time, end_time, mains_enabled=True, enabled=True)` Update a specific AC charge time period for an SPH inverter (periods 1-3). + +`api.sph_write_ac_discharge_time(device_sn, period_id, discharge_power, discharge_stop_soc, start_time, end_time, enabled=True)` Update a specific AC discharge time period for an SPH inverter (periods 1-3). + +`api.sph_read_ac_charge_times(device_sn, settings_data=None)` Read all AC charge time periods from an SPH inverter. Optionally pass settings_data to avoid redundant API calls. + +`api.sph_read_ac_discharge_times(device_sn, settings_data=None)` Read all AC discharge time periods from an SPH inverter. Optionally pass settings_data to avoid redundant API calls. + ### Variables Some variables you may want to set. @@ -341,11 +361,66 @@ For MIN/TLX systems, the public V1 API provides a more robust way to read and wr * `device_sn`: The device serial number * `settings_data`: Optional settings data to avoid redundant API calls +## SPH Inverter Settings Using V1 API + +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_time` + * parameters: + * `device_sn`: The device serial number + * `period_id`: Period number (1-3) + * `charge_power`: Charging power percentage (0-100) + * `charge_stop_soc`: Stop charging at this SOC percentage (0-100) + * `start_time`: Datetime.time object for period start + * `end_time`: Datetime.time object for period end + * `mains_enabled`: Boolean to enable/disable grid charging (default: True) + * `enabled`: Boolean to enable/disable period (default: True) + +* **AC Discharge Time Periods** + * function: `api.sph_write_ac_discharge_time` + * parameters: + * `device_sn`: The device serial number + * `period_id`: Period number (1-3) + * `discharge_power`: Discharge power percentage (0-100) + * `discharge_stop_soc`: Stop discharging at this SOC percentage (0-100) + * `start_time`: Datetime.time object for period start + * `end_time`: Datetime.time object for period end + * `enabled`: Boolean to enable/disable period (default: True) + +* **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 + ## Noah Settings + The noah settings function allow you to change individual values on your noah system e.g. system default output power, battery management, operation mode and currency From what has been reverse engineered from the api, each setting has a `setting_type` and a set of `parameters` that are relevant to it. Known working settings & parameters are as follows (all parameter values are strings): + * **Change "System Default Output Power"** * function: `api.update_noah_settings` * setting type: `default_power` @@ -388,4 +463,4 @@ The library contains functions that allow you to modify the configuration of you To the best of our knowledge only the `settings` functions perform modifications to your system and all other operations are read only. Regardless of the operation: -***The library is used entirely at your own risk.*** \ No newline at end of file +***The library is used entirely at your own risk.*** diff --git a/examples/sph_example.py b/examples/sph_example.py new file mode 100644 index 0000000..bea3ee7 --- /dev/null +++ b/examples/sph_example.py @@ -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 datetime +import json +from pathlib import Path + +import requests + +from . 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 Path("inverter_data.json").open("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 Path("energy_data.json").open("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 Path("energy_history.json").open("w") as f: + json.dump( + energy_history_data.get("datas", []), + f, + indent=4, + sort_keys=True, + ) + + # Get settings + settings_data = api.sph_settings( + device_sn=inverter_sn, + ) + print("Saving settings data to settings_data.json") # noqa: T201 + with Path("settings_data.json").open("w") as f: + json.dump(settings_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=settings_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=settings_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 period 1: charge at 50% power to 95% SOC between 00:00-06:00 + # api.sph_write_ac_charge_time( + # device_sn=inverter_sn, + # period_id=1, + # charge_power=50, # 50% charging power + # charge_stop_soc=95, # Stop at 95% SOC + # start_time=datetime.time(0, 0), + # end_time=datetime.time(6, 0), + # mains_enabled=True, # Enable grid charging + # enabled=True + # ) + # print("AC charge period 1 updated successfully") + + # Set AC discharge time period 1: discharge at 100% power to 20% SOC between 17:00-22:00 + # api.sph_write_ac_discharge_time( + # device_sn=inverter_sn, + # period_id=1, + # discharge_power=100, # 100% discharge power + # discharge_stop_soc=20, # Stop at 20% SOC + # start_time=datetime.time(17, 0), + # end_time=datetime.time(22, 0), + # enabled=True + # ) + # print("AC discharge period 1 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 diff --git a/growattServer/__init__.py b/growattServer/__init__.py index 927f010..75698ac 100755 --- a/growattServer/__init__.py +++ b/growattServer/__init__.py @@ -1,7 +1,7 @@ # Import everything from base_api to ensure backward compatibility from .base_api import * -# Import the V1 API class -from .open_api_v1 import OpenApiV1 +# Import the V1 API class and DeviceType enum +from .open_api_v1 import OpenApiV1, DeviceType # Import exceptions from .exceptions import GrowattError, GrowattParameterError, GrowattV1ApiError diff --git a/growattServer/open_api_v1.py b/growattServer/open_api_v1.py index 19af885..3446fa6 100644 --- a/growattServer/open_api_v1.py +++ b/growattServer/open_api_v1.py @@ -1,14 +1,30 @@ import warnings from datetime import date, timedelta +from enum import Enum from . import GrowattApi import platform from .exceptions import GrowattParameterError, GrowattV1ApiError +class DeviceType(Enum): + """Enumeration of Growatt device types.""" + + INVERTER = 1 + STORAGE = 2 + OTHER = 3 + MAX = 4 + SPH = 5 # (MIX) + SPA = 6 + MIN = 7 + PCS = 8 + HPS = 9 + PBD = 10 + + class OpenApiV1(GrowattApi): """ Extended Growatt API client with V1 API support. - This class extends the base GrowattApi class with methods for MIN inverters using + This class extends the base GrowattApi class with methods for MIN and SPH devices using the public V1 API described here: https://www.showdoc.com.cn/262556420217021/0 """ @@ -642,3 +658,538 @@ def min_read_time_segments(self, device_sn, settings_data=None): segments.append(segment) return segments + + # SPH Device Methods (Device Type 5) + + def sph_detail(self, device_sn): + """ + Get detailed data for an SPH inverter. + + Args: + device_sn (str): The serial number of the SPH inverter. + + Returns: + dict: A dictionary containing the SPH inverter details. + + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + response = self.session.get( + self._get_url('device/mix/mix_data_info'), + params={ + 'device_sn': device_sn + } + ) + + return self._process_response(response.json(), "getting SPH inverter details") + + def sph_energy(self, device_sn): + """ + Get energy data for an SPH inverter. + + Args: + device_sn (str): The serial number of the SPH inverter. + + Returns: + dict: A dictionary containing the SPH inverter energy data. + + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + response = self.session.post( + url=self._get_url("device/mix/mix_last_data"), + data={ + "mix_sn": device_sn, + }, + ) + + return self._process_response(response.json(), "getting SPH inverter energy data") + + def sph_energy_history(self, device_sn, start_date=None, end_date=None, timezone=None, page=None, limit=None): + """ + Get SPH inverter data history. + + Args: + device_sn (str): The ID of the SPH inverter. + start_date (date, optional): Start date. Defaults to today. + end_date (date, optional): End date. Defaults to today. + timezone (str, optional): Timezone ID. + page (int, optional): Page number. + limit (int, optional): Results per page. + + Returns: + dict: A dictionary containing the SPH inverter history data. + + Raises: + GrowattParameterError: If date interval is invalid (exceeds 7 days). + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + if start_date is None and end_date is None: + start_date = date.today() + end_date = date.today() + elif start_date is None: + start_date = end_date + elif end_date is None: + end_date = start_date + + # check interval validity + if end_date - start_date > timedelta(days=7): + raise GrowattParameterError("date interval must not exceed 7 days") + + response = self.session.post( + url=self._get_url('device/mix/mix_data'), + data={ + "mix_sn": device_sn, + "start_date": start_date.strftime("%Y-%m-%d"), + "end_date": end_date.strftime("%Y-%m-%d"), + "timezone_id": timezone, + "page": page, + "perpage": limit, + } + ) + + return self._process_response(response.json(), "getting SPH inverter energy history") + + def sph_settings(self, device_sn): + """ + Get settings for an SPH inverter. + + Args: + device_sn (str): The serial number of the SPH inverter. + + Returns: + dict: A dictionary containing the SPH inverter settings. + + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + response = self.session.get( + self._get_url('device/mix/mix_data_info'), + params={ + 'device_sn': device_sn + } + ) + + return self._process_response(response.json(), "getting SPH inverter settings") + + def sph_read_parameter(self, device_sn, parameter_id, start_address=None, end_address=None): + """ + Read setting from SPH inverter. + + Args: + device_sn (str): The ID of the SPH inverter. + parameter_id (str): Parameter ID to read. Don't use start_address and end_address if this is set. + start_address (int, optional): Register start address (for set_any_reg). Don't use parameter_id if this is set. + end_address (int, optional): Register end address (for set_any_reg). Don't use parameter_id if this is set. + + Returns: + dict: A dictionary containing the setting value. + + Raises: + GrowattParameterError: If parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + if parameter_id is None and start_address is None: + raise GrowattParameterError( + "specify either parameter_id or start_address/end_address") + elif parameter_id is not None and start_address is not None: + raise GrowattParameterError( + "specify either parameter_id or start_address/end_address - not both." + ) + elif parameter_id is not None: + # named parameter + start_address = 0 + end_address = 0 + else: + # address range + parameter_id = "set_any_reg" + + response = self.session.post( + self._get_url('readMixParam'), + data={ + "mix_sn": device_sn, + "type": parameter_id, + "param1": start_address, + "param2": end_address + } + ) + + return self._process_response(response.json(), f"reading parameter {parameter_id}") + + def sph_write_parameter(self, device_sn, parameter_id, parameter_values=None): + """ + Set parameters on an SPH inverter. + + Args: + device_sn (str): Serial number of the inverter + parameter_id (str): Setting type to be configured + parameter_values: Parameter values to be sent to the system. + Can be a single string (for param1 only), + a list of strings (for sequential params), + or a dictionary mapping param positions to values + + Returns: + dict: JSON response from the server + + Raises: + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + # Initialize all parameters as empty strings + parameters = {i: "" for i in range(1, 20)} + + # Process parameter values based on type + if parameter_values is not None: + if isinstance(parameter_values, (str, int, float, bool)): + # Single value goes to param1 + parameters[1] = str(parameter_values) + elif isinstance(parameter_values, list): + # List of values go to sequential params + for i, value in enumerate(parameter_values, 1): + if i <= 19: # Only use up to 19 parameters + parameters[i] = str(value) + elif isinstance(parameter_values, dict): + # Dict maps param positions to values + for pos, value in parameter_values.items(): + pos = int(pos) if not isinstance(pos, int) else pos + if 1 <= pos <= 19: # Validate parameter positions + parameters[pos] = str(value) + + # IMPORTANT: Create a data dictionary with ALL parameters explicitly included + request_data = { + "mix_sn": device_sn, + "type": parameter_id + } + + # Add all 19 parameters to the request + for i in range(1, 20): + request_data[f"param{i}"] = str(parameters[i]) + + # Send the request + response = self.session.post( + self._get_url('mixSet'), + data=request_data + ) + + return self._process_response(response.json(), f"writing parameter {parameter_id}") + + def sph_write_ac_charge_time(self, device_sn, period_id, charge_power, charge_stop_soc, + start_time, end_time, mains_enabled=True, enabled=True): + """ + Set an AC charge time period for an SPH inverter. + + Args: + device_sn (str): The serial number of the inverter. + period_id (int): Period ID (1-3). + charge_power (int): Charging power percentage (0-100). + charge_stop_soc (int): Stop charging at this SOC percentage (0-100). + start_time (datetime.time): Start time for the period. + end_time (datetime.time): End time for the period. + mains_enabled (bool): Enable grid charging. Default True. + enabled (bool): Whether this period is enabled. Default True. + + Returns: + dict: The server response. + + Raises: + GrowattParameterError: If parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + if not 1 <= period_id <= 3: + raise GrowattParameterError("period_id must be between 1 and 3") + + if not 0 <= charge_power <= 100: + raise GrowattParameterError("charge_power must be between 0 and 100") + + if not 0 <= charge_stop_soc <= 100: + raise GrowattParameterError("charge_stop_soc must be between 0 and 100") + + # Initialize ALL 19 parameters as empty strings + all_params = { + "mix_sn": device_sn, + "type": "mix_ac_charge_time_period" + } + + # Period-specific parameter offsets + base_param = (period_id - 1) * 5 + + # Set parameters according to SPH AC charge format + all_params["param1"] = str(charge_power) + all_params["param2"] = str(charge_stop_soc) + all_params["param3"] = "1" if mains_enabled else "0" + all_params[f"param{base_param + 4}"] = str(start_time.hour) + all_params[f"param{base_param + 5}"] = str(start_time.minute) + all_params[f"param{base_param + 6}"] = str(end_time.hour) + all_params[f"param{base_param + 7}"] = str(end_time.minute) + all_params[f"param{base_param + 8}"] = "1" if enabled else "0" + + # Add empty strings for all other parameters + for i in range(1, 20): + if f"param{i}" not in all_params: + all_params[f"param{i}"] = "" + + # Send the request + response = self.session.post( + self._get_url('mixSet'), + data=all_params + ) + + return self._process_response(response.json(), f"writing AC charge period {period_id}") + + def sph_write_ac_discharge_time(self, device_sn, period_id, discharge_power, discharge_stop_soc, + start_time, end_time, enabled=True): + """ + Set an AC discharge time period for an SPH inverter. + + Args: + device_sn (str): The serial number of the inverter. + period_id (int): Period ID (1-3). + discharge_power (int): Discharging power percentage (0-100). + discharge_stop_soc (int): Stop discharging at this SOC percentage (0-100). + start_time (datetime.time): Start time for the period. + end_time (datetime.time): End time for the period. + enabled (bool): Whether this period is enabled. Default True. + + Returns: + dict: The server response. + + Raises: + GrowattParameterError: If parameters are invalid. + GrowattV1ApiError: If the API returns an error response. + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + if not 1 <= period_id <= 3: + raise GrowattParameterError("period_id must be between 1 and 3") + + if not 0 <= discharge_power <= 100: + raise GrowattParameterError("discharge_power must be between 0 and 100") + + if not 0 <= discharge_stop_soc <= 100: + raise GrowattParameterError("discharge_stop_soc must be between 0 and 100") + + # Initialize ALL 19 parameters as empty strings + all_params = { + "mix_sn": device_sn, + "type": "mix_ac_discharge_time_period" + } + + # Period-specific parameter offsets + base_param = (period_id - 1) * 5 + + # Set parameters according to SPH AC discharge format + all_params["param1"] = str(discharge_power) + all_params["param2"] = str(discharge_stop_soc) + all_params[f"param{base_param + 3}"] = str(start_time.hour) + all_params[f"param{base_param + 4}"] = str(start_time.minute) + all_params[f"param{base_param + 5}"] = str(end_time.hour) + all_params[f"param{base_param + 6}"] = str(end_time.minute) + all_params[f"param{base_param + 7}"] = "1" if enabled else "0" + + # Add empty strings for all other parameters + for i in range(1, 20): + if f"param{i}" not in all_params: + all_params[f"param{i}"] = "" + + # Send the request + response = self.session.post( + self._get_url('mixSet'), + data=all_params + ) + + return self._process_response(response.json(), f"writing AC discharge period {period_id}") + + def sph_read_ac_charge_times(self, device_sn, settings_data=None): + """ + Read AC charge time periods from an SPH inverter. + + Retrieves all 3 AC charge time periods from an SPH inverter and + parses them into a structured format. + + Note that this function uses sph_settings() internally to get the settings data. + To avoid endpoint rate limit, you can pass the settings_data parameter + with the data returned from sph_settings(). + + Args: + device_sn (str): The device serial number of the inverter + settings_data (dict, optional): Settings data from sph_settings call to avoid repeated API calls. + + Returns: + list: A list of dictionaries, each containing details for one time period: + - period_id (int): The period number (1-3) + - start_time (str): Start time in format "HH:MM" + - end_time (str): End time in format "HH:MM" + - enabled (bool): Whether the period is enabled + + Example: + # Option 1: Make a single call + charge_times = api.sph_read_ac_charge_times("DEVICE_SERIAL_NUMBER") + + # Option 2: Reuse existing settings data + settings_response = api.sph_settings("DEVICE_SERIAL_NUMBER") + charge_times = api.sph_read_ac_charge_times("DEVICE_SERIAL_NUMBER", settings_response) + + Raises: + GrowattV1ApiError: If the API request fails + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + # Process the settings data + if settings_data is None: + settings_data = self.sph_settings(device_sn=device_sn) + + periods = [] + + # Process each time period (1-3 for SPH) + for i in range(1, 4): + # Get raw time values + start_time_raw = settings_data.get(f'forcedChargeTimeStart{i}', "0:0") + end_time_raw = settings_data.get(f'forcedChargeTimeStop{i}', "0:0") + enabled_raw = settings_data.get(f'forcedChargeStopSwitch{i}', 0) + + # Handle 'null' string values + if start_time_raw == 'null' or not start_time_raw: + start_time_raw = "0:0" + if end_time_raw == 'null' or not end_time_raw: + end_time_raw = "0:0" + + # Format times with leading zeros (HH:MM) + try: + start_parts = start_time_raw.split(":") + start_hour = int(start_parts[0]) + start_min = int(start_parts[1]) + start_time = f"{start_hour:02d}:{start_min:02d}" + except (ValueError, IndexError): + start_time = "00:00" + + try: + end_parts = end_time_raw.split(":") + end_hour = int(end_parts[0]) + end_min = int(end_parts[1]) + end_time = f"{end_hour:02d}:{end_min:02d}" + except (ValueError, IndexError): + end_time = "00:00" + + # Get the enabled status + if enabled_raw == 'null' or enabled_raw is None: + enabled = False + else: + try: + enabled = int(enabled_raw) == 1 + except (ValueError, TypeError): + enabled = False + + period = { + 'period_id': i, + 'start_time': start_time, + 'end_time': end_time, + 'enabled': enabled + } + + periods.append(period) + + return periods + + def sph_read_ac_discharge_times(self, device_sn, settings_data=None): + """ + Read AC discharge time periods from an SPH inverter. + + Retrieves all 3 AC discharge time periods from an SPH inverter and + parses them into a structured format. + + Note that this function uses sph_settings() internally to get the settings data. + To avoid endpoint rate limit, you can pass the settings_data parameter + with the data returned from sph_settings(). + + Args: + device_sn (str): The device serial number of the inverter + settings_data (dict, optional): Settings data from sph_settings call to avoid repeated API calls. + + Returns: + list: A list of dictionaries, each containing details for one time period: + - period_id (int): The period number (1-3) + - start_time (str): Start time in format "HH:MM" + - end_time (str): End time in format "HH:MM" + - enabled (bool): Whether the period is enabled + + Example: + # Option 1: Make a single call + discharge_times = api.sph_read_ac_discharge_times("DEVICE_SERIAL_NUMBER") + + # Option 2: Reuse existing settings data + settings_response = api.sph_settings("DEVICE_SERIAL_NUMBER") + discharge_times = api.sph_read_ac_discharge_times("DEVICE_SERIAL_NUMBER", settings_response) + + Raises: + GrowattV1ApiError: If the API request fails + requests.exceptions.RequestException: If there is an issue with the HTTP request. + """ + + # Process the settings data + if settings_data is None: + settings_data = self.sph_settings(device_sn=device_sn) + + periods = [] + + # Process each time period (1-3 for SPH) + for i in range(1, 4): + # Get raw time values + start_time_raw = settings_data.get(f'forcedDischargeTimeStart{i}', "0:0") + end_time_raw = settings_data.get(f'forcedDischargeTimeStop{i}', "0:0") + enabled_raw = settings_data.get(f'forcedDischargeStopSwitch{i}', 0) + + # Handle 'null' string values + if start_time_raw == 'null' or not start_time_raw: + start_time_raw = "0:0" + if end_time_raw == 'null' or not end_time_raw: + end_time_raw = "0:0" + + # Format times with leading zeros (HH:MM) + try: + start_parts = start_time_raw.split(":") + start_hour = int(start_parts[0]) + start_min = int(start_parts[1]) + start_time = f"{start_hour:02d}:{start_min:02d}" + except (ValueError, IndexError): + start_time = "00:00" + + try: + end_parts = end_time_raw.split(":") + end_hour = int(end_parts[0]) + end_min = int(end_parts[1]) + end_time = f"{end_hour:02d}:{end_min:02d}" + except (ValueError, IndexError): + end_time = "00:00" + + # Get the enabled status + if enabled_raw == 'null' or enabled_raw is None: + enabled = False + else: + try: + enabled = int(enabled_raw) == 1 + except (ValueError, TypeError): + enabled = False + + period = { + 'period_id': i, + 'start_time': start_time, + 'end_time': end_time, + 'enabled': enabled + } + + periods.append(period) + + return periods