Skip to main content

Method Signature

pub async fn get_plants(&mut self) -> Result<PlantList>

Description

Retrieves a list of all solar plants associated with the authenticated Growatt account. This method automatically handles authentication by checking the current session and re-authenticating if necessary.

Parameters

This method takes no parameters.

Return Type

Returns Result<PlantList> where:
  • Success: PlantList - A wrapper around Vec<Plant> containing all plants
  • Error: GrowattError - See error cases below

PlantList Structure

pub struct PlantList(pub Vec<Plant>);

Plant Fields

plant_id
String
required
Unique identifier for the plant (mapped from JSON field id)
plant_name
String
required
Name of the plant (mapped from JSON field name or plantName)
plant_address
Option<String>
Physical address of the plant installation (mapped from JSON field plantAddress)
plant_watts
Option<f64>
Installed capacity of the plant in watts (mapped from JSON field plantPower)
is_share
Option<bool>
Indicates whether the plant is shared with other users (mapped from JSON field isShare)

Code Examples

Basic Usage

use growatt_api::Growatt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Growatt::new();
    
    // Login first
    client.login("username", "password").await?;
    
    // Get all plants
    let plants = client.get_plants().await?;
    
    // Iterate through plants
    for plant in plants.0.iter() {
        println!("Plant ID: {}", plant.plant_id);
        println!("Plant Name: {}", plant.plant_name);
        
        if let Some(address) = &plant.plant_address {
            println!("Address: {}", address);
        }
        
        if let Some(watts) = plant.plant_watts {
            println!("Capacity: {} W", watts);
        }
    }
    
    Ok(())
}

Using Environment Variables

use growatt_api::Growatt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load credentials from .env file
    let mut client = Growatt::from_env();
    
    // Login using stored credentials
    if let (Some(username), Some(password)) = (
        std::env::var("GROWATT_USERNAME").ok(),
        std::env::var("GROWATT_PASSWORD").ok()
    ) {
        client.login(&username, &password).await?;
    }
    
    // Get plants
    let plants = client.get_plants().await?;
    
    println!("Found {} plants", plants.0.len());
    
    Ok(())
}

Accessing First Plant

use growatt_api::Growatt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Growatt::new();
    client.login("username", "password").await?;
    
    let plants = client.get_plants().await?;
    
    if let Some(first_plant) = plants.0.first() {
        println!("First plant: {} (ID: {})", 
                 first_plant.plant_name, 
                 first_plant.plant_id);
    } else {
        println!("No plants found");
    }
    
    Ok(())
}

Error Cases

The method may return the following errors:

GrowattError::NotLoggedIn

Thrown when the session is invalid and no credentials are stored for automatic re-authentication.
GrowattError::NotLoggedIn
Solution: Call login() with valid credentials before calling this method.

GrowattError::RequestError

Thrown when the HTTP request fails due to network issues or server problems.
GrowattError::RequestError(reqwest::Error)
Common causes:
  • Network connectivity issues
  • Growatt server is down or unreachable
  • Request timeout

GrowattError::InvalidResponse

Thrown when the API returns an empty response or unexpected data structure.
GrowattError::InvalidResponse("Empty response. Please ensure you are logged in.")
Common causes:
  • Session expired during the request
  • Account has no plants associated with it
  • API response format changed

GrowattError::JsonError

Thrown when the response cannot be parsed into the expected structure.
GrowattError::JsonError(serde_json::Error)
Common causes:
  • API response format changed
  • Corrupted response data

Implementation Details

API Endpoint

POST {base_url}/index/getPlantListTitle
Default base URL: https://server.growatt.com

Authentication

This method automatically calls check_login() before making the API request, which:
  1. Checks if the current session is valid
  2. Re-authenticates using stored credentials if the session expired
  3. Returns NotLoggedIn error if no credentials are available

Session Management

The method benefits from automatic session management:
  • Sessions are valid for 30 minutes by default (configurable)
  • Automatic re-authentication happens transparently
  • No need to manually check session validity

Build docs developers (and LLMs) love