Skip to main content

a simple websocket utility to communicate with lares units

Project description

kseniaWebsocketLibrary

Library for connecting to Ksenia webserver .

  • Supported components: outputs, zones, partitions, scenarios, powerlines. For other components, open a new issue
  • This library may lower the security of your alarm system. Please be aware of it!
  • To establish communication it's necessary to provide an access PIN, DO NOT USE THE ADMINISTRATOR PIN, create an ad hoc user + dedicated pin.
  • The unit allows two modes of communication to the webserver: unencrypted transmission and via SSL. Depending on the version of the unit, SSL communication may not work due to outdated protocols on the unit, which python websocket library can no longer communicate with. Adjust the conf.py as needed.
  • The Lares SSL certificate is self signed. For this reason, SSL Certificate authentication is disabled, this is vulnerable to man in the middle attacks (The same is true when accessing your Lares Ksenia 4.0 central from your PC/Browser!).

PyPI

Requirements

  • Python 3.6+
  • websockets library
  • asyncio library

Installation

To install the required libraries, run:

pip install websockets asyncio

Configuration

Create a kwsConf.py file in your project directory with the following content:

lares_ip = "xxx.xxx.xxx.xxx"        #ip of the lares unit
pin = "xxxxxx"                      #pin to access the unit. DO NOT USE THE ADMIN PIN
ssl = True                          #true or false, based on the net settings of the unit. It allows SSL communication

Usage

WebSocketManager

The WebSocketManager class is used to manage the WebSocket connection to the Ksenia system. It provides the following methods:

  • connect(): Establishes a connection to the websocket.
  • connectSecure(): Establishes a secure SSL connection to the websocket.
  • stop(): Closes the websocket connection.
  • getLights(): Retrieves the list of lights from the system.
  • getDom(): Retrieves the list of domus components from the system.
  • getSwitches(): Retrieves the list of switches from the system.
  • getSensor(sensor_name): Retrieves the list of sensors based on the provided sensor name.
  • getScenarios(): Retrieves the list of scenarios from the system.
  • getSystem(): Retrieves the list of main units from the system.
  • turnOnOutput(output_id): Turns on the output with the specified ID.
  • turnOffOutput(output_id): Turns off the output with the specified ID.
  • executeScenario(scenario_id): Executes the scenario with the specified ID.

Example

import asyncio
import logging
from kseniaWebsocketUtil.websocketmanager import WebSocketManager
import kseniaWebsocketUtil.conf as conf

# Configure logging
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)

# Create WebSocketManager instance
ws = WebSocketManager(conf.lares_ip, conf.pin, logger)

async def main():
    if conf.ssl:
        await ws.connectSecure()
    else:
        await ws.connect()
    
    # Example functions
    lights = await ws.getLights()
    print(lights)
    
    await ws.stop()

# Run the example
asyncio.run(main())

Testing

In order to test the library, you will need a test module and a plugin for async calls as pytest and pytest-asyncio

To run the tests, use the following command:

pytest tests/test_websocketmanager.py

License

This project is licensed under the MIT license. See the LICENSE file for details.

Support

If you find this project useful, consider making a donation to support its development:

Donate with PayPal

thx to https://github.com/gvisconti1983/lares-hass for helpful insights

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

kseniawebsocketlibrary-0.0.50.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

kseniaWebsocketLibrary-0.0.50-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file kseniawebsocketlibrary-0.0.50.tar.gz.

File metadata

  • Download URL: kseniawebsocketlibrary-0.0.50.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for kseniawebsocketlibrary-0.0.50.tar.gz
Algorithm Hash digest
SHA256 f80ec3121206a9f692b6b776bfacae45611465d0a468a0cd3273414afd00aa03
MD5 040b42e47697e59ef57cbf233dd43498
BLAKE2b-256 9b53d94cc6a19aba3ed823420c6c0d510b49790c1dc0a721e8f1241bc8ec7bff

See more details on using hashes here.

File details

Details for the file kseniaWebsocketLibrary-0.0.50-py3-none-any.whl.

File metadata

File hashes

Hashes for kseniaWebsocketLibrary-0.0.50-py3-none-any.whl
Algorithm Hash digest
SHA256 6fbe6516bfb7bd8ccafd11253f55e57dc82d03077afc89a27102728f96fb1f0b
MD5 429e61e5b71182fb87a2ee3849d235a0
BLAKE2b-256 8c207db86d78e5675e8f2c77fb6ae0ab18e8f567270db6975af11b5133d20e70

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