Skip to content

Commit 3f17193

Browse files
authored
Merge pull request #949 from fronzbot/add-notification-config
Added ability to get and set notification flags
2 parents 31e53d1 + e56b675 commit 3f17193

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

blinkpy/api.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,38 @@ async def request_system_disarm(blink, network, **kwargs):
166166
return response
167167

168168

169+
async def request_notification_flags(blink, **kwargs):
170+
"""
171+
Get system notification flags.
172+
173+
:param blink: Blink instance.
174+
"""
175+
url = (
176+
f"{blink.urls.base_url}/api/v1/accounts/{blink.account_id}"
177+
"/notifications/configuration"
178+
)
179+
response = await http_get(blink, url)
180+
await wait_for_command(blink, response)
181+
return response
182+
183+
184+
async def request_set_notification_flag(blink, data_dict):
185+
"""
186+
Set a system notification flag.
187+
188+
:param blink: Blink instance.
189+
:param data_dict: Dictionary of notifications to set.
190+
"""
191+
url = (
192+
f"{blink.urls.base_url}/api/v1/accounts/{blink.account_id}"
193+
"/notifications/configuration"
194+
)
195+
data = dumps({"notifications": data_dict})
196+
response = await http_post(blink, url, data=data, json=False)
197+
await wait_for_command(blink, response)
198+
return response
199+
200+
169201
async def request_command_status(blink, network, command_id):
170202
"""
171203
Request command status.

blinkpy/blinkpy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,21 @@ async def save(self, file_name):
320320
"""Save login data to file."""
321321
await util.json_save(self.auth.login_attributes, file_name)
322322

323+
async def get_status(self):
324+
"""Get the blink system notification status."""
325+
response = await api.request_notification_flags(self)
326+
return response.get("notifications", response)
327+
328+
async def set_status(self, data_dict={}):
329+
"""
330+
Set the blink system notification status.
331+
332+
:param data_dict: Dictionary of notification keys to modify.
333+
Example: {'low_battery': False, 'motion': False}
334+
"""
335+
response = await api.request_set_notification_flag(self, data_dict)
336+
return response
337+
323338
async def download_videos(
324339
self, path, since=None, camera="all", stop=10, delay=1, debug=False
325340
):

tests/test_api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ async def test_request_camera_usage(self, mock_resp):
106106
await api.request_camera_usage(self.blink), {"cameras": "1111"}
107107
)
108108

109+
async def test_request_notification_flags(self, mock_resp):
110+
"""Test notification flag request."""
111+
mock_resp.return_value = {"notifications": {"some_key": False}}
112+
self.assertEqual(
113+
await api.request_notification_flags(self.blink),
114+
{"notifications": {"some_key": False}},
115+
)
116+
117+
async def test_request_set_notification_flag(self, mock_resp):
118+
"""Test set of notifiaction flags."""
119+
mock_resp.side_effect = (
120+
mresp.MockResponse(COMMAND_RESPONSE, 200),
121+
COMMAND_COMPLETE,
122+
)
123+
response = await api.request_set_notification_flag(self.blink, {})
124+
self.assertEqual(response.status, 200)
125+
109126
async def test_request_motion_detection_enable(self, mock_resp):
110127
"""Test Motion detect enable."""
111128
mock_resp.side_effect = (

tests/test_blink_functions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,21 @@ async def test_refresh(self, mock_req, mock_update):
278278
self.blink.cameras = {"bar": MockCamera(self.blink.sync)}
279279
self.blink.sync["foo"].cameras = self.blink.cameras
280280
self.assertTrue(await self.blink.refresh())
281+
282+
@mock.patch("blinkpy.blinkpy.api.request_notification_flags")
283+
async def test_get_status(self, mock_req):
284+
"""Test get of notification flags."""
285+
mock_req.return_value = {"notifications": {"foo": True}}
286+
self.assertDictEqual(await self.blink.get_status(), {"foo": True})
287+
288+
@mock.patch("blinkpy.blinkpy.api.request_notification_flags")
289+
async def test_get_status_malformed(self, mock_req):
290+
"""Test get of notification flags with malformed response."""
291+
mock_req.return_value = {"nobueno": {"foo": False}}
292+
self.assertDictEqual(await self.blink.get_status(), {"nobueno": {"foo": False}})
293+
294+
@mock.patch("blinkpy.blinkpy.api.request_set_notification_flag")
295+
async def test_set_status(self, mock_req):
296+
"""Test set of notification flags."""
297+
mock_req.return_value = True
298+
self.assertTrue(await self.blink.set_status())

0 commit comments

Comments
 (0)