示例#1
0
class TestBlinkCameraSetup(unittest.TestCase):
    """Test the Blink class in blinkpy."""
    def setUp(self):
        """Set up Blink module."""
        self.blink = Blink()
        self.blink.urls = BlinkURLHandler("test")
        self.blink.sync["test"] = BlinkSyncModule(self.blink, "test", 1234, [])
        self.camera = BlinkCamera(self.blink.sync["test"])
        self.camera.name = "foobar"
        self.blink.sync["test"].cameras["foobar"] = self.camera

    def tearDown(self):
        """Clean up after test."""
        self.blink = None
        self.camera = None

    def test_camera_update(self, mock_resp):
        """Test that we can properly update camera properties."""
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "/thumb",
        }
        self.camera.last_record = ["1"]
        self.camera.sync.last_record = {
            "new": {
                "clip": "/test.mp4",
                "time": "1970-01-01T00:00:00"
            }
        }
        mock_resp.side_effect = [
            {
                "temp": 71
            },
            "test",
            "foobar",
        ]
        self.camera.update(config)
        self.assertEqual(self.camera.name, "new")
        self.assertEqual(self.camera.camera_id, "1234")
        self.assertEqual(self.camera.network_id, "5678")
        self.assertEqual(self.camera.serial, "12345678")
        self.assertEqual(self.camera.motion_enabled, False)
        self.assertEqual(self.camera.battery, "ok")
        self.assertEqual(self.camera.temperature, 68)
        self.assertEqual(self.camera.temperature_c, 20)
        self.assertEqual(self.camera.temperature_calibrated, 71)
        self.assertEqual(self.camera.wifi_strength, 4)
        self.assertEqual(self.camera.thumbnail,
                         "https://rest-test.immedia-semi.com/thumb.jpg")
        self.assertEqual(self.camera.clip,
                         "https://rest-test.immedia-semi.com/test.mp4")
        self.assertEqual(self.camera.image_from_cache, "test")
        self.assertEqual(self.camera.video_from_cache, "foobar")

    def test_no_thumbnails(self, mock_resp):
        """Tests that thumbnail is 'None' if none found."""
        mock_resp.return_value = "foobar"
        self.camera.last_record = ["1"]
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "",
        }
        self.camera.sync.homescreen = {"devices": []}
        self.assertEqual(self.camera.temperature_calibrated, None)
        with self.assertLogs() as logrecord:
            self.camera.update(config, force=True)
        self.assertEqual(self.camera.thumbnail, None)
        self.assertEqual(self.camera.last_record, ["1"])
        self.assertEqual(self.camera.temperature_calibrated, 68)
        self.assertEqual(
            logrecord.output,
            [
                ("WARNING:blinkpy.camera:Could not retrieve calibrated "
                 "temperature."),
                ("WARNING:blinkpy.camera:Could not find thumbnail for camera new"
                 ),
            ],
        )

    def test_no_video_clips(self, mock_resp):
        """Tests that we still proceed with camera setup with no videos."""
        mock_resp.return_value = "foobar"
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "/foobar",
        }
        self.camera.sync.homescreen = {"devices": []}
        self.camera.update(config, force_cache=True)
        self.assertEqual(self.camera.clip, None)
        self.assertEqual(self.camera.video_from_cache, None)

    def test_camera_arm_status(self, mock_resp):
        """Test arming and disarming camera."""
        self.camera.motion_enabled = None
        self.assertFalse(self.camera.arm)
        self.camera.motion_enabled = False
        self.assertFalse(self.camera.arm)
        self.camera.motion_enabled = True
        self.assertTrue(self.camera.arm)

    @mock.patch("blinkpy.camera.api.request_motion_detection_enable")
    @mock.patch("blinkpy.camera.api.request_motion_detection_disable")
    def test_motion_detection_enable_disable(self, mock_dis, mock_en,
                                             mock_rep):
        """Test setting motion detection enable properly."""
        mock_dis.return_value = "disable"
        mock_en.return_value = "enable"
        self.assertEqual(self.camera.set_motion_detect(True), "enable")
        self.assertEqual(self.camera.set_motion_detect(False), "disable")

    def test_missing_attributes(self, mock_resp):
        """Test that attributes return None if missing."""
        self.camera.temperature = None
        self.camera.serial = None
        attr = self.camera.attributes
        self.assertEqual(attr["serial"], None)
        self.assertEqual(attr["temperature"], None)
        self.assertEqual(attr["temperature_c"], None)

    def test_mini_missing_attributes(self, mock_resp):
        """Test that attributes return None if missing."""
        camera = BlinkCameraMini(self.blink.sync)
        self.blink.sync.network_id = None
        self.blink.sync.name = None
        attr = camera.attributes
        for key in attr:
            self.assertEqual(attr[key], None)
