Session Management
CPAU API Session Management
This module provides the CpauApiSession class which handles authentication and session management for the CPAU portal.
- class cpau.session.CpauApiSession(userid, password)[source]
Bases:
objectRepresents an authenticated session with the CPAU web portal.
This class handles login, session management, and provides access to meter objects for retrieving usage data.
- __init__(userid, password)[source]
Initialize a CPAU API session.
- Parameters:
- Raises:
CpauAuthenticationError – If login fails
CpauConnectionError – If unable to connect to CPAU portal
- login()[source]
Authenticate with the CPAU portal.
This method is called automatically by __init__ but can be called again to re-authenticate if the session expires.
- Return type:
- Returns:
True if login successful, False otherwise
- Raises:
CpauAuthenticationError – If credentials are invalid
CpauConnectionError – If unable to connect to CPAU portal
- get_electric_meters()[source]
Retrieve all active electric meters associated with this account.
- Return type:
- Returns:
List of CpauElectricMeter objects (typically just one)
- Raises:
CpauApiError – If API request fails
- get_electric_meter(meter_number=None)[source]
Get a specific electric meter, or the default/only meter if meter_number is None.
- Parameters:
meter_number (
Optional[str]) – Optional meter number to retrieve. If None, returns the first active meter found.- Return type:
- Returns:
CpauElectricMeter object
- Raises:
CpauMeterNotFoundError – If specified meter not found
CpauApiError – If API request fails
- property session: Session
Get the underlying requests.Session object.
This is exposed for use by meter objects but should not typically be used directly by library consumers.
- close()[source]
Close the session and clean up resources.
This should be called when done with the session, or use the session as a context manager.
- Return type:
CpauApiSession
- class cpau.session.CpauApiSession(userid, password)[source]
Bases:
objectRepresents an authenticated session with the CPAU web portal.
This class handles login, session management, and provides access to meter objects for retrieving usage data.
Example
from cpau import CpauApiSession # Use as context manager for automatic cleanup with CpauApiSession(userid='user@example.com', password='password') as session: meter = session.get_electric_meter() # Session automatically closed when exiting context # Or manage manually session = CpauApiSession(userid='user@example.com', password='password') try: meter = session.get_electric_meter() # ... use meter ... finally: session.close()
- __init__(userid, password)[source]
Initialize a CPAU API session.
- Parameters:
- Raises:
CpauAuthenticationError – If login fails
CpauConnectionError – If unable to connect to CPAU portal
- login()[source]
Authenticate with the CPAU portal.
This method is called automatically by __init__ but can be called again to re-authenticate if the session expires.
- Return type:
- Returns:
True if login successful, False otherwise
- Raises:
CpauAuthenticationError – If credentials are invalid
CpauConnectionError – If unable to connect to CPAU portal
- get_electric_meters()[source]
Retrieve all active electric meters associated with this account.
- Return type:
- Returns:
List of CpauElectricMeter objects (typically just one)
- Raises:
CpauApiError – If API request fails
- get_electric_meter(meter_number=None)[source]
Get a specific electric meter, or the default/only meter if meter_number is None.
- Parameters:
meter_number (
Optional[str]) – Optional meter number to retrieve. If None, returns the first active meter found.- Return type:
- Returns:
CpauElectricMeter object
- Raises:
CpauMeterNotFoundError – If specified meter not found
CpauApiError – If API request fails
- property session: Session
Get the underlying requests.Session object.
This is exposed for use by meter objects but should not typically be used directly by library consumers.
- close()[source]
Close the session and clean up resources.
This should be called when done with the session, or use the session as a context manager.
- Return type: