pypck


Namepypck JSON
Version 0.7.19 PyPI version JSON
download
home_pagehttps://github.com/alengwenus/pypck
SummaryLCN-PCK library
upload_time2024-01-22 06:32:48
maintainer
docs_urlNone
author
requires_python>=3.9
licenseMIT
keywords lcn pck
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pypck - Asynchronous LCN-PCK library written in Python

![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/alengwenus/pypck?color=success)
![GitHub Workflow Status (dev branch)](https://github.com/alengwenus/pypck/actions/workflows/ci.yaml/badge.svg?branch=dev)
![Codecov branch](https://img.shields.io/codecov/c/github/alengwenus/pypck/dev)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/pypck)](https://pypi.org/project/pypck/)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)

## Overview

**pypck** is an open source library written in Python which allows the connection to the [LCN (local control network) system](https://www.lcn.eu). 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](https://home-assistant.io/) project.

## Example

```python
"""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](https://ipython.readthedocs.io/en/stable/interactive/autoawait.html#).

### 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](https://ipython.readthedocs.io/en/stable/interactive/autoawait.html#internals)
for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/alengwenus/pypck",
    "name": "pypck",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "lcn,pck",
    "author": "",
    "author_email": "Andre Lengwenus <alengwenus@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/92/18/9f1f75a1d1e4f8d7fa1bc670ccea067f47bd9d1017b1a85201508482a42a/pypck-0.7.19.tar.gz",
    "platform": "any",
    "description": "# pypck - Asynchronous LCN-PCK library written in Python\n\n![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/alengwenus/pypck?color=success)\n![GitHub Workflow Status (dev branch)](https://github.com/alengwenus/pypck/actions/workflows/ci.yaml/badge.svg?branch=dev)\n![Codecov branch](https://img.shields.io/codecov/c/github/alengwenus/pypck/dev)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/pypck)](https://pypi.org/project/pypck/)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)\n\n## Overview\n\n**pypck** is an open source library written in Python which allows the connection to the [LCN (local control network) system](https://www.lcn.eu). It uses the vendor protocol LCN-PCK.\nTo get started an unused license of the coupling software LCN-PCHK and a hardware coupler is necessary.\n\n**pypck** is used by the LCN integration of the [Home Assistant](https://home-assistant.io/) project.\n\n## Example\n\n```python\n\"\"\"Example for switching an output port of module 10 on and off.\"\"\"\nimport asyncio\n\nfrom pypck.connection import PchkConnectionManager\nfrom pypck.lcn_addr import LcnAddr\n\nasync def main():\n    \"\"\"Connect to PCK host, get module object and switch output port on and off.\"\"\"\n    async with PchkConnectionManager(\n        \"192.168.2.41\",\n        4114,\n        username=\"lcn\",\n        password=\"lcn\",\n        settings={\"SK_NUM_TRIES\": 0},\n    ) as pck_client:\n        module = pck_client.get_address_conn(LcnAddr(0, 10, False))\n\n        await module.dim_output(0, 100, 0)\n        await asyncio.sleep(1)\n        await module.dim_output(0, 0, 0)\n\nasyncio.run(main())\n```\n\n## pypck REPL in ipython\n\n**pypck** relies heavily on asyncio for talking to the LCN-PCHK software. This\nmakes it unusable with the standard python interactive interpreter.\nFortunately, ipython provides some support for asyncio in its interactive\ninterpreter, see\n[ipython autoawait](https://ipython.readthedocs.io/en/stable/interactive/autoawait.html#).\n\n### Requirements\n\n- **ipython** at least version 7.0 (autoawait support)\n- **pypck**\n\n### Example session\n\n```\nPython 3.8.3 (default, Jun  9 2020, 17:39:39)\nType 'copyright', 'credits' or 'license' for more information\nIPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.\n\nIn [1]: from pypck.connection import PchkConnectionManager\n   ...: from pypck.lcn_addr import LcnAddr\n   ...: import asyncio\n\nIn [2]: connection = PchkConnectionManager(host='localhost', port=4114, username='lcn', password='lcn')\n\nIn [3]: await connection.async_connect()\n\nIn [4]: module = connection.get_address_conn(LcnAddr(seg_id=0, addr_id=10, is_group=False), request_serials=False)\n\nIn [5]: await module.request_serials()\nOut[5]:\n{'hardware_serial': 127977263668,\n 'manu': 1,\n 'software_serial': 1771023,\n 'hardware_type': <HardwareType.UPU: 26>}\n\nIn [6]: await module.dim_output(0, 100, 0)\n   ...: await asyncio.sleep(1)\n   ...: await module.dim_output(0, 0, 0)\nOut[6]: True\n```\n\n### Caveats\n\nipython starts and stops the asyncio event loop for each toplevel command\nsequence. Also it only starts the loop if the toplevel commands includes async\ncode (like await or a call to an async function). This can lead to unexpected\nbehavior. For example, background tasks run only while ipython is executing\ntoplevel commands that started the event loop. Functions that use the event\nloop only internally may fail, e.g. the following would fail:\n\n```\nIn [4]: module = connection.get_address_conn(LcnAddr(seg_id=0, addr_id=10, is_group=False), request_serials=True)\n---------------------------------------------------------------------------\nRuntimeError                              Traceback (most recent call last)\n<ipython-input-7-cd663974bde2> in <module>\n----> 1 module = connection.get_address_conn(modaddr)\n\n/pypck/connection.py in get_address_conn(self, addr, request_serials)\n    457                 address_conn = ModuleConnection(self, addr)\n    458                 if request_serials:\n--> 459                     self.request_serials_task = asyncio.create_task(\n    460                         address_conn.request_serials()\n    461                     )\n\n/usr/local/lib/python3.8/asyncio/tasks.py in create_task(coro, name)\n    379     Return a Task object.\n    380     \"\"\"\n--> 381     loop = events.get_running_loop()\n    382     task = loop.create_task(coro)\n    383     _set_task_name(task, name)\n\nRuntimeError: no running event loop\n```\n\nSee\n[ipython autoawait internals](https://ipython.readthedocs.io/en/stable/interactive/autoawait.html#internals)\nfor details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "LCN-PCK library",
    "version": "0.7.19",
    "project_urls": {
        "Bug Reports": "https://github.com/alengwenus/pypck/issues",
        "Homepage": "https://github.com/alengwenus/pypck",
        "Source Code": "https://github.com/alengwenus/pypck"
    },
    "split_keywords": [
        "lcn",
        "pck"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "15f25d1f348381f8175938283638a0cfab2f0a12ef44cea2aa81d870c02fd1c3",
                "md5": "02851aecf8cac0196cd638cdb6a3f453",
                "sha256": "817c3f98bf1eba8c7b9c7364a02ab563b4c334fb6f4834683304101084e5153d"
            },
            "downloads": -1,
            "filename": "pypck-0.7.19-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "02851aecf8cac0196cd638cdb6a3f453",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 45388,
            "upload_time": "2024-01-22T06:32:46",
            "upload_time_iso_8601": "2024-01-22T06:32:46.375594Z",
            "url": "https://files.pythonhosted.org/packages/15/f2/5d1f348381f8175938283638a0cfab2f0a12ef44cea2aa81d870c02fd1c3/pypck-0.7.19-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92189f1f75a1d1e4f8d7fa1bc670ccea067f47bd9d1017b1a85201508482a42a",
                "md5": "e425a56b27767c2c2a3a0198defee2eb",
                "sha256": "4cabad20ec79c55c25b593ed672811ef6fc0a7a15166a22d4cc35063c559693b"
            },
            "downloads": -1,
            "filename": "pypck-0.7.19.tar.gz",
            "has_sig": false,
            "md5_digest": "e425a56b27767c2c2a3a0198defee2eb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 53444,
            "upload_time": "2024-01-22T06:32:48",
            "upload_time_iso_8601": "2024-01-22T06:32:48.426955Z",
            "url": "https://files.pythonhosted.org/packages/92/18/9f1f75a1d1e4f8d7fa1bc670ccea067f47bd9d1017b1a85201508482a42a/pypck-0.7.19.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-22 06:32:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "alengwenus",
    "github_project": "pypck",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pypck"
}
        
Elapsed time: 0.16584s