示例#2
0
class TestBlinkCameraSetup(unittest.TestCase):
    """Test the Blink class in blinkpy."""

    def setUp(self):
        """Set up Blink module."""
        self.blink = blinkpy.Blink(username=USERNAME,
                                   password=PASSWORD)
        header = {
            'Host': 'abc.zxc',
            'TOKEN_AUTH': mresp.LOGIN_RESPONSE['authtoken']['authtoken']
        }
        # pylint: disable=protected-access
        self.blink._auth_header = header
        self.blink.session = create_session()
        self.blink.urls = BlinkURLHandler('test')
        self.blink.sync['test'] = BlinkSyncModule(self.blink, 'test', 1234)
        self.camera = BlinkCamera(self.blink.sync['test'])
        self.camera.name = 'foobar'
        self.blink.sync['test'].cameras['foobar'] = self.camera

    def tearDown(self):
        """Clean up after test."""
        self.blink = None

    def test_check_for_motion(self, mock_sess):
        """Test check for motion function."""
        self.assertEqual(self.camera.last_record, [])
        self.assertEqual(self.camera.motion_detected, None)
        self.camera.sync.record_dates = {'foobar': [1, 3, 2, 4]}
        self.camera.check_for_motion()
        self.assertEqual(self.camera.last_record, [4])
        self.assertEqual(self.camera.motion_detected, False)
        self.camera.sync.record_dates = {'foobar': [7, 1, 3, 4]}
        self.camera.check_for_motion()
        self.assertEqual(self.camera.last_record, [7, 4])
        self.assertEqual(self.camera.motion_detected, True)
        self.camera.check_for_motion()
        self.assertEqual(self.camera.last_record, [7, 4])
        self.assertEqual(self.camera.motion_detected, False)

    def test_max_motion_clips(self, mock_sess):
        """Test that we only maintain certain number of records."""
        for i in range(0, MAX_CLIPS):
            self.camera.last_record.append(i)
        self.camera.sync.record_dates['foobar'] = [MAX_CLIPS+2]
        self.assertEqual(len(self.camera.last_record), MAX_CLIPS)
        self.camera.check_for_motion()
        self.assertEqual(self.camera.motion_detected, True)
        self.assertEqual(len(self.camera.last_record), MAX_CLIPS)

    def test_camera_update(self, mock_sess):
        """Test that we can properly update camera properties."""
        config = {
            'name': 'new',
            'camera_id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '/thumb',
        }
        self.camera.last_record = ['1']
        self.camera.sync.all_clips = {'new': {'1': '/test.mp4'}}
        mock_sess.side_effect = [
            'test',
            'foobar'
        ]
        self.camera.update(config)
        self.assertEqual(self.camera.name, 'new')
        self.assertEqual(self.camera.camera_id, '1234')
        self.assertEqual(self.camera.network_id, '5678')
        self.assertEqual(self.camera.serial, '12345678')
        self.assertEqual(self.camera.motion_enabled, False)
        self.assertEqual(self.camera.battery, 50)
        self.assertEqual(self.camera.temperature, 68)
        self.assertEqual(self.camera.temperature_c, 20)
        self.assertEqual(self.camera.wifi_strength, 4)
        self.assertEqual(self.camera.thumbnail,
                         'https://rest.test.immedia-semi.com/thumb.jpg')
        self.assertEqual(self.camera.clip,
                         'https://rest.test.immedia-semi.com/test.mp4')
        self.assertEqual(self.camera.image_from_cache, 'test')
        self.assertEqual(self.camera.video_from_cache, 'foobar')

    def test_thumbnail_not_in_info(self, mock_sess):
        """Test that we grab thumbanil if not in camera_info."""
        mock_sess.side_effect = ['foobar', 'barfoo']
        self.camera.last_record = ['1']
        self.camera.sync.record_dates['new'] = ['1']
        self.camera.sync.all_clips = {'new': {'1': '/test.mp4'}}
        config = {
            'name': 'new',
            'camera_id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '',
        }
        self.camera.sync.homescreen = {
            'devices': [
                {'foo': 'bar'},
                {'device_type': 'foobar'},
                {'device_type': 'camera',
                 'name': 'new',
                 'thumbnail': '/new/thumb'}
            ]
        }
        self.camera.update(config)
        self.assertEqual(self.camera.thumbnail,
                         'https://rest.test.immedia-semi.com/new/thumb.jpg')

    def test_no_thumbnails(self, mock_sess):
        """Tests that thumbnail is 'None' if none found."""
        mock_sess.return_value = 'foobar'
        self.camera.last_record = ['1']
        self.camera.sync.record_dates['new'] = ['1']
        self.camera.sync.all_clips = {'new': {'1': '/test.mp4'}}
        config = {
            'name': 'new',
            'camera_id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '',
        }
        self.camera.sync.homescreen = {
            'devices': []
        }
        with self.assertLogs() as logrecord:
            self.camera.update(config)
        self.assertEqual(self.camera.thumbnail, None)
        self.assertEqual(
            logrecord.output,
            ["ERROR:blinkpy.camera:Could not find thumbnail for camera new"]
        )

    def test_no_video_clips(self, mock_sess):
        """Tests that we still proceed with camera setup with no videos."""
        mock_sess.return_value = 'foobar'
        config = {
            'name': 'new',
            'camera_id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '/foobar',
        }
        self.camera.sync.homescreen = {
            'devices': []
        }
        self.camera.update(config, force_cache=True)
        self.assertEqual(self.camera.clip, None)
        self.assertEqual(self.camera.video_from_cache, None)
