Exemple #1
0
    def tests_device_callback(self, m):
        """Tests that 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))

        # 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)

        callback = Mock()

        # Register our device id
        self.assertTrue(
            events.add_device_callback(device.device_id, callback))

        # Set up device update URL
        device_url = str.replace(CONST.DEVICE_URL,
                                 '$DEVID$', COVER.DEVICE_ID)
        m.get(device_url, text=COVER.device(devid=COVER.DEVICE_ID,
                                            status=CONST.STATUS_OPEN,
                                            low_battery=False,
                                            no_response=False))

        # Call our device callback method
        # pylint: disable=protected-access
        events._on_device_update(device.device_id)
        callback.assert_called_with(device)

        # Test that our device updated
        self.assertEqual(device.status, CONST.STATUS_OPEN)

        # Test that no device ID cleanly returns
        events._on_device_update(None)

        # Test that an unknown device cleanly returns
        events._on_device_update(DOORCONTACT.DEVICE_ID)
Exemple #2
0
    def tests_device_registration(self, m):
        """Tests that we can register for device events with a device."""
        # 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))

        # 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)

        def _our_callback(device):
            self.assertIsNotNone(device)

        # Register our device
        self.assertTrue(events.add_device_callback(device, _our_callback))
    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)
Exemple #4
0
    def tests_cover_device_properties(self, m):
        """Tests that cover devices 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))
        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 power switch
        device = self.abode.get_device(COVER.DEVICE_ID)

        # Test our device
        self.assertIsNotNone(device)
        self.assertEqual(device.status, CONST.STATUS_CLOSED)
        self.assertFalse(device.battery_low)
        self.assertFalse(device.no_response)
        self.assertFalse(device.is_on)
        self.assertFalse(device.is_open)

        # Set up our direct device get url
        device_url = str.replace(CONST.DEVICE_URL, '$DEVID$', COVER.DEVICE_ID)

        # Change device properties
        m.get(device_url,
              text=COVER.device(devid=COVER.DEVICE_ID,
                                status=CONST.STATUS_OPEN,
                                low_battery=True,
                                no_response=True))

        # Refesh device and test changes
        device.refresh()

        self.assertEqual(device.status, CONST.STATUS_OPEN)
        self.assertTrue(device.battery_low)
        self.assertTrue(device.no_response)
        self.assertTrue(device.is_on)
        self.assertTrue(device.is_open)
Exemple #5
0
    def tests_cover_status_changes(self, m):
        """Tests that cover device changes 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))
        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 power switch
        device = self.abode.get_device(COVER.DEVICE_ID)

        # Test that we have our device
        self.assertIsNotNone(device)
        self.assertEqual(device.status, CONST.STATUS_CLOSED)
        self.assertFalse(device.is_open)

        # Set up control url response
        control_url = CONST.BASE_URL + COVER.CONTROL_URL
        m.put(control_url,
              text=DEVICES.status_put_response_ok(
                  devid=COVER.DEVICE_ID, status=CONST.STATUS_OPEN_INT))

        # Change the cover to open
        self.assertTrue(device.open_cover())
        self.assertEqual(device.status, CONST.STATUS_OPEN)
        self.assertTrue(device.is_open)

        # Change response
        m.put(control_url,
              text=DEVICES.status_put_response_ok(
                  devid=COVER.DEVICE_ID, status=CONST.STATUS_CLOSED_INT))

        # Change the mode to "off"
        self.assertTrue(device.close_cover())
        self.assertEqual(device.status, CONST.STATUS_CLOSED)
        self.assertFalse(device.is_open)
Exemple #6
0
    def tests_alarm_callback(self, m):
        """Tests that alarm 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))

        # Logout to reset everything
        self.abode.logout()

        # Get our alarm device
        alarm = self.abode.get_alarm()
        self.assertIsNotNone(alarm)

        # Get the event controller
        events = self.abode.events
        self.assertIsNotNone(events)

        callback = Mock()

        # Register our alarm for callback
        self.assertTrue(
            events.add_device_callback(alarm.device_id, callback))

        # Call our mode changed callback method
        # pylint: disable=protected-access
        events._on_mode_change(CONST.MODE_HOME)
        callback.assert_called_with(alarm)

        # Test that our alarm state is set properly
        self.assertEqual(alarm.mode, CONST.MODE_HOME)

        # Test that no mode cleanly returns
        events._on_mode_change(None)

        # Test that an unknown mode cleanly returns
        events._on_mode_change("lol")
Exemple #7
0
    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)
Exemple #8
0
    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__))