python-kasa 0.7.0.dev4__py3-none-any.whl → 0.7.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kasa/aestransport.py +6 -2
- kasa/cli.py +117 -66
- kasa/device.py +126 -33
- kasa/discover.py +2 -1
- kasa/feature.py +90 -14
- kasa/httpclient.py +1 -1
- kasa/interfaces/__init__.py +2 -0
- kasa/interfaces/energy.py +187 -0
- kasa/interfaces/light.py +61 -1
- kasa/interfaces/lighteffect.py +41 -1
- kasa/interfaces/lightpreset.py +70 -2
- kasa/iot/iotbulb.py +1 -1
- kasa/iot/iotdevice.py +32 -102
- kasa/iot/iotstrip.py +125 -62
- kasa/iot/modules/emeter.py +30 -89
- kasa/iot/modules/led.py +5 -0
- kasa/iot/modules/lightpreset.py +2 -1
- kasa/module.py +41 -4
- kasa/smart/modules/alarm.py +1 -0
- kasa/smart/modules/autooff.py +3 -1
- kasa/smart/modules/childdevice.py +39 -1
- kasa/smart/modules/energy.py +58 -60
- kasa/smart/modules/firmware.py +4 -1
- kasa/smart/modules/frostprotection.py +0 -22
- kasa/smart/modules/humiditysensor.py +1 -0
- kasa/smart/modules/led.py +1 -1
- kasa/smart/modules/lightpreset.py +2 -1
- kasa/smart/modules/lightstripeffect.py +14 -4
- kasa/smart/modules/lighttransition.py +8 -0
- kasa/smart/modules/reportmode.py +2 -0
- kasa/smart/modules/temperaturecontrol.py +3 -2
- kasa/smart/modules/temperaturesensor.py +3 -2
- kasa/smart/modules/time.py +10 -3
- kasa/smart/modules/waterleaksensor.py +2 -0
- kasa/smart/smartdevice.py +29 -51
- kasa/tests/device_fixtures.py +1 -1
- kasa/tests/discovery_fixtures.py +3 -1
- kasa/tests/fakeprotocol_smart.py +2 -2
- kasa/tests/fixtures/smart/L920-5(EU)_1.0_1.0.7.json +350 -0
- kasa/tests/smart/modules/test_firmware.py +12 -1
- kasa/tests/test_cli.py +55 -8
- kasa/tests/test_device.py +16 -7
- kasa/tests/test_emeter.py +44 -16
- kasa/tests/test_feature.py +6 -2
- kasa/tests/test_httpclient.py +2 -2
- kasa/tests/test_iotdevice.py +14 -6
- kasa/tests/test_readme_examples.py +59 -0
- kasa/tests/test_smartdevice.py +4 -3
- kasa/tests/test_strip.py +15 -2
- kasa/xortransport.py +3 -3
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/METADATA +102 -129
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/RECORD +55 -53
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/LICENSE +0 -0
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/WHEEL +0 -0
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/entry_points.txt +0 -0
kasa/aestransport.py
CHANGED
|
@@ -10,8 +10,9 @@ import base64
|
|
|
10
10
|
import hashlib
|
|
11
11
|
import logging
|
|
12
12
|
import time
|
|
13
|
+
from collections.abc import AsyncGenerator
|
|
13
14
|
from enum import Enum, auto
|
|
14
|
-
from typing import TYPE_CHECKING, Any,
|
|
15
|
+
from typing import TYPE_CHECKING, Any, Dict, cast
|
|
15
16
|
|
|
16
17
|
from cryptography.hazmat.primitives import padding, serialization
|
|
17
18
|
from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding
|
|
@@ -371,7 +372,10 @@ class AesEncyptionSession:
|
|
|
371
372
|
handshake_key_bytes: bytes = base64.b64decode(handshake_key.encode("UTF-8"))
|
|
372
373
|
private_key_data = base64.b64decode(keypair.get_private_key().encode("UTF-8"))
|
|
373
374
|
|
|
374
|
-
private_key =
|
|
375
|
+
private_key = cast(
|
|
376
|
+
rsa.RSAPrivateKey,
|
|
377
|
+
serialization.load_der_private_key(private_key_data, None, None),
|
|
378
|
+
)
|
|
375
379
|
key_and_iv = private_key.decrypt(
|
|
376
380
|
handshake_key_bytes, asymmetric_padding.PKCS1v15()
|
|
377
381
|
)
|
kasa/cli.py
CHANGED
|
@@ -9,6 +9,7 @@ import logging
|
|
|
9
9
|
import re
|
|
10
10
|
import sys
|
|
11
11
|
from contextlib import asynccontextmanager
|
|
12
|
+
from datetime import datetime
|
|
12
13
|
from functools import singledispatch, wraps
|
|
13
14
|
from pprint import pformat as pf
|
|
14
15
|
from typing import Any, cast
|
|
@@ -70,6 +71,12 @@ except ImportError:
|
|
|
70
71
|
echo = _do_echo
|
|
71
72
|
|
|
72
73
|
|
|
74
|
+
def error(msg: str):
|
|
75
|
+
"""Print an error and exit."""
|
|
76
|
+
echo(f"[bold red]{msg}[/bold red]")
|
|
77
|
+
sys.exit(1)
|
|
78
|
+
|
|
79
|
+
|
|
73
80
|
TYPE_TO_CLASS = {
|
|
74
81
|
"plug": IotPlug,
|
|
75
82
|
"switch": IotWallSwitch,
|
|
@@ -94,9 +101,7 @@ DEVICE_FAMILY_TYPES = [device_family_type.value for device_family_type in Device
|
|
|
94
101
|
# Block list of commands which require no update
|
|
95
102
|
SKIP_UPDATE_COMMANDS = ["raw-command", "command"]
|
|
96
103
|
|
|
97
|
-
click.
|
|
98
|
-
|
|
99
|
-
pass_dev = click.make_pass_decorator(Device)
|
|
104
|
+
pass_dev = click.make_pass_decorator(Device) # type: ignore[type-abstract]
|
|
100
105
|
|
|
101
106
|
|
|
102
107
|
def CatchAllExceptions(cls):
|
|
@@ -366,9 +371,13 @@ async def cli(
|
|
|
366
371
|
credentials = None
|
|
367
372
|
|
|
368
373
|
if host is None:
|
|
374
|
+
if ctx.invoked_subcommand and ctx.invoked_subcommand != "discover":
|
|
375
|
+
error("Only discover is available without --host or --alias")
|
|
376
|
+
|
|
369
377
|
echo("No host name given, trying discovery..")
|
|
370
378
|
return await ctx.invoke(discover)
|
|
371
379
|
|
|
380
|
+
device_updated = False
|
|
372
381
|
if type is not None:
|
|
373
382
|
dev = TYPE_TO_CLASS[type](host)
|
|
374
383
|
elif device_family and encrypt_type:
|
|
@@ -386,11 +395,14 @@ async def cli(
|
|
|
386
395
|
connection_type=ctype,
|
|
387
396
|
)
|
|
388
397
|
dev = await Device.connect(config=config)
|
|
398
|
+
device_updated = True
|
|
389
399
|
else:
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
400
|
+
if device_family or encrypt_type:
|
|
401
|
+
echo(
|
|
402
|
+
"--device-family and --encrypt-type options must both be "
|
|
403
|
+
"provided or they are ignored\n"
|
|
404
|
+
f"discovering for {discovery_timeout} seconds.."
|
|
405
|
+
)
|
|
394
406
|
dev = await Discover.discover_single(
|
|
395
407
|
host,
|
|
396
408
|
port=port,
|
|
@@ -401,7 +413,7 @@ async def cli(
|
|
|
401
413
|
|
|
402
414
|
# Skip update on specific commands, or if device factory,
|
|
403
415
|
# that performs an update was used for the device.
|
|
404
|
-
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not
|
|
416
|
+
if ctx.invoked_subcommand not in SKIP_UPDATE_COMMANDS and not device_updated:
|
|
405
417
|
await dev.update()
|
|
406
418
|
|
|
407
419
|
@asynccontextmanager
|
|
@@ -596,9 +608,7 @@ def _echo_features(
|
|
|
596
608
|
id_: feat for id_, feat in features.items() if feat.category == category
|
|
597
609
|
}
|
|
598
610
|
|
|
599
|
-
|
|
600
|
-
return
|
|
601
|
-
echo(f"[bold]{title}[/bold]")
|
|
611
|
+
echo(f"{indent}[bold]{title}[/bold]")
|
|
602
612
|
for _, feat in features.items():
|
|
603
613
|
try:
|
|
604
614
|
echo(f"{indent}{feat}")
|
|
@@ -610,33 +620,40 @@ def _echo_features(
|
|
|
610
620
|
echo(f"{indent}{feat.name} ({feat.id}): [red]got exception ({ex})[/red]")
|
|
611
621
|
|
|
612
622
|
|
|
613
|
-
def _echo_all_features(features, *, verbose=False, title_prefix=None):
|
|
623
|
+
def _echo_all_features(features, *, verbose=False, title_prefix=None, indent=""):
|
|
614
624
|
"""Print out all features by category."""
|
|
615
625
|
if title_prefix is not None:
|
|
616
|
-
echo(f"[bold]\n
|
|
626
|
+
echo(f"[bold]\n{indent}== {title_prefix} ==[/bold]")
|
|
617
627
|
_echo_features(
|
|
618
628
|
features,
|
|
619
|
-
title="
|
|
629
|
+
title="== Primary features ==",
|
|
620
630
|
category=Feature.Category.Primary,
|
|
621
631
|
verbose=verbose,
|
|
632
|
+
indent=indent,
|
|
622
633
|
)
|
|
634
|
+
echo()
|
|
623
635
|
_echo_features(
|
|
624
636
|
features,
|
|
625
|
-
title="
|
|
637
|
+
title="== Information ==",
|
|
626
638
|
category=Feature.Category.Info,
|
|
627
639
|
verbose=verbose,
|
|
640
|
+
indent=indent,
|
|
628
641
|
)
|
|
642
|
+
echo()
|
|
629
643
|
_echo_features(
|
|
630
644
|
features,
|
|
631
|
-
title="
|
|
645
|
+
title="== Configuration ==",
|
|
632
646
|
category=Feature.Category.Config,
|
|
633
647
|
verbose=verbose,
|
|
648
|
+
indent=indent,
|
|
634
649
|
)
|
|
650
|
+
echo()
|
|
635
651
|
_echo_features(
|
|
636
652
|
features,
|
|
637
|
-
title="
|
|
653
|
+
title="== Debug ==",
|
|
638
654
|
category=Feature.Category.Debug,
|
|
639
655
|
verbose=verbose,
|
|
656
|
+
indent=indent,
|
|
640
657
|
)
|
|
641
658
|
|
|
642
659
|
|
|
@@ -648,38 +665,42 @@ async def state(ctx, dev: Device):
|
|
|
648
665
|
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
|
|
649
666
|
|
|
650
667
|
echo(f"[bold]== {dev.alias} - {dev.model} ==[/bold]")
|
|
651
|
-
echo(f"
|
|
652
|
-
echo(f"
|
|
653
|
-
echo(f"
|
|
668
|
+
echo(f"Host: {dev.host}")
|
|
669
|
+
echo(f"Port: {dev.port}")
|
|
670
|
+
echo(f"Device state: {dev.is_on}")
|
|
671
|
+
|
|
672
|
+
echo(f"Time: {dev.time} (tz: {dev.timezone}")
|
|
673
|
+
echo(f"Hardware: {dev.hw_info['hw_ver']}")
|
|
674
|
+
echo(f"Software: {dev.hw_info['sw_ver']}")
|
|
675
|
+
echo(f"MAC (rssi): {dev.mac} ({dev.rssi})")
|
|
676
|
+
if verbose:
|
|
677
|
+
echo(f"Location: {dev.location}")
|
|
678
|
+
|
|
679
|
+
_echo_all_features(dev.features, verbose=verbose)
|
|
680
|
+
echo()
|
|
681
|
+
|
|
654
682
|
if dev.children:
|
|
655
|
-
echo("
|
|
683
|
+
echo("[bold]== Children ==[/bold]")
|
|
656
684
|
for child in dev.children:
|
|
657
685
|
_echo_all_features(
|
|
658
686
|
child.features,
|
|
659
|
-
title_prefix=f"{child.alias} ({child.model}
|
|
687
|
+
title_prefix=f"{child.alias} ({child.model})",
|
|
660
688
|
verbose=verbose,
|
|
689
|
+
indent="\t",
|
|
661
690
|
)
|
|
662
691
|
|
|
663
692
|
echo()
|
|
664
693
|
|
|
665
|
-
echo("\t[bold]== Generic information ==[/bold]")
|
|
666
|
-
echo(f"\tTime: {dev.time} (tz: {dev.timezone}")
|
|
667
|
-
echo(f"\tHardware: {dev.hw_info['hw_ver']}")
|
|
668
|
-
echo(f"\tSoftware: {dev.hw_info['sw_ver']}")
|
|
669
|
-
echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})")
|
|
670
|
-
echo(f"\tLocation: {dev.location}")
|
|
671
|
-
|
|
672
|
-
_echo_all_features(dev.features, verbose=verbose)
|
|
673
|
-
|
|
674
|
-
echo("\n\t[bold]== Modules ==[/bold]")
|
|
675
|
-
for module in dev.modules.values():
|
|
676
|
-
echo(f"\t[green]+ {module}[/green]")
|
|
677
|
-
|
|
678
694
|
if verbose:
|
|
695
|
+
echo("\n\t[bold]== Modules ==[/bold]")
|
|
696
|
+
for module in dev.modules.values():
|
|
697
|
+
echo(f"\t[green]+ {module}[/green]")
|
|
698
|
+
|
|
679
699
|
echo("\n\t[bold]== Protocol information ==[/bold]")
|
|
680
700
|
echo(f"\tCredentials hash: {dev.credentials_hash}")
|
|
681
701
|
echo()
|
|
682
702
|
_echo_discovery_info(dev._discovery_info)
|
|
703
|
+
|
|
683
704
|
return dev.internal_state
|
|
684
705
|
|
|
685
706
|
|
|
@@ -763,7 +784,7 @@ async def emeter(dev: Device, index: int, name: str, year, month, erase):
|
|
|
763
784
|
"""
|
|
764
785
|
if index is not None or name is not None:
|
|
765
786
|
if not dev.is_strip:
|
|
766
|
-
|
|
787
|
+
error("Index and name are only for power strips!")
|
|
767
788
|
return
|
|
768
789
|
|
|
769
790
|
if index is not None:
|
|
@@ -773,11 +794,11 @@ async def emeter(dev: Device, index: int, name: str, year, month, erase):
|
|
|
773
794
|
|
|
774
795
|
echo("[bold]== Emeter ==[/bold]")
|
|
775
796
|
if not dev.has_emeter:
|
|
776
|
-
|
|
797
|
+
error("Device has no emeter")
|
|
777
798
|
return
|
|
778
799
|
|
|
779
800
|
if (year or month or erase) and not isinstance(dev, IotDevice):
|
|
780
|
-
|
|
801
|
+
error("Device has no historical statistics")
|
|
781
802
|
return
|
|
782
803
|
else:
|
|
783
804
|
dev = cast(IotDevice, dev)
|
|
@@ -864,7 +885,7 @@ async def usage(dev: Device, year, month, erase):
|
|
|
864
885
|
async def brightness(dev: Device, brightness: int, transition: int):
|
|
865
886
|
"""Get or set brightness."""
|
|
866
887
|
if not (light := dev.modules.get(Module.Light)) or not light.is_dimmable:
|
|
867
|
-
|
|
888
|
+
error("This device does not support brightness.")
|
|
868
889
|
return
|
|
869
890
|
|
|
870
891
|
if brightness is None:
|
|
@@ -884,7 +905,7 @@ async def brightness(dev: Device, brightness: int, transition: int):
|
|
|
884
905
|
async def temperature(dev: Device, temperature: int, transition: int):
|
|
885
906
|
"""Get or set color temperature."""
|
|
886
907
|
if not (light := dev.modules.get(Module.Light)) or not light.is_variable_color_temp:
|
|
887
|
-
|
|
908
|
+
error("Device does not support color temperature")
|
|
888
909
|
return
|
|
889
910
|
|
|
890
911
|
if temperature is None:
|
|
@@ -910,7 +931,7 @@ async def temperature(dev: Device, temperature: int, transition: int):
|
|
|
910
931
|
async def effect(dev: Device, ctx, effect):
|
|
911
932
|
"""Set an effect."""
|
|
912
933
|
if not (light_effect := dev.modules.get(Module.LightEffect)):
|
|
913
|
-
|
|
934
|
+
error("Device does not support effects")
|
|
914
935
|
return
|
|
915
936
|
if effect is None:
|
|
916
937
|
echo(
|
|
@@ -938,7 +959,7 @@ async def effect(dev: Device, ctx, effect):
|
|
|
938
959
|
async def hsv(dev: Device, ctx, h, s, v, transition):
|
|
939
960
|
"""Get or set color in HSV."""
|
|
940
961
|
if not (light := dev.modules.get(Module.Light)) or not light.is_color:
|
|
941
|
-
|
|
962
|
+
error("Device does not support colors")
|
|
942
963
|
return
|
|
943
964
|
|
|
944
965
|
if h is None and s is None and v is None:
|
|
@@ -957,7 +978,7 @@ async def hsv(dev: Device, ctx, h, s, v, transition):
|
|
|
957
978
|
async def led(dev: Device, state):
|
|
958
979
|
"""Get or set (Plug's) led state."""
|
|
959
980
|
if not (led := dev.modules.get(Module.Led)):
|
|
960
|
-
|
|
981
|
+
error("Device does not support led.")
|
|
961
982
|
return
|
|
962
983
|
if state is not None:
|
|
963
984
|
echo(f"Turning led to {state}")
|
|
@@ -967,15 +988,43 @@ async def led(dev: Device, state):
|
|
|
967
988
|
return led.led
|
|
968
989
|
|
|
969
990
|
|
|
970
|
-
@cli.
|
|
991
|
+
@cli.group(invoke_without_command=True)
|
|
992
|
+
@click.pass_context
|
|
993
|
+
async def time(ctx: click.Context):
|
|
994
|
+
"""Get and set time."""
|
|
995
|
+
if ctx.invoked_subcommand is None:
|
|
996
|
+
await ctx.invoke(time_get)
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
@time.command(name="get")
|
|
971
1000
|
@pass_dev
|
|
972
|
-
async def
|
|
1001
|
+
async def time_get(dev: Device):
|
|
973
1002
|
"""Get the device time."""
|
|
974
1003
|
res = dev.time
|
|
975
1004
|
echo(f"Current time: {res}")
|
|
976
1005
|
return res
|
|
977
1006
|
|
|
978
1007
|
|
|
1008
|
+
@time.command(name="sync")
|
|
1009
|
+
@pass_dev
|
|
1010
|
+
async def time_sync(dev: Device):
|
|
1011
|
+
"""Set the device time to current time."""
|
|
1012
|
+
if not isinstance(dev, SmartDevice):
|
|
1013
|
+
raise NotImplementedError("setting time currently only implemented on smart")
|
|
1014
|
+
|
|
1015
|
+
if (time := dev.modules.get(Module.Time)) is None:
|
|
1016
|
+
echo("Device does not have time module")
|
|
1017
|
+
return
|
|
1018
|
+
|
|
1019
|
+
echo("Old time: %s" % time.time)
|
|
1020
|
+
|
|
1021
|
+
local_tz = datetime.now().astimezone().tzinfo
|
|
1022
|
+
await time.set_time(datetime.now(tz=local_tz))
|
|
1023
|
+
|
|
1024
|
+
await dev.update()
|
|
1025
|
+
echo("New time: %s" % time.time)
|
|
1026
|
+
|
|
1027
|
+
|
|
979
1028
|
@cli.command()
|
|
980
1029
|
@click.option("--index", type=int, required=False)
|
|
981
1030
|
@click.option("--name", type=str, required=False)
|
|
@@ -985,7 +1034,7 @@ async def on(dev: Device, index: int, name: str, transition: int):
|
|
|
985
1034
|
"""Turn the device on."""
|
|
986
1035
|
if index is not None or name is not None:
|
|
987
1036
|
if not dev.children:
|
|
988
|
-
|
|
1037
|
+
error("Index and name are only for devices with children.")
|
|
989
1038
|
return
|
|
990
1039
|
|
|
991
1040
|
if index is not None:
|
|
@@ -1006,7 +1055,7 @@ async def off(dev: Device, index: int, name: str, transition: int):
|
|
|
1006
1055
|
"""Turn the device off."""
|
|
1007
1056
|
if index is not None or name is not None:
|
|
1008
1057
|
if not dev.children:
|
|
1009
|
-
|
|
1058
|
+
error("Index and name are only for devices with children.")
|
|
1010
1059
|
return
|
|
1011
1060
|
|
|
1012
1061
|
if index is not None:
|
|
@@ -1027,7 +1076,7 @@ async def toggle(dev: Device, index: int, name: str, transition: int):
|
|
|
1027
1076
|
"""Toggle the device on/off."""
|
|
1028
1077
|
if index is not None or name is not None:
|
|
1029
1078
|
if not dev.children:
|
|
1030
|
-
|
|
1079
|
+
error("Index and name are only for devices with children.")
|
|
1031
1080
|
return
|
|
1032
1081
|
|
|
1033
1082
|
if index is not None:
|
|
@@ -1067,7 +1116,7 @@ def _schedule_list(dev, type):
|
|
|
1067
1116
|
for rule in sched.rules:
|
|
1068
1117
|
print(rule)
|
|
1069
1118
|
else:
|
|
1070
|
-
|
|
1119
|
+
error(f"No rules of type {type}")
|
|
1071
1120
|
|
|
1072
1121
|
return sched.rules
|
|
1073
1122
|
|
|
@@ -1083,7 +1132,7 @@ async def delete_rule(dev, id):
|
|
|
1083
1132
|
echo(f"Deleting rule id {id}")
|
|
1084
1133
|
return await schedule.delete_rule(rule_to_delete)
|
|
1085
1134
|
else:
|
|
1086
|
-
|
|
1135
|
+
error(f"No rule with id {id} was found")
|
|
1087
1136
|
|
|
1088
1137
|
|
|
1089
1138
|
@cli.group(invoke_without_command=True)
|
|
@@ -1096,10 +1145,10 @@ async def presets(ctx):
|
|
|
1096
1145
|
|
|
1097
1146
|
@presets.command(name="list")
|
|
1098
1147
|
@pass_dev
|
|
1099
|
-
def presets_list(dev:
|
|
1148
|
+
def presets_list(dev: Device):
|
|
1100
1149
|
"""List presets."""
|
|
1101
1150
|
if not dev.is_bulb or not isinstance(dev, IotBulb):
|
|
1102
|
-
|
|
1151
|
+
error("Presets only supported on iot bulbs")
|
|
1103
1152
|
return
|
|
1104
1153
|
|
|
1105
1154
|
for preset in dev.presets:
|
|
@@ -1115,13 +1164,13 @@ def presets_list(dev: IotBulb):
|
|
|
1115
1164
|
@click.option("--saturation", type=int)
|
|
1116
1165
|
@click.option("--temperature", type=int)
|
|
1117
1166
|
@pass_dev
|
|
1118
|
-
async def presets_modify(dev:
|
|
1167
|
+
async def presets_modify(dev: Device, index, brightness, hue, saturation, temperature):
|
|
1119
1168
|
"""Modify a preset."""
|
|
1120
1169
|
for preset in dev.presets:
|
|
1121
1170
|
if preset.index == index:
|
|
1122
1171
|
break
|
|
1123
1172
|
else:
|
|
1124
|
-
|
|
1173
|
+
error(f"No preset found for index {index}")
|
|
1125
1174
|
return
|
|
1126
1175
|
|
|
1127
1176
|
if brightness is not None:
|
|
@@ -1143,10 +1192,10 @@ async def presets_modify(dev: IotBulb, index, brightness, hue, saturation, tempe
|
|
|
1143
1192
|
@click.option("--type", type=click.Choice(["soft", "hard"], case_sensitive=False))
|
|
1144
1193
|
@click.option("--last", is_flag=True)
|
|
1145
1194
|
@click.option("--preset", type=int)
|
|
1146
|
-
async def turn_on_behavior(dev:
|
|
1195
|
+
async def turn_on_behavior(dev: Device, type, last, preset):
|
|
1147
1196
|
"""Modify bulb turn-on behavior."""
|
|
1148
1197
|
if not dev.is_bulb or not isinstance(dev, IotBulb):
|
|
1149
|
-
|
|
1198
|
+
error("Presets only supported on iot bulbs")
|
|
1150
1199
|
return
|
|
1151
1200
|
settings = await dev.get_turn_on_behavior()
|
|
1152
1201
|
echo(f"Current turn on behavior: {settings}")
|
|
@@ -1183,9 +1232,7 @@ async def turn_on_behavior(dev: IotBulb, type, last, preset):
|
|
|
1183
1232
|
async def update_credentials(dev, username, password):
|
|
1184
1233
|
"""Update device credentials for authenticated devices."""
|
|
1185
1234
|
if not isinstance(dev, SmartDevice):
|
|
1186
|
-
|
|
1187
|
-
"Credentials can only be updated on authenticated devices."
|
|
1188
|
-
)
|
|
1235
|
+
error("Credentials can only be updated on authenticated devices.")
|
|
1189
1236
|
|
|
1190
1237
|
click.confirm("Do you really want to replace the existing credentials?", abort=True)
|
|
1191
1238
|
|
|
@@ -1203,7 +1250,7 @@ async def shell(dev: Device):
|
|
|
1203
1250
|
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
|
1204
1251
|
loop = asyncio.get_event_loop()
|
|
1205
1252
|
try:
|
|
1206
|
-
await embed(
|
|
1253
|
+
await embed( # type: ignore[func-returns-value]
|
|
1207
1254
|
globals=globals(),
|
|
1208
1255
|
locals=locals(),
|
|
1209
1256
|
return_asyncio_coroutine=True,
|
|
@@ -1218,31 +1265,35 @@ async def shell(dev: Device):
|
|
|
1218
1265
|
@click.argument("value", required=False)
|
|
1219
1266
|
@click.option("--child", required=False)
|
|
1220
1267
|
@pass_dev
|
|
1221
|
-
|
|
1268
|
+
@click.pass_context
|
|
1269
|
+
async def feature(ctx: click.Context, dev: Device, child: str, name: str, value):
|
|
1222
1270
|
"""Access and modify features.
|
|
1223
1271
|
|
|
1224
1272
|
If no *name* is given, lists available features and their values.
|
|
1225
1273
|
If only *name* is given, the value of named feature is returned.
|
|
1226
1274
|
If both *name* and *value* are set, the described setting is changed.
|
|
1227
1275
|
"""
|
|
1276
|
+
verbose = ctx.parent.params.get("verbose", False) if ctx.parent else False
|
|
1277
|
+
|
|
1228
1278
|
if child is not None:
|
|
1229
1279
|
echo(f"Targeting child device {child}")
|
|
1230
1280
|
dev = dev.get_child_device(child)
|
|
1231
1281
|
if not name:
|
|
1232
|
-
|
|
1282
|
+
_echo_all_features(dev.features, verbose=verbose, indent="")
|
|
1233
1283
|
|
|
1234
1284
|
if dev.children:
|
|
1235
1285
|
for child_dev in dev.children:
|
|
1236
|
-
|
|
1286
|
+
_echo_all_features(
|
|
1237
1287
|
child_dev.features,
|
|
1238
|
-
|
|
1239
|
-
|
|
1288
|
+
verbose=verbose,
|
|
1289
|
+
title_prefix=f"Child {child_dev.alias}",
|
|
1290
|
+
indent="\t",
|
|
1240
1291
|
)
|
|
1241
1292
|
|
|
1242
1293
|
return
|
|
1243
1294
|
|
|
1244
1295
|
if name not in dev.features:
|
|
1245
|
-
|
|
1296
|
+
error(f"No feature by name '{name}'")
|
|
1246
1297
|
return
|
|
1247
1298
|
|
|
1248
1299
|
feat = dev.features[name]
|
kasa/device.py
CHANGED
|
@@ -1,12 +1,115 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Interact with TPLink Smart Home devices.
|
|
2
|
+
|
|
3
|
+
Once you have a device via :ref:`Discovery <discover_target>` or
|
|
4
|
+
:ref:`Connect <connect_target>` you can start interacting with a device.
|
|
5
|
+
|
|
6
|
+
>>> from kasa import Discover
|
|
7
|
+
>>>
|
|
8
|
+
>>> dev = await Discover.discover_single(
|
|
9
|
+
>>> "127.0.0.2",
|
|
10
|
+
>>> username="user@example.com",
|
|
11
|
+
>>> password="great_password"
|
|
12
|
+
>>> )
|
|
13
|
+
>>>
|
|
14
|
+
|
|
15
|
+
Most devices can be turned on and off
|
|
16
|
+
|
|
17
|
+
>>> await dev.turn_on()
|
|
18
|
+
>>> await dev.update()
|
|
19
|
+
>>> print(dev.is_on)
|
|
20
|
+
True
|
|
21
|
+
|
|
22
|
+
>>> await dev.turn_off()
|
|
23
|
+
>>> await dev.update()
|
|
24
|
+
>>> print(dev.is_on)
|
|
25
|
+
False
|
|
26
|
+
|
|
27
|
+
All devices provide several informational properties:
|
|
28
|
+
|
|
29
|
+
>>> dev.alias
|
|
30
|
+
Bedroom Lamp Plug
|
|
31
|
+
>>> dev.model
|
|
32
|
+
HS110(EU)
|
|
33
|
+
>>> dev.rssi
|
|
34
|
+
-71
|
|
35
|
+
>>> dev.mac
|
|
36
|
+
50:C7:BF:00:00:00
|
|
37
|
+
|
|
38
|
+
Some information can also be changed programmatically:
|
|
39
|
+
|
|
40
|
+
>>> await dev.set_alias("new alias")
|
|
41
|
+
>>> await dev.update()
|
|
42
|
+
>>> dev.alias
|
|
43
|
+
new alias
|
|
44
|
+
|
|
45
|
+
Devices support different functionality that are exposed via
|
|
46
|
+
:ref:`modules <module_target>` that you can access via :attr:`~kasa.Device.modules`:
|
|
47
|
+
|
|
48
|
+
>>> for module_name in dev.modules:
|
|
49
|
+
>>> print(module_name)
|
|
50
|
+
Energy
|
|
51
|
+
schedule
|
|
52
|
+
usage
|
|
53
|
+
anti_theft
|
|
54
|
+
time
|
|
55
|
+
cloud
|
|
56
|
+
Led
|
|
57
|
+
|
|
58
|
+
>>> led_module = dev.modules["Led"]
|
|
59
|
+
>>> print(led_module.led)
|
|
60
|
+
False
|
|
61
|
+
>>> await led_module.set_led(True)
|
|
62
|
+
>>> await dev.update()
|
|
63
|
+
>>> print(led_module.led)
|
|
64
|
+
True
|
|
65
|
+
|
|
66
|
+
Individual pieces of functionality are also exposed via :ref:`features <feature_target>`
|
|
67
|
+
which you can access via :attr:`~kasa.Device.features` and will only be present if
|
|
68
|
+
they are supported.
|
|
69
|
+
|
|
70
|
+
Features are similar to modules in that they provide functionality that may or may
|
|
71
|
+
not be present.
|
|
72
|
+
|
|
73
|
+
Whereas modules group functionality into a common interface, features expose a single
|
|
74
|
+
function that may or may not be part of a module.
|
|
75
|
+
|
|
76
|
+
The advantage of features is that they have a simple common interface of `id`, `name`,
|
|
77
|
+
`value` and `set_value` so no need to learn the module API.
|
|
78
|
+
|
|
79
|
+
They are useful if you want write code that dynamically adapts as new features are
|
|
80
|
+
added to the API.
|
|
81
|
+
|
|
82
|
+
>>> for feature_name in dev.features:
|
|
83
|
+
>>> print(feature_name)
|
|
84
|
+
state
|
|
85
|
+
rssi
|
|
86
|
+
on_since
|
|
87
|
+
current_consumption
|
|
88
|
+
consumption_today
|
|
89
|
+
consumption_this_month
|
|
90
|
+
consumption_total
|
|
91
|
+
voltage
|
|
92
|
+
current
|
|
93
|
+
cloud_connection
|
|
94
|
+
led
|
|
95
|
+
|
|
96
|
+
>>> led_feature = dev.features["led"]
|
|
97
|
+
>>> print(led_feature.value)
|
|
98
|
+
True
|
|
99
|
+
>>> await led_feature.set_value(False)
|
|
100
|
+
>>> await dev.update()
|
|
101
|
+
>>> print(led_feature.value)
|
|
102
|
+
False
|
|
103
|
+
"""
|
|
2
104
|
|
|
3
105
|
from __future__ import annotations
|
|
4
106
|
|
|
5
107
|
import logging
|
|
6
108
|
from abc import ABC, abstractmethod
|
|
109
|
+
from collections.abc import Mapping, Sequence
|
|
7
110
|
from dataclasses import dataclass
|
|
8
111
|
from datetime import datetime
|
|
9
|
-
from typing import TYPE_CHECKING, Any
|
|
112
|
+
from typing import TYPE_CHECKING, Any
|
|
10
113
|
from warnings import warn
|
|
11
114
|
|
|
12
115
|
from typing_extensions import TypeAlias
|
|
@@ -19,7 +122,6 @@ from .deviceconfig import (
|
|
|
19
122
|
DeviceEncryptionType,
|
|
20
123
|
DeviceFamily,
|
|
21
124
|
)
|
|
22
|
-
from .emeterstatus import EmeterStatus
|
|
23
125
|
from .exceptions import KasaException
|
|
24
126
|
from .feature import Feature
|
|
25
127
|
from .iotprotocol import IotProtocol
|
|
@@ -323,27 +425,6 @@ class Device(ABC):
|
|
|
323
425
|
def on_since(self) -> datetime | None:
|
|
324
426
|
"""Return the time that the device was turned on or None if turned off."""
|
|
325
427
|
|
|
326
|
-
@abstractmethod
|
|
327
|
-
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
328
|
-
"""Retrieve current energy readings."""
|
|
329
|
-
|
|
330
|
-
@property
|
|
331
|
-
@abstractmethod
|
|
332
|
-
def emeter_realtime(self) -> EmeterStatus:
|
|
333
|
-
"""Get the emeter status."""
|
|
334
|
-
|
|
335
|
-
@property
|
|
336
|
-
@abstractmethod
|
|
337
|
-
def emeter_this_month(self) -> float | None:
|
|
338
|
-
"""Get the emeter value for this month."""
|
|
339
|
-
|
|
340
|
-
@property
|
|
341
|
-
@abstractmethod
|
|
342
|
-
def emeter_today(self) -> float | None | Any:
|
|
343
|
-
"""Get the emeter value for today."""
|
|
344
|
-
# Return type of Any ensures consumers being shielded from the return
|
|
345
|
-
# type by @update_required are not affected.
|
|
346
|
-
|
|
347
428
|
@abstractmethod
|
|
348
429
|
async def wifi_scan(self) -> list[WifiNetwork]:
|
|
349
430
|
"""Scan for available wifi networks."""
|
|
@@ -373,12 +454,15 @@ class Device(ABC):
|
|
|
373
454
|
}
|
|
374
455
|
|
|
375
456
|
def _get_replacing_attr(self, module_name: ModuleName, *attrs):
|
|
376
|
-
|
|
457
|
+
# If module name is None check self
|
|
458
|
+
if not module_name:
|
|
459
|
+
check = self
|
|
460
|
+
elif (check := self.modules.get(module_name)) is None:
|
|
377
461
|
return None
|
|
378
462
|
|
|
379
463
|
for attr in attrs:
|
|
380
|
-
if hasattr(
|
|
381
|
-
return
|
|
464
|
+
if hasattr(check, attr):
|
|
465
|
+
return attr
|
|
382
466
|
|
|
383
467
|
return None
|
|
384
468
|
|
|
@@ -411,6 +495,16 @@ class Device(ABC):
|
|
|
411
495
|
# light preset attributes
|
|
412
496
|
"presets": (Module.LightPreset, ["_deprecated_presets", "preset_states_list"]),
|
|
413
497
|
"save_preset": (Module.LightPreset, ["_deprecated_save_preset"]),
|
|
498
|
+
# Emeter attribues
|
|
499
|
+
"get_emeter_realtime": (Module.Energy, ["get_status"]),
|
|
500
|
+
"emeter_realtime": (Module.Energy, ["status"]),
|
|
501
|
+
"emeter_today": (Module.Energy, ["consumption_today"]),
|
|
502
|
+
"emeter_this_month": (Module.Energy, ["consumption_this_month"]),
|
|
503
|
+
"current_consumption": (Module.Energy, ["current_consumption"]),
|
|
504
|
+
"get_emeter_daily": (Module.Energy, ["get_daily_stats"]),
|
|
505
|
+
"get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]),
|
|
506
|
+
# Other attributes
|
|
507
|
+
"supported_modules": (None, ["modules"]),
|
|
414
508
|
}
|
|
415
509
|
|
|
416
510
|
def __getattr__(self, name):
|
|
@@ -427,11 +521,10 @@ class Device(ABC):
|
|
|
427
521
|
(replacing_attr := self._get_replacing_attr(dep_attr[0], *dep_attr[1]))
|
|
428
522
|
is not None
|
|
429
523
|
):
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
)
|
|
524
|
+
mod = dep_attr[0]
|
|
525
|
+
dev_or_mod = self.modules[mod] if mod else self
|
|
526
|
+
replacing = f"Module.{mod} in device.modules" if mod else replacing_attr
|
|
527
|
+
msg = f"{name} is deprecated, use: {replacing} instead"
|
|
435
528
|
warn(msg, DeprecationWarning, stacklevel=1)
|
|
436
|
-
return replacing_attr
|
|
529
|
+
return getattr(dev_or_mod, replacing_attr)
|
|
437
530
|
raise AttributeError(f"Device has no attribute {name!r}")
|
kasa/discover.py
CHANGED
|
@@ -86,7 +86,8 @@ import binascii
|
|
|
86
86
|
import ipaddress
|
|
87
87
|
import logging
|
|
88
88
|
import socket
|
|
89
|
-
from
|
|
89
|
+
from collections.abc import Awaitable
|
|
90
|
+
from typing import Callable, Dict, Optional, Type, cast
|
|
90
91
|
|
|
91
92
|
# When support for cpython older than 3.11 is dropped
|
|
92
93
|
# async_timeout can be replaced with asyncio.timeout
|