2024-02-19 17:01:31 +00:00
|
|
|
"""Base class for all module implementations."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
import logging
|
|
|
|
from abc import ABC, abstractmethod
|
2024-05-03 15:01:21 +00:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
2024-05-10 18:29:28 +00:00
|
|
|
Final,
|
2024-05-03 15:01:21 +00:00
|
|
|
TypeVar,
|
|
|
|
)
|
2024-02-19 17:01:31 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
from .exceptions import KasaException
|
2024-02-19 17:01:31 +00:00
|
|
|
from .feature import Feature
|
2024-05-10 18:29:28 +00:00
|
|
|
from .modulemapping import ModuleName
|
2024-02-19 17:01:31 +00:00
|
|
|
|
2024-04-29 16:34:20 +00:00
|
|
|
if TYPE_CHECKING:
|
2024-05-10 18:29:28 +00:00
|
|
|
from .device import Device as DeviceType # avoid name clash with Device module
|
|
|
|
from .interfaces.led import Led
|
|
|
|
from .interfaces.lighteffect import LightEffect
|
|
|
|
from .iot import modules as iot
|
|
|
|
from .smart import modules as smart
|
2024-04-29 16:34:20 +00:00
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-05-03 15:01:21 +00:00
|
|
|
ModuleT = TypeVar("ModuleT", bound="Module")
|
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
class Module(ABC):
|
|
|
|
"""Base class implemention for all modules.
|
|
|
|
|
|
|
|
The base classes should implement `query` to return the query they want to be
|
|
|
|
executed during the regular update cycle.
|
|
|
|
"""
|
|
|
|
|
2024-05-10 18:29:28 +00:00
|
|
|
# Common Modules
|
|
|
|
LightEffect: Final[ModuleName[LightEffect]] = ModuleName("LightEffectModule")
|
|
|
|
Led: Final[ModuleName[Led]] = ModuleName("LedModule")
|
|
|
|
|
|
|
|
# IOT only Modules
|
|
|
|
IotAmbientLight: Final[ModuleName[iot.AmbientLight]] = ModuleName("ambient")
|
|
|
|
IotAntitheft: Final[ModuleName[iot.Antitheft]] = ModuleName("anti_theft")
|
|
|
|
IotCountdown: Final[ModuleName[iot.Countdown]] = ModuleName("countdown")
|
|
|
|
IotEmeter: Final[ModuleName[iot.Emeter]] = ModuleName("emeter")
|
|
|
|
IotMotion: Final[ModuleName[iot.Motion]] = ModuleName("motion")
|
|
|
|
IotSchedule: Final[ModuleName[iot.Schedule]] = ModuleName("schedule")
|
|
|
|
IotUsage: Final[ModuleName[iot.Usage]] = ModuleName("usage")
|
|
|
|
IotCloud: Final[ModuleName[iot.Cloud]] = ModuleName("cloud")
|
|
|
|
IotTime: Final[ModuleName[iot.Time]] = ModuleName("time")
|
|
|
|
|
|
|
|
# SMART only Modules
|
|
|
|
Alarm: Final[ModuleName[smart.AlarmModule]] = ModuleName("AlarmModule")
|
|
|
|
AutoOff: Final[ModuleName[smart.AutoOffModule]] = ModuleName("AutoOffModule")
|
|
|
|
BatterySensor: Final[ModuleName[smart.BatterySensor]] = ModuleName("BatterySensor")
|
|
|
|
Brightness: Final[ModuleName[smart.Brightness]] = ModuleName("Brightness")
|
|
|
|
ChildDevice: Final[ModuleName[smart.ChildDeviceModule]] = ModuleName(
|
|
|
|
"ChildDeviceModule"
|
|
|
|
)
|
|
|
|
Cloud: Final[ModuleName[smart.CloudModule]] = ModuleName("CloudModule")
|
|
|
|
Color: Final[ModuleName[smart.ColorModule]] = ModuleName("ColorModule")
|
|
|
|
ColorTemp: Final[ModuleName[smart.ColorTemperatureModule]] = ModuleName(
|
|
|
|
"ColorTemperatureModule"
|
|
|
|
)
|
|
|
|
ContactSensor: Final[ModuleName[smart.ContactSensor]] = ModuleName("ContactSensor")
|
|
|
|
Device: Final[ModuleName[smart.DeviceModule]] = ModuleName("DeviceModule")
|
|
|
|
Energy: Final[ModuleName[smart.EnergyModule]] = ModuleName("EnergyModule")
|
|
|
|
Fan: Final[ModuleName[smart.FanModule]] = ModuleName("FanModule")
|
|
|
|
Firmware: Final[ModuleName[smart.Firmware]] = ModuleName("Firmware")
|
|
|
|
FrostProtection: Final[ModuleName[smart.FrostProtectionModule]] = ModuleName(
|
|
|
|
"FrostProtectionModule"
|
|
|
|
)
|
|
|
|
Humidity: Final[ModuleName[smart.HumiditySensor]] = ModuleName("HumiditySensor")
|
|
|
|
LightTransition: Final[ModuleName[smart.LightTransitionModule]] = ModuleName(
|
|
|
|
"LightTransitionModule"
|
|
|
|
)
|
|
|
|
Report: Final[ModuleName[smart.ReportModule]] = ModuleName("ReportModule")
|
|
|
|
Temperature: Final[ModuleName[smart.TemperatureSensor]] = ModuleName(
|
|
|
|
"TemperatureSensor"
|
|
|
|
)
|
|
|
|
TemperatureSensor: Final[ModuleName[smart.TemperatureControl]] = ModuleName(
|
|
|
|
"TemperatureControl"
|
|
|
|
)
|
|
|
|
Time: Final[ModuleName[smart.TimeModule]] = ModuleName("TimeModule")
|
|
|
|
WaterleakSensor: Final[ModuleName[smart.WaterleakSensor]] = ModuleName(
|
|
|
|
"WaterleakSensor"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, device: DeviceType, module: str):
|
2024-02-19 17:01:31 +00:00
|
|
|
self._device = device
|
|
|
|
self._module = module
|
2024-04-17 13:39:24 +00:00
|
|
|
self._module_features: dict[str, Feature] = {}
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def query(self):
|
|
|
|
"""Query to execute during the update cycle.
|
|
|
|
|
|
|
|
The inheriting modules implement this to include their wanted
|
|
|
|
queries to the query that gets executed when Device.update() gets called.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def data(self):
|
|
|
|
"""Return the module specific raw data from the last update."""
|
|
|
|
|
2024-04-30 06:56:09 +00:00
|
|
|
def _initialize_features(self): # noqa: B027
|
|
|
|
"""Initialize features after the initial update.
|
|
|
|
|
|
|
|
This can be implemented if features depend on module query responses.
|
|
|
|
"""
|
|
|
|
|
2024-02-19 17:01:31 +00:00
|
|
|
def _add_feature(self, feature: Feature):
|
|
|
|
"""Add module feature."""
|
2024-05-07 09:13:35 +00:00
|
|
|
id_ = feature.id
|
|
|
|
if id_ in self._module_features:
|
|
|
|
raise KasaException("Duplicate id detected %s" % id_)
|
|
|
|
self._module_features[id_] = feature
|
2024-02-19 17:01:31 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return (
|
|
|
|
f"<Module {self.__class__.__name__} ({self._module})"
|
|
|
|
f" for {self._device.host}>"
|
|
|
|
)
|