Skip to main content

Python library for the Turn Touch smart home remote

Project description

TurnTouch Python library

This library provides support for the Turn Touch bluetooth smart home remote.

It is written in Python 3, originally for use with Home Assistant.

Usage

Install this library from PyPI:

pip install TurnTouch

Scanning for Turn Touch devices

Note: Scanning requires root privileges on Linux. To avoid this, skip to the next section and connect to the device without scanning.

import turntouch

# Example 1: Find all devices
devices = turntouch.scan()

# Example 2: Find just one device
device = turntouch.scan(only_one=True)[0]

# Example 3: Extend scan timeout to 60 seconds (default is 10)
devices = turntouch.scan(timeout=60)

turntouch.scan() returns a list of turntouch.TurnTouch objects. A connection is automatically opened to each device, so it is ready to use.

turntouch.TurnTouch is a subclass of bluepy.btle.Peripheral.

Interacting with a Turn Touch device

import turntouch

# Connect to a device by MAC address
tt = turntouch.TurnTouch('c0:ff:ee:c0:ff:ee')

# Read the device nickname and battery percentage
print("Name: {}\nBattery: {}".format(tt.name, tt.battery))

# Update the device nickname (max. 32 characters)
tt.name = 'Living Room Remote'

Listening for button presses

from turntouch import TurnTouch, DefaultActionHandler

class MyHandler(DefaultActionHandler):
    def action_north(self):
        print("Up button pressed.")
    def action_east_double_tap(self):
        print("Right button double-tapped.")
    def action_south_hold(self):
        print("Down button held.")

tt = TurnTouch('c0:ff:ee:c0:ff:ee')
tt.handler = MyHandler()
tt.listen_forever()

# One-liner alternative (same as listen_forever)
TurnTouch('c0:ff:ee:c0:ff:ee', handler=MyHandler(), listen=True)

More advanced usage

Here's a more complex example, triggering some existing functions.

import turntouch

# Define a handler
class MyFancyHandler(turntouch.DefaultActionHandler):

    def __init__(some_object, other_function):
        """Use the __init__ method to pass references to parts of your code,
        such as objects, methods, or variables."""
        self.thing_1 = some_object
        self.other_func = other_function

    def action_any(action):
        """Special handler which is fired for ALL actions.
        `action` is an instance of turntouch.Action."""
        if action.name == "North":
            self.thing_1.some_method()
        elif action.name in ["South", "East", "West"]:
            self.thing_1.other_method()
        else:
            self.other_func()

    def action_south_hold():
        print("You can combine per-button handlers with action_any!")


# Instantiate the handler, passing some application data into it
my_handler = MyFancyHandler(some_object_from_my_application, a_function)

# Scan until we find a device
devices = []
while not devices:
    devices = turntouch.scan(only_one=True)
tt = devices[0]

# Assign the handler to your device.
tt.handler = my_handler

tt.listen_forever()

Listening for just one button press

If you don't want the listener to run forever, do this:

tt = TurnTouch('c0:ff:ee:c0:ff:ee', handler=SomeHandler)
tt.listen()  # Will return as soon as one action occurs.

Error handling

Connection failures will raise turntouch.TurnTouchException. You may want to catch and ignore this exception to retry connecting.

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

TurnTouch-0.3.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

TurnTouch-0.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file TurnTouch-0.3.tar.gz.

File metadata

  • Download URL: TurnTouch-0.3.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for TurnTouch-0.3.tar.gz
Algorithm Hash digest
SHA256 16ddfb3f38c9f4b1abd5ffff9a011a499b7d5b38cbe6d250179aa07c6783b3b5
MD5 d943065c66a7863d89aa0672929fb483
BLAKE2b-256 cf69cc9604094dd8b3d5643627b9705902f80649ee70d3824c0ce6a924b38fbb

See more details on using hashes here.

File details

Details for the file TurnTouch-0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for TurnTouch-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 190063aa238d024d20961c3cbdac737f7cc62e73dfe9e9a2facf536b315372ba
MD5 6baf341446991e48799a7fd88e55a17f
BLAKE2b-256 7fee81e1572adcbac61f198b97bd11cb9b986a803bb16ae1c542f1af66e35774

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