Skip to content

Commit

Permalink
driver/powerdriver: ykushpower: board_name support
Browse files Browse the repository at this point in the history
When 6425df9 switched from pykush to
ykushcmd, support for the YKUSH 3 and YKUSH XS was lost. Restore support
for those boards by passing the board_name argument to ykushcmd. The
board_name is an optional argument for the YKUSHPowerPort and
NetworkYKUSHPowerPort resources.

Added tests for YKUSHPowerPort and YKUSHPowerPort.

Signed-off-by: Paul Vittorino <[email protected]>
  • Loading branch information
PaulVittorino committed Oct 10, 2023
1 parent 2906645 commit 0fe4b8c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
3 changes: 2 additions & 1 deletion doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ A YKUSHPowerPort describes a YEPKIT YKUSH USB (HID) switchable USB hub.
YKUSHPowerPort:
serial: YK12345
index: 1
board_name: ykush3
The example describes port 1 on the YKUSH USB hub with the
serial "YK12345".
(use "ykushcmd -l" to get your serial...)
(use "ykushcmd ykush3 -l" to get your serial...)

Arguments:
- serial (str): serial number of the YKUSH hub
Expand Down
3 changes: 3 additions & 0 deletions labgrid/driver/powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ def __attrs_post_init__(self):
def on(self):
cmd = [
self.tool,
f"{self.port.board_name}",
"-s", f"{self.port.serial}",
"-u", f"{self.port.index}"
]
Expand All @@ -298,6 +299,7 @@ def on(self):
def off(self):
cmd = [
self.tool,
f"{self.port.board_name}",
"-s", f"{self.port.serial}",
"-d", f"{self.port.index}"
]
Expand All @@ -314,6 +316,7 @@ def cycle(self):
def get(self):
cmd = [
self.tool,
f"{self.port.board_name}",
"-s", f"{self.port.serial}",
"-g", f"{self.port.index}"
]
Expand Down
37 changes: 30 additions & 7 deletions labgrid/resource/ykushpowerport.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,42 @@ class YKUSHPowerPort(Resource):
Args:
serial (str): serial of the YKUSH device
index (int): port index"""
index (int): port index
board_name (str, default="ykush"): optional, model of YKUSH board (ykush, ykushxs, ykush3)
"""

serial = attr.ib(validator=attr.validators.instance_of(str))
index = attr.ib(validator=attr.validators.instance_of(int),
converter=int)
index = attr.ib(validator=attr.validators.instance_of(int), converter=int)
board_name = attr.ib(
default="ykush",
validator=attr.validators.optional(
attr.validators.and_(
attr.validators.instance_of(str),
attr.validators.in_(["ykush", "ykushxs", "ykush3"]),
)
),
)


@target_factory.reg_resource
@attr.s(eq=False)
class NetworkYKUSHPowerPort(NetworkResource):
""""This resource describes a remote YEPKIT YKUSH switchable USB hub.
"""This resource describes a remote YEPKIT YKUSH switchable USB hub.
Args:
serial (str): serial of the YKUSH device
index (int): port index"""
index (int): port index
board_name (str, default="ykush"): optional, model of YKUSH board (ykush, ykushxs, ykush3)
"""

serial = attr.ib(validator=attr.validators.instance_of(str))
index = attr.ib(validator=attr.validators.instance_of(int),
converter=int)
index = attr.ib(validator=attr.validators.instance_of(int), converter=int)
board_name = attr.ib(
default="ykush",
validator=attr.validators.optional(
attr.validators.and_(
attr.validators.instance_of(str),
attr.validators.in_(["ykush", "ykushxs", "ykush3"]),
)
),
)
54 changes: 52 additions & 2 deletions tests/test_powerdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import pytest

from labgrid.resource import NetworkPowerPort
from labgrid.driver.powerdriver import ExternalPowerDriver, ManualPowerDriver, NetworkPowerDriver
from labgrid.resource import NetworkPowerPort, YKUSHPowerPort
from labgrid.driver.powerdriver import (
ExternalPowerDriver,
ManualPowerDriver,
NetworkPowerDriver,
YKUSHPowerDriver,
)
from labgrid.util.helper import processwrapper


class TestManualPowerDriver:
Expand Down Expand Up @@ -264,3 +270,47 @@ def test_import_backend_tplink(self):
def test_import_backend_siglent(self):
pytest.importorskip("vxi11")
import labgrid.driver.power.siglent

class TestYKUSHPowerDriver:
FAKE_SERIAL = 'YK12345'
def test_create_default(self, target):
resource = YKUSHPowerPort(target, 'power', serial=self.FAKE_SERIAL, index=1)
device = YKUSHPowerDriver(target, 'power')
assert isinstance(device, YKUSHPowerDriver)

def test_create_ykush3(self, target):
resource = YKUSHPowerPort(
target, 'power', serial=self.FAKE_SERIAL, index=1, board_name='ykush3'
)
device = YKUSHPowerDriver(target, 'power')
assert isinstance(device, YKUSHPowerDriver)

def test_default_off(self, target, mocker):
check_call_mock = mocker.patch(
'labgrid.util.helper.processwrapper.check_output'
)
resource = YKUSHPowerPort(
target, 'power', serial=self.FAKE_SERIAL, index=2, board_name='power'
)
device = YKUSHPowerDriver(target, 'power')
target.activate(device)
device.off()

check_call_mock.assert_called_with(
['ykushcmd', 'ykush', '-s', self.FAKE_SERIAL, '-d', '2']
)

def test_ykush3_on(self, target, mocker):
check_call_mock = mocker.patch(
'labgrid.util.helper.processwrapper.check_output'
)
resource = YKUSHPowerPort(
target, 'power', serial=self.FAKE_SERIAL, index=3, board_name='ykush3'
)
device = YKUSHPowerDriver(target, 'power')
target.activate(device)
device.on()

check_call_mock.assert_called_with(
['ykushcmd', 'ykush3', '-s', self.FAKE_SERIAL, '-u', '3']
)

0 comments on commit 0fe4b8c

Please sign in to comment.