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: object

Represents 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:
  • userid (str) – CPAU account username

  • password (str) – CPAU account password

Raises:
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:

bool

Returns:

True if login successful, False otherwise

Raises:
get_electric_meters()[source]

Retrieve all active electric meters associated with this account.

Return type:

list[CpauElectricMeter]

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:

CpauElectricMeter

Returns:

CpauElectricMeter object

Raises:
property is_authenticated: bool

Check if the session is currently authenticated.

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:

None

__enter__()[source]

Support for context manager (with statement).

Return type:

CpauApiSession

__exit__(exc_type, exc_val, exc_tb)[source]

Clean up when exiting context manager.

Return type:

None

CpauApiSession

class cpau.session.CpauApiSession(userid, password)[source]

Bases: object

Represents 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:
  • userid (str) – CPAU account username

  • password (str) – CPAU account password

Raises:
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:

bool

Returns:

True if login successful, False otherwise

Raises:
get_electric_meters()[source]

Retrieve all active electric meters associated with this account.

Return type:

list[CpauElectricMeter]

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:

CpauElectricMeter

Returns:

CpauElectricMeter object

Raises:
property is_authenticated: bool

Check if the session is currently authenticated.

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:

None

__enter__()[source]

Support for context manager (with statement).

Return type:

CpauApiSession

__exit__(exc_type, exc_val, exc_tb)[source]

Clean up when exiting context manager.

Return type:

None