Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions examples/ac_current_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python3
"""
Example script demonstrating AC current control functionality.

This script shows how to:
1. Set the AC current limit for a vehicle
2. Get the current AC current settings
3. Monitor changes in AC current settings

Requirements:
- Python 3.7+
- lucidmotors package installed
- Valid Lucid account credentials
"""

import asyncio
import logging
from lucidmotors import LucidAPI

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


async def main():
"""Main function demonstrating AC current control."""

# Initialize the API client
async with LucidAPI(auto_wake=True) as api:
try:
# Login with your credentials
logger.info("Logging in...")
await api.login("your_email@example.com", "your_password")
Comment thread
borski marked this conversation as resolved.
logger.info("Login successful!")

# Get your vehicles
vehicles = await api.fetch_vehicles()
if not vehicles:
logger.error("No vehicles found in your account")
return

vehicle = vehicles[0] # Use the first vehicle
logger.info(f"Using vehicle: {vehicle.vehicle_id}")

# Get current AC current settings
logger.info("Getting current AC current settings...")
current_settings = await api.get_ac_current_settings(vehicle)

logger.info("Current AC Current Settings:")
logger.info(
f" Active Session Limit: {current_settings['active_session_limit']}A"
)
logger.info(f" Energy Limit: {current_settings['energy_limit']}A")
logger.info(f" Requested Limit: {current_settings['requested_limit']}A")

# Example: Set AC current limit to 32A (common for home charging)
new_limit = 32
logger.info(f"Setting AC current limit to {new_limit}A...")
await api.set_ac_current_limit(vehicle, new_limit)
logger.info("AC current limit set successfully!")

# Wait a moment for the change to take effect
await asyncio.sleep(2)

# Get updated settings to confirm the change
logger.info("Getting updated AC current settings...")
updated_settings = await api.get_ac_current_settings(vehicle)

logger.info("Updated AC Current Settings:")
logger.info(
f" Active Session Limit: {updated_settings['active_session_limit']}A"
)
logger.info(f" Energy Limit: {updated_settings['energy_limit']}A")
logger.info(f" Requested Limit: {updated_settings['requested_limit']}A")

# Example: Set AC current limit to 16A (lower power charging)
lower_limit = 16
logger.info(f"Setting AC current limit to {lower_limit}A...")
await api.set_ac_current_limit(vehicle, lower_limit)
logger.info("AC current limit set successfully!")

# Wait and get final settings
await asyncio.sleep(2)
final_settings = await api.get_ac_current_settings(vehicle)

logger.info("Final AC Current Settings:")
logger.info(
f" Active Session Limit: {final_settings['active_session_limit']}A"
)
logger.info(f" Energy Limit: {final_settings['energy_limit']}A")
logger.info(f" Requested Limit: {final_settings['requested_limit']}A")

except Exception as e:
logger.error(f"Error: {e}")
raise


if __name__ == "__main__":
print("AC Current Control Example")
print("=" * 30)
print(
"This script demonstrates how to control AC current limits for your Lucid vehicle."
)
print("Make sure to update the credentials in the script before running.")
print()

# Run the example
asyncio.run(main())
51 changes: 51 additions & 0 deletions lucidmotors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,54 @@ async def creature_comfort_control(
await _check_for_api_error(
self._vehicle_service.SetCreatureComfortMode(request)
)

async def set_ac_current_limit(self, vehicle: Vehicle, current_limit: int) -> None:

Copilot AI Aug 19, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current_limit parameter lacks validation for reasonable AC current values. Consider adding validation to ensure the current_limit is within a safe and practical range (e.g., 6-80 amperes) to prevent potentially dangerous or invalid values from being sent to the vehicle.

Copilot uses AI. Check for mistakes.
"""
Set the AC current limit for a specific vehicle.

:param vehicle: The vehicle to set the AC current limit for
:param current_limit: The AC current limit in amperes (A)
"""

if self._auto_wake and not self.vehicle_is_awake(vehicle):
await self.wakeup_vehicle(vehicle)

request = vehicle_state_service_pb2.SetACCurrLimitRequest(
ac_curr_lim=current_limit,
vehicle_id=vehicle.vehicle_id,
)
await _check_for_api_error(self._vehicle_service.SetACCurrLimit(request))

async def get_ac_current_settings(self, vehicle: Vehicle) -> dict[str, int]:
"""
Get the current AC current settings for a specific vehicle.

:param vehicle: The vehicle to get AC current settings for
:return: Dictionary containing AC current settings:
- 'active_session_limit': Current AC current limit for active charging session (A)
- 'energy_limit': Energy AC current limit (A)
- 'requested_limit': Requested AC current limit (A)
"""

# Get fresh vehicle state to ensure we have current AC current settings
await self.fetch_vehicles()
Comment thread
borski marked this conversation as resolved.

# Find the updated vehicle data
updated_vehicle = next(
(v for v in self._vehicles if v.vehicle_id == vehicle.vehicle_id), None
)

if not updated_vehicle:
raise APIValueError(f"Vehicle {vehicle.vehicle_id} not found")

return {
'active_session_limit': getattr(
updated_vehicle.state.charging, 'active_session_ac_current_limit', 0
),
'energy_limit': getattr(
updated_vehicle.state.charging, 'energy_ac_current_limit', 0
),
'requested_limit': getattr(
updated_vehicle.state.mobile_app_request, 'ac_current_limit_req', 0
),
}