2024-06-03 09:14:10 +00:00
.. py:currentmodule :: kasa
2022-04-05 23:13:27 +00:00
2024-06-03 09:14:10 +00:00
Base Device
===========
2020-06-30 00:29:52 +00:00
2022-10-18 17:08:10 +00:00
.. contents :: Contents
:local:
2024-06-03 09:14:10 +00:00
SmartDevice class
***** ***** ***** **
2022-10-18 17:08:10 +00:00
2024-06-03 09:14:10 +00:00
The basic functionalities of all supported devices are accessible using the common :class: `SmartDevice` base class.
2020-06-30 00:29:52 +00:00
2024-06-03 09:14:10 +00:00
The property accesses use the data obtained before by awaiting :func: `SmartDevice.update()` .
2020-06-30 00:29:52 +00:00
The values are cached until the next update call. In practice this means that property accesses do no I/O and are dependent, while I/O producing methods need to be awaited.
2024-06-03 09:14:10 +00:00
See :ref: `topics-update-cycle` for more detailed information.
2022-04-05 23:13:27 +00:00
.. note ::
The device instances share the communication socket in background to optimize I/O accesses.
This means that you need to use the same event loop for subsequent requests.
The library gives a warning ("Detected protocol reuse between different event loop") to hint if you are accessing the device incorrectly.
2020-06-30 00:29:52 +00:00
2024-06-03 09:14:10 +00:00
Methods changing the state of the device do not invalidate the cache (i.e., there is no implicit :func: `SmartDevice.update()` call made by the library).
2020-06-30 00:29:52 +00:00
You can assume that the operation has succeeded if no exception is raised.
These methods will return the device response, which can be useful for some use cases.
2024-06-03 09:14:10 +00:00
Errors are raised as :class: `SmartDeviceException` instances for the library user to handle.
2020-06-30 00:29:52 +00:00
2024-06-03 09:14:10 +00:00
Simple example script showing some functionality:
2020-07-20 14:42:37 +00:00
.. code-block :: python
import asyncio
from kasa import SmartPlug
async def main():
p = SmartPlug("127.0.0.1")
2022-04-05 23:13:27 +00:00
await p.update() # Request the update
print(p.alias) # Print out the alias
print(p.emeter_realtime) # Print out current emeter status
await p.turn_off() # Turn the device off
2020-07-20 14:42:37 +00:00
2022-04-05 23:13:27 +00:00
if __name__ == "__main__":
asyncio.run(main())
2020-07-20 14:42:37 +00:00
2022-04-05 23:13:27 +00:00
If you want to perform updates in a loop, you need to make sure that the device accesses are done in the same event loop:
.. code-block :: python
import asyncio
from kasa import SmartPlug
async def main():
dev = SmartPlug("127.0.0.1") # We create the instance inside the main loop
while True:
await dev.update() # Request an update
print(dev.emeter_realtime)
await asyncio.sleep(0.5) # Sleep some time between updates
2020-07-20 14:42:37 +00:00
if __name__ == "__main__":
asyncio.run(main())
Refer to device type specific classes for more examples:
2022-04-05 23:13:27 +00:00
:class: `SmartPlug` , :class: `SmartBulb` , :class: `SmartStrip` ,
:class: `SmartDimmer` , :class: `SmartLightStrip` .
Energy Consumption and Usage Statistics
***** ***** ***** ***** ***** ***** ***** *** *
.. note ::
In order to use the helper methods to calculate the statistics correctly, your devices need to have correct time set.
The devices use NTP and public servers from `NTP Pool Project <https://www.ntppool.org/> `_ to synchronize their time.
Energy Consumption
~~~~~~~~~~~~~~~~~~
The availability of energy consumption sensors depend on the device.
While most of the bulbs support it, only specific switches (e.g., HS110) or strips (e.g., HS300) support it.
You can use :attr: `~SmartDevice.has_emeter` to check for the availability.
Usage statistics
~~~~~~~~~~~~~~~~
You can use :attr: `~SmartDevice.on_since` to query for the time the device has been turned on.
Some devices also support reporting the usage statistics on daily or monthly basis.
You can access this information using through the usage module (:class: `kasa.modules.Usage` ):
.. code-block :: python
2020-07-20 14:42:37 +00:00
2022-04-05 23:13:27 +00:00
dev = SmartPlug("127.0.0.1")
usage = dev.modules["usage"]
print(f"Minutes on this month: {usage.usage_this_month}")
print(f"Minutes on today: {usage.usage_today}")
2020-06-30 00:29:52 +00:00
2020-12-09 09:13:14 +00:00
API documentation
2022-04-05 23:13:27 +00:00
***** ***** ***** **
2020-12-09 09:13:14 +00:00
2024-06-03 09:14:10 +00:00
.. autoclass :: SmartDevice
2024-01-10 19:13:14 +00:00
:members:
:undoc-members: