switchbot.bluetooth

switchbot.bluetooth

class platypush.plugins.switchbot.bluetooth.SwitchbotBluetoothPlugin(interface=None, connect_timeout=None, scan_timeout=2, devices=None, **kwargs)[source]

Plugin to interact with a Switchbot (https://www.switch-bot.com/) device and programmatically control switches over a Bluetooth interface.

See platypush.plugins.bluetooth.ble.BluetoothBlePlugin for how to enable BLE permissions for the platypush user (a simple solution may be to run it as root, but that’s usually NOT a good idea).

Requires:

  • pybluez (pip install pybluez)

  • gattlib (pip install gattlib)

  • libboost (on Debian `apt-get install libboost-python-dev libboost-thread-dev)

class Command(value)[source]

Base64 encoded commands

__init__(interface=None, connect_timeout=None, scan_timeout=2, devices=None, **kwargs)[source]
Parameters:
  • interface (str) – Bluetooth interface to use (e.g. hci0) default: first available one

  • connect_timeout (float) – Timeout for the connection to the Switchbot device - default: None

  • scan_timeout (float) – Timeout for the scan operations

  • devices (dict) – Devices to control, as a MAC address -> name map

connect(device: str, interface: str = None, wait: bool = True, channel_type: str = 'public', security_level: str = 'low', psm: int = 0, mtu: int = 0, timeout: float = 10.0)

Connect to a bluetooth LE device

Parameters:
  • device – Device address to connect to

  • interface – Bluetooth adapter name to use (default configured if None)

  • wait – If True then wait for the connection to be established before returning (no timeout)

  • channel_type – Channel type, usually ‘public’ or ‘random’

  • security_level – Security level - possible values: [‘low’, ‘medium’, ‘high’]

  • psm – PSM value (default: 0)

  • mtu – MTU value (default: 0)

  • timeout – Connection timeout if wait is not set (default: 10 seconds)

disconnect(device: str)

Disconnect from a connected device

Parameters:

device – Device address

discover_characteristics(device: str, interface: str = None, **kwargs) BluetoothDiscoverCharacteristicsResponse

Discover the characteristics of a LE bluetooth device

Parameters:
  • device – Device address to connect to

  • interface – Bluetooth adapter name to use (default configured if None)

  • kwargs – Extra arguments to be passed to connect()

discover_primary(device: str, interface: str = None, **kwargs) BluetoothDiscoverPrimaryResponse

Discover the primary services advertised by a LE bluetooth device

Parameters:
  • device – Device address to connect to

  • interface – Bluetooth adapter name to use (default configured if None)

  • kwargs – Extra arguments to be passed to connect()

get_data(*args, **kwargs)

Alias for get_measurement

get_measurement(interface: Optional[str] = None, duration: Optional[int] = 10, *args, **kwargs) Dict[str, dict]

Wrapper for scan that returns bluetooth devices in a format usable by sensor backends.

Parameters:
  • interface – Bluetooth adapter name to use (default configured if None)

  • duration – Scan duration in seconds

Returns:

Device address -> info map.

off(device, **kwargs)[source]

Send a press-off button command to a device

Parameters:

device (str) – Device name or address

on(device, **kwargs)[source]

Send a press-on button command to a device

Parameters:

device (str) – Device name or address

press(device)[source]

Send a press button command to a device

Parameters:

device (str) – Device name or address

read(device: str, interface: str = None, uuid: str = None, handle: int = None, binary: bool = False, disconnect_on_recv: bool = True, **kwargs) str

Read a message from a device

Parameters:
  • device – Device address to connect to

  • interface – Bluetooth adapter name to use (default configured if None)

  • uuid – Service UUID. Either the UUID or the device handle must be specified

  • handle – Device handle. Either the UUID or the device handle must be specified

  • binary – Set to true to return data as a base64-encoded binary string

  • disconnect_on_recv – If True (default) disconnect when the response is received

  • kwargs – Extra arguments to be passed to connect()

scan(interface: str = None, duration: int = 10) BluetoothScanResponse[source]

Scan for available Switchbot devices nearby.

Parameters:
  • interface – Bluetooth interface to scan (default: default configured interface)

  • duration – Scan duration in seconds

status(device=None, *args, **kwargs) Union[dict, List[dict]]

Get the status of all the devices, or filter by device name or ID (alias for switch_status()).

Parameters:

device – Filter by device name or ID.

Returns:

[
  {
    "id": "Device unique ID",
    "name": "Device name",
    "on": "True if the device is on, False otherwise"
  }
]

switch_status(device=None) Union[dict, List[dict]]

Get the status of a specified device or of all the configured devices (default).

Parameters:

device – Filter by device name or ID.

Returns:

[
  {
    "id": "Device unique ID",
    "name": "Device name",
    "on": "True if the device is on, False otherwise"
  }
]

property switches: List[dict]
Returns:

[
  {
    "id": "Device unique ID",
    "name": "Device name",
    "on": "True if the device is on, False otherwise"
  }
]

toggle(device, **kwargs)[source]

Toggle the device status (on/off)

write(device: str, data, handle: int = None, interface: str = None, binary: bool = False, disconnect_on_recv: bool = True, **kwargs) str

Writes data to a device

Parameters:
  • device – Device address to connect to

  • data – Data to be written (str or bytes)

  • interface – Bluetooth adapter name to use (default configured if None)

  • handle – Device handle. Either the UUID or the device handle must be specified

  • binary – Set to true if data is a base64-encoded binary string

  • disconnect_on_recv – If True (default) disconnect when the response is received

  • kwargs – Extra arguments to be passed to connect()