Skip to content

API reference documentation – messages module

KMP protocol application level decoding/encoding (requests and responses).

Concrete message types

pykmp.messages.GetTypeRequest

Bases: BaseRequest['GetTypeResponse'], WithoutDataMixin

Request the meter type and software revision.

response_type: type[GetTypeResponse] class-attribute

command_id: int = constants.CommandId.GET_TYPE.value class-attribute

command_name: str = 'GetType' class-attribute

get_response_type() -> type[Res_t_co] classmethod

Return the response class (type) matching this request class.

decode(data: codec.ApplicationData) -> Self classmethod

Decode the message (command ID only, no data expected for this message).

encode() -> codec.ApplicationData

Encode the message (command ID only, no data expected for this message).

pykmp.messages.GetTypeResponse

Bases: BaseResponse[GetTypeRequest], WithDataMixin, SupportsDecode, SupportsEncode

Response with the meter type and its software revision.

data_raw: codec.ApplicationDataBytes | None = attrs.field(default=None) class-attribute instance-attribute

request_type: type = GetTypeRequest class-attribute

command_id: int = GetTypeRequest.command_id class-attribute

command_name: str = GetTypeRequest.command_name class-attribute

meter_type_bytes: bytes = attrs.field() class-attribute instance-attribute

software_revision: str | None = attrs.field() class-attribute instance-attribute

METER_TYPE_LENGTH_ENCODED: int = 2 class-attribute

SOFTWARE_REVISION_LENGTH_ENCODED: int = 2 class-attribute

SOFTWARE_REVISION_LETTER_INT_MIN: int = 1 class-attribute

SOFTWARE_REVISION_LETTER_INT_MAX: int = 26 class-attribute

SOFTWARE_REVISION_STR_RE: re.Pattern[str] = re.compile(f'^(?P<letter>[A-Z])(?P<number>{ZERO_TO_255_RE.pattern})$', re.VERBOSE) class-attribute

SOFTWARE_REVISION_UNAVAILABLE_BYTES: bytes = b'\x00\x00' class-attribute

get_request_type() -> type[Req_t_co] classmethod

Return the request class (type) matching this response class.

decode(data: codec.ApplicationData) -> Self classmethod

Decode GetType response.

encode() -> codec.ApplicationData

Encode GetType response.

pykmp.messages.GetSerialRequest

Bases: BaseRequest['GetSerialResponse'], WithoutDataMixin

Request the meter's serial number.

response_type: type[GetSerialResponse] class-attribute

command_id: int = constants.CommandId.GET_SERIAL.value class-attribute

command_name: str = 'GetSerialNo' class-attribute

get_response_type() -> type[Res_t_co] classmethod

Return the response class (type) matching this request class.

decode(data: codec.ApplicationData) -> Self classmethod

Decode the message (command ID only, no data expected for this message).

encode() -> codec.ApplicationData

Encode the message (command ID only, no data expected for this message).

pykmp.messages.GetSerialResponse

Bases: BaseResponse[GetSerialRequest], WithDataMixin, SupportsDecode, SupportsEncode

Response with the serial number of the meter.

data_raw: codec.ApplicationDataBytes | None = attrs.field(default=None) class-attribute instance-attribute

request_type: type class-attribute

command_id: int = GetSerialRequest.command_id class-attribute

command_name: str = GetSerialRequest.command_name class-attribute

serial: str = attrs.field() class-attribute instance-attribute

SERIAL_LENGTH_ENCODED: int = 4 class-attribute

SERIAL_VALUE_MAX: int = 2 ** SERIAL_LENGTH_ENCODED * 8 - 1 class-attribute

get_request_type() -> type[Req_t_co] classmethod

Return the request class (type) matching this response class.

digits_only_validator(_: attrs.Attribute[Self], value: str) -> None

Validate all characters in string of serial number are digits.

decode(data: codec.ApplicationData) -> Self classmethod

Decode GetSerialNo response.

encode() -> codec.ApplicationData

Encode GetSerialNo response.

pykmp.messages.GetRegisterRequest

Bases: BaseRequest['GetRegisterResponse'], WithDataMixin, SupportsDecode, SupportsEncode

Request register values.

data_raw: codec.ApplicationDataBytes | None = attrs.field(default=None) class-attribute instance-attribute

response_type: type[GetRegisterResponse] class-attribute

command_id: int = constants.CommandId.GET_REGISTER.value class-attribute

command_name: str = 'GetRegister' class-attribute

registers: Collection[RegisterID] = attrs.field(converter=_ints2register_ids) class-attribute instance-attribute

NUM_REGISTERS_LENGTH_ENCODED: int = 1 class-attribute

NUM_REGISTERS_MAX: int = 8 class-attribute

REGISTER_ID_LENGTH_ENCODED: int = 2 class-attribute

