Skip to content

Commit 822bec9

Browse files
authored
Merge pull request #52 from zweckj/vnext
fix websocket parsing
2 parents 6e988f3 + 651d28e commit 822bec9

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

lmcloud/lm_grinder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .const import GrinderModel, PhysicalKey
88
from .client_cloud import LaMarzoccoCloudClient
99
from .client_local import LaMarzoccoLocalClient
10-
from .lm_device import LaMarzoccoDevice, LaMarzoccoStatistics
10+
from .lm_device import LaMarzoccoDevice
1111
from .models import LaMarzoccoGrinderConfig
1212

1313

lmcloud/lm_machine.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def on_websocket_message_received(self, message: str | bytes) -> None:
367367
message = str(message)
368368

369369
_LOGGER.debug("Received message from websocket, message %s", message)
370+
notify = False
370371
try:
371372
notify = self._parse_websocket_message(message)
372373
except UnknownWebSocketMessage as exc:
@@ -410,51 +411,63 @@ def _parse_dict_message(self, message: Any) -> bool:
410411
def _parse_list_message(self, message: list[dict[str, Any]]) -> bool:
411412
"""Parse websocket message that is a list."""
412413

414+
property_updated = False
413415
for msg in message:
414416

415417
if "SteamBoilerUpdateTemperature" in msg:
416418
self.config.boilers[BoilerType.STEAM].current_temperature = msg[
417419
"SteamBoilerUpdateTemperature"
418420
]
421+
property_updated = True
419422

420423
elif "CoffeeBoiler1UpdateTemperature" in msg:
421424
self.config.boilers[BoilerType.COFFEE].current_temperature = msg[
422425
"CoffeeBoiler1UpdateTemperature"
423426
]
427+
property_updated = True
424428

425429
elif "Sleep" in msg:
426430
self.config.turned_on = False
431+
property_updated = True
427432

428433
elif "SteamBoilerEnabled" in msg:
429434
value = msg["SteamBoilerEnabled"]
430435
self.config.boilers[BoilerType.STEAM].enabled = value
436+
property_updated = True
431437

432438
elif "WakeUp" in msg:
433439
self.config.turned_on = True
440+
property_updated = True
434441

435442
elif "MachineStatistics" in msg:
436443
self.parse_statistics(json.loads(msg["MachineStatistics"]))
444+
property_updated = True
437445

438446
elif "BrewingUpdateGroup1Time" in msg:
439447
self.config.brew_active_duration = msg["BrewingUpdateGroup1Time"]
448+
property_updated = True
440449

441450
elif "BrewingStartedGroup1StopType" in msg:
442451
self.config.brew_active = True
452+
property_updated = True
443453

444454
elif (
445455
"BrewingStoppedGroup1StopType" in msg or "BrewingSnapshotGroup1" in msg
446456
):
447457
self.config.brew_active = False
458+
property_updated = True
448459

449460
elif "SteamBoilerUpdateSetPoint" in msg:
450461
self.config.boilers[BoilerType.STEAM].target_temperature = msg[
451462
"SteamBoilerUpdateSetPoint"
452463
]
464+
property_updated = True
453465

454466
elif "CoffeeBoiler1UpdateSetPoint" in msg:
455467
self.config.boilers[BoilerType.COFFEE].target_temperature = msg[
456468
"CoffeeBoiler1UpdateSetPoint"
457469
]
470+
property_updated = True
458471

459472
elif "BoilersTargetTemperature" in msg:
460473
boilers = json.loads(msg["BoilersTargetTemperature"])
@@ -463,10 +476,12 @@ def _parse_list_message(self, message: list[dict[str, Any]]) -> bool:
463476
self.config.boilers[BoilerType(boiler["id"])].target_temperature = (
464477
value
465478
)
479+
property_updated = True
466480

467481
elif "Boilers" in msg:
468482
boilers = json.loads(msg["Boilers"])
469483
self.config.boilers = parse_boilers(boilers)
484+
property_updated = True
470485

471486
elif "PreinfusionSettings" in msg:
472487
settings: dict[str, Any] = {}
@@ -475,5 +490,8 @@ def _parse_list_message(self, message: list[dict[str, Any]]) -> bool:
475490
self.config.prebrew_mode, self.config.prebrew_configuration = (
476491
parse_preinfusion_settings(settings)
477492
)
493+
property_updated = True
478494

479-
raise UnknownWebSocketMessage(f"Unknown websocket message: {message}")
495+
if not property_updated:
496+
raise UnknownWebSocketMessage(f"Unknown websocket message: {message}")
497+
return True

run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def main():
2222
username=data["username"],
2323
password=data["password"],
2424
)
25-
fleet = await cloud_client.get_customer_fleet()
25+
# fleet = await cloud_client.get_customer_fleet()
2626

2727
# serial = list(fleet.keys())[0]
2828

