Skip to main content

LCN-PCK library

Project description

pypck - Asynchronous LCN-PCK library written in Python

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

Buy Me A Coffee

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.24.tar.gz (54.2 kB view details)

Uploaded Source

Built Distribution

pypck-0.7.24-py3-none-any.whl (45.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pypck-0.7.24.tar.gz
  • Upload date:
  • Size: 54.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pypck-0.7.24.tar.gz
Algorithm Hash digest
SHA256 c54e9e4ea354402972a143eb8dc308552a8293d55ea6f449c1dedbf73f0bcfb6
MD5 63e088e9d0ee024a0cad9bb2b476809a
BLAKE2b-256 c59f589bdf5595b79baba97f66aae54ae33229cb10c7e8306f73b92029f8a63a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypck-0.7.24-py3-none-any.whl
  • Upload date:
  • Size: 45.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pypck-0.7.24-py3-none-any.whl
Algorithm Hash digest
SHA256 b436b73fe141d56c2f2abe065cd1519428cd7b5ff20dbd386d92f90e5cd47c84
MD5 f0f1de269feaeb085ec262036c15c0ae
BLAKE2b-256 e06f5574c84dfd5716d835caa940531dde25f82db1b70df463a861db6782d8a3

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