Skip to main content

LCN-PCK library

Project description

pypck - Asynchronous LCN-PCK library written in Python

GitHub release (latest SemVer) GitHub Workflow Status (branch) Codecov branch PyPI - Downloads pre-commit

Overview

pypck is an open source library written in Python which allows the connection to the LCN (local control network) system. It uses the vendor protocol LCN-PCK. To get started an unused license of the coupling software LCN-PCHK and a hardware coupler is necessary.

pypck is used by the LCN integration of the Home Assistant project.

Example

"""Example for switching an output port of module 10 on and off."""
import asyncio

from pypck.connection import PchkConnectionManager
from pypck.lcn_addr import LcnAddr

async def main():
    """Connect to PCK host, get module object and switch output port on and off."""
    async with PchkConnectionManager(
        "192.168.2.41",
        4114,
        username="lcn",
        password="lcn",
        settings={"SK_NUM_TRIES": 0},
    ) as pck_client:
        module = pck_client.get_address_conn(LcnAddr(0, 10, False))

        await module.dim_output(0, 100, 0)
        await asyncio.sleep(1)
        await module.dim_output(0, 0, 0)

asyncio.run(main())

pypck REPL in ipython

pypck relies heavily on asyncio for talking to the LCN-PCHK software. This makes it unusable with the standard python interactive interpreter. Fortunately, ipython provides some support for asyncio in its interactive interpreter, see ipython autoawait.

Requirements

  • ipython at least version 7.0 (autoawait support)
  • pypck

Example session

Python 3.8.3 (default, Jun  9 2020, 17:39:39)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from pypck.connection import PchkConnectionManager
   ...: from pypck.lcn_addr import LcnAddr
   ...: import asyncio

In [2]: connection = PchkConnectionManager(host='localhost', port=4114, username='lcn', password='lcn')

In [3]: await connection.async_connect()

In [4]: module = connection.get_address_conn(LcnAddr(seg_id=0, addr_id=10, is_group=False), request_serials=False)

In [5]: await module.request_serials()
Out[5]:
{'hardware_serial': 127977263668,
 'manu': 1,
 'software_serial': 1771023,
 'hardware_type': <HardwareType.UPU: 26>}

In [6]: await module.dim_output(0, 100, 0)
   ...: await asyncio.sleep(1)
   ...: await module.dim_output(0, 0, 0)
Out[6]: True

Caveats

ipython starts and stops the asyncio event loop for each toplevel command sequence. Also it only starts the loop if the toplevel commands includes async code (like await or a call to an async function). This can lead to unexpected behavior. For example, background tasks run only while ipython is executing toplevel commands that started the event loop. Functions that use the event loop only internally may fail, e.g. the following would fail:

In [4]: module = connection.get_address_conn(LcnAddr(seg_id=0, addr_id=10, is_group=False), request_serials=True)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-cd663974bde2> in <module>
----> 1 module = connection.get_address_conn(modaddr)

/pypck/connection.py in get_address_conn(self, addr, request_serials)
    457                 address_conn = ModuleConnection(self, addr)
    458                 if request_serials:
--> 459                     self.request_serials_task = asyncio.create_task(
    460                         address_conn.request_serials()
    461                     )

/usr/local/lib/python3.8/asyncio/tasks.py in create_task(coro, name)
    379     Return a Task object.
    380     """
--> 381     loop = events.get_running_loop()
    382     task = loop.create_task(coro)
    383     _set_task_name(task, name)

RuntimeError: no running event loop

See ipython autoawait internals 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

pypck-0.7.13.tar.gz (49.5 kB view details)

Uploaded Source

Built Distribution

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

pypck-0.7.13-py3-none-any.whl (52.4 kB view details)

Uploaded Python 3

File details

Details for the file pypck-0.7.13.tar.gz.

File metadata

  • Download URL: pypck-0.7.13.tar.gz
  • Upload date:
  • Size: 49.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.9.7

File hashes

Hashes for pypck-0.7.13.tar.gz
Algorithm Hash digest
SHA256 9268ee5270b243f5fea4548beafa3a5541d1e09643e31a51b9b7d2b8786cc648
MD5 ad9560992b2ad41b1a53217473db70ca
BLAKE2b-256 db8019d7d1ee61469771c2702c023b9ca1f1c250895c8e4b04fa70b93700a989

See more details on using hashes here.

File details

Details for the file pypck-0.7.13-py3-none-any.whl.

File metadata

  • Download URL: pypck-0.7.13-py3-none-any.whl
  • Upload date:
  • Size: 52.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.9.7

File hashes

Hashes for pypck-0.7.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f1147ebf10f594bb0e8f12eb533321ab931ef9d06fa8775143f91285bf2e8379
MD5 71434931beb712c1cd204024917ab349
BLAKE2b-256 74e396b51f4502f469e03b5deeba2fdbfe33c3de3f23de7979657b395732ec50

See more details on using hashes here.

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