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
|
|
|
|
|
|
|
|
from .device import Device
|
2024-02-21 15:52:55 +00:00
|
|
|
from .exceptions import KasaException
|
2024-02-19 17:01:31 +00:00
|
|
|
from .feature import Feature
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
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-04-17 13:39:24 +00:00
|
|
|
def __init__(self, device: Device, 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."""
|
|
|
|
|
|
|
|
def _add_feature(self, feature: Feature):
|
|
|
|
"""Add module feature."""
|
2024-03-06 18:04:09 +00:00
|
|
|
|
|
|
|
def _slugified_name(name):
|
|
|
|
return name.lower().replace(" ", "_").replace("'", "_")
|
|
|
|
|
|
|
|
feat_name = _slugified_name(feature.name)
|
2024-02-19 17:01:31 +00:00
|
|
|
if feat_name in self._module_features:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Duplicate name detected %s" % feat_name)
|
2024-02-19 17:01:31 +00:00
|
|
|
self._module_features[feat_name] = feature
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return (
|
|
|
|
f"<Module {self.__class__.__name__} ({self._module})"
|
|
|
|
f" for {self._device.host}>"
|
|
|
|
)
|