Skip to main content

A pure Python async client for KNXD's (EIBD's) native Layer 4

Project description

KNXDclient

A pure Python async client for KNXD's (EIBD's) native Layer 4 KNX protocol.

EIBD is a *NIX daemon for routing EIB/KNX telegrams through various interfaces and programmatically accessing the EIB/KNX bus. It's part of the no-longer maintained BCUSDK. However, there's a fork called KNXD, which is still actively maintained.

This package reimplements small parts of the EIBD client (see BCUSDK documentation, section 7.7) in pure Python 3, based on asynchronous coroutines (asyncio). Currently, it allows to open a Group Socket for sending and receiving KNX telegrams to/for any group address via KNXD. Additionally, this package includes helper methods encode_value() and decode_value() to convert the send/received data from/to native Python types according to a known KNX Datapoint Type (DPT).

Usage example

import asyncio
import knxdclient


def handler(packet: knxdclient.ReceivedGroupAPDU) -> None:
    print("Received group telegram: {}".format(packet))

async def main() -> None:
    connection = knxdclient.KNXDConnection()
    connection.set_group_apdu_handler(handler)
    await connection.connect()
    # Connection was successful. Start receive loop:
    run_task = asyncio.create_task(connection.run())
    # Now that the receive loop is running, we can open the KNXd Group Socket:
    await connection.open_group_socket()

    # Startup completed. Now our `handler()` will receive incoming telegrams and we can send some:
    await connection.group_write(knxdclient.GroupAddress(1,3,2),
                                 knxdclient.KNXDAPDUType.WRITE,
                                 knxdclient.encode_value(True, knxdclient.KNXDPT.BOOLEAN))
    
    await asyncio.sleep(5)
    # Let's stop the connection and wait for graceful termination of the receive loop:
    await connection.stop()
    await run_task


asyncio.run(main())

Alternatively, an async iterator can be used for receiving group telegrams:

import asyncio
import knxdclient

# KNX Datapoint Types of some known group addresses, used for decoding incoming values and encoding group RESPONSE
DPTs = {
    knxdclient.GroupAddress(1, 2, 3): knxdclient.KNXDPT.BOOLEAN,
    knxdclient.GroupAddress(4, 5, 6): knxdclient.KNXDPT.FLOAT16,
}

# Some values for responding to group READ telegrams
CURRENT_VALUE = {
    knxdclient.GroupAddress(1, 2, 3): True
}


async def main() -> None:
    connection = knxdclient.KNXDConnection()
    await connection.connect()
    try:
        # Start run task and open group socket
        run_task = asyncio.create_task(connection.run())
        await connection.open_group_socket()

        # Iterate asynchronously over incoming group telegrams
        packet: knxdclient.ReceivedGroupAPDU
        async for packet in connection.iterate_group_telegrams():
            # Respond to GROUP READ telegrams with known values from CURRENT_VALUE dict
            if packet.payload.type == knxdclient.KNXDAPDUType.READ:
                address = packet.dst
                if address in CURRENT_VALUE:
                    await connection.group_write(address,
                                                 knxdclient.KNXDAPDUType.RESPONSE,
                                                 knxdclient.encode_value(CURRENT_VALUE[address], DPTs[address]))
            # Decode and log incoming group WRITE and RESPONSE telegrams 
            else:
                if packet.dst not in DPTs:
                    # Skip telegrams with unknown datatype
                    continue
                value = knxdclient.decode_value(packet.payload.value, DPTs[packet.dst])
                print(f"Telegram from {packet.src} to GAD {packet.dst}: {value}")

    finally:
        # Let's stop the connection and wait for graceful termination of the receive loop:
        await connection.stop()
        await run_task


asyncio.run(main())

License

This package is published under the terms of the Apache License 2.0.

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

knxdclient-1.0.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

knxdclient-1.0.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file knxdclient-1.0.0.tar.gz.

File metadata

  • Download URL: knxdclient-1.0.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for knxdclient-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ba83519463699c39aed577054c87d76c915dcd052d5ec92ad8b0e718a8a079db
MD5 6e28567c68c08f80dfab990a36b826b1
BLAKE2b-256 c073cdab729779bdf9b6f4adaf2f0a95d6fd89c224ac1366add3b73d7367c844

See more details on using hashes here.

File details

Details for the file knxdclient-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: knxdclient-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for knxdclient-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5670c598af36968fe9d8ff2945f3c776e8f20060aa72d9ef8a2bdb86e33682fc
MD5 0792d919d4aa6dd8a7cb3dbb2eb25a34
BLAKE2b-256 0b578ff975edea91b07c7566b26bd313be50c052510c47dad257b1ca877c593b

See more details on using hashes here.

Supported by

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