From 4328cabd78d11b74e61cb162e7aae9ff5a09881b Mon Sep 17 00:00:00 2001 From: Joe Cieplinski Date: Tue, 19 Aug 2025 13:37:37 -0700 Subject: [PATCH] Add AC Current Limit functions - Added set_ac_current_limit - Added get_ac_current_settings, in case you want to poll for the current settings - Added an example file --- examples/ac_current_control.py | 108 +++++++++++++++++++++++++++++++++ lucidmotors/__init__.py | 51 ++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 examples/ac_current_control.py diff --git a/examples/ac_current_control.py b/examples/ac_current_control.py new file mode 100644 index 0000000..d5b0252 --- /dev/null +++ b/examples/ac_current_control.py @@ -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") + 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()) diff --git a/lucidmotors/__init__.py b/lucidmotors/__init__.py index 8e56fe3..cabf699 100644 --- a/lucidmotors/__init__.py +++ b/lucidmotors/__init__.py @@ -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: + """ + 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() + + # 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 + ), + }