Skip to content

Commit ee33c27

Browse files
committed
updates after QA
1 parent 5a164ad commit ee33c27

File tree

3 files changed

+33
-57
lines changed

3 files changed

+33
-57
lines changed

docs/examples/motion_sensor_callbacks.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22
from time import sleep
33

44
pir = MotionSensor(2)
5-
led = LED(14)
65

7-
print("Advanced PIR Motion Sensor Example")
8-
print("Using event callbacks for motion detection...")
6+
# Create an LED on pin 25 (built-in LED)
7+
led = LED(25)
98

109

1110
def motion_detected():
12-
print("🚶 Motion detected!")
13-
led.on() # Turn on LED when motion detected
11+
led.on()
1412

1513

1614
def no_motion():
17-
print("😴 No motion detected")
18-
led.off() # Turn off LED when no motion
15+
led.off()
1916

2017

2118
# Set up event callbacks

picozero/picozero.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,35 @@ class Button(Switch):
19991999
Button.when_released = Button.when_deactivated
20002000

20012001

2002+
class MotionSensor(DigitalInputDevice):
2003+
"""
2004+
Represents a PIR (Passive Infrared) motion sensor (e.g. HC-SR501)
2005+
2006+
:param int pin:
2007+
The pin that the motion sensor is connected to.
2008+
2009+
:param bool pull_up:
2010+
If :data:`True` (the default), the device will be pulled up to
2011+
HIGH. If :data:`False`, the device will be pulled down to LOW.
2012+
Most PIR sensors work with pull_up=False.
2013+
2014+
:param float bounce_time:
2015+
The bounce time for the device. If set, the device will ignore
2016+
any motion events that happen within the bounce time after a
2017+
motion event. This is useful to prevent false triggers.
2018+
Defaults to 0.02 seconds.
2019+
"""
2020+
2021+
def __init__(self, pin, pull_up=False, bounce_time=0.02):
2022+
super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time)
2023+
2024+
2025+
MotionSensor.motion_detected = MotionSensor.is_active
2026+
# Note: No alias for is_inactive - use 'not pir.motion_detected' for clarity
2027+
MotionSensor.when_motion = MotionSensor.when_activated
2028+
MotionSensor.when_no_motion = MotionSensor.when_deactivated
2029+
2030+
20022031
class AnalogInputDevice(InputDevice, PinMixin):
20032032
"""
20042033
Represents a generic input device with analogue functionality, e.g.
@@ -2247,31 +2276,3 @@ def max_distance(self):
22472276
Returns the maximum distance that the sensor will measure in metres.
22482277
"""
22492278
return self._max_distance
2250-
2251-
2252-
class MotionSensor(DigitalInputDevice):
2253-
"""
2254-
Represents a PIR (Passive Infrared) motion sensor (e.g. HC-SR501)
2255-
2256-
:param int pin:
2257-
The pin that the motion sensor is connected to.
2258-
2259-
:param bool pull_up:
2260-
If :data:`True` (the default), the device will be pulled up to
2261-
HIGH. If :data:`False`, the device will be pulled down to LOW.
2262-
Most PIR sensors work with pull_up=False.
2263-
2264-
:param float bounce_time:
2265-
The bounce time for the device. If set, the device will ignore
2266-
any motion events that happen within the bounce time after a
2267-
motion event. This is useful to prevent false triggers.
2268-
Defaults to 0.02 seconds.
2269-
"""
2270-
2271-
def __init__(self, pin, pull_up=False, bounce_time=0.02):
2272-
super().__init__(pin=pin, pull_up=pull_up, bounce_time=bounce_time)
2273-
2274-
2275-
MotionSensor.motion_detected = MotionSensor.is_active
2276-
MotionSensor.when_motion = MotionSensor.when_activated
2277-
MotionSensor.when_no_motion = MotionSensor.when_deactivated

tests/test_picozero.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -675,27 +675,5 @@ def test_pico_temp_sensor(self):
675675
self.assertEqual(pico_temp_sensor.pin, 4)
676676
self.assertIsNotNone(pico_temp_sensor.temp)
677677

678-
def test_motion_sensor_aliases(self):
679-
"""
680-
MotionSensor is a simple subclass of DigitalInputDevice that adds
681-
aliases. Tests verify the aliases exist and map correctly.
682-
"""
683-
d = MotionSensor(1)
684-
685-
# Verify that aliases exist and map to the correct properties
686-
self.assertTrue(hasattr(d, "motion_detected"))
687-
self.assertTrue(hasattr(d, "when_motion"))
688-
self.assertTrue(hasattr(d, "when_no_motion"))
689-
690-
# Verify aliases point to the correct underlying properties
691-
self.assertIs(d.motion_detected, d.is_active)
692-
self.assertIs(d.when_motion, d.when_activated)
693-
self.assertIs(d.when_no_motion, d.when_deactivated)
694-
695-
# Verify it's properly a DigitalInputDevice subclass
696-
self.assertIsInstance(d, DigitalInputDevice)
697-
698-
d.close()
699-
700678

701679
unittest.main()

0 commit comments

Comments
 (0)