mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-11-25 02:38:30 +00:00
Make get_module return typed module (#892)
Passing in a string still works and returns either `IotModule` or
`SmartModule` type when called on `IotDevice` or `SmartDevice`
respectively. When calling on `Device` will return `Module` type.
Passing in a module type is then typed to that module, i.e.:
```py
smartdev.get_module(FanModule) # type is FanModule
smartdev.get_module("FanModule") # type is SmartModule
```
Only thing this doesn't do is check that you can't pass an `IotModule`
to a `SmartDevice.get_module()`. However there is a runtime check which
will return null if the passed `ModuleType` is not a subclass of
`SmartModule`.
Many thanks to @cdce8p for helping with this.
This commit is contained in:
@@ -122,23 +122,43 @@ async def test_update_module_queries(dev: SmartDevice, mocker: MockerFixture):
|
||||
spies[device].assert_not_called()
|
||||
|
||||
|
||||
async def test_get_modules(mocker):
|
||||
async def test_get_modules():
|
||||
"""Test get_modules for child and parent modules."""
|
||||
dummy_device = await get_device_for_fixture_protocol(
|
||||
"KS240(US)_1.0_1.0.5.json", "SMART"
|
||||
)
|
||||
from kasa.iot.modules import AmbientLight
|
||||
from kasa.smart.modules import CloudModule, FanModule
|
||||
|
||||
# Modules on device
|
||||
module = dummy_device.get_module("CloudModule")
|
||||
assert module
|
||||
assert module._device == dummy_device
|
||||
assert isinstance(module, CloudModule)
|
||||
|
||||
module = dummy_device.get_module(CloudModule)
|
||||
assert module
|
||||
assert module._device == dummy_device
|
||||
assert isinstance(module, CloudModule)
|
||||
|
||||
# Modules on child
|
||||
module = dummy_device.get_module("FanModule")
|
||||
assert module
|
||||
assert module._device != dummy_device
|
||||
assert module._device._parent == dummy_device
|
||||
|
||||
module = dummy_device.get_module(FanModule)
|
||||
assert module
|
||||
assert module._device != dummy_device
|
||||
assert module._device._parent == dummy_device
|
||||
|
||||
# Invalid modules
|
||||
module = dummy_device.get_module("DummyModule")
|
||||
assert module is None
|
||||
|
||||
module = dummy_device.get_module(AmbientLight)
|
||||
assert module is None
|
||||
|
||||
|
||||
@bulb_smart
|
||||
async def test_smartdevice_brightness(dev: SmartDevice):
|
||||
|
||||
Reference in New Issue
Block a user