示例#3
0
class TestBlinkCameraSetup(unittest.TestCase):
    """Test the Blink class in blinkpy."""
    def setUp(self):
        """Set up Blink module."""
        self.blink = blinkpy.Blink(username=USERNAME, password=PASSWORD)
        header = {
            'Host': 'abc.zxc',
            'TOKEN_AUTH': mresp.LOGIN_RESPONSE['authtoken']['authtoken']
        }
        # pylint: disable=protected-access
        self.blink._auth_header = header
        self.blink.session = create_session()
        self.blink.urls = BlinkURLHandler('test')
        self.blink.sync['test'] = BlinkSyncModule(self.blink, 'test', 1234, [])
        self.camera = BlinkCamera(self.blink.sync['test'])
        self.camera.name = 'foobar'
        self.blink.sync['test'].cameras['foobar'] = self.camera

    def tearDown(self):
        """Clean up after test."""
        self.blink = None

    def test_camera_update(self, mock_sess):
        """Test that we can properly update camera properties."""
        config = {
            'name': 'new',
            'id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '/thumb',
        }
        self.camera.last_record = ['1']
        self.camera.sync.last_record = {
            'new': {
                'clip': '/test.mp4',
                'time': '1970-01-01T00:00:00'
            }
        }
        mock_sess.side_effect = [
            mresp.MockResponse({'temp': 71}, 200), 'test', 'foobar'
        ]
        self.camera.update(config)
        self.assertEqual(self.camera.name, 'new')
        self.assertEqual(self.camera.camera_id, '1234')
        self.assertEqual(self.camera.network_id, '5678')
        self.assertEqual(self.camera.serial, '12345678')
        self.assertEqual(self.camera.motion_enabled, False)
        self.assertEqual(self.camera.battery, 'ok')
        self.assertEqual(self.camera.temperature, 68)
        self.assertEqual(self.camera.temperature_c, 20)
        self.assertEqual(self.camera.temperature_calibrated, 71)
        self.assertEqual(self.camera.wifi_strength, 4)
        self.assertEqual(self.camera.thumbnail,
                         'https://rest-test.immedia-semi.com/thumb.jpg')
        self.assertEqual(self.camera.clip,
                         'https://rest-test.immedia-semi.com/test.mp4')
        self.assertEqual(self.camera.image_from_cache, 'test')
        self.assertEqual(self.camera.video_from_cache, 'foobar')

    def test_no_thumbnails(self, mock_sess):
        """Tests that thumbnail is 'None' if none found."""
        mock_sess.return_value = 'foobar'
        self.camera.last_record = ['1']
        config = {
            'name': 'new',
            'id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '',
        }
        self.camera.sync.homescreen = {'devices': []}
        self.assertEqual(self.camera.temperature_calibrated, None)
        with self.assertLogs() as logrecord:
            self.camera.update(config, force=True)
        self.assertEqual(self.camera.thumbnail, None)
        self.assertEqual(self.camera.last_record, ['1'])
        self.assertEqual(self.camera.temperature_calibrated, 68)
        self.assertEqual(
            logrecord.output,
            [("WARNING:blinkpy.camera:Could not retrieve calibrated "
              "temperature."),
             ("WARNING:blinkpy.camera:Could not find thumbnail for camera new"
              "\nNoneType: None")])

    def test_no_video_clips(self, mock_sess):
        """Tests that we still proceed with camera setup with no videos."""
        mock_sess.return_value = 'foobar'
        config = {
            'name': 'new',
            'id': 1234,
            'network_id': 5678,
            'serial': '12345678',
            'enabled': False,
            'battery_voltage': 90,
            'battery_state': 'ok',
            'temperature': 68,
            'wifi_strength': 4,
            'thumbnail': '/foobar',
        }
        self.camera.sync.homescreen = {'devices': []}
        self.camera.update(config, force_cache=True)
        self.assertEqual(self.camera.clip, None)
        self.assertEqual(self.camera.video_from_cache, None)
