2024-06-19 08:53:40 +00:00
|
|
|
"""Interact with TPLink Smart Home devices.
|
|
|
|
|
|
|
|
Once you have a device via :ref:`Discovery <discover_target>` or
|
|
|
|
:ref:`Connect <connect_target>` you can start interacting with a device.
|
|
|
|
|
|
|
|
>>> from kasa import Discover
|
|
|
|
>>>
|
|
|
|
>>> dev = await Discover.discover_single(
|
|
|
|
>>> "127.0.0.2",
|
|
|
|
>>> username="user@example.com",
|
|
|
|
>>> password="great_password"
|
|
|
|
>>> )
|
|
|
|
>>>
|
|
|
|
|
|
|
|
Most devices can be turned on and off
|
|
|
|
|
|
|
|
>>> await dev.turn_on()
|
|
|
|
>>> await dev.update()
|
|
|
|
>>> print(dev.is_on)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> await dev.turn_off()
|
|
|
|
>>> await dev.update()
|
|
|
|
>>> print(dev.is_on)
|
|
|
|
False
|
|
|
|
|
|
|
|
All devices provide several informational properties:
|
|
|
|
|
|
|
|
>>> dev.alias
|
|
|
|
Bedroom Lamp Plug
|
|
|
|
>>> dev.model
|
|
|
|
HS110(EU)
|
|
|
|
>>> dev.rssi
|
|
|
|
-71
|
|
|
|
>>> dev.mac
|
|
|
|
50:C7:BF:00:00:00
|
|
|
|
|
|
|
|
Some information can also be changed programmatically:
|
|
|
|
|
|
|
|
>>> await dev.set_alias("new alias")
|
|
|
|
>>> await dev.update()
|
|
|
|
>>> dev.alias
|
|
|
|
new alias
|
|
|
|
|
|
|
|
Devices support different functionality that are exposed via
|
|
|
|
:ref:`modules <module_target>` that you can access via :attr:`~kasa.Device.modules`:
|
|
|
|
|
|
|
|
>>> for module_name in dev.modules:
|
|
|
|
>>> print(module_name)
|
|
|
|
Energy
|
|
|
|
schedule
|
|
|
|
usage
|
|
|
|
anti_theft
|
|
|
|
time
|
|
|
|
cloud
|
|
|
|
Led
|
|
|
|
|
|
|
|
>>> led_module = dev.modules["Led"]
|
|
|
|
>>> print(led_module.led)
|
|
|
|
False
|
|
|
|
>>> await led_module.set_led(True)
|
|
|
|
>>> await dev.update()
|
|
|
|
>>> print(led_module.led)
|
|
|
|
True
|
|
|
|
|
|
|
|
Individual pieces of functionality are also exposed via :ref:`features <feature_target>`
|
|
|
|
which you can access via :attr:`~kasa.Device.features` and will only be present if
|
|
|
|
they are supported.
|
|
|
|
|
|
|
|
Features are similar to modules in that they provide functionality that may or may
|
|
|
|
not be present.
|
|
|
|
|
|
|
|
Whereas modules group functionality into a common interface, features expose a single
|
|
|
|
function that may or may not be part of a module.
|
|
|
|
|
|
|
|
The advantage of features is that they have a simple common interface of `id`, `name`,
|
|
|
|
`value` and `set_value` so no need to learn the module API.
|
|
|
|
|
|
|
|
They are useful if you want write code that dynamically adapts as new features are
|
|
|
|
added to the API.
|
|
|
|
|
|
|
|
>>> for feature_name in dev.features:
|
|
|
|
>>> print(feature_name)
|
|
|
|
state
|
|
|
|
rssi
|
|
|
|
on_since
|
2024-07-24 13:47:38 +00:00
|
|
|
reboot
|
2024-06-19 08:53:40 +00:00
|
|
|
current_consumption
|
|
|
|
consumption_today
|
|
|
|
consumption_this_month
|
|
|
|
consumption_total
|
|
|
|
voltage
|
|
|
|
current
|
|
|
|
cloud_connection
|
|
|
|
led
|
|
|
|
|
|
|
|
>>> led_feature = dev.features["led"]
|
|
|
|
>>> print(led_feature.value)
|
|
|
|
True
|
|
|
|
>>> await led_feature.set_value(False)
|
|
|
|
>>> await dev.update()
|
|
|
|
>>> print(led_feature.value)
|
|
|
|
False
|
|
|
|
"""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
import logging
|
|
|
|
from abc import ABC, abstractmethod
|
2024-06-19 18:24:12 +00:00
|
|
|
from collections.abc import Mapping, Sequence
|
2024-02-04 15:20:08 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from datetime import datetime
|
2024-06-19 18:24:12 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2024-05-13 17:52:08 +00:00
|
|
|
from warnings import warn
|
2024-02-04 15:20:08 +00:00
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
from typing_extensions import TypeAlias
|
|
|
|
|
|
|
|
from .credentials import Credentials as _Credentials
|
2024-02-04 15:20:08 +00:00
|
|
|
from .device_type import DeviceType
|
2024-06-03 18:06:54 +00:00
|
|
|
from .deviceconfig import (
|
|
|
|
DeviceConfig,
|
|
|
|
DeviceConnectionParameters,
|
|
|
|
DeviceEncryptionType,
|
|
|
|
DeviceFamily,
|
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
from .exceptions import KasaException
|
2024-02-15 15:25:08 +00:00
|
|
|
from .feature import Feature
|
2024-02-04 15:20:08 +00:00
|
|
|
from .iotprotocol import IotProtocol
|
2024-05-10 18:29:28 +00:00
|
|
|
from .module import Module
|
2024-02-04 15:20:08 +00:00
|
|
|
from .protocol import BaseProtocol
|
|
|
|
from .xortransport import XorTransport
|
|
|
|
|
2024-05-10 18:29:28 +00:00
|
|
|
if TYPE_CHECKING:
|
2024-05-15 17:49:08 +00:00
|
|
|
from .modulemapping import ModuleMapping, ModuleName
|
2024-05-10 18:29:28 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class WifiNetwork:
|
|
|
|
"""Wifi network container."""
|
|
|
|
|
|
|
|
ssid: str
|
|
|
|
key_type: int
|
|
|
|
# These are available only on softaponboarding
|
2024-04-17 13:39:24 +00:00
|
|
|
cipher_type: int | None = None
|
|
|
|
bssid: str | None = None
|
|
|
|
channel: int | None = None
|
|
|
|
rssi: int | None = None
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
# For SMART devices
|
2024-04-17 13:39:24 +00:00
|
|
|
signal_level: int | None = None
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Device(ABC):
|
|
|
|
"""Common device interface.
|
|
|
|
|
|
|
|
Do not instantiate this class directly, instead get a device instance from
|
|
|
|
:func:`Device.connect()`, :func:`Discover.discover()`
|
|
|
|
or :func:`Discover.discover_single()`.
|
|
|
|
"""
|
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
# All types required to create devices directly via connect are aliased here
|
|
|
|
# to avoid consumers having to do multiple imports.
|
|
|
|
|
|
|
|
#: The type of device
|
|
|
|
Type: TypeAlias = DeviceType
|
|
|
|
#: The credentials for authentication
|
|
|
|
Credentials: TypeAlias = _Credentials
|
|
|
|
#: Configuration for connecting to the device
|
|
|
|
Config: TypeAlias = DeviceConfig
|
|
|
|
#: The family of the device, e.g. SMART.KASASWITCH.
|
|
|
|
Family: TypeAlias = DeviceFamily
|
|
|
|
#: The encryption for the device, e.g. Klap or Aes
|
|
|
|
EncryptionType: TypeAlias = DeviceEncryptionType
|
|
|
|
#: The connection type for the device.
|
|
|
|
ConnectionParameters: TypeAlias = DeviceConnectionParameters
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
config: DeviceConfig | None = None,
|
|
|
|
protocol: BaseProtocol | None = None,
|
2024-02-04 15:20:08 +00:00
|
|
|
) -> None:
|
|
|
|
"""Create a new Device instance.
|
|
|
|
|
|
|
|
:param str host: host name or IP address of the device
|
|
|
|
:param DeviceConfig config: device configuration
|
|
|
|
:param BaseProtocol protocol: protocol for communicating with the device
|
|
|
|
"""
|
|
|
|
if config and protocol:
|
|
|
|
protocol._transport._config = config
|
|
|
|
self.protocol: BaseProtocol = protocol or IotProtocol(
|
|
|
|
transport=XorTransport(config=config or DeviceConfig(host=host)),
|
|
|
|
)
|
|
|
|
_LOGGER.debug("Initializing %s of type %s", self.host, type(self))
|
|
|
|
self._device_type = DeviceType.Unknown
|
|
|
|
# TODO: typing Any is just as using Optional[Dict] would require separate
|
|
|
|
# checks in accessors. the @updated_required decorator does not ensure
|
|
|
|
# mypy that these are not accessed incorrectly.
|
|
|
|
self._last_update: Any = None
|
2024-04-17 13:39:24 +00:00
|
|
|
self._discovery_info: dict[str, Any] | None = None
|
2024-02-04 15:20:08 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
self._features: dict[str, Feature] = {}
|
|
|
|
self._parent: Device | None = None
|
|
|
|
self._children: Mapping[str, Device] = {}
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def connect(
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
host: str | None = None,
|
|
|
|
config: DeviceConfig | None = None,
|
|
|
|
) -> Device:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Connect to a single device by the given hostname or device configuration.
|
|
|
|
|
|
|
|
This method avoids the UDP based discovery process and
|
|
|
|
will connect directly to the device.
|
|
|
|
|
|
|
|
It is generally preferred to avoid :func:`discover_single()` and
|
|
|
|
use this function instead as it should perform better when
|
|
|
|
the WiFi network is congested or the device is not responding
|
|
|
|
to discovery requests.
|
|
|
|
|
|
|
|
:param host: Hostname of device to query
|
|
|
|
:param config: Connection parameters to ensure the correct protocol
|
|
|
|
and connection options are used.
|
|
|
|
:rtype: SmartDevice
|
|
|
|
:return: Object for querying/controlling found device.
|
|
|
|
"""
|
|
|
|
from .device_factory import connect # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
return await connect(host=host, config=config) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def update(self, update_children: bool = True):
|
|
|
|
"""Update the device."""
|
|
|
|
|
|
|
|
async def disconnect(self):
|
|
|
|
"""Disconnect and close any underlying connection resources."""
|
|
|
|
await self.protocol.close()
|
|
|
|
|
2024-04-29 16:34:20 +00:00
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-05-10 18:29:28 +00:00
|
|
|
def modules(self) -> ModuleMapping[Module]:
|
2024-04-29 16:34:20 +00:00
|
|
|
"""Return the device modules."""
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the device is on."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_off(self) -> bool:
|
|
|
|
"""Return True if device is off."""
|
|
|
|
return not self.is_on
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
async def turn_on(self, **kwargs) -> dict | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Turn on the device."""
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
async def turn_off(self, **kwargs) -> dict | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Turn off the device."""
|
|
|
|
|
2024-05-22 14:52:00 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def set_state(self, on: bool):
|
|
|
|
"""Set the device state to *on*.
|
|
|
|
|
|
|
|
This allows turning the device on and off.
|
|
|
|
See also *turn_off* and *turn_on*.
|
|
|
|
"""
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
@property
|
|
|
|
def host(self) -> str:
|
|
|
|
"""The device host."""
|
|
|
|
return self.protocol._transport._host
|
|
|
|
|
|
|
|
@host.setter
|
|
|
|
def host(self, value):
|
|
|
|
"""Set the device host.
|
|
|
|
|
|
|
|
Generally used by discovery to set the hostname after ip discovery.
|
|
|
|
"""
|
|
|
|
self.protocol._transport._host = value
|
|
|
|
self.protocol._transport._config.host = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self) -> int:
|
|
|
|
"""The device port."""
|
|
|
|
return self.protocol._transport._port
|
|
|
|
|
|
|
|
@property
|
2024-06-03 18:06:54 +00:00
|
|
|
def credentials(self) -> _Credentials | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""The device credentials."""
|
|
|
|
return self.protocol._transport._credentials
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def credentials_hash(self) -> str | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""The protocol specific hash of the credentials the device is using."""
|
|
|
|
return self.protocol._transport.credentials_hash
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_type(self) -> DeviceType:
|
|
|
|
"""Return the device type."""
|
|
|
|
return self._device_type
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def update_from_discover_info(self, info):
|
|
|
|
"""Update state from info from the discover call."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self) -> DeviceConfig:
|
|
|
|
"""Return the device configuration."""
|
|
|
|
return self.protocol.config
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def model(self) -> str:
|
|
|
|
"""Returns the device model."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def alias(self) -> str | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Returns the device alias or nickname."""
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def _raw_query(self, request: str | dict) -> Any:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Send a raw query to the device."""
|
|
|
|
return await self.protocol.query(request=request)
|
|
|
|
|
2024-07-01 10:51:06 +00:00
|
|
|
@property
|
|
|
|
def parent(self) -> Device | None:
|
|
|
|
"""Return the parent on child devices."""
|
|
|
|
return self._parent
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def children(self) -> Sequence[Device]:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Returns the child devices."""
|
2024-02-22 19:46:19 +00:00
|
|
|
return list(self._children.values())
|
|
|
|
|
2024-07-02 13:11:19 +00:00
|
|
|
def get_child_device(self, name_or_id: str) -> Device | None:
|
|
|
|
"""Return child device by its device_id or alias."""
|
|
|
|
if name_or_id in self._children:
|
|
|
|
return self._children[name_or_id]
|
|
|
|
name_lower = name_or_id.lower()
|
|
|
|
for child in self.children:
|
|
|
|
if child.alias and child.alias.lower() == name_lower:
|
|
|
|
return child
|
|
|
|
return None
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def sys_info(self) -> dict[str, Any]:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Returns the device info."""
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def get_plug_by_name(self, name: str) -> Device:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return child device for the given name."""
|
|
|
|
for p in self.children:
|
|
|
|
if p.alias == name:
|
|
|
|
return p
|
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException(f"Device has no child with {name}")
|
2024-02-04 15:20:08 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def get_plug_by_index(self, index: int) -> Device:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return child device for the given index."""
|
|
|
|
if index + 1 > len(self.children) or index < 0:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException(
|
2024-02-04 15:20:08 +00:00
|
|
|
f"Invalid index {index}, device has {len(self.children)} plugs"
|
|
|
|
)
|
|
|
|
return self.children[index]
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def time(self) -> datetime:
|
|
|
|
"""Return the time."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def timezone(self) -> dict:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return the timezone and time_difference."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def hw_info(self) -> dict:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return hardware info for the device."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def location(self) -> dict:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return the device location."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def rssi(self) -> int | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return the rssi."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def mac(self) -> str:
|
|
|
|
"""Return the mac formatted with colons."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def device_id(self) -> str:
|
|
|
|
"""Return the device id."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def internal_state(self) -> Any:
|
|
|
|
"""Return all the internal state data."""
|
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def state_information(self) -> dict[str, Any]:
|
2024-03-26 18:28:39 +00:00
|
|
|
"""Return available features and their values."""
|
|
|
|
return {feat.name: feat.value for feat in self._features.values()}
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
@property
|
2024-04-17 13:39:24 +00:00
|
|
|
def features(self) -> dict[str, Feature]:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return the list of supported features."""
|
2024-02-15 15:25:08 +00:00
|
|
|
return self._features
|
|
|
|
|
|
|
|
def _add_feature(self, feature: Feature):
|
|
|
|
"""Add a new feature to the device."""
|
2024-04-23 17:20:12 +00:00
|
|
|
if feature.id in self._features:
|
|
|
|
raise KasaException("Duplicate feature id %s" % feature.id)
|
|
|
|
assert feature.id is not None # TODO: hack for typing # noqa: S101
|
|
|
|
self._features[feature.id] = feature
|
2024-02-04 15:20:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def has_emeter(self) -> bool:
|
|
|
|
"""Return if the device has emeter."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
def on_since(self) -> datetime | None:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Return the time that the device was turned on or None if turned off."""
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-04-17 13:39:24 +00:00
|
|
|
async def wifi_scan(self) -> list[WifiNetwork]:
|
2024-02-04 15:20:08 +00:00
|
|
|
"""Scan for available wifi networks."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def wifi_join(self, ssid: str, password: str, keytype: str = "wpa2_psk"):
|
|
|
|
"""Join the given wifi network."""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def set_alias(self, alias: str):
|
|
|
|
"""Set the device name (alias)."""
|
|
|
|
|
2024-09-21 14:37:38 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def reboot(self, delay: int = 1) -> None:
|
|
|
|
"""Reboot the device.
|
|
|
|
|
|
|
|
Note that giving a delay of zero causes this to block,
|
|
|
|
as the device reboots immediately without responding to the call.
|
|
|
|
"""
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
def __repr__(self):
|
|
|
|
if self._last_update is None:
|
2024-02-22 13:34:55 +00:00
|
|
|
return f"<{self.device_type} at {self.host} - update() needed>"
|
2024-03-05 12:35:19 +00:00
|
|
|
return f"<{self.device_type} at {self.host} - {self.alias} ({self.model})>"
|
2024-05-13 17:52:08 +00:00
|
|
|
|
2024-05-15 17:49:08 +00:00
|
|
|
_deprecated_device_type_attributes = {
|
2024-05-13 17:52:08 +00:00
|
|
|
# is_type
|
2024-05-15 17:49:08 +00:00
|
|
|
"is_bulb": (Module.Light, DeviceType.Bulb),
|
|
|
|
"is_dimmer": (Module.Light, DeviceType.Dimmer),
|
|
|
|
"is_light_strip": (Module.LightEffect, DeviceType.LightStrip),
|
|
|
|
"is_plug": (Module.Led, DeviceType.Plug),
|
|
|
|
"is_wallswitch": (Module.Led, DeviceType.WallSwitch),
|
|
|
|
"is_strip": (None, DeviceType.Strip),
|
|
|
|
"is_strip_socket": (None, DeviceType.StripSocket),
|
2024-05-13 17:52:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 17:49:08 +00:00
|
|
|
def _get_replacing_attr(self, module_name: ModuleName, *attrs):
|
2024-06-17 10:22:05 +00:00
|
|
|
# If module name is None check self
|
|
|
|
if not module_name:
|
|
|
|
check = self
|
|
|
|
elif (check := self.modules.get(module_name)) is None:
|
2024-05-15 17:49:08 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
for attr in attrs:
|
2024-06-17 10:22:05 +00:00
|
|
|
if hasattr(check, attr):
|
|
|
|
return attr
|
2024-05-15 17:49:08 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
_deprecated_other_attributes = {
|
|
|
|
# light attributes
|
|
|
|
"is_color": (Module.Light, ["is_color"]),
|
|
|
|
"is_dimmable": (Module.Light, ["is_dimmable"]),
|
|
|
|
"is_variable_color_temp": (Module.Light, ["is_variable_color_temp"]),
|
|
|
|
"brightness": (Module.Light, ["brightness"]),
|
|
|
|
"set_brightness": (Module.Light, ["set_brightness"]),
|
|
|
|
"hsv": (Module.Light, ["hsv"]),
|
|
|
|
"set_hsv": (Module.Light, ["set_hsv"]),
|
|
|
|
"color_temp": (Module.Light, ["color_temp"]),
|
|
|
|
"set_color_temp": (Module.Light, ["set_color_temp"]),
|
|
|
|
"valid_temperature_range": (Module.Light, ["valid_temperature_range"]),
|
|
|
|
"has_effects": (Module.Light, ["has_effects"]),
|
2024-05-19 10:20:18 +00:00
|
|
|
"_deprecated_set_light_state": (Module.Light, ["has_effects"]),
|
2024-05-15 17:49:08 +00:00
|
|
|
# led attributes
|
|
|
|
"led": (Module.Led, ["led"]),
|
|
|
|
"set_led": (Module.Led, ["set_led"]),
|
|
|
|
# light effect attributes
|
|
|
|
# The return values for effect is a str instead of dict so the lightstrip
|
|
|
|
# modules have a _deprecated method to return the value as before.
|
|
|
|
"effect": (Module.LightEffect, ["_deprecated_effect", "effect"]),
|
|
|
|
# The return values for effect_list includes the Off effect so the lightstrip
|
|
|
|
# modules have a _deprecated method to return the values as before.
|
|
|
|
"effect_list": (Module.LightEffect, ["_deprecated_effect_list", "effect_list"]),
|
|
|
|
"set_effect": (Module.LightEffect, ["set_effect"]),
|
|
|
|
"set_custom_effect": (Module.LightEffect, ["set_custom_effect"]),
|
2024-05-19 10:20:18 +00:00
|
|
|
# light preset attributes
|
|
|
|
"presets": (Module.LightPreset, ["_deprecated_presets", "preset_states_list"]),
|
|
|
|
"save_preset": (Module.LightPreset, ["_deprecated_save_preset"]),
|
2024-06-17 10:22:05 +00:00
|
|
|
# Emeter attribues
|
|
|
|
"get_emeter_realtime": (Module.Energy, ["get_status"]),
|
|
|
|
"emeter_realtime": (Module.Energy, ["status"]),
|
|
|
|
"emeter_today": (Module.Energy, ["consumption_today"]),
|
|
|
|
"emeter_this_month": (Module.Energy, ["consumption_this_month"]),
|
|
|
|
"current_consumption": (Module.Energy, ["current_consumption"]),
|
|
|
|
"get_emeter_daily": (Module.Energy, ["get_daily_stats"]),
|
|
|
|
"get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]),
|
|
|
|
# Other attributes
|
|
|
|
"supported_modules": (None, ["modules"]),
|
2024-05-15 17:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
# is_device_type
|
|
|
|
if dep_device_type_attr := self._deprecated_device_type_attributes.get(name):
|
|
|
|
module = dep_device_type_attr[0]
|
2024-05-13 17:52:08 +00:00
|
|
|
msg = f"{name} is deprecated"
|
|
|
|
if module:
|
|
|
|
msg += f", use: {module} in device.modules instead"
|
|
|
|
warn(msg, DeprecationWarning, stacklevel=1)
|
2024-05-15 17:49:08 +00:00
|
|
|
return self.device_type == dep_device_type_attr[1]
|
|
|
|
# Other deprecated attributes
|
|
|
|
if (dep_attr := self._deprecated_other_attributes.get(name)) and (
|
|
|
|
(replacing_attr := self._get_replacing_attr(dep_attr[0], *dep_attr[1]))
|
|
|
|
is not None
|
|
|
|
):
|
2024-06-17 10:22:05 +00:00
|
|
|
mod = dep_attr[0]
|
|
|
|
dev_or_mod = self.modules[mod] if mod else self
|
|
|
|
replacing = f"Module.{mod} in device.modules" if mod else replacing_attr
|
|
|
|
msg = f"{name} is deprecated, use: {replacing} instead"
|
2024-05-15 17:49:08 +00:00
|
|
|
warn(msg, DeprecationWarning, stacklevel=1)
|
2024-06-17 10:22:05 +00:00
|
|
|
return getattr(dev_or_mod, replacing_attr)
|
2024-05-13 17:52:08 +00:00
|
|
|
raise AttributeError(f"Device has no attribute {name!r}")
|