From 98f7921733b48c6ba9dcfd48d0335d0e6e7420ef Mon Sep 17 00:00:00 2001 From: Josef Zweck <24647999+zweckj@users.noreply.github.com> Date: Tue, 31 Dec 2024 21:50:49 +0100 Subject: [PATCH] more fixes --- pylamarzocco/clients/bluetooth.py | 38 +++------- pyproject.toml | 2 +- tests/conftest.py | 2 + tests/test_bluetooth.py | 114 +++++++++++++++--------------- 4 files changed, 71 insertions(+), 85 deletions(-) diff --git a/pylamarzocco/clients/bluetooth.py b/pylamarzocco/clients/bluetooth.py index 0065679..40557c5 100644 --- a/pylamarzocco/clients/bluetooth.py +++ b/pylamarzocco/clients/bluetooth.py @@ -15,7 +15,6 @@ BleakScanner, BLEDevice, ) -from bleak.backends.characteristic import BleakGATTCharacteristic from pylamarzocco.const import ( AUTH_CHARACTERISTIC, @@ -122,13 +121,6 @@ async def _write_bluetooth_message( async with BleakClient(self._address_or_ble_device) as client: - auth_c = client.services.get_characteristic(AUTH_CHARACTERISTIC) - if auth_c is not None: - _logger.warning("Found auth characteristic: %s", auth_c.handle) - settings_c = client.services.get_characteristic(SETTINGS_CHARACTERISTIC) - if settings_c is not None: - _logger.warning("Found settings characteristic: %s", settings_c.handle) - async def authenticate() -> None: """Build authentication string and send it to the machine.""" @@ -139,21 +131,17 @@ async def authenticate() -> None: base64.b64encode(user_bytes) + b"@" + base64.b64encode(token) ) - auth_char: BleakGATTCharacteristic | None = None - for service in client.services: - for char in service.characteristics: - if char.uuid == AUTH_CHARACTERISTIC: - auth_char = char - _logger.debug("Found auth characteristic: %s", char.handle) - break - if auth_char is None: + auth_characteristic = client.services.get_characteristic( + AUTH_CHARACTERISTIC + ) + if auth_characteristic is None: raise BluetoothConnectionFailed( f"Could not find auth characteristic {AUTH_CHARACTERISTIC} on machine." ) try: await client.write_gatt_char( - char_specifier=auth_char, + char_specifier=auth_characteristic, data=auth_string, response=True, ) @@ -168,20 +156,16 @@ async def authenticate() -> None: "Sending bluetooth message: %s to %s", message, characteristic ) - settings_char: BleakGATTCharacteristic | None = None - for service in client.services: - for char in service.characteristics: - if char.uuid == characteristic: - settings_char = char - _logger.debug("Found settings characteristic: %s", char.handle) - break - if settings_char is None: + settings_characteristic = client.services.get_characteristic( + SETTINGS_CHARACTERISTIC + ) + if settings_characteristic is None: raise BluetoothConnectionFailed( - f"Could not find characteristic {characteristic} on machine." + f"Could not find settings characteristic {SETTINGS_CHARACTERISTIC} on machine." ) await client.write_gatt_char( - char_specifier=settings_char, + char_specifier=settings_characteristic, data=message, response=True, ) diff --git a/pyproject.toml b/pyproject.toml index 3460d6b..5a17142 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pylamarzocco" -version = "1.4.6a6" +version = "1.4.6" license = { text = "MIT" } description = "A Python implementation of the new La Marzocco API" readme = "README.md" diff --git a/tests/conftest.py b/tests/conftest.py index 03ec488..1cd9afa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -161,4 +161,6 @@ def mock_bleak() -> Generator[AsyncMock, None, None]: """Mock BleakClient.""" with patch("pylamarzocco.clients.bluetooth.BleakClient") as bleak_client: aenter = bleak_client.return_value.__aenter__.return_value + aenter.services.get_characteristic = lambda uuid: uuid + yield aenter diff --git a/tests/test_bluetooth.py b/tests/test_bluetooth.py index cb29ba3..605d0f1 100644 --- a/tests/test_bluetooth.py +++ b/tests/test_bluetooth.py @@ -1,71 +1,71 @@ -# """"Test the Bluetooth client.""" +""""Test the Bluetooth client.""" -# from collections.abc import Generator -# from unittest.mock import AsyncMock +from collections.abc import Generator +from unittest.mock import AsyncMock -# from pylamarzocco.clients.bluetooth import LaMarzoccoBluetoothClient -# from pylamarzocco.const import ( -# AUTH_CHARACTERISTIC, -# SETTINGS_CHARACTERISTIC, -# BoilerType, -# ) +from pylamarzocco.clients.bluetooth import LaMarzoccoBluetoothClient +from pylamarzocco.const import ( + AUTH_CHARACTERISTIC, + SETTINGS_CHARACTERISTIC, + BoilerType, +) -# async def test_bluetooth_set_power( -# bluetooth_client: LaMarzoccoBluetoothClient, -# mock_bleak: Generator[AsyncMock, None, None], -# ): -# """Test setting the power.""" -# await bluetooth_client.set_power(True) +async def test_bluetooth_set_power( + bluetooth_client: LaMarzoccoBluetoothClient, + mock_bleak: Generator[AsyncMock, None, None], +): + """Test setting the power.""" + await bluetooth_client.set_power(True) -# mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] -# char_specifier=AUTH_CHARACTERISTIC, -# data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", -# response=True, -# ) + mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] + char_specifier=AUTH_CHARACTERISTIC, + data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", + response=True, + ) -# mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] -# char_specifier=SETTINGS_CHARACTERISTIC, -# data=b'{"name":"MachineChangeMode","parameter":{"mode":"BrewingMode"}}\x00', -# response=True, -# ) + mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] + char_specifier=SETTINGS_CHARACTERISTIC, + data=b'{"name":"MachineChangeMode","parameter":{"mode":"BrewingMode"}}\x00', + response=True, + ) -# async def test_bluetooth_set_steam( -# bluetooth_client: LaMarzoccoBluetoothClient, -# mock_bleak: Generator[AsyncMock, None, None], -# ): -# """Test setting the steam.""" -# await bluetooth_client.set_steam(True) +async def test_bluetooth_set_steam( + bluetooth_client: LaMarzoccoBluetoothClient, + mock_bleak: Generator[AsyncMock, None, None], +): + """Test setting the steam.""" + await bluetooth_client.set_steam(True) -# mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] -# char_specifier=AUTH_CHARACTERISTIC, -# data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", -# response=True, -# ) + mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] + char_specifier=AUTH_CHARACTERISTIC, + data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", + response=True, + ) -# mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] -# char_specifier=SETTINGS_CHARACTERISTIC, -# data=b'{"name":"SettingBoilerEnable","parameter":{"identifier":"SteamBoiler","state":true}}\x00', -# response=True, -# ) + mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] + char_specifier=SETTINGS_CHARACTERISTIC, + data=b'{"name":"SettingBoilerEnable","parameter":{"identifier":"SteamBoiler","state":true}}\x00', + response=True, + ) -# async def test_bluetooth_set_temperature( -# bluetooth_client: LaMarzoccoBluetoothClient, -# mock_bleak: Generator[AsyncMock, None, None], -# ): -# """Test setting the temp.""" -# await bluetooth_client.set_temp(BoilerType.STEAM, 131) +async def test_bluetooth_set_temperature( + bluetooth_client: LaMarzoccoBluetoothClient, + mock_bleak: Generator[AsyncMock, None, None], +): + """Test setting the temp.""" + await bluetooth_client.set_temp(BoilerType.STEAM, 131) -# mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] -# char_specifier=AUTH_CHARACTERISTIC, -# data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", -# response=True, -# ) + mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined] + char_specifier=AUTH_CHARACTERISTIC, + data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=", + response=True, + ) -# mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] -# char_specifier=SETTINGS_CHARACTERISTIC, -# data=b'{"name":"SettingBoilerTarget","parameter":{"identifier":"SteamBoiler","value":131}}\x00', -# response=True, -# ) + mock_bleak.write_gatt_char.assert_called_with( # type: ignore[attr-defined] + char_specifier=SETTINGS_CHARACTERISTIC, + data=b'{"name":"SettingBoilerTarget","parameter":{"identifier":"SteamBoiler","value":131}}\x00', + response=True, + )