Skip to main content

A Python client for the Datadis API.

Project description

Python Datadis API Client

A Python client library to interact with the Datadis API.

Datadis is the Spanish electricity distributor's data access platform that allows users to retrieve information about their electricity consumption and contracts.

Installation

pip install datadis-client

Usage

Authentication

from datadis_client import create_client

# Method 1: Create the client and authenticate in one step
client = create_client(username="YOUR_NIF", password="YOUR_PASSWORD")

# Method 2: Create the client and authenticate separately
from datadis_client import DatadisClient
client = DatadisClient(username="YOUR_NIF", password="YOUR_PASSWORD")
client.authenticate()

Getting supplies

# Get all supplies associated with your account
supplies = client.get_supplies()

# Get supplies for a specific distributor
supplies = client.get_supplies(distributor_code="0123")

# Get supplies for an authorized NIF
supplies = client.get_supplies(authorized_nif="AUTHORIZED_NIF")

Getting contract details

# Get contract details for a specific CUPS
contract = client.get_contract_detail(
    cups="ES0021000000000000AA1F",
    distributor_code="0123"
)

Getting consumption data

# Get hourly consumption data for a specific period
consumption = client.get_consumption_data(
    cups="ES0021000000000000AA1F",
    distributor_code="0123",
    start_date="2023/01",
    end_date="2023/02",
    measurement_type="0",  # 0 for hourly, 1 for quarter-hourly
    point_type="5"
)

# Access consumption readings
for reading in consumption.get('timeCurve', []):
    date = reading.get('date')
    time = reading.get('time')
    consumption_kwh = reading.get('consumptionKWh')
    print(f"{date} {time}: {consumption_kwh} kWh")

Other available methods

The client provides access to all Datadis API endpoints:

  • get_max_power - Get maximum power data
  • get_reactive_data - Get reactive energy data
  • get_distributors_with_supplies - Get distributors with supplies
  • api_search - Search for consumption data by parameters
  • api_sum_search - Search for summarized consumption data
  • api_search_auto - Search for auto-consumption data

Examples

Check the examples directory for complete usage examples.

Development

Setting up a development environment

Clone the repository and install development dependencies:

git clone https://github.com/agaliste/python-datadis.git
cd python-datadis
pip install -e ".[dev]"

Running tests

Run the unit tests with pytest:

pytest tests/test_auth.py tests/test_consumption.py tests/test_supplies.py tests/test_search.py

Run tests with coverage:

pytest --cov=datadis_client

Generate coverage reports:

pytest --cov=datadis_client --cov-report=html

Running integration tests

Integration tests make actual API calls to Datadis and require valid credentials. These tests verify that the client works correctly with the real Datadis API.

  1. Create a .env file in the project root with your Datadis credentials:

    # Datadis API credentials (required)
    DATADIS_USERNAME=your_nif_here
    DATADIS_PASSWORD=your_password_here
    
    # Test data (optional)
    # If not provided, tests will auto-discover a CUPS from your account
    TEST_CUPS=ES0021000000000000XXXX
    TEST_DISTRIBUTOR_CODE=0000
    TEST_POINT_TYPE=5
    
  2. Run the integration tests using the provided scripts:

    # Unix/macOS
    ./run_tests.sh
    
    # Windows
    run_tests.bat
    

    Or run them directly:

    pytest tests/test_integration.py -v
    

    If you don't provide specific TEST_CUPS values, the integration tests will automatically use the first CUPS found in your account.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_datadis-0.0.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_datadis-0.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file python_datadis-0.0.1.tar.gz.

File metadata

  • Download URL: python_datadis-0.0.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_datadis-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3780f9a4062e93840fb61989d2783d268a889850ea5b4812826075c0badeb1ed
MD5 17000558af2e44a98b2991a5318728a4
BLAKE2b-256 8bc9a2cea55418be16dcccdc98ce8de2b02e23d2b71aa6f55022fccd427d1e66

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_datadis-0.0.1.tar.gz:

Publisher: build_and_publish_package.yaml on agaliste/python-datadis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_datadis-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_datadis-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_datadis-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 755502beabed0d476533864502d1b55e7c60358a944b52e67f1880420fcf4c50
MD5 6318ca65fae8e9ca548d06104ab18a20
BLAKE2b-256 b1f8f325a2ee1da8cccd5b9b0b8f98c4631090c1d746e8c395c9ba6dda1a5b3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_datadis-0.0.1-py3-none-any.whl:

Publisher: build_and_publish_package.yaml on agaliste/python-datadis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page