コード例 #1
0
    def tests_events_callback(self):
        """Tests that event updates callback correctly."""
        # Get the event controller
        events = self.abode.events
        self.assertIsNotNone(events)

        # Create mock callbacks
        capture_callback = Mock()
        alarm_callback = Mock()

        # Register our events
        self.assertTrue(
            events.add_event_callback(
                TIMELINE.CAPTURE_GROUP, capture_callback))

        self.assertTrue(
            events.add_event_callback(TIMELINE.ALARM_GROUP, alarm_callback))

        # Call our events callback method and trigger a capture group event
        # pylint: disable=protected-access
        event_json = json.loads(IRCAMERA.timeline_event())
        events._on_timeline_update(event_json)

        # Our capture callback should get one, but our alarm should not
        capture_callback.assert_called_with(event_json)
        alarm_callback.assert_not_called()

        # Test that an invalid event exits cleanly
        events._on_timeline_update({"invalid": "event"})
コード例 #2
0
    def tests_timeline_callback(self):
        """Tests that timeline updates callback correctly."""
        # Get the event controller
        events = self.abode.events
        self.assertIsNotNone(events)

        # Create mock callbacks
        all_callback = Mock()
        image_callback = Mock()
        opened_callback = Mock()

        # Register our events
        self.assertTrue(
            events.add_timeline_callback(TIMELINE.ALL, all_callback))

        self.assertTrue(
            events.add_timeline_callback(TIMELINE.CAPTURE_IMAGE,
                                         image_callback))

        self.assertTrue(
            events.add_timeline_callback(TIMELINE.OPENED, opened_callback))

        # Call our events callback method and trigger an image capture event
        # pylint: disable=protected-access
        event_json = json.loads(IRCAMERA.timeline_event())
        events._on_timeline_update(event_json)

        # all and image callbacks should have one, opened none
        all_callback.assert_called_with(event_json)
        image_callback.assert_called_with(event_json)
        opened_callback.assert_not_called()

        # Test that an invalid event exits cleanly
        events._on_timeline_update({"invalid": "event"})
コード例 #3
0
ファイル: test_camera.py プロジェクト: netmanchris/abodepy
    def tests_camera_image_write(self, m):
        """Tests that camera images will write to a file."""
        # 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=IRCAMERA.device(devid=IRCAMERA.DEVICE_ID,
                                   status=CONST.STATUS_ONLINE,
                                   low_battery=False,
                                   no_response=False))

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

        # Get our camera
        device = self.abode.get_device(IRCAMERA.DEVICE_ID)

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

        # Set up timeline response
        url = str.replace(CONST.TIMELINE_IMAGES_ID_URL, '$DEVID$',
                          IRCAMERA.DEVICE_ID)
        m.get(url,
              text='[' + IRCAMERA.timeline_event(IRCAMERA.DEVICE_ID) + ']')

        # Set up our file path response
        file_path = CONST.BASE_URL + IRCAMERA.FILE_PATH
        m.head(file_path,
               status_code=302,
               headers={'Location': IRCAMERA.LOCATION_HEADER})

        # Set up our image response
        image_response = "this is a beautiful jpeg image"
        m.get(IRCAMERA.LOCATION_HEADER, text=image_response)

        # Refresh the image
        path = "test.jpg"
        self.assertTrue(device.image_to_file(path, get_image=True))

        # Test the file written and cleanup
        image_data = open(path, 'r').read()
        self.assertTrue(image_response, image_data)
        os.remove(path)

        # Test that bad response returns False
        m.get(IRCAMERA.LOCATION_HEADER, status_code=400)
        with self.assertRaises(abodepy.AbodeException):
            device.image_to_file(path, get_image=True)

        # Test that the image fails to update returns False
        m.get(url, text='[]')
        self.assertFalse(device.image_to_file(path, get_image=True))
コード例 #4
0
    def tests_execute_callback(self):
        """Tests that callbacks that throw exceptions don't bomb."""
        # Get the event controller
        events = self.abode.events
        self.assertIsNotNone(events)

        # Create callbacks
        def _callback(event_json):
            raise Exception("CHAOS!!!")

        # Register events callback
        self.assertTrue(
            events.add_timeline_callback(TIMELINE.CAPTURE_IMAGE, _callback))

        # Call our events callback method and trigger an image capture event
        # pylint: disable=protected-access
        event_json = json.loads(IRCAMERA.timeline_event())
        events._on_timeline_update(event_json)
コード例 #5
0
    def tests_multi_timeline_callback(self):
        """Tests that multiple timeline updates callback correctly."""
        # Get the event controller
        events = self.abode.events
        self.assertIsNotNone(events)

        # Create mock callback
        callback = Mock()

        # Register our events
        self.assertTrue(
            events.add_timeline_callback(
                [TIMELINE.CAPTURE_IMAGE, TIMELINE.OPENED], callback))

        # Call our events callback method and trigger a capture group event
        # pylint: disable=protected-access
        event_json = json.loads(IRCAMERA.timeline_event())
        events._on_timeline_update(event_json)

        # Ensure our callback was called
        callback.assert_called_with(event_json)
コード例 #6
0
    def tests_camera_image_update(self, m):
        """Tests that camera devices update correctly via timeline request."""
        # 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=IRCAMERA.device(devid=IRCAMERA.DEVICE_ID,
                                   status=CONST.STATUS_ONLINE,
                                   low_battery=False,
                                   no_response=False))

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

        # Get our camera
        device = self.abode.get_device(IRCAMERA.DEVICE_ID)

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

        # Set up timeline response
        url = str.replace(CONST.TIMELINE_IMAGES_ID_URL, '$DEVID$',
                          IRCAMERA.DEVICE_ID)
        m.get(url,
              text='[' + IRCAMERA.timeline_event(IRCAMERA.DEVICE_ID) + ']')

        # Set up our file path response
        file_path = CONST.BASE_URL + IRCAMERA.FILE_PATH
        m.head(file_path,
               status_code=302,
               headers={'Location': IRCAMERA.LOCATION_HEADER})

        # Refresh the image
        self.assertTrue(device.refresh_image())

        # Verify the image location
        self.assertEqual(device.image_url, IRCAMERA.LOCATION_HEADER)

        # Test that a bad file_path response header results in an exception
        file_path = CONST.BASE_URL + IRCAMERA.FILE_PATH
        m.head(file_path, status_code=302)

        with self.assertRaises(abodepy.AbodeException):
            device.refresh_image()

        # Test that a bad file_path response code results in an exception
        file_path = CONST.BASE_URL + IRCAMERA.FILE_PATH
        m.head(file_path,
               status_code=200,
               headers={'Location': IRCAMERA.LOCATION_HEADER})

        with self.assertRaises(abodepy.AbodeException):
            device.refresh_image()

        # Test that an an empty timeline event throws exception
        url = str.replace(CONST.TIMELINE_IMAGES_ID_URL, '$DEVID$',
                          IRCAMERA.DEVICE_ID)
        m.get(url,
              text='[' +
              IRCAMERA.timeline_event(IRCAMERA.DEVICE_ID, file_path='') + ']')

        with self.assertRaises(abodepy.AbodeException):
            device.refresh_image()

        # Test that an unexpected timeline event throws exception
        url = str.replace(CONST.TIMELINE_IMAGES_ID_URL, '$DEVID$',
                          IRCAMERA.DEVICE_ID)
        m.get(url,
              text='[' +
              IRCAMERA.timeline_event(IRCAMERA.DEVICE_ID, event_code='1234') +
              ']')

        with self.assertRaises(abodepy.AbodeException):
            device.refresh_image()