2727import json
2828import logging
2929import threading
30+ from collections .abc import Callable
3031from dataclasses import asdict , dataclass
3132from pathlib import Path
3233from typing import Any , cast
4344from roborock .devices .cache import Cache , CacheData
4445from roborock .devices .device import RoborockDevice
4546from roborock .devices .device_manager import DeviceManager , create_device_manager , create_home_data_api
47+ from roborock .devices .traits import Trait
4648from roborock .protocol import MessageParser
4749from roborock .version_1_apis .roborock_mqtt_client_v1 import RoborockMqttClientV1
4850from roborock .web_api import RoborockApiClient
@@ -377,23 +379,30 @@ async def execute_scene(ctx, scene_id):
377379 await client .execute_scene (cache_data .user_data , scene_id )
378380
379381
382+ async def _v1_trait (context : RoborockContext , device_id : str , display_func : Callable [[], Trait ]) -> Trait :
383+ device_manager = await context .get_device_manager ()
384+ device = await device_manager .get_device (device_id )
385+ if device .v1_properties is None :
386+ raise RoborockException (f"Device { device .name } does not support V1 protocol" )
387+
388+ trait = display_func (device .v1_properties )
389+ await trait .refresh ()
390+ return trait
391+
392+
393+ async def _display_v1_trait (context : RoborockContext , device_id : str , display_func : Callable [[], Trait ]) -> None :
394+ trait = await _v1_trait (context , device_id , display_func )
395+ click .echo (dump_json (trait .as_dict ()))
396+
397+
380398@session .command ()
381399@click .option ("--device_id" , required = True )
382400@click .pass_context
383401@async_command
384402async def status (ctx , device_id : str ):
385403 """Get device status."""
386404 context : RoborockContext = ctx .obj
387-
388- device_manager = await context .get_device_manager ()
389- device = await device_manager .get_device (device_id )
390-
391- if not (status_trait := device .traits .get ("status" )):
392- click .echo (f"Device { device .name } does not have a status trait" )
393- return
394-
395- status_result = await status_trait .get_status ()
396- click .echo (dump_json (status_result .as_dict ()))
405+ await _display_v1_trait (context , device_id , lambda v1 : v1 .status )
397406
398407
399408@session .command ()
@@ -403,15 +412,7 @@ async def status(ctx, device_id: str):
403412async def clean_summary (ctx , device_id : str ):
404413 """Get device clean summary."""
405414 context : RoborockContext = ctx .obj
406-
407- device_manager = await context .get_device_manager ()
408- device = await device_manager .get_device (device_id )
409- if not (clean_summary_trait := device .traits .get ("clean_summary" )):
410- click .echo (f"Device { device .name } does not have a clean summary trait" )
411- return
412-
413- clean_summary_result = await clean_summary_trait .get_clean_summary ()
414- click .echo (dump_json (clean_summary_result .as_dict ()))
415+ await _display_v1_trait (context , device_id , lambda v1 : v1 .clean_summary )
415416
416417
417418@session .command ()
@@ -421,17 +422,7 @@ async def clean_summary(ctx, device_id: str):
421422async def volume (ctx , device_id : str ):
422423 """Get device volume."""
423424 context : RoborockContext = ctx .obj
424-
425- device_manager = await context .get_device_manager ()
426- device = await device_manager .get_device (device_id )
427-
428- if not (volume_trait := device .traits .get ("sound_volume" )):
429- click .echo (f"Device { device .name } does not have a volume trait" )
430- return
431-
432- volume_result = await volume_trait .get_volume ()
433- click .echo (f"Device { device_id } volume:" )
434- click .echo (volume_result )
425+ await _display_v1_trait (context , device_id , lambda v1 : v1 .sound_volume )
435426
436427
437428@session .command ()
@@ -442,14 +433,7 @@ async def volume(ctx, device_id: str):
442433async def set_volume (ctx , device_id : str , volume : int ):
443434 """Set the devicevolume."""
444435 context : RoborockContext = ctx .obj
445-
446- device_manager = await context .get_device_manager ()
447- device = await device_manager .get_device (device_id )
448-
449- if not (volume_trait := device .traits .get ("sound_volume" )):
450- click .echo (f"Device { device .name } does not have a volume trait" )
451- return
452-
436+ volume_trait = await _v1_trait (context , device_id , lambda v1 : v1 .sound_volume )
453437 await volume_trait .set_volume (volume )
454438 click .echo (f"Set Device { device_id } volume to { volume } " )
455439
0 commit comments