-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configurable PlugLibrary with multiple plug types
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
Showing
6 changed files
with
87 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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()) |
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,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() |
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,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 |
This file was deleted.
Oops, something went wrong.
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