bluetooth#

class platypush.plugins.bluetooth.BluetoothPlugin(*_, **__)[source]#

Bases: RunnablePlugin, EnumSwitchEntityManager

Plugin to interact with Bluetooth devices.

This plugin uses _Bleak_ to interact with the Bluetooth stack and _Theengs_ to map the services exposed by the devices into native entities.

The full list of devices natively supported can be found here.

It also supports legacy Bluetooth services, as well as the transfer of files.

Note that the support for Bluetooth low-energy devices requires a Bluetooth adapter compatible with the Bluetooth 5.0 specification or higher.

Requires:

  • bleak (pip install bleak)

  • bluetooth-numbers (pip install bluetooth-numbers)

  • TheengsDecoder (pip install TheengsDecoder)

  • pydbus (pip install pydbus)

  • pybluez (pip install git+https://github.com/pybluez/pybluez)

Triggers:

__init__(interface: str | None = None, connect_timeout: float = 20, service_uuids: Collection[UUID | int] | None = None, scan_paused_on_start: bool = False, poll_interval: float = 10.0, exclude_known_noisy_beacons: bool = True, ignored_device_addresses: Collection[str] | None = None, ignored_device_names: Collection[str] | None = None, ignored_device_manufacturers: Collection[str] | None = None, **kwargs)[source]#
Parameters:
  • interface – Name of the Bluetooth interface to use (e.g. hci0 on Linux). Default: first available interface.

  • connect_timeout – Timeout in seconds for the connection to a Bluetooth device. Default: 20 seconds.

  • service_uuids – List of service UUIDs to discover. Default: all.

  • scan_paused_on_start – If True, the plugin will not the scanning thread until scan_resume() is called (default: False).

  • exclude_known_noisy_beacons – Exclude BLE beacons from devices known for being very noisy. It mainly includes tracking services on Google, Apple, Microsoft and Samsung devices. These devices are also known for refreshing their MAC address very frequently, which may result in a large (and constantly increasing) list of devices. Disable this flag if you need to track BLE beacons from these devices, but beware that you may need periodically clean up your list of scanned devices.

  • ignored_device_addresses – List of device addresses to ignore.

  • ignored_device_names – List of device names to ignore.

  • ignored_device_manufacturers – List of device manufacturers to ignore.

connect(device: str, port: int | None = None, service_uuid: str | UUID | int | None = None, interface: str | None = None, timeout: float | None = None)[source]#

Pair and connect to a device by address or name.

Parameters:
  • device – The device address or name.

  • port – The port to connect to. Either port or service_uuid is required for non-BLE devices.

  • service_uuid – The UUID of the service to connect to. Either port or service_uuid is required for non-BLE devices.

  • interface – The Bluetooth interface to use (it overrides the default interface).

  • timeout – The connection timeout in seconds (it overrides the default connect_timeout).

disconnect(device: str, port: int | None = None, service_uuid: UUID | int | None = None)[source]#

Close an active connection to a device.

Note that this method can only close connections that have been initiated by the application. It can’t close connections owned by other applications or agents.

Parameters:
  • device – The device address or name.

  • port – If connected to a non-BLE device, the optional port to disconnect.

  • service_uuid – The optional UUID of the service to disconnect from, for non-BLE devices.

main()[source]#

Implementation of the main loop of the plugin.

read(device: str, service_uuid: UUID | int, interface: str | None = None, connect_timeout: float | None = None) str[source]#

Read a message from a device.

Parameters:
  • device – Name or address of the device to read from.

  • service_uuid – Service UUID.

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

  • connect_timeout – Connection timeout in seconds (default: same as the configured connect_timeout).

Returns:

The base64-encoded response received from the device.

scan(duration: float | None = None, devices: Collection[str] | None = None, service_uuids: Collection[UUID | int] | None = None) List[BluetoothDevice][source]#

Scan for Bluetooth devices nearby and return the results as a list of entities.

Parameters:
  • duration – Scan duration in seconds (default: same as the plugin’s poll_interval configuration parameter)

  • devices – List of device addresses or names to scan for.

  • service_uuids – List of service UUIDs to discover. Default: all.

scan_pause(duration: float | None = None)[source]#

Pause the scanning thread.

Parameters:

duration – For how long the scanning thread should be paused (default: null = indefinitely).

scan_resume(duration: float | None = None)[source]#

Resume the scanning thread, if inactive.

Parameters:

duration – For how long the scanning thread should be running (default: null = indefinitely).

send_file(file: str, device: str, data: str | bytes | bytearray | None = None, binary: bool = False)[source]#

Send a file to a device that exposes an OBEX Object Push service.

Parameters:
  • file – Path of the file to be sent. If data is specified then file should include the proposed file on the receiving host.

  • data – Alternatively to a file on disk you can send raw (string or binary) content.

  • device – Device address or name.

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

set(entity: str, value: Any, **_)[source]#

Set the value of an entity.

This is currently only supported for Switchbot devices, where the value can be one among on, off and press.

Parameters:
  • entity – The entity to set the value for. It can be the full entity ID in the format <mac-address>::<service>, or just the MAC address if the plugin supports it.

  • value – The value to set the entity to.

status(*_, duration: float | None = None, devices: Collection[str] | None = None, service_uuids: Collection[UUID | int] | None = None, **__) List[BluetoothDevice][source]#

Retrieve the status of all the devices, or the matching devices/services.

If scanning is currently disabled, it will enable it and perform a scan.

The differences between this method and scan() are:

  1. status() will return the status of all the devices known to the application, while scan() will return the status only of the devices discovered in the provided time window.

  2. status() will not initiate a new scan if scanning is already enabled (it will only returned the status of the known devices), while scan() will initiate a new scan.

Parameters:
  • duration – Scan duration in seconds, if scanning is disabled (default: same as the plugin’s poll_interval configuration parameter)

  • devices – List of device addresses or names to filter for. Default: all.

  • service_uuids – List of service UUIDs to filter for. Default: all.

stop()[source]#

Upon stop request, it stops any pending scans and closes all active connections.

transform_entities(entities: Collection[BluetoothDevice]) Collection[BluetoothDevice][source]#

This method takes a list of entities in any (plugin-specific) format and converts them into a standardized collection of Entity objects. Since this method is called by publish_entities() before entity updates are published, you may usually want to extend it to pre-process the entities managed by your extension into the standard format before they are stored and published to all the consumers.

write(device: str, data: str, service_uuid: UUID | int, interface: str | None = None, connect_timeout: float | None = None)[source]#

Writes data to a device

Parameters:
  • device – Name or address of the device to read from.

  • data – Data to be written, as a base64-encoded string.

  • service_uuid – Service UUID.

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

  • connect_timeout – Connection timeout in seconds (default: same as the configured connect_timeout).