Skip to content

Commit c7f8aef

Browse files
change peripheral function inputs
1 parent 4de303e commit c7f8aef

File tree

4 files changed

+94
-76
lines changed

4 files changed

+94
-76
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ sidecar-starter-pack/
238238
| `control_servo()` | Set servo angle between 0-100 degrees. |
239239
| `control_peripheral()` | Set peripheral value (digital ON/OFF or analog value from 0-255). |
240240
| `toggle_peripheral()` | Toggles the state of a specific peripheral between 'ON' (100%) and 'OFF' (0%). |
241-
| `on()` | Turns specified peripheral 'ON' (100%). |
242-
| `off()` | Turns specified peripheral 'OFF' (0%). |
241+
| `on()` | Turns specified pin number 'ON' (100%). |
242+
| `off()` | Turns specified pin number 'OFF' (0%). |
243243

244244
### resources.py
245245

functions/peripherals.py

+51-27
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,34 @@ def control_servo(self, pin, angle):
4141
self.state.print_status(description=f"Set servo angle to {angle}.")
4242
return
4343

44-
def control_peripheral(self, peripheral_id, value, mode=None):
44+
def get_peripheral(self, peripheral_name):
45+
"""Find a peripheral by name."""
46+
peripherals = self.info.api_get("peripherals")
47+
peripheral_names = [peripheral["label"] for peripheral in peripherals]
48+
if peripheral_name not in peripheral_names:
49+
error = f"ERROR: '{peripheral_name}' peripheral not in {peripheral_names}."
50+
self.state.print_status(description=error, update_only=True)
51+
self.state.error = error
52+
return
53+
54+
peripheral = [p for p in peripherals if p["label"] == peripheral_name][0]
55+
return peripheral
56+
57+
def control_peripheral(self, peripheral_name, value, mode=None):
4558
"""Set peripheral value and mode."""
59+
self.state.print_status(description=f"Setting {peripheral_name} to {value}.")
4660

47-
if mode is None:
48-
peripheral = self.info.api_get("peripherals", peripheral_id)
49-
mode = peripheral["mode"]
61+
peripheral = self.get_peripheral(peripheral_name)
62+
if peripheral is None:
63+
return
64+
peripheral_id = peripheral["id"]
65+
pin_mode = peripheral["mode"] if mode is None else mode
5066

