Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery level #837

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions blinkpy/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, sync):
self.thumbnail = None
self.serial = None
self.motion_enabled = None
self.battery_voltage = None
self.battery_level = None
self.clip = None
# A clip remains in the recent clips list until is has
# been downloaded or has been expired.
Expand Down Expand Up @@ -56,7 +56,7 @@ def attributes(self):
"temperature_c": self.temperature_c,
"temperature_calibrated": self.temperature_calibrated,
"battery": self.battery,
"battery_voltage": self.battery_voltage,
"battery_level": self.battery_level,
"thumbnail": self.thumbnail,
"video": self.clip,
"recent_clips": self.recent_clips,
Expand Down Expand Up @@ -237,12 +237,10 @@ def extract_config_info(self, config):
self.temperature = config.get("temperature")
if signals := config.get("signals"):
self.wifi_strength = signals.get("wifi")
self.battery_voltage = signals.get("battery")
self.battery_level = signals.get("battery")
self.sync_signal_strength = signals.get("lfr")
else:
self.wifi_strength = config.get("wifi_strength")
self.battery_voltage = config.get("battery_voltage")

self.product_type = config.get("type")

async def get_sensor_info(self):
Expand Down
106 changes: 28 additions & 78 deletions tests/test_camera_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
from blinkpy.camera import BlinkCamera, BlinkCameraMini, BlinkDoorbell
import tests.mock_responses as mresp

CAMERA_CFG = {
"camera": [
{
"battery_voltage": 90,
"motion_alert": True,
"wifi_strength": -30,
"temperature": 68,
}
]
CONFIG = {
"name": "new",
"id": 1234,
"network_id": 5678,
"serial": "12345678",
"enabled": False,
"battery_state": "ok",
"temperature": 68,
"signals": {"lfr": 5, "wifi": 4, "battery": 3},
"thumbnail": "/thumb",
}


Expand All @@ -47,18 +48,6 @@ def tearDown(self):

async 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,
"signals": {"lfr": 5, "wifi": 4, "battery": 3},
"thumbnail": "/thumb",
}
self.camera.last_record = ["1"]
self.camera.sync.last_records = {
"new": [{"clip": "/test.mp4", "time": "1970-01-01T00:00:00"}]
Expand All @@ -70,7 +59,7 @@ async def test_camera_update(self, mock_resp):
]
self.assertIsNone(self.camera.image_from_cache)

await self.camera.update(config, expire_clips=False)
await self.camera.update(CONFIG, expire_clips=False)
self.assertEqual(self.camera.name, "new")
self.assertEqual(self.camera.camera_id, "1234")
self.assertEqual(self.camera.network_id, "5678")
Expand Down Expand Up @@ -107,17 +96,12 @@ async def test_no_thumbnails(self, mock_resp):
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": "",
**CONFIG,
**{
"thumbnail": "",
},
}

