Make module names consistent and remove redundant module casting (#909)

Address the inconsistent naming of smart modules by removing all "Module" suffixes and aligning filenames with class names. Removes the casting of modules to the correct module type now that is is redundant. Update the adding of iot modules to use the ModuleName class rather than a free string.
This commit is contained in:
Steven B
2024-05-11 19:28:18 +01:00
committed by GitHub
parent 9473d97ad2
commit f259a8f162
34 changed files with 162 additions and 171 deletions

View File

@@ -13,6 +13,7 @@ from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..feature import Feature
from ..module import Module
from ..protocol import BaseProtocol
from .iotdevice import IotDevice, KasaException, requires_update
from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage
@@ -198,13 +199,17 @@ class IotBulb(IotDevice, Bulb):
) -> None:
super().__init__(host=host, config=config, protocol=protocol)
self._device_type = DeviceType.Bulb
self.add_module("schedule", Schedule(self, "smartlife.iot.common.schedule"))
self.add_module("usage", Usage(self, "smartlife.iot.common.schedule"))
self.add_module("antitheft", Antitheft(self, "smartlife.iot.common.anti_theft"))
self.add_module("time", Time(self, "smartlife.iot.common.timesetting"))
self.add_module("emeter", Emeter(self, self.emeter_type))
self.add_module("countdown", Countdown(self, "countdown"))
self.add_module("cloud", Cloud(self, "smartlife.iot.common.cloud"))
self.add_module(
Module.IotSchedule, Schedule(self, "smartlife.iot.common.schedule")
)
self.add_module(Module.IotUsage, Usage(self, "smartlife.iot.common.schedule"))
self.add_module(
Module.IotAntitheft, Antitheft(self, "smartlife.iot.common.anti_theft")
)
self.add_module(Module.IotTime, Time(self, "smartlife.iot.common.timesetting"))
self.add_module(Module.IotEmeter, Emeter(self, self.emeter_type))
self.add_module(Module.IotCountdown, Countdown(self, "countdown"))
self.add_module(Module.IotCloud, Cloud(self, "smartlife.iot.common.cloud"))
async def _initialize_features(self):
await super()._initialize_features()

View File

@@ -30,7 +30,7 @@ from ..module import Module
from ..modulemapping import ModuleMapping, ModuleName
from ..protocol import BaseProtocol
from .iotmodule import IotModule
from .modules import Emeter, Time
from .modules import Emeter
_LOGGER = logging.getLogger(__name__)
@@ -347,7 +347,7 @@ class IotDevice(Device):
_LOGGER.debug(
"The device has emeter, querying its information along sysinfo"
)
self.add_module("emeter", Emeter(self, self.emeter_type))
self.add_module(Module.IotEmeter, Emeter(self, self.emeter_type))
# TODO: perhaps modules should not have unsupported modules,
# making separate handling for this unnecessary
@@ -440,27 +440,27 @@ class IotDevice(Device):
@requires_update
def time(self) -> datetime:
"""Return current time from the device."""
return cast(Time, self.modules["time"]).time
return self.modules[Module.IotTime].time
@property
@requires_update
def timezone(self) -> dict:
"""Return the current timezone."""
return cast(Time, self.modules["time"]).timezone
return self.modules[Module.IotTime].timezone
async def get_time(self) -> datetime | None:
"""Return current time from the device, if available."""
_LOGGER.warning(
"Use `time` property instead, this call will be removed in the future."
)
return await cast(Time, self.modules["time"]).get_time()
return await self.modules[Module.IotTime].get_time()
async def get_timezone(self) -> dict:
"""Return timezone information."""
_LOGGER.warning(
"Use `timezone` property instead, this call will be removed in the future."
)
return await cast(Time, self.modules["time"]).get_timezone()
return await self.modules[Module.IotTime].get_timezone()
@property # type: ignore
@requires_update
@@ -541,26 +541,26 @@ class IotDevice(Device):
def emeter_realtime(self) -> EmeterStatus:
"""Return current energy readings."""
self._verify_emeter()
return EmeterStatus(cast(Emeter, self.modules["emeter"]).realtime)
return EmeterStatus(self.modules[Module.IotEmeter].realtime)
async def get_emeter_realtime(self) -> EmeterStatus:
"""Retrieve current energy readings."""
self._verify_emeter()
return EmeterStatus(await cast(Emeter, self.modules["emeter"]).get_realtime())
return EmeterStatus(await self.modules[Module.IotEmeter].get_realtime())
@property
@requires_update
def emeter_today(self) -> float | None:
"""Return today's energy consumption in kWh."""
self._verify_emeter()
return cast(Emeter, self.modules["emeter"]).emeter_today
return self.modules[Module.IotEmeter].emeter_today
@property
@requires_update
def emeter_this_month(self) -> float | None:
"""Return this month's energy consumption in kWh."""
self._verify_emeter()
return cast(Emeter, self.modules["emeter"]).emeter_this_month
return self.modules[Module.IotEmeter].emeter_this_month
async def get_emeter_daily(
self, year: int | None = None, month: int | None = None, kwh: bool = True
@@ -574,7 +574,7 @@ class IotDevice(Device):
:return: mapping of day of month to value
"""
self._verify_emeter()
return await cast(Emeter, self.modules["emeter"]).get_daystat(
return await self.modules[Module.IotEmeter].get_daystat(
year=year, month=month, kwh=kwh
)
@@ -589,15 +589,13 @@ class IotDevice(Device):
:return: dict: mapping of month to value
"""
self._verify_emeter()
return await cast(Emeter, self.modules["emeter"]).get_monthstat(
year=year, kwh=kwh
)
return await self.modules[Module.IotEmeter].get_monthstat(year=year, kwh=kwh)
@requires_update
async def erase_emeter_stats(self) -> dict:
"""Erase energy meter statistics."""
self._verify_emeter()
return await cast(Emeter, self.modules["emeter"]).erase_stats()
return await self.modules[Module.IotEmeter].erase_stats()
@requires_update
async def current_consumption(self) -> float:

View File

@@ -8,6 +8,7 @@ from typing import Any
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..feature import Feature
from ..module import Module
from ..protocol import BaseProtocol
from .iotdevice import KasaException, requires_update
from .iotplug import IotPlug
@@ -81,8 +82,8 @@ class IotDimmer(IotPlug):
self._device_type = DeviceType.Dimmer
# TODO: need to be verified if it's okay to call these on HS220 w/o these
# TODO: need to be figured out what's the best approach to detect support
self.add_module("motion", Motion(self, "smartlife.iot.PIR"))
self.add_module("ambient", AmbientLight(self, "smartlife.iot.LAS"))
self.add_module(Module.IotMotion, Motion(self, "smartlife.iot.PIR"))
self.add_module(Module.IotAmbientLight, AmbientLight(self, "smartlife.iot.LAS"))
async def _initialize_features(self):
await super()._initialize_features()

View File

@@ -9,7 +9,7 @@ from ..protocol import BaseProtocol
from .effects import EFFECT_NAMES_V1
from .iotbulb import IotBulb
from .iotdevice import KasaException, requires_update
from .modules.lighteffectmodule import LightEffectModule
from .modules.lighteffect import LightEffect
class IotLightStrip(IotBulb):
@@ -58,7 +58,7 @@ class IotLightStrip(IotBulb):
self._device_type = DeviceType.LightStrip
self.add_module(
Module.LightEffect,
LightEffectModule(self, "smartlife.iot.lighting_effect"),
LightEffect(self, "smartlife.iot.lighting_effect"),
)
@property # type: ignore

View File

@@ -9,7 +9,7 @@ from ..deviceconfig import DeviceConfig
from ..module import Module
from ..protocol import BaseProtocol
from .iotdevice import IotDevice, requires_update
from .modules import Antitheft, Cloud, LedModule, Schedule, Time, Usage
from .modules import Antitheft, Cloud, Led, Schedule, Time, Usage
_LOGGER = logging.getLogger(__name__)
@@ -53,12 +53,12 @@ class IotPlug(IotDevice):
) -> None:
super().__init__(host=host, config=config, protocol=protocol)
self._device_type = DeviceType.Plug
self.add_module("schedule", Schedule(self, "schedule"))
self.add_module("usage", Usage(self, "schedule"))
self.add_module("antitheft", Antitheft(self, "anti_theft"))
self.add_module("time", Time(self, "time"))
self.add_module("cloud", Cloud(self, "cnCloud"))
self.add_module(Module.Led, LedModule(self, "system"))
self.add_module(Module.IotSchedule, Schedule(self, "schedule"))
self.add_module(Module.IotUsage, Usage(self, "schedule"))
self.add_module(Module.IotAntitheft, Antitheft(self, "anti_theft"))
self.add_module(Module.IotTime, Time(self, "time"))
self.add_module(Module.IotCloud, Cloud(self, "cnCloud"))
self.add_module(Module.Led, Led(self, "system"))
@property # type: ignore
@requires_update