5167
control_peripheral_message = {
5268
"kind": "write_pin",
5369
"args": {
5470
"pin_value": value, # Controls ON/OFF or slider value from 0-255
55-
"pin_mode": mode, # Controls digital (0) or analog (1) mode
71+
"pin_mode": pin_mode, # Controls digital (0) or analog (1) mode
5672
"pin_number": {
5773
"kind": "named_pin",
5874
"args": {
@@ -65,11 +81,13 @@ def control_peripheral(self, peripheral_id, value, mode=None):
6581

6682
self.broker.publish(control_peripheral_message)
6783

68-
self.state.print_status(description=f"Set peripheral {peripheral_id} to {value} with mode={mode}.")
69-
70-
def toggle_peripheral(self, peripheral_id):
84+
def toggle_peripheral(self, peripheral_name):
7185
"""Toggles the state of a specific peripheral between `on` and `off`."""
72-
86+
self.state.print_status(description=f"Toggling {peripheral_name}.")
87+
peripheral = self.get_peripheral(peripheral_name)
88+
if peripheral is None:
89+
return
90+
peripheral_id = peripheral["id"]
7391
toggle_peripheral_message = {
7492
"kind": "toggle_pin",
7593
"args": {
@@ -85,26 +103,32 @@ def toggle_peripheral(self, peripheral_id):
85103

86104
self.broker.publish(toggle_peripheral_message)
87105

88-
self.state.print_status(description=f"Triggered toggle peripheral {peripheral_id}.")
89-
90-
def on(self, peripheral_id):
91-
"""Turns specified peripheral `on` (100%)."""
92-
self.state.print_status(description=f"Turning ON peripheral {peripheral_id}.")
93-
94-
peripheral = self.info.api_get("peripherals", peripheral_id)
95-
mode = peripheral["mode"]
106+
def on(self, pin_number):
107+
"""Turns specified pin number `on` (100%)."""
108+
self.state.print_status(description=f"Turning ON pin number {pin_number}.")
96109

97-
if mode == 1:
98-
self.control_peripheral(peripheral_id, 255, mode)
99-
elif mode == 0:
100-
self.control_peripheral(peripheral_id, 1, mode)
110+
on_message = {
111+
"kind": "write_pin",
112+
"args": {
113+
"pin_value": 1,
114+
"pin_mode": 0,
115+
"pin_number": pin_number,
116+
}
117+
}
101118

102-
return
119+
self.broker.publish(on_message)
103120

104-
def off(self, peripheral_id):
105-
"""Turns specified peripheral `off` (0%)."""
106-
self.state.print_status(description=f"Turning OFF peripheral {peripheral_id}.")
121+
def off(self, pin_number):
122+
"""Turns specified pin number `off` (0%)."""
123+
self.state.print_status(description=f"Turning OFF pin number {pin_number}.")
107124

108-
self.control_peripheral(peripheral_id, 0)
125+
off_message = {
126+
"kind": "write_pin",
127+
"args": {
128+
"pin_value": 0,
129+
"pin_mode": 0,
130+
"pin_number": pin_number,
131+
}
132+
}
109133

110-
return
134+
self.broker.publish(off_message)

main.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,21 @@ def control_servo(self, pin, angle):
199199
"""Set servo angle between 0-100 degrees."""
200200
return self.peripherals.control_servo(pin, angle)
201201

202-
def control_peripheral(self, peripheral_id, value, mode=None):
202+
def control_peripheral(self, peripheral_name, value, mode=None):
203203
"""Set peripheral value and mode."""
204-
return self.peripherals.control_peripheral(peripheral_id, value, mode)
204+
return self.peripherals.control_peripheral(peripheral_name, value, mode)
205205

206-
def toggle_peripheral(self, peripheral_id):
206+
def toggle_peripheral(self, peripheral_name):
207207
"""Toggles the state of a specific peripheral between `on` and `off`."""
208-
return self.peripherals.toggle_peripheral(peripheral_id)
208+
return self.peripherals.toggle_peripheral(peripheral_name)
209209

210-
def on(self, peripheral_id):
211-
"""Turns specified peripheral `on` (100%)."""
212-
return self.peripherals.on(peripheral_id)
210+
def on(self, pin_number):
211+
"""Turns specified pin number `on` (100%)."""
212+
return self.peripherals.on(pin_number)
213213

214-
def off(self, peripheral_id):
215-
"""Turns specified peripheral `off` (0%)."""
216-
return self.peripherals.off(peripheral_id)
214+
def off(self, pin_number):
215+
"""Turns specified pin number `off` (0%)."""
216+
return self.peripherals.off(pin_number)
217217

218218
# resources.py
219219

tests/tests_main.py

+31-37
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ def exec_command():
822822
def test_toggle_peripheral(self):
823823
'''Test toggle_peripheral command'''
824824
def exec_command():
825-
self.fb.toggle_peripheral(123)
825+
self.fb.toggle_peripheral('New Peripheral')
826826
self.send_command_test_helper(
827827
exec_command,
828828
expected_command={
@@ -835,67 +835,51 @@ def exec_command():
835835
},
836836
},
837837
extra_rpc_args={},
838-
mock_api_response={})
838+
mock_api_response=[{'label': 'New Peripheral', 'id': 123}])
839839

840-
def test_on_digital(self):
841-
'''Test on command: digital'''
840+
def test_toggle_peripheral_not_found(self):
841+
'''Test toggle_peripheral command: peripheral not found'''
842842
def exec_command():
843-
self.fb.on(123)
843+
self.fb.toggle_peripheral('New Peripheral')
844844
self.send_command_test_helper(
845845
exec_command,
846-
expected_command={
847-
'kind': 'write_pin',
848-
'args': {
849-
'pin_value': 1,
850-
'pin_mode': 0,
851-
'pin_number': {
852-
'kind': 'named_pin',
853-
'args': {'pin_type': 'Peripheral', 'pin_id': 123},
854-
},
855-
},
856-
},
846+
expected_command=None,
857847
extra_rpc_args={},
858-
mock_api_response={'mode': 0})
848+
mock_api_response=[])
859849

860-
def test_on_analog(self):
861-
'''Test on command: analog'''
850+
def test_on_digital(self):
851+
'''Test on command: digital'''
862852
def exec_command():
863-
self.fb.on(123)
853+
self.fb.on(13)
864854
self.send_command_test_helper(
865855
exec_command,
866856
expected_command={
867857
'kind': 'write_pin',
868858
'args': {
869-
'pin_value': 255,
870-
'pin_mode': 1,
871-
'pin_number': {
872-
'kind': 'named_pin',
873-
'args': {'pin_type': 'Peripheral', 'pin_id': 123},
874-
},
859+
'pin_value': 1,
860+
'pin_mode': 0,
861+
'pin_number': 13,
875862
},
876863
},
877864
extra_rpc_args={},
878-
mock_api_response={'mode': 1})
865+
mock_api_response={})
879866

880867
def test_off(self):
881868
'''Test off command'''
882869
def exec_command():
883-
self.fb.off(123)
870+
self.fb.off(13)
884871
self.send_command_test_helper(
885872
exec_command,
886873
expected_command={
887874
'kind': 'write_pin',
888875
'args': {
889876
'pin_value': 0,
890-
'pin_mode': 1,
891-
'pin_number': {
892-
'kind': 'named_pin',
893-
'args': {'pin_type': 'Peripheral', 'pin_id': 123},
894-
},
877+
'pin_mode': 0,
878+
'pin_number': 13,
895879
},
896880
},
897881
extra_rpc_args={},
898-
mock_api_response={'mode': 1})
882+
mock_api_response={})
899883

900884
def test_move(self):
901885
'''Test move command'''
@@ -963,13 +947,13 @@ def exec_command():
963947
def test_control_peripheral(self):
964948
'''Test control_peripheral command'''
965949
def exec_command():
966-
self.fb.control_peripheral(123, 456, 0)
950+
self.fb.control_peripheral('New Peripheral', 1)
967951
self.send_command_test_helper(
968952
exec_command,
969953
expected_command={
970954
'kind': 'write_pin',
971955
'args': {
972-
'pin_value': 456,
956+
'pin_value': 1,
973957
'pin_mode': 0,
974958
'pin_number': {
975959
'kind': 'named_pin',
@@ -978,7 +962,17 @@ def exec_command():
978962
},
979963
},
980964
extra_rpc_args={},
981-
mock_api_response={'mode': 0})
965+
mock_api_response=[{'label': 'New Peripheral', 'mode': 0, 'id': 123}])
966+
967+
def test_control_peripheral_not_found(self):
968+
'''Test control_peripheral command: peripheral not found'''
969+
def exec_command():
970+
self.fb.control_peripheral('New Peripheral', 1)
971+
self.send_command_test_helper(
972+
exec_command,
973+
expected_command=None,
974+
extra_rpc_args={},
975+
mock_api_response=[])
982976

983977
def test_measure_soil_height(self):
984978
'''Test measure_soil_height command'''

0 commit comments

Comments
 (0)