From 53f4e29b641f4da61607864a640da631fca66f69 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 28 Mar 2026 10:35:07 -0600 Subject: [PATCH] Updating controller hotspot and bluetooth toggle commands with real paths for RPi. --- app/src/hooks/customHooks.js | 2 +- controller/lib/admin.py | 20 +++--- controller/test/test_admin.py | 115 +++++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 13 deletions(-) diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index ace1aa5e..07cd1468 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -986,7 +986,7 @@ export const useHotspot = () => { } else if (hotspot_status === 'in_use') { setInUse(true); setOn(false); - } else if (hotspot_status === 'disconnected' || hotspot_status === 'unavailable') { + } else if (hotspot_status === 'disconnected' || hotspot_status === 'off') { setOn(false); setInUse(false); } else { diff --git a/controller/lib/admin.py b/controller/lib/admin.py index b9c09ae4..b04616a0 100644 --- a/controller/lib/admin.py +++ b/controller/lib/admin.py @@ -22,11 +22,11 @@ DEFAULT_CPU_FREQUENCY = get_config_value('throttle.default_governor', 'ondemand' class HotspotStatus(enum.Enum): - """Hotspot status enum matching wrolpi/admin.py""" + """Hotspot status enum.""" disconnected = enum.auto() # Radio is on, but Hotspot is not connected. - unavailable = enum.auto() # Radio is off. + off = enum.auto() # Radio is software-disabled. Can be turned on. connected = enum.auto() # Radio is on, Hotspot is on. - unknown = enum.auto() # Unknown status. Hotspot may not be supported. + unknown = enum.auto() # Unknown status. No nmcli, Docker, or no WiFi hardware. in_use = enum.auto() # Wi-Fi device is in use for a network connection. @@ -105,7 +105,7 @@ def get_hotspot_status() -> HotspotStatus: elif line.startswith(f'{device}: disconnected'): return HotspotStatus.disconnected elif line.startswith(f'{device}: unavailable'): - return HotspotStatus.unavailable + return HotspotStatus.off return HotspotStatus.unknown @@ -133,13 +133,13 @@ def get_hotspot_status_dict() -> dict: # Map enum status to enabled/available flags enabled = status == HotspotStatus.connected - available = status not in (HotspotStatus.unknown, HotspotStatus.unavailable) + # Hotspot is available even when radio is off because enable_hotspot() + # turns the radio on first. + available = status != HotspotStatus.unknown reason = None if status == HotspotStatus.unknown: reason = "Hotspot not supported or nmcli unavailable" - elif status == HotspotStatus.unavailable: - reason = "WiFi radio is off" elif status == HotspotStatus.in_use: reason = "WiFi device is connected to a network" @@ -259,7 +259,7 @@ async def get_bluetooth_status() -> BluetoothStatus: try: proc = await asyncio.create_subprocess_exec( - "rfkill", "-J", + "/usr/sbin/rfkill", "-J", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) @@ -331,7 +331,7 @@ async def enable_bluetooth() -> dict: logger.info("Enabling Bluetooth radio") try: proc = await asyncio.create_subprocess_exec( - "rfkill", "unblock", "bluetooth", + "/usr/sbin/rfkill", "unblock", "bluetooth", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) @@ -369,7 +369,7 @@ async def disable_bluetooth() -> dict: logger.info("Disabling Bluetooth radio") try: proc = await asyncio.create_subprocess_exec( - "rfkill", "block", "bluetooth", + "/usr/sbin/rfkill", "block", "bluetooth", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) diff --git a/controller/test/test_admin.py b/controller/test/test_admin.py index c2d72b22..f4b3c1d0 100644 --- a/controller/test/test_admin.py +++ b/controller/test/test_admin.py @@ -16,6 +16,7 @@ from controller.lib.admin import ( enable_throttle, get_bluetooth_status, get_hotspot_status, + get_hotspot_status_dict, get_throttle_status, get_timezone_status_dict, reboot_system, @@ -141,7 +142,7 @@ class TestEnableBluetooth: result = await enable_bluetooth() assert result["success"] is True mock_exec.assert_called_once_with( - "rfkill", "unblock", "bluetooth", + "/usr/sbin/rfkill", "unblock", "bluetooth", stdout=mock.ANY, stderr=mock.ANY, ) @@ -179,7 +180,7 @@ class TestDisableBluetooth: result = await disable_bluetooth() assert result["success"] is True mock_exec.assert_called_once_with( - "rfkill", "block", "bluetooth", + "/usr/sbin/rfkill", "block", "bluetooth", stdout=mock.ANY, stderr=mock.ANY, ) @@ -225,6 +226,116 @@ class TestGetHotspotStatus: assert result == HotspotStatus.unknown +class TestGetHotspotStatusWithRealOutput: + """Tests for get_hotspot_status using real nmcli output from RPi (10.0.0.9).""" + + # Real nmcli output when WiFi radio is off (wlan0 software-disabled). + NMCLI_RADIO_OFF = ( + "eth0: connected to Wired connection 1\n" + "\t\"eth0\"\n" + "\tethernet (macb), 2C:CF:67:08:87:88, hw, mtu 1500\n" + "\n" + "lo: connected (externally) to lo\n" + "\t\"lo\"\n" + "\tloopback (unknown), 00:00:00:00:00:00, sw, mtu 65536\n" + "\n" + "wlan0: unavailable\n" + "\t\"Broadcom Wi-Fi\"\n" + "\twifi (brcmfmac), 2C:CF:67:08:87:89, sw disabled, hw, mtu 1500\n" + ) + + # Real nmcli output when WiFi radio is on but no hotspot (wlan0 disconnected). + NMCLI_RADIO_ON_NO_HOTSPOT = ( + "eth0: connected to Wired connection 1\n" + "\t\"eth0\"\n" + "\tethernet (macb), 2C:CF:67:08:87:88, hw, mtu 1500\n" + "\n" + "lo: connected (externally) to lo\n" + "\t\"lo\"\n" + "\tloopback (unknown), 00:00:00:00:00:00, sw, mtu 65536\n" + "\n" + "wlan0: disconnected\n" + "\t\"Broadcom Wi-Fi\"\n" + "\twifi (brcmfmac), 2C:CF:67:08:87:89, hw, mtu 1500\n" + ) + + # Real nmcli output when hotspot is active (wlan0 connected to Hotspot). + NMCLI_HOTSPOT_ACTIVE = ( + "wlan0: connected to Hotspot\n" + "\t\"Broadcom Wi-Fi\"\n" + "\twifi (brcmfmac), 2C:CF:67:08:87:89, hw, mtu 1500\n" + "\n" + "eth0: connected to Wired connection 1\n" + "\t\"eth0\"\n" + "\tethernet (macb), 2C:CF:67:08:87:88, hw, mtu 1500\n" + "\n" + "lo: connected (externally) to lo\n" + "\t\"lo\"\n" + "\tloopback (unknown), 00:00:00:00:00:00, sw, mtu 65536\n" + ) + + def test_radio_off_returns_off(self): + """nmcli 'wlan0: unavailable' (radio sw disabled) should return HotspotStatus.off.""" + mock_result = mock.Mock() + mock_result.returncode = 0 + mock_result.stdout = self.NMCLI_RADIO_OFF + + with mock.patch("controller.lib.admin.is_docker_mode", return_value=False): + with mock.patch("controller.lib.admin.get_current_ssid", return_value=None): + with mock.patch("subprocess.run", return_value=mock_result): + result = get_hotspot_status() + assert result == HotspotStatus.off + + def test_radio_on_no_hotspot_returns_disconnected(self): + """nmcli 'wlan0: disconnected' (radio on, no hotspot) should return disconnected.""" + mock_result = mock.Mock() + mock_result.returncode = 0 + mock_result.stdout = self.NMCLI_RADIO_ON_NO_HOTSPOT + + with mock.patch("controller.lib.admin.is_docker_mode", return_value=False): + with mock.patch("controller.lib.admin.get_current_ssid", return_value=None): + with mock.patch("subprocess.run", return_value=mock_result): + result = get_hotspot_status() + assert result == HotspotStatus.disconnected + + def test_hotspot_active_returns_connected(self): + """nmcli 'wlan0: connected to Hotspot' should return HotspotStatus.connected.""" + mock_result = mock.Mock() + mock_result.returncode = 0 + mock_result.stdout = self.NMCLI_HOTSPOT_ACTIVE + + with mock.patch("controller.lib.admin.is_docker_mode", return_value=False): + with mock.patch("controller.lib.admin.get_current_ssid", return_value=None): + with mock.patch("subprocess.run", return_value=mock_result): + result = get_hotspot_status() + assert result == HotspotStatus.connected + + +class TestGetHotspotStatusDict: + """Tests for get_hotspot_status_dict function.""" + + def test_available_when_radio_off(self): + """Hotspot should be available when WiFi radio is off, + because enable_hotspot() turns the radio on first.""" + with mock.patch("controller.lib.admin.get_hotspot_status", return_value=HotspotStatus.off): + result = get_hotspot_status_dict() + assert result["available"] is True + assert result["enabled"] is False + + def test_unavailable_when_unknown(self): + """Hotspot should be unavailable when status is unknown (Docker, no nmcli).""" + with mock.patch("controller.lib.admin.get_hotspot_status", return_value=HotspotStatus.unknown): + result = get_hotspot_status_dict() + assert result["available"] is False + + def test_enabled_when_connected(self): + """Hotspot should be enabled and available when connected.""" + with mock.patch("controller.lib.admin.get_hotspot_status", return_value=HotspotStatus.connected): + result = get_hotspot_status_dict() + assert result["available"] is True + assert result["enabled"] is True + + class TestEnableHotspot: """Tests for enable_hotspot function."""