REGISTER_ID_VALUE_MAX: int = 2 ** REGISTER_ID_LENGTH_ENCODED * 8 - 1 class-attribute

get_response_type() -> type[Res_t_co] classmethod

Return the response class (type) matching this request class.

register_id_validator(_: attrs.Attribute[Self], value: Collection[RegisterID]) -> None

Validate all register IDs in collection requested are within valid range.

decode(data: codec.ApplicationData) -> Self classmethod

Decode GetRegister request.

encode() -> codec.ApplicationData

Encode GetRegister request.

pykmp.messages.GetRegisterResponse

Bases: BaseResponse[GetRegisterRequest], WithDataMixin, SupportsDecode, SupportsEncode

Response with register value(s).

data_raw: codec.ApplicationDataBytes | None = attrs.field(default=None) class-attribute instance-attribute

request_type: type = GetRegisterRequest class-attribute

command_id: int = GetRegisterRequest.command_id class-attribute

command_name: str = GetRegisterRequest.command_name class-attribute

registers: Mapping[RegisterID, RegisterData] = attrs.field(factory=dict) class-attribute instance-attribute

REGISTER_ID_LENGTH_ENCODED: int = 2 class-attribute

REGISTER_UNIT_LENGTH_ENCODED: int = 1 class-attribute

REGISTER_VALUE_LENGTH_LENGTH_ENCODED: int = 1 class-attribute

REGISTER_VALUE_FORMAT_LENGTH_ENCODED: int = 1 class-attribute

get_request_type() -> type[Req_t_co] classmethod

Return the request class (type) matching this response class.

decode(data: codec.ApplicationData) -> Self classmethod

Decode the GetRegister response.

encode() -> codec.ApplicationData

Encode the GetRegister response.

Data classes & types

pykmp.messages.RegisterData

Partially decoded register data.

id_: RegisterID instance-attribute

unit: RegisterUnit instance-attribute

value: RegisterValueBytes instance-attribute

pykmp.messages.RegisterID = NewType('RegisterID', int) module-attribute

pykmp.messages.RegisterUnit = NewType('RegisterUnit', int) module-attribute

pykmp.messages.RegisterValueBytes = NewType('RegisterValueBytes', bytes) module-attribute

pykmp.messages.Req_t_co = TypeVar('Req_t_co', bound='BaseRequest[Any]', covariant=True) module-attribute

pykmp.messages.Res_t_co = TypeVar('Res_t_co', bound='BaseResponse[Any]', covariant=True) module-attribute

Base classes

pykmp.messages.HasCommandIdAndName

Bases: Protocol

command_id: int class-attribute

command_name: str class-attribute

pykmp.messages.BaseRequest

Bases: HasCommandIdAndName, Protocol[Res_t_co]

Base protocol for any message that is a request (client to meter).

Generically type-annotated for the response type matching the request.

command_id: int class-attribute

command_name: str class-attribute

response_type: type class-attribute

get_response_type() -> type[Res_t_co] classmethod

Return the response class (type) matching this request class.

pykmp.messages.BaseResponse

Bases: HasCommandIdAndName, Protocol[Req_t_co]

Base protocol for any message that is a response (meter to client).

Generically type-annotated for the request type matching the response.

command_id: int class-attribute

command_name: str class-attribute

request_type: type class-attribute

get_request_type() -> type[Req_t_co] classmethod

Return the request class (type) matching this response class.

pykmp.messages.SupportsDecode

Bases: Protocol

Base protocol to indicate it supports decoding (has decode() class method).

decode(data: codec.ApplicationData) -> Self classmethod

Decode application data to structured message instance (of type itself).

pykmp.messages.SupportsEncode

Bases: Protocol

Base protocol to indicate it supports encoding (has encode() class method).

encode() -> codec.ApplicationData

Encode itself (structured message instance) to application data.

pykmp.messages.WithDataMixin

Bases: HasCommandIdAndName, Protocol

Protocol with 'data_raw' attribute to reference the pre-decoded original data.

command_id: int class-attribute

command_name: str class-attribute

data_raw: codec.ApplicationDataBytes | None = attrs.field(default=None) class-attribute instance-attribute

Exceptions

pykmp.messages.MessageCidMismatchError

Bases: BaseCodecError

Command ID in data does not match the ID defined for the class to decode to.

message_class_name: str instance-attribute

cid_expected: int instance-attribute

actual: int instance-attribute

__str__() -> str

pykmp.messages.DataWithNoDataError

Bases: BaseCodecError

Unexpected data for command that does not accept any (aside the command ID).

message_class: type[HasCommandIdAndName] instance-attribute

__str__() -> str

pykmp.messages.SoftwareRevisionInvalidError

Bases: BaseCodecError

revision_string: str instance-attribute

__str__() -> str

pykmp.messages.SerialNumberInvalidError

Bases: BaseCodecError

serial: str instance-attribute

__str__() -> str