Skip to content

Commit

Permalink
Configurable PlugLibrary with multiple plug types
Browse files Browse the repository at this point in the history
Configurable PlugLibrary with support for multiple plug types.

Signed-off-by: Mika Tammi <[email protected]>
  • Loading branch information
Mika Tammi committed Sep 11, 2023
1 parent 19cbd78 commit 31574ae
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 60 deletions.
20 changes: 0 additions & 20 deletions Robot-Framework/lib/KasaPlug/kasaplug.py

This file was deleted.

19 changes: 19 additions & 0 deletions Robot-Framework/lib/PlugLibrary/KasaPlug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
# SPDX-License-Identifier: Apache-2.0

import asyncio
from kasa import SmartPlug
from robot.libraries.BuiltIn import BuiltIn


class KasaPlug:
def _get_plug_address(self, *args, **kwargs):
return BuiltIn().get_variable_value("${SOCKET_IP_ADDRESS}")

def turn_plug_on(self):
plug = SmartPlug(self._get_plug_address())
asyncio.run(plug.turn_on())

def turn_plug_off(self):
plug = SmartPlug(self._get_plug_address())
asyncio.run(plug.turn_off())
36 changes: 36 additions & 0 deletions Robot-Framework/lib/PlugLibrary/PlugLibrary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
# SPDX-License-Identifier: Apache-2.0

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


class PlugLibrary:
def __init__(self, *args, **kwargs):
self._plug = None

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 = TapoP100()
elif plug_type == "KASAPLUG":
self._plug = KasaPlug()
return self._plug

# Returns TAPOP100 if not undetected type
def _get_plug_type(self):
allowed_types = ["TAPOP100", "KASAPLUG"]
raw = BuiltIn().get_variable_value("${PLUG_TYPE}")
if raw in allowed_types:
return raw
else:
return allowed_types[0]

def turn_plug_on(self):
self._get_plug().turn_plug_on()

def turn_plug_off(self):
self._get_plug().turn_plug_off()
31 changes: 31 additions & 0 deletions Robot-Framework/lib/PlugLibrary/TapoP100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-FileCopyrightText: 2022-2023 Technology Innovation Institute (TII)
# SPDX-License-Identifier: Apache-2.0

from PyP100 import PyP100
from robot.libraries.BuiltIn import BuiltIn


class TapoP100:
def __init__(self, *args, **kwargs):
self._p100 = None

def _get_p100(self):
if not self._p100:
ip_address = BuiltIn().get_variable_value("${SOCKET_IP_ADDRESS}")
username = BuiltIn().get_variable_value("${PLUG_USERNAME}")
password = BuiltIn().get_variable_value("${PLUG_PASSWORD}")
self._p100 = PyP100.P100(
ip_address, username, password
) # Creating a P100 plug object
self._p100.handshake() # Creates the cookies required for further methods
self._p100.login() # Sends credentials to the plug and creates AES Key and IV for further methods
return self._p100

def turn_plug_on(self):
self._get_p100().turnOn() # Sends the turn on request

def turn_plug_off(self):
self._get_p100().turnOff() # Sends the turn off request

def get_plug_info(self):
return self._get_p100().getDeviceInfo() # Returns dict with all the device info
37 changes: 0 additions & 37 deletions Robot-Framework/lib/TapoP100/tapo_p100.py

This file was deleted.

4 changes: 1 addition & 3 deletions Robot-Framework/test-suites/boot_test.robot
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*** Settings ***
Documentation Testing target device booting up.
Force Tags ssh_boot_test
Library ../lib/TapoP100/tapo_p100.py
# Alternatively you may use Kasa Plug
# Library ../lib/KasaPlug/kasaplug.py
Library ../lib/PlugLibrary/PlugLibrary.py
Resource ../resources/serial_keywords.resource
Resource ../resources/ssh_keywords.resource
Resource ../config/variables.robot
Expand Down

0 comments on commit 31574ae

Please sign in to comment.