2020-05-27 14:55:18 +00:00
|
|
|
"""Module for smart plugs (HS100, HS110, ..)."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-04-14 12:24:58 +00:00
|
|
|
import logging
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
from ..device_type import DeviceType
|
|
|
|
from ..deviceconfig import DeviceConfig
|
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 .iotdevice import IotDevice, requires_update
|
2024-05-10 18:29:28 +00:00
|
|
|
from .modules import Antitheft, Cloud, LedModule, Schedule, Time, Usage
|
2017-04-14 12:24:58 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class IotPlug(IotDevice):
|
2024-03-01 18:32:45 +00:00
|
|
|
r"""Representation of a TP-Link Smart Plug.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
To initialize, you have to await :func:`update()` at least once.
|
|
|
|
This will allow accessing the properties using the exposed properties.
|
2019-11-15 15:05:46 +00:00
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
All changes to the device are done using awaitable methods,
|
2023-10-29 22:15:42 +00:00
|
|
|
which will not change the cached values,
|
|
|
|
but you must await :func:`update()` separately.
|
2019-11-15 15:05:46 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
Errors reported by the device are raised as :class:`KasaException`\s,
|
2020-06-30 00:29:52 +00:00
|
|
|
and should be handled by the user of the library.
|
2019-11-15 15:05:46 +00:00
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
Examples:
|
|
|
|
>>> import asyncio
|
2024-02-04 15:20:08 +00:00
|
|
|
>>> plug = IotPlug("127.0.0.1")
|
2020-06-30 00:29:52 +00:00
|
|
|
>>> asyncio.run(plug.update())
|
|
|
|
>>> plug.alias
|
|
|
|
Kitchen
|
2019-11-15 15:05:46 +00:00
|
|
|
|
2020-06-30 00:29:52 +00:00
|
|
|
Setting the LED state:
|
|
|
|
|
|
|
|
>>> asyncio.run(plug.set_led(True))
|
|
|
|
>>> asyncio.run(plug.update())
|
|
|
|
>>> plug.led
|
|
|
|
True
|
|
|
|
|
|
|
|
For more examples, see the :class:`SmartDevice` class.
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
|
|
|
|
2023-09-13 13:46:38 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
config: DeviceConfig | None = None,
|
|
|
|
protocol: BaseProtocol | None = None,
|
2023-09-13 13:46:38 +00:00
|
|
|
) -> None:
|
2023-12-29 19:17:15 +00:00
|
|
|
super().__init__(host=host, config=config, protocol=protocol)
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
self._device_type = DeviceType.Plug
|
2021-11-07 01:41:12 +00:00
|
|
|
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"))
|
2024-05-10 18:29:28 +00:00
|
|
|
self.add_module(Module.Led, LedModule(self, "system"))
|
2024-02-15 15:25:08 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def is_on(self) -> bool:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Return whether device is on."""
|
2019-11-15 16:48:36 +00:00
|
|
|
sys_info = self.sys_info
|
2019-11-11 16:42:12 +00:00
|
|
|
return bool(sys_info["relay_state"])
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2020-07-06 14:10:28 +00:00
|
|
|
async def turn_on(self, **kwargs):
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Turn the switch on."""
|
2020-05-24 15:57:54 +00:00
|
|
|
return await self._query_helper("system", "set_relay_state", {"state": 1})
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2020-07-06 14:10:28 +00:00
|
|
|
async def turn_off(self, **kwargs):
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Turn the switch off."""
|
2020-05-24 15:57:54 +00:00
|
|
|
return await self._query_helper("system", "set_relay_state", {"state": 0})
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2019-12-12 09:46:40 +00:00
|
|
|
@property # type: ignore
|
2019-11-15 16:48:36 +00:00
|
|
|
@requires_update
|
|
|
|
def led(self) -> bool:
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Return the state of the led."""
|
2024-05-10 18:29:28 +00:00
|
|
|
return self.modules[Module.Led].led
|
API and tests cleanup (#151)
* Add new cli commands: raw_command and dump_discover
- raw_command can be used to execute raw commands with given parameters
* Useful for testing new calls before implementing them properly
- dump_discover can be used to dump the device discovery information (into a file)
* The discovery is extended to request more modules and methods from devices
* smartlife.iot.dimmer get_dimmer_parameters
* smartlife.iot.common.emeter get_realtime
* smartlife.iot.smartbulb.lightingservice get_light_state
* This is used to dump more information for proper tests, and will also allow better discovery in the future
This commit contains also some documentation updates and dropping click_datetime in favor of click's built-in datetime
* Docstring fixes
* Major API cleanup
Properties shall no more change the state of the device, this work in still in progress, the main goal being making the API more user-friendly and to make implementing new features simpler.
The newly deprecated functionality will remain working and will simply warn the user about deprecation.
Previously deprecated 'features' property and 'identify' method are now finally removed.
Deprecate and replace the following property setters:
* state with turn_on() and turn_off()
* hsv with set_hsv()
* color_temp with set_color_temp()
* brightness with set_brightness()
* led with set_led()
* alias with set_alias()
* mac with set_mac()
And getters:
* state with is_on and is_off
The {BULB,PLUG}_STATE_{ON,OFF} is simplified to STATE_ON and STATE_OFF, UNKNOWN state is removed.
These are now deprecated and will be removed in the future.
* is_on and is_off can be used to check for the state
* turn_on() and turn_off() for changing the device state.
Trying to use functionality not supported by the device will cause SmartDeviceExceptions instead of failing silently and/or returning None.
This includes, e.g., trying to set a color temperature on non-supported bulb.
ValueErrors are raised instead of SmartDeviceExceptions where appropriate (e.g. when trying to set an invalid hsv or brightness).
New enum type DeviceType is added to allow detecting device types without resorting to isinstance() calling. SmartDevice class' device_type property can be used to query the type. is_plug and is_bulb helpers are added.
* Cleanup tests and improve test coverage
* Make writing tests easier by sharing code for common implementations
* Instead of storing test data inside python files, dump-discover based information is used
* This will simplify adding new tests and remove code duplication
* fixtures are based on https://github.com/plasticrake/tplink-smarthome-simulator
* run black on newfakes
* Add HS300 tests and update SmartStrip API according to earlier changes, still WIP
* run black and avoid wildcard imports
* Black on conftest
* bump minimum required version to 3.5
* Rename fixture_tests to test_fixtures for autocollect
* fix typoed type to _type, black
* run black on several files with -79 to fix hound issues
* Fix broken merge on hue
* Fix tests (hue update, pass context to smartdevice), add is_strip property, disable emeter tests for HS300 until a solution for API is found.
* Fix old tests
* Run black on changed files
* Add real HS220 discovery, thanks to @poiyo
* add is_dimmable and is_variable_color_temp to smartdevice class, simplifies interfacing with homeassistant
* add KL120(US) fixture
* Add a simple query cache
This commit adds a simple query cache to speed up the process for users
requesting lots of different properties from the device, as done by the
cli tool as well as homeassistant.
The logic for caching is very simple:
1. A timestamp for last fetch for each module+command is stored alongside the response.
2. If the issued command starts with `get_` and the TTL has not expired, the cache result is returned.
3. Otherwise the cache for the whole corresponding module gets invalidated, the device will be queried and the result will be stored in the cache.
* add deprecation to tox.ini
* make tests pass again
* remove old tests, add flake8 to tox reqs
* run black against pyhs100 module, add it to precommit hooks, fix flake8 configuration to conform to black standards (https://ljvmiranda921.github.io/notebook/2018/06/21/precommits-using-black-and-flake8/)
* fix syntax
* cleanup conftest
2019-06-16 21:05:00 +00:00
|
|
|
|
2019-11-11 16:42:12 +00:00
|
|
|
async def set_led(self, state: bool):
|
2020-05-27 14:55:18 +00:00
|
|
|
"""Set the state of the led (night mode)."""
|
2024-05-10 18:29:28 +00:00
|
|
|
return await self.modules[Module.Led].set_led(state)
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2024-03-01 18:32:45 +00:00
|
|
|
|
|
|
|
class IotWallSwitch(IotPlug):
|
|
|
|
"""Representation of a TP-Link Smart Wall Switch."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
*,
|
2024-04-17 13:39:24 +00:00
|
|
|
config: DeviceConfig | None = None,
|
|
|
|
protocol: BaseProtocol | None = None,
|
2024-03-01 18:32:45 +00:00
|
|
|
) -> None:
|
|
|
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
|
|
self._device_type = DeviceType.WallSwitch
|