diff --git a/doc/configuration.rst b/doc/configuration.rst index 553418d03..e86f53452 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -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 diff --git a/labgrid/driver/powerdriver.py b/labgrid/driver/powerdriver.py index 80c8377fb..c04dcefcd 100644 --- a/labgrid/driver/powerdriver.py +++ b/labgrid/driver/powerdriver.py @@ -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}" ] @@ -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}" ] @@ -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}" ] diff --git a/labgrid/resource/ykushpowerport.py b/labgrid/resource/ykushpowerport.py index b7a967816..6759fab96 100644 --- a/labgrid/resource/ykushpowerport.py +++ b/labgrid/resource/ykushpowerport.py @@ -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"]), + ) + ), + ) diff --git a/tests/test_powerdriver.py b/tests/test_powerdriver.py index 3c7b3dbab..366f4773d 100644 --- a/tests/test_powerdriver.py +++ b/tests/test_powerdriver.py @@ -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: @@ -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'] + )