示例#4
0
class TestBlinkCameraSetup(unittest.TestCase):
    """Test the Blink class in blinkpy."""
    def setUp(self):
        """Set up Blink module."""
        self.blink = blinkpy.Blink(username=USERNAME, password=PASSWORD)
        header = {
            "Host": "abc.zxc",
            "TOKEN_AUTH": mresp.LOGIN_RESPONSE["authtoken"]["authtoken"],
        }
        # pylint: disable=protected-access
        self.blink._auth_header = header
        self.blink.session = create_session()
        self.blink.urls = BlinkURLHandler("test")
        self.blink.sync["test"] = BlinkSyncModule(self.blink, "test", 1234, [])
        self.camera = BlinkCamera(self.blink.sync["test"])
        self.camera.name = "foobar"
        self.blink.sync["test"].cameras["foobar"] = self.camera

    def tearDown(self):
        """Clean up after test."""
        self.blink = None

    def test_camera_update(self, mock_sess):
        """Test that we can properly update camera properties."""
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "/thumb",
        }
        self.camera.last_record = ["1"]
        self.camera.sync.last_record = {
            "new": {
                "clip": "/test.mp4",
                "time": "1970-01-01T00:00:00"
            }
        }
        mock_sess.side_effect = [
            mresp.MockResponse({"temp": 71}, 200),
            "test",
            "foobar",
        ]
        self.camera.update(config)
        self.assertEqual(self.camera.name, "new")
        self.assertEqual(self.camera.camera_id, "1234")
        self.assertEqual(self.camera.network_id, "5678")
        self.assertEqual(self.camera.serial, "12345678")
        self.assertEqual(self.camera.motion_enabled, False)
        self.assertEqual(self.camera.battery, "ok")
        self.assertEqual(self.camera.temperature, 68)
        self.assertEqual(self.camera.temperature_c, 20)
        self.assertEqual(self.camera.temperature_calibrated, 71)
        self.assertEqual(self.camera.wifi_strength, 4)
        self.assertEqual(self.camera.thumbnail,
                         "https://rest-test.immedia-semi.com/thumb.jpg")
        self.assertEqual(self.camera.clip,
                         "https://rest-test.immedia-semi.com/test.mp4")
        self.assertEqual(self.camera.image_from_cache, "test")
        self.assertEqual(self.camera.video_from_cache, "foobar")

    def test_no_thumbnails(self, mock_sess):
        """Tests that thumbnail is 'None' if none found."""
        mock_sess.return_value = "foobar"
        self.camera.last_record = ["1"]
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "",
        }
        self.camera.sync.homescreen = {"devices": []}
        self.assertEqual(self.camera.temperature_calibrated, None)
        with self.assertLogs() as logrecord:
            self.camera.update(config, force=True)
        self.assertEqual(self.camera.thumbnail, None)
        self.assertEqual(self.camera.last_record, ["1"])
        self.assertEqual(self.camera.temperature_calibrated, 68)
        self.assertEqual(
            logrecord.output,
            [
                ("WARNING:blinkpy.camera:Could not retrieve calibrated "
                 "temperature."),
                ("WARNING:blinkpy.camera:Could not find thumbnail for camera new"
                 "\nNoneType: None"),
            ],
        )

    def test_no_video_clips(self, mock_sess):
        """Tests that we still proceed with camera setup with no videos."""
        mock_sess.return_value = "foobar"
        config = {
            "name": "new",
            "id": 1234,
            "network_id": 5678,
            "serial": "12345678",
            "enabled": False,
            "battery_voltage": 90,
            "battery_state": "ok",
            "temperature": 68,
            "wifi_strength": 4,
            "thumbnail": "/foobar",
        }
        self.camera.sync.homescreen = {"devices": []}
        self.camera.update(config, force_cache=True)
        self.assertEqual(self.camera.clip, None)
        self.assertEqual(self.camera.video_from_cache, None)