Skip to content
Permalink
Browse files

driver: onewiredriver: fix get() method

To read the physical value, the /sensed.N property needs to be read.
Also check for possible error conditions.

Signed-off-by: Jan Luebbe <jlu@pengutronix.de>
  • Loading branch information...
jluebbe committed Jul 5, 2019
1 parent b7109fe commit 4993a4c27436634c17da9e3cc076688dca1fc4c2
Showing with 21 additions and 9 deletions.
  1. +14 −3 labgrid/driver/onewiredriver.py
  2. +7 −6 tests/test_onewire.py
@@ -1,11 +1,13 @@
from importlib import import_module
import attr

from ..exceptions import InvalidConfigError
from ..factory import target_factory
from ..resource import OneWirePIO
from ..protocol import DigitalOutputProtocol
from ..util.proxy import proxymanager
from .common import Driver
from .exception import ExecutionError

@target_factory.reg_driver
@attr.s(cmp=False)
@@ -17,6 +19,10 @@ def __attrs_post_init__(self):
super().__attrs_post_init__()
self._module = import_module('onewire')
self._onewire = None
if "PIO" not in self.port.path:
raise InvalidConfigError(
"Invalid OneWire path {} (needs to be in the form of ??.????????????/PIO.?)"
.format(self.port.path))

def on_activate(self):
# we can only forward if the backend knows which port to use
@@ -39,8 +45,13 @@ def set(self, status):

@Driver.check_active
def get(self):

status = self._onewire.get(self.port.path)
path = self.port.path.replace("PIO", "sensed")
status = self._onewire.get(path)
if status is None:
raise ExecutionError("Failed to get OneWire value for {}".format(path))
if status not in ['0', '1']:
raise ExecutionError("Invalid OneWire value ({})".format(repr(status)))
status = True if status == '1' else False
if self.port.invert:
status = not status
return status == '1'
return status
@@ -7,13 +7,13 @@

@pytest.fixture(scope='function')
def onewire_port(target):
return OneWirePIO(target, "pio", "testhost", "path")
return OneWirePIO(target, "pio", "testhost", "29.123450000000/PIO.6")

@pytest.fixture(scope='function')
def onewire_driver(target, onewire_port, monkeypatch, mocker):
onewire_mock = mocker.MagicMock
onewire_mock.set = mocker.MagicMock()
onewire_mock.get = mocker.MagicMock()
onewire_mock.get = mocker.MagicMock(return_value='1')
import onewire
monkeypatch.setattr(onewire, 'Onewire', onewire_mock)
s = OneWirePIODriver(target, "pio")
@@ -29,12 +29,13 @@ def test_onewire_driver_instance(target, onewire_driver):

def test_onewire_set(onewire_driver):
onewire_driver.set(True)
onewire_driver._onewire.set.assert_called_once_with("path", '1')
onewire_driver._onewire.set.assert_called_once_with("29.123450000000/PIO.6", '1')

def test_onewire_unset(onewire_driver):
onewire_driver.set(False)
onewire_driver._onewire.set.assert_called_once_with("path", '0')
onewire_driver._onewire.set.assert_called_once_with("29.123450000000/PIO.6", '0')

def test_onewire_get(onewire_driver):
onewire_driver.get()
onewire_driver._onewire.get.assert_called_once_with("path")
val = onewire_driver.get()
onewire_driver._onewire.get.assert_called_once_with("29.123450000000/sensed.6")
assert val == True

0 comments on commit 4993a4c

Please sign in to comment.
You can’t perform that action at this time.