Example #1
0
 def setUp(self):
     """Set up Skybell module."""
     self.skybell_no_cred = skybellpy.Skybell(login_sleep=False)
     self.skybell = skybellpy.Skybell(username=USERNAME,
                                      password=PASSWORD,
                                      disable_cache=True,
                                      login_sleep=False)
Example #2
0
    def test_empty_cookies(self, m):
        """Check that empty cookies file is loaded successfully."""
        m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok())

        # Test empty cookies file
        empty_cache_path = "./test_cookies_empty.pickle"

        # Remove the file if it exists
        if os.path.exists(empty_cache_path):
            os.remove(empty_cache_path)

        # Create an empty file
        with open(empty_cache_path, 'a'):
            os.utime(empty_cache_path, None)

        # Assert that empty cookies file exists
        self.assertTrue(os.path.exists(empty_cache_path))

        # Cookies are created
        empty_skybell = skybellpy.Skybell(username='******',
                                          password='******',
                                          auto_login=False,
                                          cache_path=empty_cache_path,
                                          login_sleep=False)

        # Test that our cookies are fully realized prior to login
        # pylint: disable=W0212
        self.assertIsNotNone(empty_skybell._cache['app_id'])
        self.assertIsNotNone(empty_skybell._cache['client_id'])
        self.assertIsNotNone(empty_skybell._cache['token'])
        self.assertIsNone(empty_skybell._cache['access_token'])
Example #3
0
    def tests_auto_fetch(self, m):
        """Test that automatic device retrieval works."""
        access_token = MOCK.ACCESS_TOKEN
        login_json = LOGIN.post_response_ok(access_token)

        m.post(CONST.LOGIN_URL, text=login_json)
        m.get(CONST.DEVICES_URL, text=DEVICE.EMPTY_DEVICE_RESPONSE)

        skybell = skybellpy.Skybell(username='******',
                                    password='******',
                                    get_devices=True,
                                    disable_cache=True)

        # pylint: disable=W0212
        self.assertEqual(skybell._username, 'fizz')
        self.assertEqual(skybell._password, 'buzz')
        self.assertEqual(skybell._cache['access_token'], MOCK.ACCESS_TOKEN)
        self.assertEqual(len(skybell._devices), 0)

        skybell.logout()

        skybell = None
Example #4
0
    def tests_auto_login(self, m):
        """Test that automatic login works."""
        access_token = MOCK.ACCESS_TOKEN
        login_json = LOGIN.post_response_ok(access_token)

        m.post(CONST.LOGIN_URL, text=login_json)

        skybell = skybellpy.Skybell(username='******',
                                    password='******',
                                    auto_login=True,
                                    get_devices=False,
                                    disable_cache=True)

        # pylint: disable=W0212
        self.assertEqual(skybell._username, 'fizz')
        self.assertEqual(skybell._password, 'buzz')
        self.assertEqual(skybell._cache['access_token'], MOCK.ACCESS_TOKEN)
        self.assertIsNone(skybell._devices)

        skybell.logout()

        skybell = None
Example #5
0
 def setUp(self):
     """Set up Skybell module."""
     self.skybell = skybellpy.Skybell(username=USERNAME,
                                      password=PASSWORD,
                                      disable_cache=True)
Example #6
0
    def tests_cookies(self, m):
        """Check that cookies are saved and loaded successfully."""
        m.post(CONST.LOGIN_URL, text=LOGIN.post_response_ok())

        # Define test pickle file and cleanup old one if exists
        cache_path = "./test_cookies.pickle"

        if os.path.exists(cache_path):
            os.remove(cache_path)

        # Assert that no cookies file exists
        self.assertFalse(os.path.exists(cache_path))

        # Cookies are created
        skybell = skybellpy.Skybell(username='******',
                                    password='******',
                                    auto_login=False,
                                    cache_path=cache_path,
                                    login_sleep=False)

        # Test that our cookies are fully realized prior to login
        # pylint: disable=W0212
        self.assertIsNotNone(skybell._cache['app_id'])
        self.assertIsNotNone(skybell._cache['client_id'])
        self.assertIsNotNone(skybell._cache['token'])
        self.assertIsNone(skybell._cache['access_token'])

        # Login to get the access_token
        skybell.login()

        # Test that we now have an access token
        self.assertIsNotNone(skybell._cache['access_token'])

        # Test that we now have a cookies file
        self.assertTrue(os.path.exists(cache_path))

        # Copy our current cookies file and data
        first_pickle = open(cache_path, 'rb').read()
        first_cookies_data = skybell._cache

        # Test that logout clears the auth token
        skybell.logout()

        self.assertIsNone(skybell._cache['access_token'])

        # Tests that our pickle file has changed with the cleared token
        self.assertNotEqual(first_pickle, open(cache_path, 'rb').read())

        # New skybell instance reads in old data
        skybell = skybellpy.Skybell(username='******',
                                    password='******',
                                    auto_login=False,
                                    cache_path=cache_path,
                                    login_sleep=False)

        # Test that the cookie data is the same
        self.assertEqual(skybell._cache['app_id'],
                         first_cookies_data['app_id'])
        self.assertEqual(skybell._cache['client_id'],
                         first_cookies_data['client_id'])
        self.assertEqual(skybell._cache['token'],
                         first_cookies_data['token'])

        # Cleanup cookies
        os.remove(cache_path)
Example #7
0
def call():
    """Execute command line helper."""
    args = get_arguments()

    # Set up logging
    if args.debug:
        log_level = logging.DEBUG
    elif args.quiet:
        log_level = logging.WARN
    else:
        log_level = logging.INFO

    setup_logging(log_level)

    skybell = None

    try:
        # Create skybellpy instance.
        skybell = skybellpy.Skybell(username=args.username,
                                    password=args.password,
                                    get_devices=True)

    # # Set setting
    # for setting in args.set or []:
    #     keyval = setting.split("=")
    #     if skybell.set_setting(keyval[0], keyval[1]):
    #         _LOGGER.info("Setting %s changed to %s", keyval[0], keyval[1])

        # Output Json
        for device_id in args.json or []:
            device = skybell.get_device(device_id)

            if device:
                # pylint: disable=protected-access
                _LOGGER.info(device_id + " JSON:\n" +
                             json.dumps(device._device_json, sort_keys=True,
                                        indent=4, separators=(',', ': ')))
            else:
                _LOGGER.warning("Could not find device with id: %s", device_id)

        # Print
        def _device_print(dev, append=''):
            _LOGGER.info("%s%s",
                         dev.desc, append)

        # Print out all devices.
        if args.devices:
            for device in skybell.get_devices():
                _device_print(device)

        # Print out specific devices by device id.
        if args.device:
            for device_id in args.device:
                device = skybell.get_device(device_id)

                if device:
                    _device_print(device)
                else:
                    _LOGGER.warning(
                        "Could not find device with id: %s", device_id)

        # Print out last motion event
        if args.last_json:
            for device_id in args.last_json:
                device = skybell.get_device(device_id)

                if device:
                    _LOGGER.info(device.latest(CONST.EVENT_MOTION))
                else:
                    _LOGGER.warning(
                        "Could not find device with id: %s", device_id)

        # Print out last motion event
        if args.last_image:
            for device_id in args.last_image:
                device = skybell.get_device(device_id)

                if device:
                    _LOGGER.info(device.image)
                else:
                    _LOGGER.warning(
                        "Could not find device with id: %s", device_id)

    except SkybellException as exc:
        _LOGGER.error(exc)