match names to cli

This commit is contained in:
J. Nick Koston 2023-10-31 20:06:02 -05:00
parent 5e290633ed
commit a63e0bed2d
No known key found for this signature in database
4 changed files with 27 additions and 17 deletions

View File

@ -12,14 +12,13 @@ import asyncclick as click
from kasa import (
Credentials,
DeviceType,
Discover,
SmartBulb,
SmartDevice,
SmartDimmer,
SmartLightStrip,
SmartPlug,
SmartStrip,
)
from kasa.discover import DEVICE_TYPE_TO_CLASS
try:
from rich import print as _do_echo
@ -43,11 +42,9 @@ except ImportError:
echo = _do_echo
TYPE_TO_CLASS = {
"plug": SmartPlug,
"bulb": SmartBulb,
"dimmer": SmartDimmer,
"strip": SmartStrip,
"lightstrip": SmartLightStrip,
device_type.value: DEVICE_TYPE_TO_CLASS[device_type]
for device_type in DeviceType
if device_type in DEVICE_TYPE_TO_CLASS
}
click.anyio_backend = "asyncio"

View File

@ -340,6 +340,7 @@ class Discover:
The device type is discovered by querying the device.
:param host: Hostname of device to query
:param device_type: Device type to use for the device.
:rtype: SmartDevice
:return: Object for querying/controlling found device.
"""

View File

@ -32,13 +32,13 @@ _LOGGER = logging.getLogger(__name__)
class DeviceType(Enum):
"""Device type enum."""
Plug = "Plug"
Bulb = "Bulb"
Strip = "Strip"
StripSocket = "StripSocket"
Dimmer = "Dimmer"
LightStrip = "LightStrip"
Unknown = "Unknown"
Plug = "plug"
Bulb = "bulb"
Strip = "strip"
StripSocket = "stripsocket"
Dimmer = "dimmer"
LightStrip = "lightstrip"
Unknown = "unknown"
@staticmethod
def from_value(name: str) -> "DeviceType":

View File

@ -65,8 +65,20 @@ async def test_smart_device_from_value():
assert DeviceType.from_value(name.value) is not None
assert DeviceType.from_value("nonexistent") is DeviceType.Unknown
assert DeviceType.from_value("Plug") is DeviceType.Plug
assert DeviceType.Plug.value == "Plug"
assert DeviceType.from_value("plug") is DeviceType.Plug
assert DeviceType.Plug.value == "plug"
assert DeviceType.from_value("bulb") is DeviceType.Bulb
assert DeviceType.Bulb.value == "bulb"
assert DeviceType.from_value("dimmer") is DeviceType.Dimmer
assert DeviceType.Dimmer.value == "dimmer"
assert DeviceType.from_value("strip") is DeviceType.Strip
assert DeviceType.Strip.value == "strip"
assert DeviceType.from_value("lightstrip") is DeviceType.LightStrip
assert DeviceType.LightStrip.value == "lightstrip"
async def test_query_helper(dev):