self.camera.sync.homescreen = {"devices": []}
self.assertEqual(self.camera.temperature_calibrated, None)
with self.assertLogs() as logrecord:
Expand All @@ -144,16 +128,10 @@ async 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",
**CONFIG,
**{
"thumbnail": "/foobar",
},
}
mock_resp.return_value = mresp.MockResponse({"test": 200}, 200, raw_data="")
self.camera.sync.homescreen = {"devices": []}
Expand All @@ -167,51 +145,27 @@ async def test_recent_video_clips(self, mock_resp):
Tests that the last records in the sync module are added
to the camera recent clips list.
"""
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.sync.last_records["foobar"] = []
record2 = {"clip": "/clip2", "time": "2022-12-01 00:00:10+00:00"}
self.camera.sync.last_records["foobar"].append(record2)
record1 = {"clip": "/clip1", "time": "2022-12-01 00:00:00+00:00"}
self.camera.sync.last_records["foobar"].append(record1)
self.camera.sync.motion["foobar"] = True
await self.camera.update_images(config, expire_clips=False)
await self.camera.update_images(CONFIG, expire_clips=False)
record1["clip"] = self.blink.urls.base_url + "/clip1"
record2["clip"] = self.blink.urls.base_url + "/clip2"
self.assertEqual(self.camera.recent_clips[0], record1)
self.assertEqual(self.camera.recent_clips[1], record2)

async def test_recent_video_clips_missing_key(self, mock_resp):
"""Tests that the missing key failst."""
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.sync.last_records["foobar"] = []
record2 = {"clip": "/clip2"}
self.camera.sync.last_records["foobar"].append(record2)
self.camera.sync.motion["foobar"] = True

with self.assertLogs(level="ERROR") as dl_log:
await self.camera.update_images(config, expire_clips=False)
await self.camera.update_images(CONFIG, expire_clips=False)

self.assertIsNotNone(dl_log.output)

Expand Down Expand Up @@ -412,15 +366,11 @@ async def test_save_recent_clips_exception(self, mock_clip, mock_open, mock_resp
async def test_missing_keys(self, mock_resp):
"""Tests missing signal keys."""
config = {
"name": "new",
"id": 1234,
"network_id": 5678,
"serial": "12345678",
"enabled": False,
"battery_state": "ok",
"temperature": 68,
"signals": {"junk": 1},
"thumbnail": "",
**CONFIG,
**{
"signals": {"junk": 1},
"thumbnail": "",
},
}
self.camera.sync.homescreen = {"devices": []}
mock_resp.side_effect = [
Expand All @@ -430,4 +380,4 @@ async def test_missing_keys(self, mock_resp):
]
await self.camera.update(config, expire_clips=False, force=True)
self.assertEqual(self.camera.wifi_strength, None)
self.assertEqual(self.camera.battery_voltage, None)
self.assertEqual(self.camera.battery_level, None)
65 changes: 20 additions & 45 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
from blinkpy.camera import BlinkCamera, BlinkCameraMini, BlinkDoorbell
import tests.mock_responses as mresp

CAMERA_CFG = {
"camera": [
{
"battery_voltage": 90,
"motion_alert": True,
"wifi_strength": -30,
"temperature": 68,
}
]
CONFIG = {
"name": "new",
"id": 1234,
"network_id": 5678,
"serial": "12345678",
"enabled": False,
"battery_state": "ok",
"temperature": 68,
"thumbnail": 1357924680,
"signals": {"lfr": 5, "wifi": 4, "battery": 3},
"type": "test",
}


Expand Down Expand Up @@ -128,41 +130,21 @@ async def test_camera_stream(self, mock_resp):
async def test_different_thumb_api(self, mock_resp):
"""Test that the correct url is created with new api."""
thumb_endpoint = "https://rest-test.immedia-semi.com/api/v3/media/accounts/9999/networks/5678/test/1234/thumbnail/thumbnail.jpg?ts=1357924680&ext="
config = {
"name": "new",
"id": 1234,
"network_id": 5678,
"serial": "12345678",
"enabled": False,
"battery_voltage": 90,
"battery_state": "ok",
"temperature": 68,
"wifi_strength": 4,
"thumbnail": 1357924680,
"type": "test",
}
mock_resp.side_effect = [
{"temp": 71},
mresp.MockResponse({"test": 200}, 200, raw_data="test"),
]
self.camera.sync.blink.account_id = 9999
await self.camera.update(config, expire_clips=False)
await self.camera.update(CONFIG, expire_clips=False)
self.assertEqual(self.camera.thumbnail, thumb_endpoint)

async def test_thumb_return_none(self, mock_resp):
"""Test that a 'None" thumbnail is doesn't break system."""
config = {
"name": "new",
"id": 1234,
"network_id": 5678,
"serial": "12345678",
"enabled": False,
"battery_voltage": 90,
"battery_state": "ok",
"temperature": 68,
"wifi_strength": 4,
"thumbnail": None,
"type": "test",
**CONFIG,
**{
"thumbnail": None,
},
}
mock_resp.side_effect = [
{"temp": 71},
Expand All @@ -178,17 +160,10 @@ async def test_new_thumb_url_returned(self, mock_resp):
"test/1234/thumbnail/thumbnail.jpg?ts=1357924680&ext="
)
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_return,
"type": "test",
**CONFIG,
**{
"thumbnail": thumb_return,
},
}
mock_resp.side_effect = [
{"temp": 71},
Expand Down
Loading