2023-12-05 19:07:10 +00:00
|
|
|
"""Module for tapo-branded smart bulbs (L5**)."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
2023-12-05 19:07:10 +00:00
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from ..bulb import HSV, Bulb, BulbPreset, ColorTempRange
|
2024-02-21 15:52:55 +00:00
|
|
|
from ..exceptions import KasaException
|
2024-04-20 18:29:07 +00:00
|
|
|
from .modules import Brightness, ColorModule, ColorTemperatureModule
|
2024-02-04 15:20:08 +00:00
|
|
|
from .smartdevice import SmartDevice
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
AVAILABLE_EFFECTS = {
|
|
|
|
"L1": "Party",
|
|
|
|
"L2": "Relax",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class SmartBulb(SmartDevice, Bulb):
|
2023-12-05 19:07:10 +00:00
|
|
|
"""Representation of a TP-Link Tapo Bulb.
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
Documentation TBD. See :class:`~kasa.iot.Bulb` for now.
|
2023-12-05 19:07:10 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_color(self) -> bool:
|
|
|
|
"""Whether the bulb supports color changes."""
|
2024-04-20 15:18:35 +00:00
|
|
|
return "ColorModule" in self.modules
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_dimmable(self) -> bool:
|
|
|
|
"""Whether the bulb supports brightness changes."""
|
2024-04-17 11:33:10 +00:00
|
|
|
return "Brightness" in self.modules
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_variable_color_temp(self) -> bool:
|
|
|
|
"""Whether the bulb supports color temperature changes."""
|
2024-04-20 15:18:35 +00:00
|
|
|
return "ColorTemperatureModule" in self.modules
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def valid_temperature_range(self) -> ColorTempRange:
|
|
|
|
"""Return the device-specific white temperature range (in Kelvin).
|
|
|
|
|
|
|
|
:return: White temperature range in Kelvin (minimum, maximum)
|
|
|
|
"""
|
2024-01-03 18:31:42 +00:00
|
|
|
if not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Color temperature not supported")
|
2024-01-03 18:31:42 +00:00
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
return cast(
|
|
|
|
ColorTemperatureModule, self.modules["ColorTemperatureModule"]
|
|
|
|
).valid_temperature_range
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_effects(self) -> bool:
|
|
|
|
"""Return True if the device supports effects."""
|
|
|
|
return "dynamic_light_effect_enable" in self._info
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def effect(self) -> dict:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""Return effect state.
|
|
|
|
|
|
|
|
This follows the format used by SmartLightStrip.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
{'brightness': 50,
|
|
|
|
'custom': 0,
|
|
|
|
'enable': 0,
|
|
|
|
'id': '',
|
|
|
|
'name': ''}
|
|
|
|
"""
|
|
|
|
# If no effect is active, dynamic_light_effect_id does not appear in info
|
|
|
|
current_effect = self._info.get("dynamic_light_effect_id", "")
|
|
|
|
data = {
|
|
|
|
"brightness": self.brightness,
|
|
|
|
"enable": current_effect != "",
|
|
|
|
"id": current_effect,
|
|
|
|
"name": AVAILABLE_EFFECTS.get(current_effect, ""),
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def effect_list(self) -> list[str] | None:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""Return built-in effects list.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
['Party', 'Relax', ...]
|
|
|
|
"""
|
|
|
|
return list(AVAILABLE_EFFECTS.keys()) if self.has_effects else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hsv(self) -> HSV:
|
|
|
|
"""Return the current HSV state of the bulb.
|
|
|
|
|
|
|
|
:return: hue, saturation and value (degrees, %, %)
|
|
|
|
"""
|
|
|
|
if not self.is_color:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support color.")
|
2023-12-05 19:07:10 +00:00
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
return cast(ColorModule, self.modules["ColorModule"]).hsv
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self) -> int:
|
|
|
|
"""Whether the bulb supports color temperature changes."""
|
|
|
|
if not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support colortemp.")
|
2023-12-05 19:07:10 +00:00
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
return cast(
|
|
|
|
ColorTemperatureModule, self.modules["ColorTemperatureModule"]
|
|
|
|
).color_temp
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self) -> int:
|
|
|
|
"""Return the current brightness in percentage."""
|
|
|
|
if not self.is_dimmable: # pragma: no cover
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb is not dimmable.")
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
return self._info.get("brightness", -1)
|
|
|
|
|
|
|
|
async def set_hsv(
|
|
|
|
self,
|
|
|
|
hue: int,
|
|
|
|
saturation: int,
|
2024-04-17 13:39:24 +00:00
|
|
|
value: int | None = None,
|
2023-12-05 19:07:10 +00:00
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
transition: int | None = None,
|
|
|
|
) -> dict:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""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]
|
2024-04-20 15:18:35 +00:00
|
|
|
:param int value: value between 1 and 100
|
2023-12-05 19:07:10 +00:00
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
|
|
|
if not self.is_color:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support color.")
|
2023-12-05 19:07:10 +00:00
|
|
|
|
2024-04-20 15:18:35 +00:00
|
|
|
return await cast(ColorModule, self.modules["ColorModule"]).set_hsv(
|
|
|
|
hue, saturation, value
|
|
|
|
)
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
async def set_color_temp(
|
2024-04-17 13:39:24 +00:00
|
|
|
self, temp: int, *, brightness=None, transition: int | None = None
|
|
|
|
) -> dict:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""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 not self.is_variable_color_temp:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb does not support colortemp.")
|
2024-04-20 15:18:35 +00:00
|
|
|
return await cast(
|
|
|
|
ColorTemperatureModule, self.modules["ColorTemperatureModule"]
|
|
|
|
).set_color_temp(temp)
|
2023-12-05 19:07:10 +00:00
|
|
|
|
|
|
|
async def set_brightness(
|
2024-04-17 13:39:24 +00:00
|
|
|
self, brightness: int, *, transition: int | None = None
|
|
|
|
) -> dict:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""Set the brightness in percentage.
|
|
|
|
|
|
|
|
Note, transition is not supported and will be ignored.
|
|
|
|
|
|
|
|
:param int brightness: brightness in percent
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
|
|
|
if not self.is_dimmable: # pragma: no cover
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Bulb is not dimmable.")
|
2023-12-05 19:07:10 +00:00
|
|
|
|
2024-04-20 18:29:07 +00:00
|
|
|
return await cast(Brightness, self.modules["Brightness"]).set_brightness(
|
|
|
|
brightness
|
2023-12-05 19:07:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def set_effect(
|
|
|
|
self,
|
|
|
|
effect: str,
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
brightness: int | None = None,
|
|
|
|
transition: int | None = None,
|
2023-12-05 19:07:10 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set an effect on the device."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def presets(self) -> list[BulbPreset]:
|
2023-12-05 19:07:10 +00:00
|
|
|
"""Return a list of available bulb setting presets."""
|
|
|
|
return []
|