Skip to content

Commit d9d2f1a

Browse files
rytilahtisdb9696
andauthored
Remove SmartPlug in favor of SmartDevice (python-kasa#781)
With the move towards autodetecting available features, there is no reason to keep SmartPlug around. kasa.smart.SmartPlug is removed in favor of kasa.smart.SmartDevice which offers the same functionality. Information about auto_off can be accessed using Features of the AutoOffModule on supported devices. Co-authored-by: Steven B. <[email protected]>
1 parent 8c39e81 commit d9d2f1a

File tree

10 files changed

+70
-61
lines changed

10 files changed

+70
-61
lines changed

kasa/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def __getattr__(name):
134134
from . import smart
135135

136136
smart.SmartDevice("127.0.0.1")
137-
smart.SmartPlug("127.0.0.1")
138137
smart.SmartBulb("127.0.0.1")
139138
iot.IotDevice("127.0.0.1")
140139
iot.IotPlug("127.0.0.1")

kasa/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
)
2828
from kasa.discover import DiscoveryResult
2929
from kasa.iot import IotBulb, IotDevice, IotDimmer, IotLightStrip, IotPlug, IotStrip
30-
from kasa.smart import SmartBulb, SmartDevice, SmartPlug
30+
from kasa.smart import SmartBulb, SmartDevice
3131

3232
try:
3333
from pydantic.v1 import ValidationError
@@ -72,7 +72,7 @@ def wrapper(message=None, *args, **kwargs):
7272
"iot.dimmer": IotDimmer,
7373
"iot.strip": IotStrip,
7474
"iot.lightstrip": IotLightStrip,
75-
"smart.plug": SmartPlug,
75+
"smart.plug": SmartDevice,
7676
"smart.bulb": SmartBulb,
7777
}
7878

kasa/device.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,32 +194,32 @@ def sys_info(self) -> Dict[str, Any]:
194194
@property
195195
def is_bulb(self) -> bool:
196196
"""Return True if the device is a bulb."""
197-
return self._device_type == DeviceType.Bulb
197+
return self.device_type == DeviceType.Bulb
198198

199199
@property
200200
def is_light_strip(self) -> bool:
201201
"""Return True if the device is a led strip."""
202-
return self._device_type == DeviceType.LightStrip
202+
return self.device_type == DeviceType.LightStrip
203203

204204
@property
205205
def is_plug(self) -> bool:
206206
"""Return True if the device is a plug."""
207-
return self._device_type == DeviceType.Plug
207+
return self.device_type == DeviceType.Plug
208208

209209
@property
210210
def is_strip(self) -> bool:
211211
"""Return True if the device is a strip."""
212-
return self._device_type == DeviceType.Strip
212+
return self.device_type == DeviceType.Strip
213213

214214
@property
215215
def is_strip_socket(self) -> bool:
216216
"""Return True if the device is a strip socket."""
217-
return self._device_type == DeviceType.StripSocket
217+
return self.device_type == DeviceType.StripSocket
218218

219219
@property
220220
def is_dimmer(self) -> bool:
221221
"""Return True if the device is a dimmer."""
222-
return self._device_type == DeviceType.Dimmer
222+
return self.device_type == DeviceType.Dimmer
223223

224224
@property
225225
def is_dimmable(self) -> bool:
@@ -354,9 +354,9 @@ async def set_alias(self, alias: str):
354354

355355
def __repr__(self):
356356
if self._last_update is None:
357-
return f"<{self._device_type} at {self.host} - update() needed>"
357+
return f"<{self.device_type} at {self.host} - update() needed>"
358358
return (
359-
f"<{self._device_type} model {self.model} at {self.host}"
359+
f"<{self.device_type} model {self.model} at {self.host}"
360360
f" ({self.alias}), is_on: {self.is_on}"
361361
f" - dev specific: {self.state_information}>"
362362
)

kasa/device_factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
BaseProtocol,
1515
BaseTransport,
1616
)
17-
from .smart import SmartBulb, SmartPlug
17+
from .smart import SmartBulb, SmartDevice
1818
from .smartprotocol import SmartProtocol
1919
from .xortransport import XorTransport
2020