@@ -52,8 +52,8 @@ async def main():
5252
model=MachineModel(data["model"]),
5353
serial_number=data["serial"],
5454
name=data["serial"],
55-
# cloud_client=cloud_client,
56-
local_client=local_client,
55+
cloud_client=cloud_client,
56+
# local_client=local_client,
5757
)
5858

5959
await machine.websocket_connect()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="lmcloud",
10-
version="1.0.0b5",
10+
version="1.0.0b6",
1111
description="A Python implementation of the new La Marzocco API",
1212
long_description=readme,
1313
long_description_content_type="text/markdown",

tests/__snapshots__/test_machine.ambr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
websocket_connected=False,
2121
)
2222
# ---
23+
# name: test_websocket_message
24+
"LaMarzoccoMachineConfig(turned_on=True, doses={<PhysicalKey.A: 1>: 135, <PhysicalKey.B: 2>: 97, <PhysicalKey.C: 3>: 108, <PhysicalKey.D: 4>: 121}, boilers={<BoilerType.STEAM: 'SteamBoiler'>: LaMarzoccoBoiler(enabled=True, current_temperature=113, target_temperature=131), <BoilerType.COFFEE: 'CoffeeBoiler1'>: LaMarzoccoBoiler(enabled=True, current_temperature=81, target_temperature=94)}, prebrew_mode=<PrebrewMode.PREINFUSION: 'TypeB'>, plumbed_in=True, prebrew_configuration={<PhysicalKey.A: 1>: LaMarzoccoPrebrewConfiguration(on_time=0.5, off_time=1), <PhysicalKey.B: 2>: LaMarzoccoPrebrewConfiguration(on_time=0.5, off_time=1), <PhysicalKey.C: 3>: LaMarzoccoPrebrewConfiguration(on_time=3.299999952316284, off_time=3.299999952316284), <PhysicalKey.D: 4>: LaMarzoccoPrebrewConfiguration(on_time=2, off_time=2)}, dose_hot_water=8, water_contact=True, auto_on_off_schedule=LaMarzoccoSchedule(enabled=True, days={<WeekDay.MONDAY: 'monday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.TUESDAY: 'tuesday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.WEDNESDAY: 'wednesday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.THURSDAY: 'thursday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.FRIDAY: 'friday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.SATURDAY: 'saturday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0), <WeekDay.SUNDAY: 'sunday'>: LaMarzoccoScheduleDay(enabled=True, h_on=6, h_off=16, m_on=0, m_off=0)}), brew_active=False, brew_active_duration=0)"
25+
# ---

tests/test_machine.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from lmcloud.client_cloud import LaMarzoccoCloudClient
99
from lmcloud.client_local import LaMarzoccoLocalClient
10-
from lmcloud.const import BoilerType, MachineModel, WeekDay
10+
from lmcloud.const import BoilerType, MachineModel, PhysicalKey, WeekDay
1111
from lmcloud.lm_machine import LaMarzoccoMachine
1212

1313
from . import init_machine, MACHINE_SERIAL
@@ -71,12 +71,12 @@ async def test_set_prebrew_infusion(
7171
3.5,
7272
)
7373
assert result is True
74-
assert machine.config.prebrew_configuration[1].on_time == 1.0
75-
assert machine.config.prebrew_configuration[1].off_time == 3.5
74+
assert machine.config.prebrew_configuration[PhysicalKey.A].on_time == 1.0
75+
assert machine.config.prebrew_configuration[PhysicalKey.A].off_time == 3.5
7676

7777
result = await machine.set_preinfusion_time(4.5)
7878
assert result is True
79-
assert machine.config.prebrew_configuration[1].off_time == 4.5
79+
assert machine.config.prebrew_configuration[PhysicalKey.A].off_time == 4.5
8080

8181

8282
async def test_set_schedule(
@@ -95,3 +95,22 @@ async def test_set_schedule(
9595
m_off=0,
9696
)
9797
assert result is True
98+
99+
100+
async def test_websocket_message(
101+
cloud_client: LaMarzoccoCloudClient,
102+
local_machine_client: LaMarzoccoLocalClient,
103+
snapshot: SnapshotAssertion,
104+
):
105+
"""Test parsing of websocket messages."""
106+
machine = await LaMarzoccoMachine.create(
107+
model=MachineModel.GS3_AV,
108+
serial_number=MACHINE_SERIAL,
109+
name="MyMachine",
110+
cloud_client=cloud_client,
111+
local_client=local_machine_client,
112+
)
113+
114+
message = r'[{"Boilers":"[{\"id\":\"SteamBoiler\",\"isEnabled\":true,\"target\":131,\"current\":113},{\"id\":\"CoffeeBoiler1\",\"isEnabled\":true,\"target\":94,\"current\":81}]"}]'
115+
machine.on_websocket_message_received(message)
116+
assert str(machine.config) == snapshot

0 commit comments

Comments
 (0)