Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Dec 31, 2024
1 parent 51a7cbd commit 98f7921
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 85 deletions.
38 changes: 11 additions & 27 deletions pylamarzocco/clients/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
BleakScanner,
BLEDevice,
)
from bleak.backends.characteristic import BleakGATTCharacteristic

from pylamarzocco.const import (
AUTH_CHARACTERISTIC,
Expand Down Expand Up @@ -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."""

Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
114 changes: 57 additions & 57 deletions tests/test_bluetooth.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 98f7921

Please sign in to comment.