Skip to content

Commit cd38486

Browse files
committed
feat: Update cli to use new command interface
1 parent a88e961 commit cd38486

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

roborock/cli.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,14 @@ async def home(ctx, device_id: str, refresh: bool):
596596
@async_command
597597
async def command(ctx, cmd, device_id, params):
598598
context: RoborockContext = ctx.obj
599-
cache_data = await context.get_devices()
600-
601-
home_data = cache_data.home_data
602-
devices = home_data.get_all_devices()
603-
device = next(device for device in devices if device.duid == device_id)
604-
model = next(
605-
(product.model for product in home_data.products if device is not None and product.id == device.product_id),
606-
None,
607-
)
608-
if model is None:
609-
raise RoborockException(f"Could not find model for device {device.name}")
610-
device_info = DeviceData(device=device, model=model)
611-
mqtt_client = RoborockMqttClientV1(cache_data.user_data, device_info)
612-
await mqtt_client.send_command(cmd, json.loads(params) if params is not None else None)
613-
await mqtt_client.async_release()
599+
device_manager = await context.get_device_manager()
600+
device = await device_manager.get_device(device_id)
601+
if device.v1_properties is None:
602+
raise RoborockException(f"Device {device.name} does not support V1 protocol")
603+
command_trait: Trait = device.v1_properties.command
604+
result = await command_trait.send(cmd, json.loads(params) if params is not None else None)
605+
if result:
606+
click.echo(dump_json(result))
614607

615608

616609
@click.command()

roborock/devices/traits/v1/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .clean_summary import CleanSummaryTrait
1313
from .common import V1TraitMixin
14+
from .command import CommandTrait
1415
from .consumeable import ConsumableTrait
1516
from .device_features import DeviceFeaturesTrait
1617
from .do_not_disturb import DoNotDisturbTrait
@@ -35,6 +36,7 @@
3536
"ConsumableTrait",
3637
"HomeTrait",
3738
"DeviceFeaturesTrait",
39+
"CommandTrait",
3840
]
3941

4042

@@ -47,6 +49,7 @@ class PropertiesApi(Trait):
4749

4850
# All v1 devices have these traits
4951
status: StatusTrait
52+
command: CommandTrait
5053
dnd: DoNotDisturbTrait
5154
clean_summary: CleanSummaryTrait
5255
sound_volume: SoundVolumeTrait

roborock/devices/traits/v1/command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def __post_init__(self) -> None:
1414
"""
1515
self._rpc_channel = None
1616

17-
async def send(self, command: RoborockCommand, params: dict[str, Any] | None = None) -> None:
17+
async def send(self, command: RoborockCommand, params: dict[str, Any] | None = None) -> Any:
1818
"""Send a command to the device."""
1919
if not self._rpc_channel:
2020
raise ValueError("Device trait in invalid state")
21-
await self._rpc_channel.send_command(command, params=params)
21+
return await self._rpc_channel.send_command(command, params=params)

0 commit comments

Comments
 (0)