View File

@@ -10,6 +10,7 @@ from typing import Any
from ..device_type import DeviceType
from ..deviceconfig import DeviceConfig
from ..exceptions import KasaException
from ..module import Module
from ..protocol import BaseProtocol
from .iotdevice import (
EmeterStatus,
@@ -95,11 +96,11 @@ class IotStrip(IotDevice):
super().__init__(host=host, config=config, protocol=protocol)
self.emeter_type = "emeter"
self._device_type = DeviceType.Strip
self.add_module("antitheft", Antitheft(self, "anti_theft"))
self.add_module("schedule", Schedule(self, "schedule"))
self.add_module("usage", Usage(self, "schedule"))
self.add_module("time", Time(self, "time"))
self.add_module("countdown", Countdown(self, "countdown"))
self.add_module(Module.IotAntitheft, Antitheft(self, "anti_theft"))
self.add_module(Module.IotSchedule, Schedule(self, "schedule"))
self.add_module(Module.IotUsage, Usage(self, "schedule"))
self.add_module(Module.IotTime, Time(self, "time"))
self.add_module(Module.IotCountdown, Countdown(self, "countdown"))
@property # type: ignore
@requires_update

View File

@@ -5,7 +5,8 @@ from .antitheft import Antitheft
from .cloud import Cloud
from .countdown import Countdown
from .emeter import Emeter
from .ledmodule import LedModule
from .led import Led
from .lighteffect import LightEffect
from .motion import Motion
from .rulemodule import Rule, RuleModule
from .schedule import Schedule
@@ -18,7 +19,8 @@ __all__ = [
"Cloud",
"Countdown",
"Emeter",
"LedModule",
"Led",
"LightEffect",
"Motion",
"Rule",
"RuleModule",

View File

@@ -2,11 +2,11 @@
from __future__ import annotations
from ...interfaces.led import Led
from ...interfaces.led import Led as LedInterface
from ..iotmodule import IotModule
class LedModule(IotModule, Led):
class Led(IotModule, LedInterface):
"""Implementation of led controls."""
def query(self) -> dict:

View File

@@ -2,12 +2,12 @@
from __future__ import annotations
from ...interfaces.lighteffect import LightEffect
from ...interfaces.lighteffect import LightEffect as LightEffectInterface
from ..effects import EFFECT_MAPPING_V1, EFFECT_NAMES_V1
from ..iotmodule import IotModule
class LightEffectModule(IotModule, LightEffect):
class LightEffect(IotModule, LightEffectInterface):
"""Implementation of dynamic light effects."""
@property