Skip to main content

Asynchronous Python TCP Client for FlicHub

Project description

Asynchronous Python TCP Client for FlicHub

Get events from the FlicHub when a Flic/Twist Button is clicked and send them to home-assistant-flichub.

To be able to use this client you need to enable the Flic Hub SDK described on this page.

Create a new module and name it pyflichub-tcpclient (or any name) and paste the code found in tcpserver.js in the editor and press play. Check the box "Restart after crash or reboot."

This will open a TCP Server on port 8124 (configurable by changing PORT)

Usage

import asyncio
from pyflichub.client import FlicHubTcpClient
from pyflichub.button import FlicButton
from pyflichub.event import Event

def event_callback(button: FlicButton, event: Event):
    print(f"Received event: {event.event}")
    if button:
        print(f"Button: {button.name} ({button.bdaddr})")

    if event.event == 'button':
        print(f"Action: {event.action}")

def command_callback(cmd):
    print(f"Received command: {cmd.command}")

async def main():
    loop = asyncio.get_event_loop()
    client = FlicHubTcpClient(
        ip='192.168.1.100',
        port=8124,
        loop=loop,
        event_callback=event_callback,
        command_callback=command_callback
    )

    await client.async_connect()

    # Retrieve all buttons
    buttons = await client.get_buttons()
    print(f"Found {len(buttons)} buttons.")

    # Keep the connection alive
    while True:
        await asyncio.sleep(1)

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

Emitted Events

The following events are explicitly dispatched to the event_callback provided during initialization:

  • button: Fired when a button interaction occurs (e.g. click, double-click, hold). Provides the action type in event.action ('down', 'up', 'single', 'double', 'hold', 'idle').
  • buttonAdded: Fired when a new button is paired to the hub. The library will automatically try to fetch its details in the background. The button argument to the callback may be None initially.
  • buttonDeleted: Fired when a button is unpaired/deleted.
  • buttonConnected: Fired when a button makes a physical connection to the hub.
  • buttonDisconnected: Fired when the connection to the button drops.
  • buttonReady: Fired when the button connection has been fully verified and is ready for use.
  • actionMessage: Fired when a Flic Hub Studio message action is executed (configured as a trigger in the Flic app).
  • virtualDeviceUpdate: Fired when a Flic Twist rotates to control a virtual device. Contains values in event.values (like brightness, volume, etc.).

Handling Twist Jitter/Sensitivity

When working with virtual device outputs mapping to Flic Twist, you may find the outputs to be twitchy or experience jitter. To resolve this, you can utilize the provided RateDetentController utility. This allows variable-speed adjusters with features like sticky neutral and debounce to smooth out inputs.

from pyflichub.twist_controller import RateDetentController

# Inside your application...
def on_volume_change(new_volume_pct):
    print(f"Setting volume to: {new_volume_pct}%")

# Create a controller once for the button
volume_controller = RateDetentController(
    cfg={"minOutPct": 0, "maxOutPct": 100},
    on_change_callback=on_volume_change
)

def event_callback(button: FlicButton, event: Event):
    if event.event == 'virtualDeviceUpdate':
        # Push raw values through the controller
        # Make sure you only pass value updates matching the button and device
        volume_controller.update_raw(event.values['volume'] * 100)

Disclaimer

This python library was not made by Flic. It is not official, not developed, and not supported by Flic.

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

pyflichub_tcpclient-0.1.16.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

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

pyflichub_tcpclient-0.1.16-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file pyflichub_tcpclient-0.1.16.tar.gz.

File metadata

  • Download URL: pyflichub_tcpclient-0.1.16.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pyflichub_tcpclient-0.1.16.tar.gz
Algorithm Hash digest
SHA256 0fa53893e79ca99b97aa01cfde75d24b18be5e542c6337c659199ab9e716060e
MD5 9ca39fd71c4c658e26b508f4ea32adbd
BLAKE2b-256 e41552e3751358e3ff286bd5ab1561a2daff2e7511ed8c8840492193957a3831

See more details on using hashes here.

File details

Details for the file pyflichub_tcpclient-0.1.16-py3-none-any.whl.

File metadata

File hashes

Hashes for pyflichub_tcpclient-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 7d278a59ef6633a2aa00c66ba7d9cf90b23ad65f503956f2b5c4f83b76646d1a
MD5 90a975988a8d2a960951f161dcea776f
BLAKE2b-256 a3c8ab0b8d7deb5744e103580aba96c9d4695090927a07afbed6a2466c675171

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