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:
Steven B
2024-05-03 16:01:21 +01:00
committed by GitHub
parent 530fb841b0
commit c5d65b624b
8 changed files with 114 additions and 16 deletions

View File

@@ -4,7 +4,10 @@ from __future__ import annotations
import logging
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
from typing import (
TYPE_CHECKING,
TypeVar,
)
from .exceptions import KasaException
from .feature import Feature
@@ -14,6 +17,8 @@ if TYPE_CHECKING:
_LOGGER = logging.getLogger(__name__)
ModuleT = TypeVar("ModuleT", bound="Module")
class Module(ABC):
"""Base class implemention for all modules.