8000 Make module names consistent and remove redundant module casting by sdb9696 · Pull Request #909 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Make module names consistent and remove redundant module casting #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions kasa/iot/iotbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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
Expand Down Expand Up @@ -198,13 +199,17 @@ def __init__(
) -> 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()
Expand Down
28 changes: 13 additions & 15 deletions kasa/iot/iotdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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__)

Expand Down Expand Up @@ -347,7 +347,7 @@
_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
Expand Down Expand Up @@ -440,27 +440,27 @@
@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
Expand Down Expand Up @@ -541,26 +541,26 @@
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
Expand All @@ -574,7 +574,7 @@
: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
)

Expand All @@ -589,15 +589,13 @@
: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()

Check warning on line 598 in kasa/iot/iotdevice.py

View check run for this annotation

Codecov / codecov/patch

kasa/iot/iotdevice.py#L598

Added line #L598 was not covered by tests

@requires_update
async def current_consumption(self) -> float:
Expand Down
5 changes: 3 additions & 2 deletions kasa/iot/iotdimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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
Expand Down Expand Up @@ -81,8 +82,8 @@ def __init__(
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()
Expand Down
4 changes: 2 additions & 2 deletions kasa/iot/iotlightstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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):
Expand Down Expand Up @@ -58,7 +58,7 @@ def __init__(
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
Expand Down
14 changes: 7 additions & 7 deletions kasa/iot/iotplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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__)

Expand Down Expand Up @@ -53,12 +53,12 @@ def __init__(
) -> 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
Expand Down
11 changes: 6 additions & 5 deletions kasa/iot/iotstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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,
Expand Down Expand Up @@ -95,11 +96,11 @@ def __init__(
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
Expand Down
6 changes: 4 additions & 2 deletions kasa/iot/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
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
Expand All @@ -18,7 +19,8 @@
"Cloud",
"Countdown",
"Emeter",
"LedModule",
"Led",
"LightEffect",
"Motion",
"Rule",
"RuleModule",
Expand Down
4 changes: 2 additions & 2 deletions kasa/iot/modules/ledmodule.py → kasa/iot/modules/led.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions kasa/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .modulemapping import ModuleName

if TYPE_CHECKING:
from .device import Device as DeviceType # avoid name clash with Device module
from .device import Device
from .interfaces.led import Led
from .interfaces.lighteffect import LightEffect
from .iot import modules as iot
Expand All @@ -34,8 +34,8 @@ class Module(ABC):
"""

# Common Modules
LightEffect: Final[ModuleName[LightEffect]] = ModuleName("LightEffectModule")
Led: Final[ModuleName[Led]] = ModuleName("LedModule")
LightEffect: Final[ModuleName[LightEffect]] = ModuleName("LightEffect")
Led: Final[ModuleName[Led]] = ModuleName("Led")

# IOT only Modules
IotAmbientLight: Final[ModuleName[iot.AmbientLight]] = ModuleName("ambient")
Expand All @@ -49,43 +49,43 @@ class Module(ABC):
IotTime: Final[ModuleName[iot.Time]] = ModuleName("time")

# SMART only Modules
Alarm: Final[ModuleName[smart.AlarmModule]] = ModuleName("AlarmModule")
AutoOff: Final[ModuleName[smart.AutoOffModule]] = ModuleName("AutoOffModule")
Alarm: Final[ModuleName[smart.Alarm]] = ModuleName("Alarm")
AutoOff: Final[ModuleName[smart.AutoOff]] = ModuleName("AutoOff")
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"
ChildDevice: Final[ModuleName[smart.ChildDevice]] = ModuleName("ChildDevice")
Cloud: Final[ModuleName[smart.Cloud]] = ModuleName("Cloud")
Color: Final[ModuleName[smart.Color]] = ModuleName("Color")
ColorTemperature: Final[ModuleName[smart.ColorTemperature]] = ModuleName(
"ColorTemperature"
)
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")
DeviceModule: Final[ModuleName[smart.DeviceModule]] = ModuleName("DeviceModule")
Energy: Final[ModuleName[smart.Energy]] = ModuleName("Energy")
Fan: Final[ModuleName[smart.Fan]] = ModuleName("Fan")
Firmware: Final[ModuleName[smart.Firmware]] = ModuleName("Firmware")
FrostProtection: Final[ModuleName[smart.FrostProtectionModule]] = ModuleName(
"FrostProtectionModule"
FrostProtection: Final[ModuleName[smart.FrostProtection]] = ModuleName(
"FrostProtection"
)
HumiditySensor: Final[ModuleName[smart.HumiditySensor]] = ModuleName(
"HumiditySensor"
)
Humidity: Final[ModuleName[smart.HumiditySensor]] = ModuleName("HumiditySensor")
LightTransition: Final[ModuleName[smart.LightTransitionModule]] = ModuleName(
"LightTransitionModule"
LightTransition: Final[ModuleName[smart.LightTransition]] = ModuleName(
"LightTransition"
)
Report: Final[ModuleName[smart.ReportModule]] = ModuleName("ReportModule")
Temperature: Final[ModuleName[smart.TemperatureSensor]] = ModuleName(
ReportMode: Final[ModuleName[smart.ReportMode]] = ModuleName("ReportMode")
TemperatureSensor: Final[ModuleName[smart.TemperatureSensor]] = ModuleName(
"TemperatureSensor"
)
TemperatureSensor: Final[ModuleName[smart.TemperatureControl]] = ModuleName(
TemperatureControl: Final[ModuleName[smart.TemperatureControl]] = ModuleName(
"TemperatureControl"
)
Time: Final[ModuleName[smart.TimeModule]] = ModuleName("TimeModule")
Time: Final[ModuleName[smart.Time]] = ModuleName("Time")
WaterleakSensor: Final[ModuleName[smart.WaterleakSensor]] = ModuleName(
"WaterleakSensor"
)

def __init__(self, device: DeviceType, module: str):
def __init__(self, device: Device, module: str):
self._device = device
self._module = module
self._module_features: dict[str, Feature] = {}
Expand Down
Loading
Loading
0