def test_all_device_refresh(self, m): """Check that device refresh works and reuses the same objects.""" dc1_devid = 'RF:01' dc1a = DOOR_CONTACT.device( devid=dc1_devid, status=CONST.STATUS_ON) dc2_devid = 'RF:02' dc2a = DOOR_CONTACT.device( devid=dc2_devid, status=CONST.STATUS_OFF) m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.get(CONST.OAUTH_TOKEN_URL, text=OAUTH_CLAIMS.get_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.DEVICES_URL, text='[' + dc1a + ',' + dc2a + ']') m.get(CONST.PANEL_URL, text=PANEL.get_response_ok()) # Reset self.abode.logout() # Get all devices self.abode.get_devices() # Get and check devices # pylint: disable=W0212 dc1a_dev = self.abode.get_device(dc1_devid) self.assertEqual(json.loads(dc1a)['id'], dc1a_dev.device_id) dc2a_dev = self.abode.get_device(dc2_devid) self.assertEqual(json.loads(dc2a)['id'], dc2a_dev.device_id) # Change device states dc1b = DOOR_CONTACT.device( devid=dc1_devid, status=CONST.STATUS_OFF) dc2b = DOOR_CONTACT.device( devid=dc2_devid, status=CONST.STATUS_ON) m.get(CONST.DEVICES_URL, text='[' + dc1b + ',' + dc2b + ']') # Refresh all devices self.abode.get_devices(refresh=True) # Get and check devices again, ensuring they are the same object # Future note: "if a is b" tests that the object is the same # Thus asserting dc1a_dev is dc1b_dev tests if they are the same object dc1b_dev = self.abode.get_device(dc1_devid) self.assertEqual(json.loads(dc1b)['id'], dc1b_dev.device_id) self.assertIs(dc1a_dev, dc1b_dev) dc2b_dev = self.abode.get_device(dc2_devid) self.assertEqual(json.loads(dc2b)['id'], dc2b_dev.device_id) self.assertIs(dc2a_dev, dc2b_dev)
def tests_invalid_all_device_unregister(self, m): """Tests that invalid devices are not all unregistered.""" # Set up URL's m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.get(CONST.OAUTH_TOKEN_URL, text=OAUTH_CLAIMS.get_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.PANEL_URL, text=PANEL.get_response_ok(mode=CONST.MODE_STANDBY)) m.get(CONST.DEVICES_URL, text=COVER.device(devid=COVER.DEVICE_ID, status=CONST.STATUS_CLOSED, low_battery=False, no_response=False)) # Logout to reset everything self.abode.logout() # Get our device device = self.abode.get_device(COVER.DEVICE_ID) self.assertIsNotNone(device) # Get the event controller events = self.abode.events self.assertIsNotNone(events) # Test that no device returns false self.assertFalse(events.remove_all_device_callbacks(None)) # Create a fake device and attempt to unregister that fake_device = AbodeBinarySensor(json.loads(DOORCONTACT.device()), self.abode) with self.assertRaises(abodepy.AbodeException): events.remove_all_device_callbacks(fake_device)
def tests_multiple_devices(self, m): """Tests that multiple devices are returned properly.""" # Set up URL's m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.get(CONST.OAUTH_TOKEN_URL, text=OAUTH_CLAIMS.get_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.PANEL_URL, text=PANEL.get_response_ok(mode=CONST.MODE_STANDBY)) # Set up a list of devices dev_list = '[' + \ POWERSENSOR.device() + "," + \ DOOR_CONTACT.device() + "," + \ GLASS.device() + ']' m.get(CONST.DEVICES_URL, text=dev_list) # Logout to reset everything self.abode.logout() # Get our devices devices = self.abode.get_devices() # Assert four devices - three from above + 1 alarm self.assertIsNotNone(devices) self.assertEqual(len(devices), 4) # Get each individual device by device ID psd = self.abode.get_device(POWERSENSOR.DEVICE_ID) self.assertIsNotNone(psd) # Get each individual device by device ID psd = self.abode.get_device(DOOR_CONTACT.DEVICE_ID) self.assertIsNotNone(psd) # Get each individual device by device ID psd = self.abode.get_device(GLASS.DEVICE_ID) self.assertIsNotNone(psd)
def tests_binary_sensor_properties(self, m): """Tests that binary sensor device properties work as expected.""" # Set up URL's m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.get(CONST.OAUTH_TOKEN_URL, text=OAUTH_CLAIMS.get_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.PANEL_URL, text=PANEL.get_response_ok(mode=CONST.MODE_STANDBY)) # Set up all Binary Sensor Devices in "off states" all_devices = '[' + \ DOOR_CONTACT.device(devid=DOOR_CONTACT.DEVICE_ID, status=CONST.STATUS_CLOSED, low_battery=False, no_response=False) + ',' + \ GLASS.device(devid=GLASS.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ',' + \ KEYPAD.device(devid=KEYPAD.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ',' + \ REMOTE_CONTROLLER.device(devid=REMOTE_CONTROLLER.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ',' + \ SIREN.device(devid=SIREN.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ',' + \ STATUS_DISPLAY.device(devid=STATUS_DISPLAY.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ',' + \ WATER_SENSOR.device(devid=WATER_SENSOR.DEVICE_ID, status=CONST.STATUS_OFFLINE, low_battery=False, no_response=False) + ']' m.get(CONST.DEVICES_URL, text=all_devices) # Logout to reset everything self.abode.logout() # Test our devices for device in self.abode.get_devices(): # Skip alarm devices if device.type == CONST.DEVICE_ALARM: continue self.assertFalse(device.is_on, device.type + " is_on failed") self.assertFalse(device.battery_low, device.type + " battery_low failed") self.assertFalse(device.no_response, device.type + " no_response failed") # Set up all Binary Sensor Devices in "off states" all_devices = '[' + \ DOOR_CONTACT.device(devid=DOOR_CONTACT.DEVICE_ID, status=CONST.STATUS_OPEN, low_battery=True, no_response=True) + ',' + \ GLASS.device(devid=GLASS.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ',' + \ KEYPAD.device(devid=KEYPAD.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ',' + \ REMOTE_CONTROLLER.device(devid=REMOTE_CONTROLLER.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ',' + \ SIREN.device(devid=SIREN.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ',' + \ STATUS_DISPLAY.device(devid=STATUS_DISPLAY.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ',' + \ WATER_SENSOR.device(devid=WATER_SENSOR.DEVICE_ID, status=CONST.STATUS_ONLINE, low_battery=True, no_response=True) + ']' m.get(CONST.DEVICES_URL, text=all_devices) # Refesh devices and test changes for device in self.abode.get_devices(refresh=True): # Skip alarm devices if device.type_tag == CONST.DEVICE_ALARM: continue self.assertTrue(device.is_on, device.type + " is_on failed") self.assertTrue(device.battery_low, device.type + " battery_low failed") self.assertTrue(device.no_response, device.type + " no_response failed")
def tests_multi_device_callback(self, m): """Tests that multiple device updates callback correctly.""" # Set up URL's m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.PANEL_URL, text=PANEL.get_response_ok(mode=CONST.MODE_STANDBY)) m.get(CONST.DEVICES_URL, text='[' + COVER.device(devid=COVER.DEVICE_ID, status=CONST.STATUS_CLOSED, low_battery=False, no_response=False) + ", " + DOORCONTACT.device(devid=DOORCONTACT.DEVICE_ID, status=CONST.STATUS_CLOSED) + ']') # Logout to reset everything self.abode.logout() # Get our devices cover = self.abode.get_device(COVER.DEVICE_ID) self.assertIsNotNone(cover) doorcontact = self.abode.get_device(DOORCONTACT.DEVICE_ID) self.assertIsNotNone(doorcontact) # Get the event controller events = self.abode.events self.assertIsNotNone(events) callback = Mock() # Register our devices self.assertTrue( events.add_device_callback([cover, doorcontact], callback)) # Set up device update URL's cover_url = str.replace(CONST.DEVICE_URL, '$DEVID$', COVER.DEVICE_ID) m.get(cover_url, text=COVER.device(devid=COVER.DEVICE_ID, status=CONST.STATUS_OPEN, low_battery=False, no_response=False)) door_url = str.replace(CONST.DEVICE_URL, '$DEVID$', DOORCONTACT.DEVICE_ID) m.get(door_url, text=DOORCONTACT.device(devid=COVER.DEVICE_ID, status=CONST.STATUS_OPEN)) # Call our device callback method for our cover # pylint: disable=protected-access events._on_device_update(cover.device_id) callback.assert_called_with(cover) # Test that our device updated self.assertEqual(cover.status, CONST.STATUS_OPEN) # Test that our other device didn't update self.assertEqual(doorcontact.status, CONST.STATUS_CLOSED) # Call our device callback method for our door contact events._on_device_update(doorcontact.device_id) callback.assert_has_calls([call(cover), call(doorcontact)]) # Test that our door updated now self.assertEqual(doorcontact.status, CONST.STATUS_OPEN)
def tests_all_devices(self, m): """Tests that all mocked devices are mapped correctly.""" # Set up URL's m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok()) m.get(CONST.OAUTH_TOKEN_URL, text=OAUTH_CLAIMS.get_response_ok()) m.post(CONST.LOGOUT_URL, text=LOGOUT.post_response_ok()) m.get(CONST.PANEL_URL, text=PANEL.get_response_ok(mode=CONST.MODE_STANDBY)) # Create all devices all_devices = '[' + \ DOOR_CONTACT.device() + ',' + \ DOOR_LOCK.device() + ',' + \ GLASS.device() + ',' + \ IR_CAMERA.device() + ',' + \ KEYPAD.device() + ',' + \ PIR.device() + ',' + \ POWERMETER.device() + ',' + \ POWERSENSOR.device() + ',' + \ REMOTE_CONTROLLER.device() + ',' + \ SECUREBARRIER.device() + ',' + \ SIREN.device() + ',' + \ STATUS_DISPLAY.device() + ',' + \ WATER_SENSOR.device() + ']' m.get(CONST.DEVICES_URL, text=all_devices) # Logout to reset everything self.abode.logout() # Loop through all devices for device in self.abode.get_devices(): class_type = { # Alarm CONST.TYPE_ALARM: AbodeAlarm, # Binary Sensors CONST.TYPE_CONNECTIVITY: AbodeBinarySensor, CONST.TYPE_MOISTURE: AbodeBinarySensor, CONST.TYPE_OPENING: AbodeBinarySensor, CONST.TYPE_MOTION: AbodeBinarySensor, CONST.TYPE_OCCUPANCY: AbodeBinarySensor, # Camera CONST.TYPE_CAMERA: AbodeDevice, # Cover CONST.TYPE_COVER: AbodeCover, # Dimmer CONST.TYPE_LIGHT: AbodeLight, # Lock CONST.TYPE_LOCK: AbodeLock, # Switch CONST.TYPE_SWITCH: AbodeSwitch }.get(device.generic_type) self.assertIsNotNone(class_type, device.type + ' is not mapped.') self.assertTrue( isinstance(device, class_type), device.type + ' is of class ' + str(device.__class__.__name__) + ' but mapped to ' + str(class_type.__name__))