@@ -135,10 +135,10 @@ def get_device_class_from_sys_info(info: Dict[str, Any]) -> Type[IotDevice]:
135135
def get_device_class_from_family(device_type: str) -> Optional[Type[Device]]:
136136
"""Return the device class from the type name."""
137137
supported_device_types: Dict[str, Type[Device]] = {
138-
"SMART.TAPOPLUG": SmartPlug,
138+
"SMART.TAPOPLUG": SmartDevice,
139139
"SMART.TAPOBULB": SmartBulb,
140140
"SMART.TAPOSWITCH": SmartBulb,
141-
"SMART.KASAPLUG": SmartPlug,
141+
"SMART.KASAPLUG": SmartDevice,
142142
"SMART.KASASWITCH": SmartBulb,
143143
"IOT.SMARTPLUGSWITCH": IotPlug,
144144
"IOT.SMARTBULB": IotBulb,

kasa/smart/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
from .smartbulb import SmartBulb
33
from .smartchilddevice import SmartChildDevice
44
from .smartdevice import SmartDevice
5-
from .smartplug import SmartPlug
65

7-
__all__ = ["SmartDevice", "SmartPlug", "SmartBulb", "SmartChildDevice"]
6+
__all__ = ["SmartDevice", "SmartBulb", "SmartChildDevice"]

kasa/smart/smartdevice.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,28 @@ async def factory_reset(self) -> None:
485485
Note, this does not downgrade the firmware.
486486
"""
487487
await self.protocol.query("device_reset")
488+
489+
@property
490+
def device_type(self) -> DeviceType:
491+
"""Return the device type."""
492+
if self._device_type is not DeviceType.Unknown:
493+
return self._device_type
494+
495+
if self.children:
496+
if "SMART.TAPOHUB" in self.sys_info["type"]:
497+
pass # TODO: placeholder for future hub PR
498+
else:
499+
self._device_type = DeviceType.Strip
500+
elif "light_strip" in self._components:
501+
self._device_type = DeviceType.LightStrip
502+
elif "dimmer_calibration" in self._components:
503+
self._device_type = DeviceType.Dimmer
504+
elif "brightness" in self._components:
505+
self._device_type = DeviceType.Bulb
506+
elif "PLUG" in self.sys_info["type"]:
507+
self._device_type = DeviceType.Plug
508+
else:
509+
_LOGGER.warning("Unknown device type, falling back to plug")
510+
self._device_type = DeviceType.Plug
511+
512+
return self._device_type

kasa/smart/smartplug.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

kasa/tests/conftest.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from kasa.iot import IotBulb, IotDimmer, IotLightStrip, IotPlug, IotStrip
2222
from kasa.protocol import BaseTransport
23-
from kasa.smart import SmartBulb, SmartPlug
23+
from kasa.smart import SmartBulb, SmartDevice
2424
from kasa.xortransport import XorEncryption
2525

2626
from .fakeprotocol_iot import FakeIotProtocol
@@ -108,7 +108,6 @@
108108
"EP25",
109109
"KS205",
110110
"P125M",
111-
"P135",
112111
"S505",
113112
"TP15",
114113
}
@@ -121,7 +120,7 @@
121120
STRIPS = {*STRIPS_IOT, *STRIPS_SMART}
122121

123122
DIMMERS_IOT = {"ES20M", "HS220", "KS220M", "KS230", "KP405"}
124-
DIMMERS_SMART = {"S500D"}
123+
DIMMERS_SMART = {"S500D", "P135"}
125124
DIMMERS = {
126125
*DIMMERS_IOT,
127126
*DIMMERS_SMART,
@@ -346,7 +345,7 @@ def device_for_file(model, protocol):
346345
if protocol == "SMART":
347346
for d in PLUGS_SMART:
348347
if d in model:
349-
return SmartPlug
348+
return SmartDevice
350349
for d in BULBS_SMART:
351350
if d in model:
352351
return SmartBulb
@@ -355,7 +354,7 @@ def device_for_file(model, protocol):
355354
return SmartBulb
356355
for d in STRIPS_SMART:
357356
if d in model:
358-
return SmartPlug
357+
return SmartDevice
359358
else:
360359
for d in STRIPS_IOT:
361360
if d in model:

kasa/tests/test_plug.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ async def test_led(dev):
3737
@plug_smart
3838
async def test_plug_device_info(dev):
3939
assert dev._info is not None
40-
# PLUG_SCHEMA(dev.sys_info)
41-
4240
assert dev.model is not None
4341

4442
assert dev.device_type == DeviceType.Plug or dev.device_type == DeviceType.Strip
45-
# assert dev.is_plug or dev.is_strip

kasa/tests/test_smartdevice.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222

2323
import kasa
2424
from kasa import Credentials, Device, DeviceConfig, KasaException
25+
from kasa.device_type import DeviceType
2526
from kasa.exceptions import SmartErrorCode
2627
from kasa.iot import IotDevice
2728
from kasa.smart import SmartChildDevice, SmartDevice
2829

2930
from .conftest import (
31+
bulb,
3032
device_iot,
3133
device_smart,
34+
dimmer,
3235
handle_turn_on,
3336
has_emeter_iot,
37+
lightstrip,
3438
no_emeter_iot,
39+
plug,
3540
turn_on,
3641
)
3742
from .fakeprotocol_iot import FakeIotProtocol
@@ -416,3 +421,25 @@ def check_mac(x):
416421
},
417422
extra=REMOVE_EXTRA,
418423
)
424+
425+
426+
@dimmer
427+
def test_device_type_dimmer(dev):
428+
assert dev.device_type == DeviceType.Dimmer
429+
430+
431+
@bulb
432+
def test_device_type_bulb(dev):
433+
if dev.is_light_strip:
434+
pytest.skip("bulb has also lightstrips to test the api")
435+
assert dev.device_type == DeviceType.Bulb
436+
437+
438+
@plug
439+
def test_device_type_plug(dev):
440+
assert dev.device_type == DeviceType.Plug
441+
442+
443+
@lightstrip
444+
def test_device_type_lightstrip(dev):
445+
assert dev.device_type == DeviceType.LightStrip

0 commit comments

Comments
 (0)