From b4c490b40976d126c7617421a00ece7efbade558 Mon Sep 17 00:00:00 2001 From: Matthieu Houdebine Date: Sat, 17 Sep 2022 01:08:22 +0200 Subject: [PATCH] Remove PyBluez dependency, use Python socket instead --- README.md | 7 ++----- custom_components/timebox_mini/__init__.py | 5 ++--- custom_components/timebox_mini/manifest.json | 5 ++--- custom_components/timebox_mini/timebox.py | 8 +++++--- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 1be5c2c..b48170b 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,9 @@ If you try to control another Divoom device, the connection to the first device ## Requirements ### Bluetooth Hardware -This component uses PyBluez library for Bluetooth communication. PyBluez is based on BlueZ official Linux Bluetooth stack. +This component uses Python sockets for Bluetooth communication. -Any Bluetooth hardware supported by your operating system should work. The Bluetooth interface built in to the Raspberry Pi 3 probably works, but hasn't yet been tested. +Any Bluetooth hardware supported by your operating system should work. The Bluetooth interface built in to the Raspberry Pi 3 probably works, but hasn't been tested yet. To check if your Bluetooth hardware is supported, run the following command on your system: ```bash @@ -57,9 +57,6 @@ Your Bluetooth interface should be listed as "hciX". If you have more than one, ### Home Assistant installation Any Home assistant installation should be supported: OS, Container, Core, Supervised. -**Notes for Home Assistant Core Installations**: This platform requires pybluez to be installed. On Debian based installs, run -`sudo apt install bluetooth libbluetooth-dev python3-bluez` - If you run Home Assistant in a virtual machine, you have to connect your computer Bluetooth hardware to the VM: - On VMWare: VM > Removable devices > (Your Bluetooth device i.e. Intel Wireless Bluetooth) > Connect - On VirtualBox: Devices > USB devices > (Your Bluetooth device i.e. Intel Wireless Bluetooth) diff --git a/custom_components/timebox_mini/__init__.py b/custom_components/timebox_mini/__init__.py index e0181cc..5ea68a4 100755 --- a/custom_components/timebox_mini/__init__.py +++ b/custom_components/timebox_mini/__init__.py @@ -3,7 +3,6 @@ from homeassistant.util import slugify from itertools import product from .timebox import Timebox -import bluetooth import datetime import logging import math @@ -250,8 +249,8 @@ def handle_action(call): dev = Timebox(mac) dev.connect() _LOGGER.debug('Connected to %s' % mac) - except bluetooth.BluetoothError as be: - _LOGGER.error('Error connecting to %s : %s' % (mac, be)) + except Exception as e: + _LOGGER.error('Error connecting to %s : %s' % (mac, e)) return c = color_convert(Color("white").get_rgb()) diff --git a/custom_components/timebox_mini/manifest.json b/custom_components/timebox_mini/manifest.json index 280e606..b2ec11d 100755 --- a/custom_components/timebox_mini/manifest.json +++ b/custom_components/timebox_mini/manifest.json @@ -1,14 +1,13 @@ { "domain": "timebox_mini", "name": "Timebox Mini", - "version": "1.0.1", + "version": "1.0.2", "documentation": "https://github.com/mathoudebine/homeassistant-timebox-mini", "issue_tracker": "https://github.com/mathoudebine/homeassistant-timebox-mini/issues", "requirements": [ "colour", "pillow", - "python-dateutil", - "pybluez" + "python-dateutil" ], "dependencies": [], "codeowners": [ diff --git a/custom_components/timebox_mini/timebox.py b/custom_components/timebox_mini/timebox.py index 9873aba..72afd70 100644 --- a/custom_components/timebox_mini/timebox.py +++ b/custom_components/timebox_mini/timebox.py @@ -1,17 +1,19 @@ -import bluetooth import logging +import socket _LOGGER = logging.getLogger(__name__) +BTPROTO_RFCOMM = 3 + class Timebox: debug = False def __init__(self, target): - if (isinstance(target, bluetooth.BluetoothSocket)): + if isinstance(target, socket.socket): self.sock = target self.addr, _ = self.sock.getpeername() else: - self.sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) + self.sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, BTPROTO_RFCOMM) self.addr = target self.sock.connect((self.addr, 4))