27
27
EncryptType ,
28
28
Feature ,
29
29
KasaException ,
30
- Light ,
30
+ Module ,
31
31
UnsupportedDeviceError ,
32
32
)
33
33
from kasa .discover import DiscoveryResult
@@ -859,18 +859,18 @@ async def usage(dev: Device, year, month, erase):
859
859
@click .argument ("brightness" , type = click .IntRange (0 , 100 ), default = None , required = False )
860
860
@click .option ("--transition" , type = int , required = False )
861
861
@pass_dev
862
- async def brightness (dev : Light , brightness : int , transition : int ):
862
+ async def brightness (dev : Device , brightness : int , transition : int ):
863
863
"""Get or set brightness."""
864
- if not dev .is_dimmable :
864
+ if not ( light := dev . modules . get ( Module . Light )) or not light .is_dimmable :
865
865
echo ("This device does not support brightness." )
866
866
return
867
867
868
868
if brightness is None :
869
- echo (f"Brightness: { dev .brightness } " )
870
- return dev .brightness
869
+ echo (f"Brightness: { light .brightness } " )
870
+ return light .brightness
871
871
else :
872
872
echo (f"Setting brightness to { brightness } " )
873
- return await dev .set_brightness (brightness , transition =
57AE
transition )
873
+ return await light .set_brightness (brightness , transition = transition )
874
874
875
875
876
876
@cli .command ()
@@ -879,47 +879,50 @@ async def brightness(dev: Light, brightness: int, transition: int):
879
879
)
880
880
@click .option ("--transition" , type = int , required = False )
881
881
@pass_dev
882
- async def temperature (dev : Light , temperature : int , transition : int ):
882
+ async def temperature (dev : Device , temperature : int , transition : int ):
883
883
"""Get or set color temperature."""
884
- if not dev .is_variable_color_temp :
884
+ if not ( light := dev . modules . get ( Module . Light )) or not light .is_variable_color_temp :
885
885
echo ("Device does not support color temperature" )
886
886
return
887
887
888
888
if temperature is None :
889
- echo (f"Color temperature: { dev .color_temp } " )
890
- valid_temperature_range = dev .valid_temperature_range
889
+ echo (f"Color temperature: { light .color_temp } " )
890
+ valid_temperature_range = light .valid_temperature_range
891
891
if valid_temperature_range != (0 , 0 ):
892
892
echo ("(min: {}, max: {})" .format (* valid_temperature_range ))
893
893
else :
894
894
echo (
895
895
"Temperature range unknown, please open a github issue"
896
896
f" or a pull request for model '{ dev .model } '"
897
897
)
898
- return dev .valid_temperature_range
898
+ return light .valid_temperature_range
899
899
else :
900
900
echo (f"Setting color temperature to { temperature } " )
901
- return await dev .set_color_temp (temperature , transition = transition )
901
+ return await light .set_color_temp (temperature , transition = transition )
902
902
903
903
904
904
@cli .command ()
905
905
@click .argument ("effect" , type = click .STRING , default = None , required = False )
906
906
@click .pass_context
907
907
@pass_dev
908
- async def effect (dev , ctx , effect ):
908
+ async def effect (dev : Device , ctx , effect ):
909
909
"""Set an effect."""
910
- if not dev .has_effects :
910
+ if not ( light_effect := dev .modules . get ( Module . LightEffect )) :
911
911
echo ("Device does not support effects" )
912
912
return
913
913
if effect is None :
914
914
raise click .BadArgumentUsage (
915
- f"Setting an effect requires a named built-in effect: { dev .effect_list } " ,
915
+ "Setting an effect requires a named built-in effect: "
916
+ + f"{ light_effect .effect_list } " ,
916
917
ctx ,
917
918
)
918
- if effect not in dev .effect_list :
919
- raise click .BadArgumentUsage (f"Effect must be one of: { dev .effect_list } " , ctx )
919
+ if effect not in light_effect .effect_list :
920
+ raise click .BadArgumentUsage (
921
+ f"Effect must be one of: { light_effect .effect_list } " , ctx
922
+ )
920
923
921
924
echo (f"Setting Effect: { effect } " )
922
- return await dev .set_effect (effect )
925
+ return await light_effect .set_effect (effect )
923
926
924
927
925
928
@cli .command ()
@@ -929,33 +932,36 @@ async def effect(dev, ctx, effect):
929
932
@click .option ("--transition" , type = int , required = False )
930
933
@click .pass_context
931
934
@pass_dev
932
- async def hsv (dev , ctx , h , s , v , transition ):
935
+ async def hsv (dev : Device , ctx , h , s , v , transition ):
933
936
"""Get or set color in HSV."""
934
- if not dev .is_color :
937
+ if not ( light := dev . modules . get ( Module . Light )) or not light .is_color :
935
938
echo ("Device does not support colors" )
936
939
return
937
940
938
- if h is None or s is None or v is None :
939
- echo (f"Current HSV: { dev .hsv } " )
940
- return dev .hsv
941
+ if h is None and s is None and v is None :
942
+ echo (f"Current HSV: { light .hsv } " )
943
+ return light .hsv
941
944
elif s is None or v is None :
942
945
raise click .BadArgumentUsage ("Setting a color requires 3 values." , ctx )
943
946
else :
944
947
echo (f"Setting HSV: { h } { s } { v } " )
945
- return await dev .set_hsv (h , s , v , transition = transition )
948
+ return await light .set_hsv (h , s , v , transition = transition )
946
949
947
950
948
951
@cli .command ()
949
952
@click .argument ("state" , type = bool , required = False )
950
953
@pass_dev
951
- async def led (dev , state ):
954
+ async def led (dev : Device , state ):
952
955
"""Get or set (Plug's) led state."""
956
+ if not (led := dev .modules .get (Module .Led )):
957
+ echo ("Device does not support led." )
958
+ return
953
959
if state is not None :
954
960
echo (f"Turning led to { state } " )
955
- return await dev .set_led (state )
961
+ return await led .set_led (state )
956
962
else :
957
- echo (f"LED state: { dev .led } " )
958
- return dev .led
963
+ echo (f"LED state: { led .led } " )
964
+ return led .led
959
965
960
966
961
967
@cli .command ()
@@ -975,8 +981,8 @@ async def time(dev):
975
981
async def on (dev : Device , index : int , name : str , transition : int ):
976
982
"""Turn the device on."""
977
983
if index is not None or name is not None :
978
- if not dev .is_strip :
979
- echo ("Index and name are only for power strips! " )
984
+ if not dev .children :
985
+ echo ("Index and name are only for devices with children. " )
980
986
return
981
987
982
988
if index is not None :
@@ -996,8 +1002,8 @@ async def on(dev: Device, index: int, name: str, transition: int):
996
1002
async def off (dev : Device , index : int , name : str , transition : int ):
997
1003
"""Turn the device off."""
998
1004
if index is not None or name is not None :
999
-
1C6A
if not dev .is_strip :
1000
- echo ("Index and name are only for power strips! " )
1005
+ if not dev .children :
1006
+ echo ("Index and name are only for devices with children. " )
1001
1007
return
1002
1008
1003
1009
if index is not None :
@@ -1017,8 +1023,8 @@ async def off(dev: Device, index: int, name: str, transition: int):
1017
1023
async def toggle (dev : Device , index : int , name : str , transition : int ):
1018
1024
"""Toggle the device on/off."""
1019
1025
if index is not None or name is not None :
1020
- if not dev .is_strip :
1021
- echo ("Index and name are only for power strips! " )
1026
+ if not dev .children :
1027
+ echo ("Index and name are only for devices with children. " )
1022
1028
return
1023
1029
1024
1030
if index is not None :
0 commit comments