-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Testing a couple of things, might not work
- Loading branch information
Showing
4 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
__author__ = "Nicolas Gutierrez" | ||
|
||
# Standard libraries | ||
import os | ||
import platform | ||
import asyncio | ||
from threading import Lock | ||
# Third party libraries | ||
import kasa | ||
# Custom libraries | ||
|
||
|
||
if platform.system() == 'Windows': | ||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
|
||
|
||
class KasaPlug: | ||
def __init__(self, ip_address: str) -> None: | ||
self.device_lock = Lock() | ||
|
||
self.ip_address = ip_address | ||
self.plug_object = kasa.SmartPlug(ip_address) | ||
asyncio.run(self.__device_update()) | ||
|
||
async def __device_update(self): | ||
await self.plug_object.update() | ||
|
||
async def __power_function(self): | ||
self.device_lock.acquire() | ||
try: | ||
power = self.plug_object.emeter_realtime | ||
except Exception as inst: | ||
print(f"\tError while requesting power from device {inst}") | ||
power = -1 | ||
self.device_lock.release() | ||
return power, "W" | ||
|
||
def power_request(self): | ||
power, unit = asyncio.run(self.__power_function()) | ||
return power, unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
import asyncio | ||
import time | ||
from kasadevices.kasaplug import KasaPlug | ||
from power.power_function import power_function | ||
import kasa | ||
|
||
plug = KasaPlug("192.168.0.49") | ||
|
||
while True: | ||
time.sleep(2) | ||
power, unit = power_function("192.168.0.49") | ||
print(f"{time.time()} the Power in the plug is {power} {unit}") | ||
|
||
|
||
# async def get_plug(): | ||
# plug = kasa.SmartPlug("192.168.0.49") | ||
# return plug | ||
# | ||
# | ||
# async def set_plug(): | ||
# res = get_plug() | ||
# bulb = await res | ||
# await bulb.update() | ||
# assert bulb.is_bulb | ||
# | ||
# | ||
# if __name__ == '__main__': | ||
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
# asyncio.run(set_plug()) |