mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-14 19:38:02 +00:00
Merge remote-tracking branch 'upstream/master' into feat/device_update
This commit is contained in:
@@ -5,6 +5,9 @@ from .antitheft import Antitheft
|
||||
from .cloud import Cloud
|
||||
from .countdown import Countdown
|
||||
from .emeter import Emeter
|
||||
from .led import Led
|
||||
from .light import Light
|
||||
from .lighteffect import LightEffect
|
||||
from .motion import Motion
|
||||
from .rulemodule import Rule, RuleModule
|
||||
from .schedule import Schedule
|
||||
@@ -17,6 +20,9 @@ __all__ = [
|
||||
"Cloud",
|
||||
"Countdown",
|
||||
"Emeter",
|
||||
"Led",
|
||||
"Light",
|
||||
"LightEffect",
|
||||
"Motion",
|
||||
"Rule",
|
||||
"RuleModule",
|
||||
|
@@ -4,19 +4,22 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import date
|
||||
from typing import Optional
|
||||
from typing import Callable, Coroutine, Optional
|
||||
|
||||
from pydantic.v1 import BaseModel, Field, validator
|
||||
|
||||
from ...feature import Feature
|
||||
from ...firmware import (
|
||||
from ...interfaces.firmware import (
|
||||
Firmware,
|
||||
UpdateResult,
|
||||
)
|
||||
from ...firmware import (
|
||||
FirmwareUpdate as FirmwareUpdateInterface,
|
||||
from ...interfaces.firmware import (
|
||||
FirmwareDownloadState as FirmwareDownloadStateInterface,
|
||||
)
|
||||
from ..iotmodule import IotModule, merge
|
||||
from ...interfaces.firmware import (
|
||||
FirmwareUpdateInfo as FirmwareUpdateInfoInterface,
|
||||
)
|
||||
from ..iotmodule import IotModule
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -89,8 +92,11 @@ class Cloud(IotModule, Firmware):
|
||||
|
||||
# TODO: this is problematic, as it will fail the whole query on some
|
||||
# devices if they are not connected to the internet
|
||||
if self._module in self._device._last_update and self.is_connected:
|
||||
req = merge(req, self.get_available_firmwares())
|
||||
|
||||
# The following causes a recursion error as self.is_connected
|
||||
# accesses self.data which calls query. Also get_available_firmwares is async
|
||||
# if self._module in self._device._last_update and self.is_connected:
|
||||
# req = merge(req, self.get_available_firmwares())
|
||||
|
||||
return req
|
||||
|
||||
@@ -130,7 +136,12 @@ class Cloud(IotModule, Firmware):
|
||||
"""Disconnect from the cloud."""
|
||||
return await self.call("unbind")
|
||||
|
||||
async def update_firmware(self, *, progress_cb=None) -> UpdateResult:
|
||||
async def update_firmware(
|
||||
self,
|
||||
*,
|
||||
progress_cb: Callable[[FirmwareDownloadStateInterface], Coroutine]
|
||||
| None = None,
|
||||
) -> UpdateResult:
|
||||
"""Perform firmware update."""
|
||||
raise NotImplementedError
|
||||
i = 0
|
||||
@@ -144,11 +155,16 @@ class Cloud(IotModule, Firmware):
|
||||
|
||||
return UpdateResult("")
|
||||
|
||||
async def check_for_updates(self) -> FirmwareUpdateInterface:
|
||||
async def check_for_updates(self) -> FirmwareUpdateInfoInterface:
|
||||
"""Return firmware update information."""
|
||||
# TODO: naming of the common firmware API methods
|
||||
raise NotImplementedError
|
||||
|
||||
async def get_update_state(self) -> FirmwareUpdateInfoInterface:
|
||||
"""Return firmware update information."""
|
||||
fw = await self.get_firmware_update()
|
||||
|
||||
return FirmwareUpdateInterface(
|
||||
return FirmwareUpdateInfoInterface(
|
||||
update_available=fw.update_available,
|
||||
current_version=self._device.hw_info.get("sw_ver"),
|
||||
available_version=fw.version,
|
||||
|
32
kasa/iot/modules/led.py
Normal file
32
kasa/iot/modules/led.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""Module for led controls."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ...interfaces.led import Led as LedInterface
|
||||
from ..iotmodule import IotModule
|
||||
|
||||
|
||||
class Led(IotModule, LedInterface):
|
||||
"""Implementation of led controls."""
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {}
|
||||
|
||||
@property
|
||||
def mode(self):
|
||||
"""LED mode setting.
|
||||
|
||||
"always", "never"
|
||||
"""
|
||||
return "always" if self.led else "never"
|
||||
|
||||
@property
|
||||
def led(self) -> bool:
|
||||
"""Return the state of the led."""
|
||||
sys_info = self.data
|
||||
return bool(1 - sys_info["led_off"])
|
||||
|
||||
async def set_led(self, state: bool):
|
||||
"""Set the state of the led (night mode)."""
|
||||
return await self.call("set_led_off", {"off": int(not state)})
|
200
kasa/iot/modules/light.py
Normal file
200
kasa/iot/modules/light.py
Normal file
@@ -0,0 +1,200 @@
|
||||
"""Implementation of brightness module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, cast
|
||||
|
||||
from ...device_type import DeviceType
|
||||
from ...exceptions import KasaException
|
||||
from ...feature import Feature
|
||||
from ...interfaces.light import HSV, ColorTempRange
|
||||
from ...interfaces.light import Light as LightInterface
|
||||
from ..iotmodule import IotModule
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..iotbulb import IotBulb
|
||||
from ..iotdimmer import IotDimmer
|
||||
|
||||
|
||||
BRIGHTNESS_MIN = 0
|
||||
BRIGHTNESS_MAX = 100
|
||||
|
||||
|
||||
class Light(IotModule, LightInterface):
|
||||
"""Implementation of brightness module."""
|
||||
|
||||
_device: IotBulb | IotDimmer
|
||||
|
||||
def _initialize_features(self):
|
||||
"""Initialize features."""
|
||||
super()._initialize_features()
|
||||
device = self._device
|
||||
|
||||
if self._device.is_dimmable:
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device,
|
||||
id="brightness",
|
||||
name="Brightness",
|
||||
container=self,
|
||||
attribute_getter="brightness",
|
||||
attribute_setter="set_brightness",
|
||||
minimum_value=BRIGHTNESS_MIN,
|
||||
maximum_value=BRIGHTNESS_MAX,
|
||||
type=Feature.Type.Number,
|
||||
category=Feature.Category.Primary,
|
||||
)
|
||||
)
|
||||
if self._device.is_variable_color_temp:
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=device,
|
||||
id="color_temperature",
|
||||
name="Color temperature",
|
||||
container=self,
|
||||
attribute_getter="color_temp",
|
||||
attribute_setter="set_color_temp",
|
||||
range_getter="valid_temperature_range",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.Number,
|
||||
)
|
||||
)
|
||||
if self._device.is_color:
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device=device,
|
||||
id="hsv",
|
||||
name="HSV",
|
||||
container=self,
|
||||
attribute_getter="hsv",
|
||||
attribute_setter="set_hsv",
|
||||
# TODO proper type for setting hsv
|
||||
type=Feature.Type.Unknown,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
# Brightness is contained in the main device info response.
|
||||
return {}
|
||||
|
||||
def _get_bulb_device(self) -> IotBulb | None:
|
||||
"""For type checker this gets an IotBulb.
|
||||
|
||||
IotDimmer is not a subclass of IotBulb and using isinstance
|
||||
here at runtime would create a circular import.
|
||||
"""
|
||||
if self._device.device_type in {DeviceType.Bulb, DeviceType.LightStrip}:
|
||||
return cast("IotBulb", self._device)
|
||||
return None
|
||||
|
||||
@property # type: ignore
|
||||
def is_dimmable(self) -> int:
|
||||
"""Whether the bulb supports brightness changes."""
|
||||
return self._device._is_dimmable
|
||||
|
||||
@property # type: ignore
|
||||
def brightness(self) -> int:
|
||||
"""Return the current brightness in percentage."""
|
||||
return self._device.brightness
|
||||
|
||||
async def set_brightness(
|
||||
self, brightness: int, *, transition: int | None = None
|
||||
) -> dict:
|
||||
"""Set the brightness in percentage.
|
||||
|
||||
:param int brightness: brightness in percent
|
||||
:param int transition: transition in milliseconds.
|
||||
"""
|
||||
return await self._device.set_brightness(brightness, transition=transition)
|
||||
|
||||
@property
|
||||
def is_color(self) -> bool:
|
||||
"""Whether the light supports color changes."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb._is_color
|
||||
|
||||
@property
|
||||
def is_variable_color_temp(self) -> bool:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb._is_variable_color_temp
|
||||
|
||||
@property
|
||||
def has_effects(self) -> bool:
|
||||
"""Return True if the device supports effects."""
|
||||
if (bulb := self._get_bulb_device()) is None:
|
||||
return False
|
||||
return bulb.has_effects
|
||||
|
||||
@property
|
||||
def hsv(self) -> HSV:
|
||||
"""Return the current HSV state of the bulb.
|
||||
|
||||
:return: hue, saturation and value (degrees, %, %)
|
||||
"""
|
||||
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
|
||||
raise KasaException("Light does not support color.")
|
||||
return bulb.hsv
|
||||
|
||||
async def set_hsv(
|
||||
self,
|
||||
hue: int,
|
||||
saturation: int,
|
||||
value: int | None = None,
|
||||
*,
|
||||
transition: int | None = None,
|
||||
) -> dict:
|
||||
"""Set new HSV.
|
||||
|
||||
Note, transition is not supported and will be ignored.
|
||||
|
||||
:param int hue: hue in degrees
|
||||
:param int saturation: saturation in percentage [0,100]
|
||||
:param int value: value in percentage [0, 100]
|
||||
:param int transition: transition in milliseconds.
|
||||
"""
|
||||
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
|
||||
raise KasaException("Light does not support color.")
|
||||
return await bulb.set_hsv(hue, saturation, value, transition=transition)
|
||||
|
||||
@property
|
||||
def valid_temperature_range(self) -> ColorTempRange:
|
||||
"""Return the device-specific white temperature range (in Kelvin).
|
||||
|
||||
:return: White temperature range in Kelvin (minimum, maximum)
|
||||
"""
|
||||
if (
|
||||
bulb := self._get_bulb_device()
|
||||
) is None or not bulb._is_variable_color_temp:
|
||||
raise KasaException("Light does not support colortemp.")
|
||||
return bulb.valid_temperature_range
|
||||
|
||||
@property
|
||||
def color_temp(self) -> int:
|
||||
"""Whether the bulb supports color temperature changes."""
|
||||
if (
|
||||
bulb := self._get_bulb_device()
|
||||
) is None or not bulb._is_variable_color_temp:
|
||||
raise KasaException("Light does not support colortemp.")
|
||||
return bulb.color_temp
|
||||
|
||||
async def set_color_temp(
|
||||
self, temp: int, *, brightness=None, transition: int | None = None
|
||||
) -> dict:
|
||||
"""Set the color temperature of the device in kelvin.
|
||||
|
||||
Note, transition is not supported and will be ignored.
|
||||
|
||||
:param int temp: The new color temperature, in Kelvin
|
||||
:param int transition: transition in milliseconds.
|
||||
"""
|
||||
if (
|
||||
bulb := self._get_bulb_device()
|
||||
) is None or not bulb._is_variable_color_temp:
|
||||
raise KasaException("Light does not support colortemp.")
|
||||
return await bulb.set_color_temp(
|
||||
temp, brightness=brightness, transition=transition
|
||||
)
|
97
kasa/iot/modules/lighteffect.py
Normal file
97
kasa/iot/modules/lighteffect.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""Module for light effects."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ...interfaces.lighteffect import LightEffect as LightEffectInterface
|
||||
from ..effects import EFFECT_MAPPING_V1, EFFECT_NAMES_V1
|
||||
from ..iotmodule import IotModule
|
||||
|
||||
|
||||
class LightEffect(IotModule, LightEffectInterface):
|
||||
"""Implementation of dynamic light effects."""
|
||||
|
||||
@property
|
||||
def effect(self) -> str:
|
||||
"""Return effect state.
|
||||
|
||||
Example:
|
||||
{'brightness': 50,
|
||||
'custom': 0,
|
||||
'enable': 0,
|
||||
'id': '',
|
||||
'name': ''}
|
||||
"""
|
||||
if (
|
||||
(state := self.data.get("lighting_effect_state"))
|
||||
and state.get("enable")
|
||||
and (name := state.get("name"))
|
||||
and name in EFFECT_NAMES_V1
|
||||
):
|
||||
return name
|
||||
return self.LIGHT_EFFECTS_OFF
|
||||
|
||||
@property
|
||||
def effect_list(self) -> list[str]:
|
||||
"""Return built-in effects list.
|
||||
|
||||
Example:
|
||||
['Aurora', 'Bubbling Cauldron', ...]
|
||||
"""
|
||||
effect_list = [self.LIGHT_EFFECTS_OFF]
|
||||
effect_list.extend(EFFECT_NAMES_V1)
|
||||
return effect_list
|
||||
|
||||
async def set_effect(
|
||||
self,
|
||||
effect: str,
|
||||
*,
|
||||
brightness: int | None = None,
|
||||
transition: int | None = None,
|
||||
) -> None:
|
||||
"""Set an effect on the device.
|
||||
|
||||
If brightness or transition is defined,
|
||||
its value will be used instead of the effect-specific default.
|
||||
|
||||
See :meth:`effect_list` for available effects,
|
||||
or use :meth:`set_custom_effect` for custom effects.
|
||||
|
||||
:param str effect: The effect to set
|
||||
:param int brightness: The wanted brightness
|
||||
:param int transition: The wanted transition time
|
||||
"""
|
||||
if effect == self.LIGHT_EFFECTS_OFF:
|
||||
effect_dict = dict(self.data["lighting_effect_state"])
|
||||
effect_dict["enable"] = 0
|
||||
elif effect not in EFFECT_MAPPING_V1:
|
||||
raise ValueError(f"The effect {effect} is not a built in effect.")
|
||||
else:
|
||||
effect_dict = EFFECT_MAPPING_V1[effect]
|
||||
if brightness is not None:
|
||||
effect_dict["brightness"] = brightness
|
||||
if transition is not None:
|
||||
effect_dict["transition"] = transition
|
||||
|
||||
await self.set_custom_effect(effect_dict)
|
||||
|
||||
async def set_custom_effect(
|
||||
self,
|
||||
effect_dict: dict,
|
||||
) -> None:
|
||||
"""Set a custom effect on the device.
|
||||
|
||||
:param str effect_dict: The custom effect dict to set
|
||||
"""
|
||||
return await self.call(
|
||||
"set_lighting_effect",
|
||||
effect_dict,
|
||||
)
|
||||
|
||||
@property
|
||||
def has_custom_effects(self) -> bool:
|
||||
"""Return True if the device supports setting custom effects."""
|
||||
return True
|
||||
|
||||
def query(self):
|
||||
"""Return the base query."""
|
||||
return {}
|
Reference in New Issue
Block a user