From aab4749b1d89780f9996fae9b7cedd6fd4fcffc4 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Mon, 1 Jan 2024 17:01:18 +0100 Subject: [PATCH] add bluetooth devices detection --- .../easy_computer_manager/switch.py | 6 +-- .../easy_computer_manager/utils.py | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/custom_components/easy_computer_manager/switch.py b/custom_components/easy_computer_manager/switch.py index ddaeb11..f417d06 100644 --- a/custom_components/easy_computer_manager/switch.py +++ b/custom_components/easy_computer_manager/switch.py @@ -12,8 +12,6 @@ import wakeonlan from homeassistant.components.switch import (SwitchEntity) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( - CONF_BROADCAST_ADDRESS, - CONF_BROADCAST_PORT, CONF_HOST, CONF_MAC, CONF_NAME, @@ -175,8 +173,6 @@ class ComputerSwitch(SwitchEntity): _LOGGER.debug( "Send magic packet to mac %s (broadcast: %s, port: %s)", self._mac_address, - self._broadcast_address, - self._broadcast_port, # self._broadcast_address, # self._broadcast_port, ) @@ -295,6 +291,8 @@ class ComputerSwitch(SwitchEntity): "operating_system_version": utils.get_operating_system_version(self._connection), "mac_address": self._mac_address, "ip_address": self._host, + "connected_devices": utils.get_bluetooth_devices(self._connection, only_connected=True, + format_for_hass=True), } def renew_ssh_connection(self) -> None: diff --git a/custom_components/easy_computer_manager/utils.py b/custom_components/easy_computer_manager/utils.py index c9a0161..a0ca5a7 100644 --- a/custom_components/easy_computer_manager/utils.py +++ b/custom_components/easy_computer_manager/utils.py @@ -46,6 +46,7 @@ def is_unix_system(connection: Connection): def get_operating_system_version(connection: Connection, is_unix=None): """Return the running operating system name and version.""" + if is_unix is None: is_unix = is_unix_system(connection) @@ -485,3 +486,48 @@ def get_debug_info(connection: Connection): data['monitors'] = get_monitors_config(connection) return data + + +def get_bluetooth_devices(connection: Connection, only_connected: bool = False, format_for_hass: bool = False): + """Return a list of Bluetooth devices connected to the host system.""" + + commands = { + "unix": "bash -c \'bluetoothctl devices | cut -f2 -d\\' \\' | while read uuid; do bluetoothctl info $uuid; done|grep -e \"Device\\|Connected\\|Name\"\'", + "windows": "", + } + + if is_unix_system(connection): + result = connection.run(commands["unix"]) + if result.return_code != 0: + raise HomeAssistantError(f"Could not get Bluetooth devices on system running at {connection.host}.") + + devices = [] + current_device = {} + + for line in result.stdout.split('\n'): + line = line.strip() + + if not line: + continue + + if line.startswith("Device"): + if current_device: + devices.append(current_device) + current_device = {"address": line.split()[1]} + elif line.startswith("Name:"): + current_device["name"] = line.split(":")[1].strip() + elif line.startswith("Connected:"): + current_device["connected"] = line.split(":")[1].strip() + + if current_device: + devices.append(current_device) + + if only_connected: + devices = [device for device in devices if device["connected"] == "yes"] + + if format_for_hass: + devices = [{"name": device["name"], "mac": device["address"]} for device in devices] + + return devices + else: + raise HomeAssistantError("Not implemented yet for Windows OS.")