def test_light_scenario_g1(self): d = Door() p = Photocell() p.light() l = Light(address='xx.xx.xx', devices=(d, p), mapped={ Attribute.COMMAND: (Command.CLOSE), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, ignore=({Attribute.COMMAND: Command.DARK}), name="Hallway Lights",) l.on() self.assertEqual(l.state, State.ON) d.close() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) d.open() self.assertEqual(l.state, State.OFF)
def test_delay_normal(self): # Door Open events retrigger delay # Instead of turning off in 2 secs should be 4 door = Door() self.assertIsNotNone(door) light = Light(address='D1', devices=(self.interface, door), delay={ 'command': Command.OFF, 'secs': 3, 'source': door} ) door.open() self.assertEqual(light.state, State.ON) door.close() self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.OFF) # Check to see if we can immediately and directly still turn off light.off() door.open() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF)
def test_device_level_encoded(self): d=Light(name='device_test_1') d.off() self.assertEqual(d.state, State.OFF) response = self.api.get_response(method='POST', path="device/" + str(d.type_id), data=['command=level%2C72']) self.assertEqual(d.state, (State.LEVEL, 72)) self.assertTrue('"name": "device_test_1"' in response)
def test_light_scenario_2(self): m = Motion() l = Light( address=(49, 3), devices=(m), ignore=({ Attribute.COMMAND: Command.DARK, }, { Attribute.COMMAND: Command.STILL }), time={ Attribute.TIME: '11:59pm', Attribute.COMMAND: Command.OFF }, mapped={ Attribute.COMMAND: ( Command.MOTION, Command.OPEN, Command.CLOSE, Command.LIGHT, ), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, name='Foyer Light', ) l.off() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_light_scenario_g1(self): d = Door() p = Photocell() p.light() l = Light( address='xx.xx.xx', devices=(d, p), mapped={ Attribute.COMMAND: (Command.CLOSE), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, ignore=({ Attribute.COMMAND: Command.DARK }), name="Hallway Lights", ) l.on() self.assertEqual(l.state, State.ON) d.close() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) d.open() self.assertEqual(l.state, State.OFF)
def test_trigger_in_range_gc(self): (s_h, s_m, s_s) = datetime.now().timetuple()[3:6] e_h = s_h e_m = s_m e_s = s_s + 2 d1 = StateDevice() d2 = Light(devices=d1, trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, Attribute.START: '{h}:{m}:{s}'.format( h=s_h, m=s_m, s=s_s, ), Attribute.END: '{h}:{m}:{s}'.format( h=e_h, m=e_m, s=e_s, ), }) self.assertEqual(d2.state, State.UNKNOWN) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.OFF) #Out range d2.off() self.assertEqual(d2.state, State.OFF) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.ON)
def test_light_scenario_2(self): m = Motion() l = Light( address=(49, 3), devices=(m), ignore=({ Attribute.COMMAND: Command.DARK, }, { Attribute.COMMAND: Command.STILL} ), time={ Attribute.TIME: '11:59pm', Attribute.COMMAND: Command.OFF }, mapped={ Attribute.COMMAND: ( Command.MOTION, Command.OPEN, Command.CLOSE, Command.LIGHT, ), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, name='Foyer Light', ) l.off() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_time_cron2(self): ttime = datetime.now().timetuple()[3:7] l = Light(address='12.03.BB', time={ Attribute.TIME: (ttime[2]+2,ttime[1], ttime[0], '*','*','*'), Attribute.COMMAND: Command.OFF }, name='test') l.on() self.assertEqual(l.state, State.ON) time.sleep(2) self.assertEqual(l.state, State.OFF)
def test_door_closed(self): door = Door() self.assertIsNotNone(door) door.open() self.device = Light('D1', devices=(self.interface, door)) # self.assertTrue(self.interface.initial.called) self.assertFalse(self.interface.off.called) door.close() self.assertTrue(self.interface.off.called) # self.interface.on.assert_called_once_with('') door.open() self.assertTrue(self.interface.on.called)
def test_light_restricted(self): photo = Photocell("D1", initial_state=State.LIGHT) motion = Motion("D1", initial_state=State.STILL) light = Light("D2", (motion, photo)) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) photo.dark() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON)
def test_light_restricted(self): photo = Photocell('D1', initial=State.LIGHT) self.assertEqual(photo.state, State.LIGHT) motion = Motion('D1', initial=State.STILL) light = Light('D2', devices=(motion, photo), initial=photo) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) photo.dark() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON)
def test_time_cron2(self): ttime = datetime.now().timetuple()[3:7] l = Light(address='12.03.BB', time={ Attribute.TIME: (ttime[2] + 2, ttime[1], ttime[0], '*', '*', '*'), Attribute.COMMAND: Command.OFF }, name='test') l.on() self.assertEqual(l.state, State.ON) time.sleep(2) self.assertEqual(l.state, State.OFF)
def test_scenario_g2(self): d = StateDevice() l = Light(address='1E.39.5C', devices=(d), delay={ Attribute.COMMAND: Command.OFF, Attribute.SECS: 2 }, name='Stair Lights up') self.assertEqual(l.state, State.UNKNOWN) l.off() time.sleep(3) self.assertEqual(l.state, State.OFF) l.on() self.assertEqual(l.state, State.ON)
def test_time_cron(self): light = Light('a2', time={ Attribute.COMMAND: Command.OFF, Attribute.TIME: (0, 30, range(0, 5), 0, 0) }) self.assertIsNotNone(light)
def test_motion_triggered(self): motion = Motion('D1', initial=State.STILL) self.assertEqual(motion.state, State.STILL) light = Light('D1', devices=motion) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON)
def test_light_scenario_g3(self): m1 = Motion() m2 = Motion() interface = Mock() l = Light( devices=(interface, m1, m2), ignore={ Attribute.COMMAND: Command.STILL, }, trigger=( { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m2, Attribute.SECS: 2 }, { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m1, Attribute.SECS: 10 }, ), initial=State.OFF, ) self.assertEqual(l.state, State.OFF) m1.motion() self.assertEqual(l.state, State.ON) # Interface updates us on the status l.command(command=Command.ON, source=interface) # call still just to add some noise. Should be ignored m1.still() self.assertEqual(l.state, State.ON) time.sleep(2) # Light should still be on < 10 secs self.assertEqual(l.state, State.ON) m2.motion() self.assertEqual(l.state, State.ON) # more noise to try and force an issue. Should be ignored m2.still() m1.still() self.assertEqual(l.state, State.ON) time.sleep(3) # total of 5 secs have elapsed since m1 and 3 since m2 # Light should be off as m2 set the new time to only 2 secs self.assertEqual(l.state, State.OFF)
def test_location_triggered(self): home = Location('35.2269', '-80.8433') home.local_time = datetime(2012, 6, 1, 12, 0, 0) light = Light('D1', home) self.assertEqual(light.state, State.OFF) home.local_time = datetime(2012, 6, 1, 0, 0, 0) self.assertEqual(home.state, State.DARK) self.assertEqual(light.state, State.ON)
def test_ignore_subcommand_wildcard(self): s1 = Light() s2 = Light( devices=s1, ignore={ Attribute.COMMAND: Command.LEVEL, }, ) s1.on() self.assertEqual(s2.state, State.ON) s1.off() self.assertEqual(s2.state, State.OFF) s1.level(80) self.assertEqual(s2.state, State.OFF)
def test_light_idle(self): m = Motion() m.still() l = Light( devices=(m), idle={Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, } ) l.on() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, (State.LEVEL, 30)) #Light shouldnt idle if it is off l.off() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_delegate(self): self.loc.local_time = datetime(2012,6,1,1,0,0) self.assertEqual(self.loc.state, State.DARK) l = Light(devices=self.loc) self.assertEqual(l.state, State.ON) self.assertEqual(self.loc.state, State.DARK) self.loc.local_time = datetime(2012,6,1,12,0,0) self.assertEqual(self.loc.state, State.LIGHT) self.assertEqual(l.state, State.OFF)
def test_motion_retrigger(self): i = Mock() m = Motion(devices=i, retrigger_delay={ Attribute.SECS: 2, }, ) s = Light(devices=m) s.off() self.assertEqual(s.state, State.OFF) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.ON) s.off() self.assertEqual(s.state, State.OFF) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.OFF) time.sleep(3) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.ON)
def test_pipe_interface_read(self): path = '/tmp/named_pipe_test2' pi = StateInterface(NamedPipe(path)) #pi.onCommand(self.test_pipe_interface_read_callback) d1 = Light(address=None, devices=pi) self.assertEqual(d1.state, State.UNKNOWN) pipe = os.open(path, os.O_WRONLY) os.write(pipe, State.ON) time.sleep(2) self.assertEqual(d1.state, State.ON)
def test_light_photocell_intial(self): motion = Motion() motion.still() photo = Photocell(address='asdf') photo.dark() light = Light( address='e3', devices=(photo, motion), initial=photo, ) self.assertEqual(light.state, State.ON)
def test_delay_normal(self): # Door Open events retrigger delay # Instead of turning off in 2 secs should be 4 door = Door() self.assertIsNotNone(door) light = Light(address='D1', devices=(self.interface, door), delay={ Attribute.COMMAND: Command.OFF, Attribute.SECS: 3, Attribute.SOURCE: door }) door.open() self.assertEqual(light.state, State.ON) door.close() self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.OFF) # Check to see if we can immediately and directly still turn off light.off() door.open() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF)
def test_delay_non_native_command(self): m = Motion() l = Light(devices=m, delay={ Attribute.COMMAND: Command.STILL, Attribute.SECS: 2, }, initial=State.ON) self.assertEqual(l.state, State.ON) m.still() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_trigger_in_range_gc(self): (s_h, s_m, s_s) = datetime.now().timetuple()[3:6] e_h = s_h e_m = s_m e_s = s_s + 2 d1 = StateDevice() d2 = Light( devices=d1, trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, Attribute.START: '{h}:{m}:{s}'.format( h=s_h, m=s_m, s=s_s, ), Attribute.END: '{h}:{m}:{s}'.format( h=e_h, m=e_m, s=e_s, ), } ) self.assertEqual(d2.state, State.UNKNOWN) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.OFF) #Out range d2.off() self.assertEqual(d2.state, State.OFF) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.ON)
def test_ignore_subcommand_wildcard(self): s1 = Light() s2 = Light(devices = s1, ignore={ Attribute.COMMAND: Command.LEVEL, }, ) s1.on() self.assertEqual(s2.state, State.ON) s1.off() self.assertEqual(s2.state, State.OFF) s1.level(80) self.assertEqual(s2.state, State.OFF)
def test_light_restriction_idle(self): ph = Photocell() m = Motion() ph.dark() l = Light(devices=(ph, m), idle={ Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, }) m.motion() self.assertEqual(l.state, State.ON) ph.light() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_gc_1(self): twilight_standard = Location('42.2671389', '-71.8756111', tz='US/Eastern', mode=Location.MODE.STANDARD, is_dst=True, name='Standard Twilight') twilight_standard.local_time = datetime(2012, 6, 1, 0, 0, 0) self.assertEqual(twilight_standard.state, State.DARK) _back_porch = Light( address='21.03.24', devices=(twilight_standard), initial=twilight_standard, # command=( { Attribute.COMMAND: (Command.DARK), Attribute.MAPPED: (Command.ON), Attribute.SOURCE: (twilight_standard), }, # { Attribute.COMMAND: (Command.LIGHT), Attribute.MAPPED: (Command.OFF), Attribute.SOURCE: (twilight_standard), }, ), map=( { Attribute.COMMAND: (Command.DARK), Attribute.MAPPED: (Command.ON), Attribute.SOURCE: (twilight_standard), }, { Attribute.COMMAND: (Command.LIGHT), Attribute.MAPPED: (Command.OFF), Attribute.SOURCE: (twilight_standard), }, ), ignore=({ Attribute.COMMAND: Command.OFF, Attribute.SOURCE: twilight_standard, }, ), time=({ Attribute.COMMAND: (Command.LEVEL, 30), Attribute.TIME: '11:15pm', }, ), name="Back porch light") self.assertEqual(twilight_standard.state, State.DARK) self.assertEqual(_back_porch.state, State.ON)
def test_delay_light_specific(self): # motion.off and Photocell.Light events do not retrigger motion = Motion() light = Light(address='D1', devices=(self.interface, motion), trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 3, }, ignore={ Attribute.COMMAND: Command.STILL, Attribute.SOURCE: motion, }) motion.motion() self.assertEqual(light.state, State.ON) time.sleep(2) motion.still() self.assertEqual(light.state, State.ON) time.sleep(1) self.assertEqual(light.state, State.OFF)
def test_light_scenario1(self): m = Motion() l = Light( address=(49, 6), devices=m, mapped={ Attribute.COMMAND: Command.MOTION, Attribute.SECS: 30 * 60 }, ignore=( { Attribute.COMMAND: Command.STILL }, { Attribute.COMMAND: Command.DARK }, ), name='Lamp', ) self.assertEqual(l.state, State.UNKNOWN) m.command(command=State.ON, source=None) self.assertEqual(l.state, State.UNKNOWN)
def test_light_idle(self): m = Motion() m.still() l = Light(devices=(m), idle={ Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, }) l.on() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, (State.LEVEL, 30)) #Light shouldnt idle if it is off l.off() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
def test_motion_retrigger(self): i = Mock() m = Motion( devices=i, retrigger_delay={ Attribute.SECS: 2, }, ) s = Light(devices=m) s.off() self.assertEqual(s.state, State.OFF) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.ON) s.off() self.assertEqual(s.state, State.OFF) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.OFF) time.sleep(3) m.command(command=Command.ON, source=i) self.assertEqual(s.state, State.ON)
def test_read_only(self): self.loc.local_time = datetime(2012,6,1,0,0,0) self.assertEqual(self.loc.state, State.DARK) l2 = Light() l = Light(devices=(self.loc, l2)) self.assertEqual(self.loc.state, State.DARK) l.on() self.assertEqual(l.state, State.ON) self.assertEqual(self.loc.state, State.DARK) l.off() self.assertEqual(self.loc.state, State.DARK) l2.off() self.assertEqual(self.loc.state, State.DARK) l2.on() self.assertEqual(self.loc.state, State.DARK)
class LightTests(TestCase): def setUp(self): self.interface = Mock() self.interface.state = State.UNKNOWN self.device = Light('D1', self.interface) def test_instantiation(self): self.assertIsNotNone(self.device, 'Light Device could not be instantiated') def test_on(self): self.assertEqual(self.device.state, State.UNKNOWN) self.device.on() self.assertEqual(self.device.state, State.ON) self.assertTrue(self.interface.on.called) def test_on_time(self): pass def test_door_triggered(self): door = Door() self.assertIsNotNone(door) self.device = Light('D1', devices=(self.interface, door)) door.open() self.assertTrue(self.interface.on.called) def test_door_closed(self): door = Door() self.assertIsNotNone(door) door.open() self.device = Light('D1', devices=(self.interface, door)) # self.assertTrue(self.interface.initial.called) self.assertFalse(self.interface.off.called) door.close() self.assertTrue(self.interface.off.called) # self.interface.on.assert_called_once_with('') door.open() self.assertTrue(self.interface.on.called) def test_location_triggered(self): home = Location('35.2269', '-80.8433') home.local_time = datetime(2012, 6, 1, 12, 0, 0) light = Light('D1', home) self.assertEqual(light.state, State.OFF) home.local_time = datetime(2012, 6, 1, 0, 0, 0) self.assertEqual(home.state, State.DARK) self.assertEqual(light.state, State.ON) def test_motion_triggered(self): motion = Motion('D1', initial=State.STILL) self.assertEqual(motion.state, State.STILL) light = Light('D1', devices=motion) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_photocell_triggered(self): photo = Photocell('D1', initial=State.LIGHT) light = Light('D1', devices=photo) self.assertEquals(light.state, State.OFF) photo.dark() self.assertEquals(light.state, State.ON) def test_light_restricted(self): photo = Photocell('D1', initial=State.LIGHT) self.assertEqual(photo.state, State.LIGHT) motion = Motion('D1', initial=State.STILL) light = Light('D2', devices=(motion, photo), initial=photo) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) photo.dark() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_light_unrestricted(self): photo = Photocell('D1', initial=State.LIGHT) self.assertEqual(photo.state, State.LIGHT) motion = Motion('D1', initial=State.STILL) light = Light('D2', devices=(motion, photo), initial=photo) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) motion.unrestricted = True motion.motion() self.assertEqual(light.state, State.ON) # photo.dark() # self.assertEqual(light.state, State.ON) # light.off() # self.assertEqual(light.state, State.OFF) # motion.motion() # self.assertEqual(light.state, State.ON) def test_delay_normal(self): # Door Open events retrigger delay # Instead of turning off in 2 secs should be 4 door = Door() self.assertIsNotNone(door) light = Light(address='D1', devices=(self.interface, door), delay={ Attribute.COMMAND: Command.OFF, Attribute.SECS: 3, Attribute.SOURCE: door }) door.open() self.assertEqual(light.state, State.ON) door.close() self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.OFF) # Check to see if we can immediately and directly still turn off light.off() door.open() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) def test_delay_light_specific(self): # motion.off and Photocell.Light events do not retrigger motion = Motion() light = Light(address='D1', devices=(self.interface, motion), trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 3, }, ignore={ Attribute.COMMAND: Command.STILL, Attribute.SOURCE: motion, }) motion.motion() self.assertEqual(light.state, State.ON) time.sleep(2) motion.still() self.assertEqual(light.state, State.ON) time.sleep(1) self.assertEqual(light.state, State.OFF) def test_light_photocell_intial(self): motion = Motion() motion.still() photo = Photocell(address='asdf') photo.dark() light = Light( address='e3', devices=(photo, motion), initial=photo, ) self.assertEqual(light.state, State.ON) def test_light_photocell_delay(self): ## Dont like this behavior anymore # Delay off should not trigger when photocell tells us to go dark. # Do it immediately # photo = Photocell() # photo.dark() # light = Light(address='e3', # devices=photo, # delay={ # 'command': Command.OFF, # 'secs': 3 # }) # self.assertEqual(light.state, State.ON) # photo.light() # self.assertEqual(light.state, State.OFF) pass def test_level(self): self.device.command((Command.LEVEL, 40)) self.interface.level.assert_called_with('D1', 40) def test_level_direct(self): self.device.level(50) self.interface.level.assert_called_with('D1', 50) def test_level_ramp(self): self.device.command((Command.LEVEL, 40, 20)) self.interface.level.assert_called_with('D1', 40, 20) def test_time_cron(self): light = Light('a2', time={ Attribute.COMMAND: Command.OFF, Attribute.TIME: (0, 30, range(0, 5), 0, 0) }) self.assertIsNotNone(light) def test_time_cron2(self): ttime = datetime.now().timetuple()[3:7] l = Light(address='12.03.BB', time={ Attribute.TIME: (ttime[2] + 2, ttime[1], ttime[0], '*', '*', '*'), Attribute.COMMAND: Command.OFF }, name='test') l.on() self.assertEqual(l.state, State.ON) time.sleep(2) self.assertEqual(l.state, State.OFF) def test_light_scenario1(self): m = Motion() l = Light( address=(49, 6), devices=m, mapped={ Attribute.COMMAND: Command.MOTION, Attribute.SECS: 30 * 60 }, ignore=( { Attribute.COMMAND: Command.STILL }, { Attribute.COMMAND: Command.DARK }, ), name='Lamp', ) self.assertEqual(l.state, State.UNKNOWN) m.command(command=State.ON, source=None) self.assertEqual(l.state, State.UNKNOWN) def test_light_scenario_g1(self): d = Door() p = Photocell() p.light() l = Light( address='xx.xx.xx', devices=(d, p), mapped={ Attribute.COMMAND: (Command.CLOSE), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, ignore=({ Attribute.COMMAND: Command.DARK }), name="Hallway Lights", ) l.on() self.assertEqual(l.state, State.ON) d.close() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) d.open() self.assertEqual(l.state, State.OFF) def test_light_scenario_2(self): m = Motion() l = Light( address=(49, 3), devices=(m), ignore=({ Attribute.COMMAND: Command.DARK, }, { Attribute.COMMAND: Command.STILL }), time={ Attribute.TIME: '11:59pm', Attribute.COMMAND: Command.OFF }, mapped={ Attribute.COMMAND: ( Command.MOTION, Command.OPEN, Command.CLOSE, Command.LIGHT, ), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, name='Foyer Light', ) l.off() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_scenario_g2(self): d = StateDevice() l = Light(address='1E.39.5C', devices=(d), delay={ Attribute.COMMAND: Command.OFF, Attribute.SECS: 2 }, name='Stair Lights up') self.assertEqual(l.state, State.UNKNOWN) l.off() time.sleep(3) self.assertEqual(l.state, State.OFF) l.on() self.assertEqual(l.state, State.ON) def test_delay_non_native_command(self): m = Motion() l = Light(devices=m, delay={ Attribute.COMMAND: Command.STILL, Attribute.SECS: 2, }, initial=State.ON) self.assertEqual(l.state, State.ON) m.still() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_light_scenario_g3(self): m1 = Motion() m2 = Motion() interface = Mock() l = Light( devices=(interface, m1, m2), ignore={ Attribute.COMMAND: Command.STILL, }, trigger=( { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m2, Attribute.SECS: 2 }, { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m1, Attribute.SECS: 10 }, ), initial=State.OFF, ) self.assertEqual(l.state, State.OFF) m1.motion() self.assertEqual(l.state, State.ON) # Interface updates us on the status l.command(command=Command.ON, source=interface) # call still just to add some noise. Should be ignored m1.still() self.assertEqual(l.state, State.ON) time.sleep(2) # Light should still be on < 10 secs self.assertEqual(l.state, State.ON) m2.motion() self.assertEqual(l.state, State.ON) # more noise to try and force an issue. Should be ignored m2.still() m1.still() self.assertEqual(l.state, State.ON) time.sleep(3) # total of 5 secs have elapsed since m1 and 3 since m2 # Light should be off as m2 set the new time to only 2 secs self.assertEqual(l.state, State.OFF) def test_light_restriction_idle(self): ph = Photocell() m = Motion() ph.dark() l = Light(devices=(ph, m), idle={ Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, }) m.motion() self.assertEqual(l.state, State.ON) ph.light() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_light_idle(self): m = Motion() m.still() l = Light(devices=(m), idle={ Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, }) l.on() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, (State.LEVEL, 30)) #Light shouldnt idle if it is off l.off() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_trigger_in_range_gc(self): (s_h, s_m, s_s) = datetime.now().timetuple()[3:6] e_h = s_h e_m = s_m e_s = s_s + 2 d1 = StateDevice() d2 = Light(devices=d1, trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, Attribute.START: '{h}:{m}:{s}'.format( h=s_h, m=s_m, s=s_s, ), Attribute.END: '{h}:{m}:{s}'.format( h=e_h, m=e_m, s=e_s, ), }) self.assertEqual(d2.state, State.UNKNOWN) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.OFF) #Out range d2.off() self.assertEqual(d2.state, State.OFF) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.ON) def test_trigger_out_range_gc(self): (s_h, s_m, s_s) = datetime.now().timetuple()[3:6] e_h = s_h e_m = s_m e_s = s_s + 2 d1 = StateDevice() d2 = Light(devices=d1, trigger={ Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, Attribute.START: '{h}:{m}:{s}'.format( h=s_h, m=s_m, s=s_s, ), Attribute.END: '{h}:{m}:{s}'.format( h=e_h, m=e_m, s=e_s, ), }) time.sleep(3) self.assertEqual(d2.state, State.UNKNOWN) d1.on() self.assertEqual(d2.state, State.ON) time.sleep(3) self.assertEqual(d2.state, State.ON) def test_ignore_subcommand_wildcard(self): s1 = Light() s2 = Light( devices=s1, ignore={ Attribute.COMMAND: Command.LEVEL, }, ) s1.on() self.assertEqual(s2.state, State.ON) s1.off() self.assertEqual(s2.state, State.OFF) s1.level(80) self.assertEqual(s2.state, State.OFF)
def setUp(self): self.interface = Mock() self.interface.state = State.UNKNOWN self.device = Light('D1', self.interface)
def test_door_triggered(self): door = Door() self.assertIsNotNone(door) self.device = Light('D1', devices=(self.interface, door)) door.open() self.assertTrue(self.interface.on.called)
class LightTests(TestCase): def setUp(self): self.interface = Mock() self.interface.state = State.UNKNOWN self.device = Light('D1', self.interface) def test_instantiation(self): self.assertIsNotNone(self.device, 'Light Device could not be instantiated') def test_on(self): self.assertEqual(self.device.state, State.UNKNOWN) self.device.on() self.assertEqual(self.device.state, State.ON) self.assertTrue(self.interface.on.called) def test_on_time(self): pass def test_door_triggered(self): door = Door() self.assertIsNotNone(door) self.device = Light('D1', devices=(self.interface, door)) door.open() self.assertTrue(self.interface.on.called) def test_door_closed(self): door = Door() self.assertIsNotNone(door) door.open() self.device = Light('D1', devices=(self.interface, door)) # self.assertTrue(self.interface.initial.called) self.assertFalse(self.interface.off.called) door.close() self.assertTrue(self.interface.off.called) # self.interface.on.assert_called_once_with('') door.open() self.assertTrue(self.interface.on.called) def test_location_triggered(self): home = Location('35.2269', '-80.8433') home.local_time = datetime(2012,6,1,12,0,0) light = Light('D1', home) self.assertEqual(light.state, State.OFF) home.local_time = datetime(2012,6,1,0,0,0) self.assertEqual(home.state, State.DARK) self.assertEqual(light.state, State.ON) def test_motion_triggered(self): motion = Motion('D1', initial=State.STILL) self.assertEqual(motion.state, State.STILL) light = Light('D1', devices=motion) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_photocell_triggered(self): photo = Photocell('D1', initial=State.LIGHT) light = Light('D1', devices=photo) self.assertEquals(light.state, State.OFF) photo.dark() self.assertEquals(light.state, State.ON) def test_light_restricted(self): photo = Photocell('D1', initial=State.LIGHT) self.assertEqual(photo.state, State.LIGHT) motion = Motion('D1', initial=State.STILL) light = Light('D2', devices=(motion, photo), initial=photo) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) photo.dark() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_delay_normal(self): # Door Open events retrigger delay # Instead of turning off in 2 secs should be 4 door = Door() self.assertIsNotNone(door) light = Light(address='D1', devices=(self.interface, door), delay={ 'command': Command.OFF, 'secs': 3, 'source': door} ) door.open() self.assertEqual(light.state, State.ON) door.close() self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.OFF) # Check to see if we can immediately and directly still turn off light.off() door.open() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) def test_delay_light_specific(self): # motion.off and Photocell.Light events do not retrigger motion = Motion() light = Light(address='D1', devices=(self.interface, motion), trigger={ 'command': Command.ON, 'mapped': Command.OFF, 'secs': 3, }, ignore={ 'command': Command.STILL, 'source': motion, } ) motion.motion() self.assertEqual(light.state, State.ON) time.sleep(2) motion.still() self.assertEqual(light.state, State.ON) time.sleep(1) self.assertEqual(light.state, State.OFF) def test_light_photocell_intial(self): motion = Motion() motion.still() photo = Photocell(address='asdf') photo.dark() light = Light(address='e3', devices=(photo, motion), initial=photo, ) self.assertEqual(light.state, State.ON) def test_light_photocell_delay(self): ## Dont like this behavior anymore # Delay off should not trigger when photocell tells us to go dark. # Do it immediately # photo = Photocell() # photo.dark() # light = Light(address='e3', # devices=photo, # delay={ # 'command': Command.OFF, # 'secs': 3 # }) # self.assertEqual(light.state, State.ON) # photo.light() # self.assertEqual(light.state, State.OFF) pass def test_level(self): self.device.command((Command.LEVEL, 40)) self.interface.level.assert_called_with('D1', 40) def test_time_cron(self): light = Light('a2', time={ 'command': Command.OFF, 'time':(0, 30, range(0,5), 0, 0) }) self.assertIsNotNone(light) def test_time_cron2(self): ttime = datetime.now().timetuple()[3:7] l = Light(address='12.03.BB', time={ Attribute.TIME: (ttime[2]+2,ttime[1], ttime[0], '*','*','*'), Attribute.COMMAND: Command.OFF }, name='test') l.on() self.assertEqual(l.state, State.ON) time.sleep(2) self.assertEqual(l.state, State.OFF) def test_light_scenario1(self): m = Motion() l = Light( address=(49, 6), devices=m, mapped={ Attribute.COMMAND: Command.MOTION, Attribute.SECS: 30*60 }, ignore=({ Attribute.COMMAND: Command.STILL }, { Attribute.COMMAND: Command.DARK }, ), name='Lamp', ) self.assertEqual(l.state, State.UNKNOWN) m.command(command=State.ON, source=None) self.assertEqual(l.state, State.UNKNOWN) def test_light_scenario_g1(self): d = Door() p = Photocell() p.light() l = Light(address='xx.xx.xx', devices=(d, p), mapped={ Attribute.COMMAND: (Command.CLOSE), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, ignore=({Attribute.COMMAND: Command.DARK}), name="Hallway Lights",) l.on() self.assertEqual(l.state, State.ON) d.close() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) d.open() self.assertEqual(l.state, State.OFF) def test_light_scenario_2(self): m = Motion() l = Light( address=(49, 3), devices=(m), ignore=({ Attribute.COMMAND: Command.DARK, }, { Attribute.COMMAND: Command.STILL} ), time={ Attribute.TIME: '11:59pm', Attribute.COMMAND: Command.OFF }, mapped={ Attribute.COMMAND: ( Command.MOTION, Command.OPEN, Command.CLOSE, Command.LIGHT, ), Attribute.MAPPED: Command.OFF, Attribute.SECS: 2, }, name='Foyer Light', ) l.off() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_scenario_g2(self): d = StateDevice() l = Light(address='1E.39.5C', devices=(d), delay={ Attribute.COMMAND: Command.OFF, Attribute.SECS: 2 }, name='Stair Lights up') self.assertEqual(l.state, State.UNKNOWN) l.off() time.sleep(3) self.assertEqual(l.state, State.OFF) l.on() self.assertEqual(l.state, State.ON) def test_delay_non_native_command(self): m = Motion() l = Light( devices=m, delay={ Attribute.COMMAND: Command.STILL, Attribute.SECS: 2, }, initial=State.ON ) self.assertEqual(l.state, State.ON) m.still() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_light_scenario_g3(self): m1 = Motion() m2 = Motion() interface = Mock() l = Light( devices=(interface, m1, m2), ignore={ Attribute.COMMAND: Command.STILL, }, trigger=( { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m2, Attribute.SECS: 2 }, { Attribute.COMMAND: Command.ON, Attribute.MAPPED: Command.OFF, Attribute.SOURCE: m1, Attribute.SECS: 10 }, ), initial=State.OFF, ) self.assertEqual(l.state, State.OFF) m1.motion() self.assertEqual(l.state, State.ON) # Interface updates us on the status l.command(command=Command.ON, source=interface) # call still just to add some noise. Should be ignored m1.still() self.assertEqual(l.state, State.ON) time.sleep(2) # Light should still be on < 10 secs self.assertEqual(l.state, State.ON) m2.motion() self.assertEqual(l.state, State.ON) # more noise to try and force an issue. Should be ignored m2.still() m1.still() self.assertEqual(l.state, State.ON) time.sleep(3) # total of 5 secs have elapsed since m1 and 3 since m2 # Light should be off as m2 set the new time to only 2 secs self.assertEqual(l.state, State.OFF) def test_light_restriction_idle(self): ph = Photocell() m = Motion() ph.dark() l = Light( devices=(ph, m), idle={Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, } ) m.motion() self.assertEqual(l.state, State.ON) ph.light() self.assertEqual(l.state, State.OFF) m.motion() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF) def test_light_idle(self): m = Motion() m.still() l = Light( devices=(m), idle={Attribute.MAPPED: (Command.LEVEL, 30), Attribute.SECS: 2, } ) l.on() self.assertEqual(l.state, State.ON) time.sleep(3) self.assertEqual(l.state, (State.LEVEL, 30)) #Light shouldnt idle if it is off l.off() self.assertEqual(l.state, State.OFF) time.sleep(3) self.assertEqual(l.state, State.OFF)
# invert the DIO channels for these contact sensors sg.dio_invert(1) sg.dio_invert(8) ###################### DEVICE CONFIG ######################### d_foyer = Door('D1', sg) m_family = Motion('D8', sg) # Motion sensor is hardwired and immediate OFF.. Want to give it some time to still detect motion right after m_family.delay_still(2*60) ph_sun = Location('35.2269', '-80.8433', tz='US/Eastern', mode=Location.MODE.STANDARD, is_dst=True) # Turn on the foyer light at night when either the door is opened or family PIR is tripped. l_foyer = Light((49, 3), (upb, d_foyer, m_family, ph_sun)) # After being turned on, turn off again after 2 minutes of inactivity. l_foyer.delay_off(2*60) # Turn off the light no matter what at 11:59pm l_foyer.time_off('11:59pm') # Do not turn on the light automatically when it is night time (indoor light) # Only looks at dark for restricing the whether the light should come on l_foyer.ignore_dark(True) ##################### USER CODE ############################### #Manually controlling the light l_foyer.on() l_foyer.off() # Loop control def MainLoop(*args, **kwargs):
#Create PLM and setup Raspberry PI board and web server #Note: Raspberry PI inputs require root access insteon = InsteonPLM2(Serial('/dev/ttyUSB0', 19200, xonxoff=False)) GPIO.setmode(GPIO.BOARD) web = HTTPServer(address='raspberrypi.home') ph_standard = Location('53.55', '-113.5', tz='Canada/Mountain', mode=Location.MODE.STANDARD, is_dst=True, name='Standard Photocell') ll_livingroom1 = Light(address='18.97.08', devices=(insteon), name="Living Room Lamp 1") ll_livingroom2 = Light(address='14.27.D3', devices=(insteon), name="Living Room Lamp 2") sl_livingroom1 = Light(address='17.F7.8C', devices=(insteon), name="Living Room Lamps") sl_livingroom2 = Light(address='16.67.06', devices=(insteon), name="Living Room Lights") kl_master1 = Light(address='15.64.1D', devices=(insteon), name="Master Bedroom")
class LightTests(TestCase): def setUp(self): self.interface = Mock() self.interface.state = State.UNKNOWN self.device = Light("D1", self.interface) def test_instantiation(self): self.assertIsNotNone(self.device, "Light Device could not be instantiated") def test_on(self): self.assertEqual(self.device.state, self.device.UNKNOWN) self.device.on() self.assertEqual(self.device.state, self.device.ON) self.assertTrue(self.interface.on.called) def test_on_time(self): pass def test_door_triggered(self): door = Door() self.assertIsNotNone(door) self.device = Light("D1", (self.interface, door)) door.open() self.assertTrue(self.interface.on.called) def test_door_closed(self): door = Door() self.assertIsNotNone(door) door.open() self.device = Light("D1", (self.interface, door)) self.assertTrue(self.interface.on.called) self.assertFalse(self.interface.off.called) door.closed() self.assertTrue(self.interface.off.called) # self.interface.on.assert_called_once_with('') door.open() self.assertTrue(self.interface.on.called) def test_location_triggered(self): home = Location("35.2269", "-80.8433") home.local_time = datetime(2012, 6, 1, 12, 0, 0) light = Light("D1", home) self.assertEqual(light.state, State.OFF) home.local_time = datetime(2012, 6, 1, 0, 0, 0) self.assertEqual(home.state, State.DARK) self.assertEqual(light.state, State.ON) def test_motion_triggered(self): motion = Motion("D1", initial_state=State.STILL) self.assertEqual(motion.state, State.STILL) light = Light("D1", motion) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_photocell_triggered(self): photo = Photocell("D1", initial_state=State.LIGHT) light = Light("D1", photo) self.assertEquals(light.state, State.OFF) photo.dark() self.assertEquals(light.state, State.ON) def test_light_restricted(self): photo = Photocell("D1", initial_state=State.LIGHT) motion = Motion("D1", initial_state=State.STILL) light = Light("D2", (motion, photo)) self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.OFF) photo.dark() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) motion.motion() self.assertEqual(light.state, State.ON) def test_delay_normal(self): # Door Open events retrigger delay # Instead of turning off in 2 secs should be 4 door = Door() self.assertIsNotNone(door) light = Light(address="D1", devices=(self.interface, door), delay_off=3) door.open() self.assertEqual(light.state, State.ON) door.closed() self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.ON) time.sleep(2) self.assertEqual(light.state, State.OFF) # Check to see if we can immediately and directly still turn off light.off() door.open() self.assertEqual(light.state, State.ON) light.off() self.assertEqual(light.state, State.OFF) def test_delay_light_specific(self): # Motion.Still and Photocell.Light events do not retrigger motion = Motion() light = Light(address="D1", devices=(self.interface, motion), delay_off=3) motion.motion() self.assertEqual(light.state, State.ON) time.sleep(2) motion.still() self.assertEqual(light.state, State.ON) time.sleep(1) self.assertEqual(light.state, State.OFF) def test_light_photocell_intial(self): motion = Motion() motion.still() photo = Photocell(address="asdf") photo.dark() light = Light(address="e3", devices=(photo, motion), initial_state=photo) self.assertEqual(light.state, State.ON) def test_light_photocell_delay(self): # Delay off should not trigger when photocell tells us to go dark. # Do it immediately photo = Photocell() photo.dark() light = Light(address="e3", devices=photo, delay_off=3) self.assertEqual(light.state, State.ON) photo.light() self.assertEqual(light.state, State.OFF) def test_level(self): self.device.l40() self.assertTrue(self.interface.l40.called) def test_time_cron(self): light = Light("a2", time_off=(0, 30, range(0, 5), 0, 0)) self.assertIsNotNone(light)
def test_photocell_triggered(self): photo = Photocell('D1', initial=State.LIGHT) light = Light('D1', devices=photo) self.assertEquals(light.state, State.OFF) photo.dark() self.assertEquals(light.state, State.ON)