Skip to content

Commit

Permalink
Merge pull request #77 from zweckj/fix/more-bt
Browse files Browse the repository at this point in the history
pass BT characteristic instead of UUID
  • Loading branch information
zweckj authored Dec 31, 2024
2 parents ad6a4fc + 98f7921 commit 08deb8e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
24 changes: 20 additions & 4 deletions pylamarzocco/clients/bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,19 @@ async def authenticate() -> None:
base64.b64encode(user_bytes) + b"@" + base64.b64encode(token)
)

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_CHARACTERISTIC,
char_specifier=auth_characteristic,
data=auth_string,
response=False,
response=True,
)
except (BleakError, TimeoutError) as e:
raise BluetoothConnectionFailed(
Expand All @@ -148,10 +156,18 @@ async def authenticate() -> None:
"Sending bluetooth message: %s to %s", message, characteristic
)

settings_characteristic = client.services.get_characteristic(
SETTINGS_CHARACTERISTIC
)
if settings_characteristic is None:
raise BluetoothConnectionFailed(
f"Could not find settings characteristic {SETTINGS_CHARACTERISTIC} on machine."
)

await client.write_gatt_char(
char_specifier=characteristic,
char_specifier=settings_characteristic,
data=message,
response=False,
response=True,
)

async def _write_bluetooth_json_message(
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.5"
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
12 changes: 6 additions & 6 deletions tests/test_bluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ async def test_bluetooth_set_power(
mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined]
char_specifier=AUTH_CHARACTERISTIC,
data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=",
response=False,
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=False,
response=True,
)


Expand All @@ -41,13 +41,13 @@ async def test_bluetooth_set_steam(
mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined]
char_specifier=AUTH_CHARACTERISTIC,
data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=",
response=False,
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=False,
response=True,
)


Expand All @@ -61,11 +61,11 @@ async def test_bluetooth_set_temperature(
mock_bleak.write_gatt_char.assert_any_call( # type: ignore[attr-defined]
char_specifier=AUTH_CHARACTERISTIC,
data=b"dXNlcm5hbWU6c2VyaWFs@dG9rZW4=",
response=False,
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=False,
response=True,
)

0 comments on commit 08deb8e

Please sign in to comment.