API Reference
Core Classes
OCMF
The main class for parsing, validating, and verifying OCMF data.
OCMF
Bases: BaseModel
OCMF data model with three pipe-separated sections: header, payload, and signature.
Source code in src/pyocmf/core/ocmf.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
from_string(ocmf_string: str) -> OCMF
classmethod
Parse an OCMF string into an OCMF model.
Automatically detects whether the input is plain text (starts with "OCMF|") or hex-encoded and handles both formats.
Source code in src/pyocmf/core/ocmf.py
to_string(hex: bool = False) -> str
Convert the OCMF model to string format "OCMF|{payload}|{signature}".
Set hex=True to return hex-encoded string instead of plain text.
Source code in src/pyocmf/core/ocmf.py
verify_signature(public_key: PublicKey | str) -> bool
Verify the cryptographic signature of the OCMF data.
Per OCMF spec, public keys must be transmitted out-of-band (separately from OCMF data). Requires that the OCMF was parsed from a string (not constructed programmatically) because signature verification needs the exact original payload bytes.
Source code in src/pyocmf/core/ocmf.py
check_eichrecht(other: OCMF | None = None, *, errors_only: bool = False) -> list[EichrechtIssue]
Check German calibration law (Eichrecht) compliance.
Validates that OCMF data complies with German Eichrecht requirements (MID 2014/32/EU and PTB) for billing-relevant meter readings.
Provide 'other' OCMF to check transaction pair (begin + end). Set errors_only=True to filter out warnings.
Source code in src/pyocmf/core/ocmf.py
verify(public_key: PublicKey | str, other: OCMF | None = None, eichrecht: bool = True) -> tuple[bool, list[EichrechtIssue]]
Verify both cryptographic signature and legal compliance.
Combines signature verification and Eichrecht compliance checking. Returns (signature_valid, compliance_issues). Set eichrecht=False to skip compliance checking.
Source code in src/pyocmf/core/ocmf.py
Payload
The payload section containing meter readings and metadata.
Payload
Bases: BaseModel
Source code in src/pyocmf/core/payload.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
apply_reading_inheritance(data: dict) -> dict
classmethod
Apply field inheritance for readings.
Per OCMF spec, some reading fields can be inherited from the previous reading if not specified.
Source code in src/pyocmf/core/payload.py
validate_serial_numbers() -> Payload
Either GS or MS must be present for signature component identification.
Per OCMF spec: GS is optional (0..1) but MS is mandatory (1..1). However, at least one must be non-None (though can be empty string).
Source code in src/pyocmf/core/payload.py
validate_id_format_by_type() -> Payload
Validate ID format based on the Identification Type (IT).
For most types, validation is strict (raises ValidationError). For ISO14443 and ISO15693, validation emits warnings but allows non-standard formats, as real-world RFID cards may have vendor-specific implementations.
Source code in src/pyocmf/core/payload.py
Reading
Individual meter reading data.
Reading
Bases: BaseModel
Source code in src/pyocmf/core/reading.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
validate_reading_unit(v: OCMFUnit | str) -> OCMFUnit | str
classmethod
Validate RU is spec-compliant, warn if not.
Source code in src/pyocmf/core/reading.py
Signature
Cryptographic signature data and verification.
Signature
Bases: BaseModel
Source code in src/pyocmf/core/signature.py
Compliance Checking
Eichrecht Compliance
Functions for validating OCMF data against German Eichrecht (calibration law) requirements.
check_eichrecht_reading(reading: Reading, is_begin: bool = False) -> list[EichrechtIssue]
Check a single reading for Eichrecht compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reading
|
Reading
|
The reading to check |
required |
is_begin
|
bool
|
Whether this is a transaction begin reading (affects CL checking) |
False
|
Returns:
| Type | Description |
|---|---|
list[EichrechtIssue]
|
List of compliance issues (empty if compliant) |
Source code in src/pyocmf/compliance/reading.py
check_eichrecht_transaction(begin: Payload, end: Payload) -> list[EichrechtIssue]
Check a complete charging transaction for Eichrecht compliance.
Source code in src/pyocmf/compliance/transaction.py
validate_transaction_pair(begin: OCMF, end: OCMF) -> bool
Validate transaction pair compliance (errors only, warnings ignored).
Source code in src/pyocmf/compliance/transaction.py
Compliance Models
EichrechtIssue
dataclass
Source code in src/pyocmf/compliance/models.py
IssueCode
Bases: StrEnum
Source code in src/pyocmf/compliance/models.py
IssueSeverity
Data Models
PublicKey
Public key metadata and validation per OCMF specification.
PublicKey
Bases: BaseModel
Source code in src/pyocmf/models/public_key.py
from_string(key_string: str) -> Self
classmethod
Parse DER public key string (hex or base64) and extract metadata.
Source code in src/pyocmf/models/public_key.py
OBIS
OBIS code model for meter reading identifiers.
OBIS
Bases: BaseModel
Source code in src/pyocmf/models/obis.py
OCMFTimestamp
Timestamp with time synchronization status.
OCMFTimestamp
dataclass
Source code in src/pyocmf/models/timestamp.py
from_string(timestamp_str: str) -> OCMFTimestamp
classmethod
Parse OCMF timestamp string to OCMFTimestamp.
OCMF format: "2023-06-15T14:30:45,123+0200 S" (note: comma for milliseconds).
Source code in src/pyocmf/models/timestamp.py
serialize() -> str
Serialize to OCMF timestamp format.
Uses comma for milliseconds as required by OCMF spec.
Source code in src/pyocmf/models/timestamp.py
CableLossCompensation
Cable loss compensation data.
CableLossCompensation
Bases: BaseModel
Source code in src/pyocmf/models/cable_loss.py
Registries
OBIS Code Registry
Utilities for working with OBIS codes.
get_obis_info(obis_code: str) -> OBISInfo | None
is_billing_relevant(obis_code: str) -> bool
Source code in src/pyocmf/registries/obis.py
is_accumulation_register(obis_code: str) -> bool
is_transaction_register(obis_code: str) -> bool
OBISInfo
dataclass
Source code in src/pyocmf/registries/obis.py
XML Utilities
OcmfContainer
Container for parsing OCMF data from XML transaction files.
OcmfContainer
Source code in src/pyocmf/utils/xml.py
from_xml(xml_path: pathlib.Path | str) -> OcmfContainer
classmethod
Parse OCMF data from an XML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_path
|
Path | str
|
Path to the XML file |
required |
Returns:
| Type | Description |
|---|---|
OcmfContainer
|
OcmfContainer with parsed OCMF entries |
Raises:
| Type | Description |
|---|---|
XmlParsingError
|
If the XML file cannot be parsed |
DataNotFoundError
|
If no OCMF data is found |
Source code in src/pyocmf/utils/xml.py
OcmfRecord
A single OCMF record with its associated public key.
OcmfRecord
dataclass
Source code in src/pyocmf/utils/xml.py
Exceptions
All exceptions inherit from PyOCMFError.
PyOCMFError
OCMF Parsing Errors
OcmfFormatError
Bases: PyOCMFError
OcmfPayloadError
Bases: PyOCMFError
OcmfSignatureError
Bases: PyOCMFError
Validation Errors
ValidationError
Bases: PyOCMFError, ValueError
Encoding Errors
EncodingError
EncodingTypeError
Bases: PyOCMFError, TypeError
Source code in src/pyocmf/exceptions.py
HexDecodingError
Bases: EncodingError
Base64DecodingError
Bases: EncodingError
Cryptography Errors
CryptoError
Bases: PyOCMFError
SignatureVerificationError
PublicKeyError
Data Errors
DataNotFoundError
Bases: PyOCMFError
XmlParsingError
Bases: PyOCMFError