Skip to main content

Using BLE to control a Casambi-based home lighting system via a web interface.

Project description

Casambi Web Controller

This document outlines the local Casambi lighting control system utilizing a web API interface.

System Architecture

The application functions as middleware. It employs the FastAPI framework to convert standard HTTP requests into Casambi network commands. The connection lifecycle and device interactions are managed via the casambi-bt library.

Limitations

  • The system currently supports only a single Casambi network.
  • Supported devices are exclusively lighting units capable of dimming.

Execution

Execute the main Python script. The terminal will prompt for the Casambi network password. The system will automatically discover the network, establish a connection, and initialize the web server on port 8000.

Logical Bridge Concept

  • The system introduces the concept of a Logical Bridge. This is an abstraction layer that maps physical Casambi lighting hardware to virtual nodes.
  • Instead of hardcoding device-specific commands into external platforms, the Logical Bridge exposes a standardized JSON metadata schema. Each virtual unit within this schema encapsulates its operational events with directly executable Python scripts.
  • This architectural choice ensures high interoperability, allowing any external process with a Python interpreter to dynamically interact with the local HTTP APIs without requiring prior knowledge of the Casambi protocol.

API Endpoints

Get all lights

  • URL: /api/lights
  • Method: GET
  • Description: Retrieves the complete list of available units in the network alongside their standard identifiers and current dimmer values.
  • Sample Response:
    [
      {
          "device_id": "12345678-1234-5678-1234-567812345678",
          "name": "Ceiling Light",
          "dimmer": 255
      },
      {
          "device_id": "87654321-4321-8765-4321-876543210987",
          "name": "Desk Lamp",
          "dimmer": 0
      }
    ]
    

Get specific light status

  • URL: /api/status
  • Method: GET
  • Parameters:
    • device_id (string, required): The standard identifier of the device.
  • Description: Retrieves the current dimmer state of a specifically identified unit. Returns an HTTP 404 error if the unit is not found.
  • Sample Response:
    {
      "device_id": "12345678-1234-5678-1234-567812345678",
      "name": "Ceiling Light",
      "dimmer": 128
    }
    

Control light intensity

  • URL: /api/set
  • Method: GET
  • Parameters:
    • device_id (string, required): The standard identifier of the target device.
    • dimmer (integer, required): The target brightness level, strictly constrained between 0 and 255.
  • Description: Transmits a command to adjust the target device to the specified dimmer level. Returns a success confirmation. Returns an HTTP 400 error if the dimmer value is invalid.
  • Sample Response:
    {
      "status": "success"
    }
    

Read or set light level

  • URL: /api/level
  • Method: GET
  • Parameters:
    • device_id (string, required): The standard identifier of the target device.
    • level (integer, optional): The target brightness level, constrained between 0 and 254 to comply with Matter Level Control Cluster specifications.
  • Description: Retrieves the current level mapped to the Matter logical range (0-254) if no parameter is provided. If provided, sets the dimmer level by mapping the Matter logical range to the underlying hardware range.
  • Sample Response:
    {
      "status": "success",
      "level": 127
    }
    

Get logical bridge metadata

  • URL: /api/metadata
  • Method: GET
  • Description: Dynamically detects the incoming client request context to resolve the host IP and port. It outputs a JSON payload describing the network as a logical bridge. The payload embeds executable Python scripts mapped to standard lighting events for automated HTTP integration.
  • Sample Response:
    {
      "bridge": {
        "id": "casambi_bridge_http",
        "type": "dimmable_lighting_controller",
        "network_host": "192.168.1.220",
        "network_port": 8000
      },
      "devices": [
        {
          "node_id": "casambi_12345678_1234_5678_1234_567812345678",
          "name": "Ceiling Light",
          "device_id": "12345678-1234-5678-1234-567812345678",
          "hardware_type": "dimmable_light",
          "events": {
            "turn_on": {
              "trigger": "on_off_cluster",
              "script": "import urllib.request\nurllib.request.urlopen('http://192.168.1.220:8000/api/level?device_id=12345678-1234-5678-1234-567812345678&level=254')"
            },
            "turn_off": {
              "trigger": "on_off_cluster",
              "script": "import urllib.request\nurllib.request.urlopen('http://192.168.1.220:8000/api/level?device_id=12345678-1234-5678-1234-567812345678&level=0')"
            },
            "set_level": {
              "trigger": "level_control_cluster",
              "script": "import sys, urllib.request\nmatter_level = int(sys.argv[1]) if len(sys.argv) > 1 else 254\nurllib.request.urlopen(f'http://192.168.1.220:8000/api/level?device_id=12345678-1234-5678-1234-567812345678&level={matter_level}')"
            },
            "read_level": {
              "trigger": "level_control_cluster",
              "script": "import urllib.request, json\nresponse = urllib.request.urlopen('http://192.168.1.220:8000/api/level?device_id=12345678-1234-5678-1234-567812345678')\ndata = json.loads(response.read().decode('utf-8'))\nprint(data.get('level', 0))"
            }
          }
        }
      ]
    }
    

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

casambi_web_controller-0.8.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

casambi_web_controller-0.8.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file casambi_web_controller-0.8.0.tar.gz.

File metadata

  • Download URL: casambi_web_controller-0.8.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for casambi_web_controller-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f42b2d5aa3797c6e1ddf7d0f4990c7f2ece4603bcfe87b6fed18316f4999a730
MD5 dd7cdc32cb13fdca8edf04a2cac21cc5
BLAKE2b-256 78adc970a4d9a44c7c1fa9e929e8a23d62e61e56a69bc2296abd1a4e46fb65ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for casambi_web_controller-0.8.0.tar.gz:

Publisher: python-publish.yml on dongnh/casambi_webcontrol

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file casambi_web_controller-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for casambi_web_controller-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 072788853e1ebe343d1b07c42b9ce58b4eeefe0bdf40d9f222f02c0319641fb4
MD5 f6cbffb61ef1838e776048dc59de8384
BLAKE2b-256 10e8a0123a6fe3fbf3f5dceb3739a0e3b8f60d298f34c53a37701d88a14790f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for casambi_web_controller-0.8.0-py3-none-any.whl:

Publisher: python-publish.yml on dongnh/casambi_webcontrol

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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