Skip to content

Commit edb675e

Browse files
committed
Digital actuators return int instead of bool to prevent invalid device command response messages.
1 parent 9c2e340 commit edb675e

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

myDevices/devices/digital/helper.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from myDevices.devices.digital.gpio import NativeGPIO as GPIO
1919

2020

21-
2221
class DigitalSensor():
2322

2423
def __init__(self, gpio, channel, invert=False):
@@ -53,9 +52,8 @@ def read(self):
5352
self.setGPIOInstance()
5453
value = self.gpio.digitalRead(self.channel)
5554
if self.invert:
56-
return not value
57-
else:
58-
return value
55+
value = not value
56+
return int(value)
5957

6058
class MotionSensor(DigitalSensor):
6159
def __init__(self, gpio, channel, invert=False):
@@ -79,7 +77,7 @@ def __family__(self):
7977
@response("%d")
8078
def write(self, value):
8179
if self.invert:
82-
value = not value
80+
value = int(not value)
8381
self.gpio.digitalWrite(self.channel, value)
8482
return self.read()
8583

myDevices/test/client_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def testSensors(self):
7070

7171
def testSensorInfo(self):
7272
sensors = {'actuator' : {'description': 'Digital Output', 'device': 'DigitalActuator', 'args': {'gpio': 'GPIO', 'invert': False, 'channel': 16}, 'name': 'test_actuator'},
73+
'light_switch' : {'description': 'Light Switch', 'device': 'LightSwitch', 'args': {'gpio': 'GPIO', 'invert': True, 'channel': 15}, 'name': 'test_light_switch'},
7374
'MCP3004' : {'description': 'MCP3004', 'device': 'MCP3004', 'args': {'chip': '0'}, 'name': 'test_MCP3004'},
7475
'distance' : {'description': 'Analog Distance Sensor', 'device': 'DistanceSensor', 'args': {'adc': 'test_MCP3004', 'channel': 0}, 'name': 'test_distance'}}
7576
for sensor in sensors.values():
@@ -78,6 +79,8 @@ def testSensorInfo(self):
7879
#Test setting sensor values
7980
self.setSensorValue(sensors['actuator'], 1)
8081
self.setSensorValue(sensors['actuator'], 0)
82+
self.setSensorValue(sensors['light_switch'], 1)
83+
self.setSensorValue(sensors['light_switch'], 0)
8184
#Test getting analog value
8285
retrievedSensorInfo = next(obj for obj in SensorsClientTest.client.SensorsInfo() if obj['name'] == sensors['distance']['name'])
8386
self.assertEqual(retrievedSensorInfo['float'], 0.0)

0 commit comments

Comments
 (0)