Skip to content

Commit

Permalink
Testing a couple of things, might not work
Browse files Browse the repository at this point in the history
  • Loading branch information
eolus87 committed Jun 12, 2023
1 parent 8d7fe75 commit de7b99a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 18 deletions.
Empty file added kasadevices/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions kasadevices/kasaplug.py
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
43 changes: 25 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import kasa

# Custom libraries
from control.control_functions import switch_on_function, switch_off_function

# User variables
plug_ip = "X.X.X.X"

response = ping('8.8.8.8', count=1, verbose=True)
plug_ip = "192.168.0.49"

# The general command does not seem to work
found_devices = asyncio.run(kasa.Discover.discover())
Expand All @@ -27,18 +26,26 @@

#

async def turn_on_off():
plug = kasa.SmartPlug(plug_ip)
await plug.update()
print(plug.alias)
print("Turning Off")
await plug.turn_off()
time.sleep(10)
print("Turning On")
await plug.turn_on()
time.sleep(10)
print("Turning Off")
await plug.turn_off()


asyncio.run(turn_on_off())
# async def turn_on_off():
# plug = kasa.SmartPlug(plug_ip)
# await plug.update()
# print(plug.alias)
# print(f"Current consumption: {await plug.current_consumption()} W")
# print("Turning Off")
# await plug.turn_off()
# time.sleep(10)
# print("Turning On")
# await plug.turn_on()
# time.sleep(5)
# await plug.update()
# print(f"Current consumption: {await plug.current_consumption()} W")
# time.sleep(10)
# print("Turning Off")
# await plug.turn_off()
#
# asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# asyncio.run(turn_on_off())

print(switch_on_function(plug_ip))
time.sleep(10)
print(switch_off_function(plug_ip))
30 changes: 30 additions & 0 deletions main_test_plug.py
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())

0 comments on commit de7b99a

Please sign in to comment.