alengwenus / pypck

Asynchronous LCN-PCK library written in Python
MIT License
9 stars 4 forks source link

PCK listener sample. Development tips. #125

Open krismarc opened 1 day ago

krismarc commented 1 day ago

Hi @alengwenus,

I've already made few small attempts to get some closer/deeper understanding of your lib so I could test/try things myself. However, I don't know LCN's/PCK nor HA's api yet. I tried to reverse engineer a bit what happens in HA's core LCN's module in order to understand how do you listen what's sent by LCN-PCHK. Somehow I find it yet difficult.

Having a sample, which would let me see what's sent/processed by LCN would be a good starting point, so I could start seeing and putting some debug within the lib. I'm not really developer, however, I'm doing various troubleshooting for living and usually it's a matter of time for me to understand someone's code. I just need a fair starting point :) Later on, I could follow all methods calls chains to understand what you actually do in your code.

If I am not wrong, you listen to all messages sent by LCN-PCHK, and out of them you consider them virtually as devices in HA. I'd like to mimic such a listener, that I could see PCK messages etc. So I can exclude HA itself from initial learning of this lib/pck protocol.

btw, is there any PCK spec/sdk available anywhere?or did you reverse engineer all of this completely yourself? 😄

I've got some aspirations to become junior maintainer here and learn some stuff from all of this. I'm going to be linked with LCN for many upcoming years as my home is based on it, so having deeper knowledge might be beneficial in the future for me.

Best regards, K.M.

krismarc commented 1 day ago

well..,

I've pasted your HA's init into chatgpt and asked it to make it work without HA. Actually it worked with the first attempt :)

import asyncio
import logging
from pypck.connection import PchkConnectionManager
from pypck.inputs import Input

# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

async def handle_input(inp: Input):
    """Process received input from the LCN bus."""
    # Check if the input is a ModInput (you can check for other types if needed)
    if isinstance(inp, pypck.inputs.ModInput):
        logger.info(f"Received input: {inp}")

async def main():
    # Replace these with your actual connection details
    ip_address = '***'
    port = 4114
    username = 'lcn'
    password = '***'

    # Initialize the connection
    connection = PchkConnectionManager(ip_address, port, username, password)

    try:
        # Establish the connection
        await connection.async_connect(timeout=15)
        logger.info("Connected to LCN bus.")

        # Register a callback for incoming inputs
        connection.register_for_inputs(handle_input)

        # Keep the script running to listen for incoming commands
        while True:
            await asyncio.sleep(0.3)

    except Exception as e:
        logger.error(f"An error occurred: {e}")
    finally:
        await connection.async_close()

if __name__ == "__main__":
    asyncio.run(main())