Skip to content

Commit

Permalink
Use tapop100 lib for Tapo Plugs HW ver2
Browse files Browse the repository at this point in the history
Signed-off-by: Mariia Azbeleva <[email protected]>
  • Loading branch information
azbeleva committed Oct 13, 2023
1 parent aaa349d commit 3784724
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
27 changes: 21 additions & 6 deletions Robot-Framework/lib/PlugLibrary/PlugLibrary.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
# SPDX-License-Identifier: Apache-2.0

import asyncio
from KasaPlug import KasaPlug
from TapoP100 import TapoP100
from TapoP100v2 import TapoP100v2
from robot.libraries.BuiltIn import BuiltIn


Expand All @@ -13,23 +15,30 @@
# * Kasa Plug:
# -v PLUG_TYPE:KASAPLUG (needs only SOCKET_IP_ADDRESS)
#
# * Tapo Plug Hardware Version 2.0:
# -v PLUG_TYPE:TAPOP100v2 (needs only SOCKET_IP_ADDRESS)
#
class PlugLibrary:
def __init__(self, *args, **kwargs):
self._plug = None
self._plug_type = "TAPOP100"

def _get_plug(self):
if not self._plug:
# Setting self._plug for the first time
plug_type = self._get_plug_type()
if plug_type == "TAPOP100":
self._plug_type = self._get_plug_type()
if self._plug_type == "TAPOP100":
self._plug = TapoP100()
elif plug_type == "KASAPLUG":
elif self._plug_type == "KASAPLUG":
self._plug = KasaPlug()
elif self._plug_type == "TAPOP100v2":
self._plug = TapoP100v2()
print(f"plug type: {self._plug_type}")
return self._plug

# Return TAPOP100 if PLUG_TYPE is not known
def _get_plug_type(self):
allowed_types = ["TAPOP100", "KASAPLUG"]
allowed_types = ["TAPOP100", "KASAPLUG", "TAPOP100v2"]
raw = BuiltIn().get_variable_value("${PLUG_TYPE}")
if raw in allowed_types:
return raw
Expand All @@ -38,7 +47,13 @@ def _get_plug_type(self):
return allowed_types[0]

def turn_plug_on(self):
self._get_plug().turn_plug_on()
if self._plug_type == "TAPOP100v2":
asyncio.run(self._get_plug().turn_plug_on())
else:
self._get_plug().turn_plug_on()

def turn_plug_off(self):
self._get_plug().turn_plug_off()
if self._get_plug_type() == "TAPOP100v2":
asyncio.run(self._get_plug().turn_plug_off())
else:
self._get_plug().turn_plug_off()
35 changes: 35 additions & 0 deletions Robot-Framework/lib/PlugLibrary/TapoP100v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
# SPDX-License-Identifier: Apache-2.0

from robot.libraries.BuiltIn import BuiltIn
from plugp100.api.tapo_client import TapoClient
from plugp100.common.credentials import AuthCredential
from plugp100.api.plug_device import PlugDevice


class TapoP100v2:

def __init__(self):
self._p100 = None

async def _get_p100(self):
ip_address = BuiltIn().get_variable_value("${SOCKET_IP_ADDRESS}")
username = BuiltIn().get_variable_value("${PLUG_USERNAME}")
password = BuiltIn().get_variable_value("${PLUG_PASSWORD}")
credentials = AuthCredential(username, password)

tapo_client = TapoClient(credentials, ip_address)
await tapo_client.initialize()
self._p100 = PlugDevice(tapo_client)

return self._p100

async def turn_plug_on(self):
p100 = await self._get_p100()
await p100.on()
print("Smart plug turned ON")

async def turn_plug_off(self):
p100 = await self._get_p100()
await p100.off()
print("Smart plug turned OFF")

0 comments on commit 3784724

Please sign in to comment.