Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.

Commit 3065d5d

Browse files
committed
Added app conn/disconn events. Updated tests to support latest pytest-mock version
1 parent 059b0ec commit 3065d5d

File tree

5 files changed

+124
-48
lines changed

5 files changed

+124
-48
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ Examples can be found **[here][blynk-py-examples]** Check them all to get famili
173173
- [09_sync_virtual_pin.py](https://github.com/blynkkk/lib-python/blob/master/examples/09_sync_virtual_pin.py): How to sync virtual pin states and properties
174174
- [10_rtc_sync.py](https://github.com/blynkkk/lib-python/blob/master/examples/10_rtc_sync.py): How to perform RTC sync with blynk server
175175
- [11_ssl_socket.py](https://github.com/blynkkk/lib-python/blob/master/examples/11_ssl_socket.py): SSL server connection. Feature supported only by cPython library.
176+
- [12_app_connect_disconnect.py](https://github.com/blynkkk/lib-python/blob/master/examples/12_app_connect_disconnect.py): Managing APP connect/disconnect events with Blynk Cloud.
177+
176178

177179
##### Raspberry Pi (any):
178180
Read [Raspberry Pi guide](https://github.com/blynkkk/lib-python/tree/master/examples/raspberry) first.

blynklib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019 Anton Morozenko
1+
# Copyright (c) 2019-2020 Anton Morozenko
22
# Copyright (c) 2015-2019 Volodymyr Shymanskyy.
33
# See the file LICENSE for copying permission.
44

@@ -348,7 +348,7 @@ def process(self, msg_type, msg_id, msg_len, msg_args):
348348
elif msg_type == self.MSG_PING:
349349
self.send(self.response_msg(self.STATUS_OK, msg_id=msg_id))
350350
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_INTERNAL):
351-
if msg_type == self.MSG_INTERNAL and len(msg_args) >= 2:
351+
if msg_type == self.MSG_INTERNAL:
352352
self.call_handler("{}{}".format(self._INTERNAL, msg_args[0]), msg_args[1:])
353353
elif len(msg_args) >= 3 and msg_args[0] == 'vw':
354354
self.call_handler("{}{}".format(self._VPIN_WRITE, msg_args[1]), int(msg_args[1]), msg_args[2:])

blynklib_mp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019 Anton Morozenko
1+
# Copyright (c) 2019-2020 Anton Morozenko
22
# Copyright (c) 2015-2019 Volodymyr Shymanskyy.
33
# See the file LICENSE for copying permission.
44

@@ -337,7 +337,7 @@ def process(self, msg_type, msg_id, msg_len, msg_args):
337337
elif msg_type == self.MSG_PING:
338338
self.send(self.response_msg(self.STATUS_OK, msg_id=msg_id))
339339
elif msg_type in (self.MSG_HW, self.MSG_BRIDGE, self.MSG_INTERNAL):
340-
if msg_type == self.MSG_INTERNAL and len(msg_args) >= const(2):
340+
if msg_type == self.MSG_INTERNAL:
341341
self.call_handler("{}{}".format(self._INTERNAL, msg_args[0]), msg_args[1:])
342342
elif len(msg_args) >= const(3) and msg_args[0] == 'vw':
343343
self.call_handler("{}{}".format(self._VPIN_WRITE, msg_args[1]), int(msg_args[1]), msg_args[2:])
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
[APP CONNECT DISCONNECT EVENTS EXAMPLE] ============================================================
3+
4+
Environment prepare:
5+
In your Blynk App project:
6+
- in Project Settings enable flag "Notify devices when APP connected"
7+
- define your auth token for current example and run it
8+
- Run the App (green triangle in the upper right corner).
9+
10+
This started program will call handlers and print messages for APP_CONNECT or APP_DISCONNECT events.
11+
12+
Schema:
13+
====================================================================================================
14+
+-----------+ +--------------+ +--------------+
15+
| | | | | |
16+
| blynk lib | | blynk server | | blynk app |
17+
| | | virtual pin | | |
18+
| | | | | |
19+
+-----+-----+ +------+-------+ +-------+------+
20+
| | app connected or disconnected |
21+
| | from server |
22+
| |
23+
event handler | app connect/disconnect event +<-----------------------------------+
24+
(user function) | | |
25+
+------------<-----------------------------------+ |
26+
| | | |
27+
| | | |
28+
+--------->+ | |
29+
| | |
30+
| | |
31+
| | |
32+
| | |
33+
| | |
34+
| | |
35+
| | |
36+
| | |
37+
+ + +
38+
====================================================================================================
39+
Additional blynk info you can find by examining such resources:
40+
41+
Downloads, docs, tutorials: https://blynk.io
42+
Sketch generator: http://examples.blynk.cc
43+
Blynk community: http://community.blynk.cc
44+
Social networks: http://www.fb.com/blynkapp
45+
http://twitter.com/blynk_app
46+
====================================================================================================
47+
"""
48+
49+
import blynklib
50+
51+
BLYNK_AUTH = 'YourAuthToken'
52+
53+
# initialize Blynk
54+
blynk = blynklib.Blynk(BLYNK_AUTH)
55+
56+
APP_CONNECT_PRINT_MSG = '[APP_CONNECT_EVENT]'
57+
APP_DISCONNECT_PRINT_MSG = '[APP_DISCONNECT_EVENT]'
58+
59+
60+
@blynk.handle_event('internal_acon')
61+
def app_connect_handler(*args):
62+
print(APP_CONNECT_PRINT_MSG)
63+
64+
65+
@blynk.handle_event('internal_adis')
66+
def app_disconnect_handler(*args):
67+
print(APP_DISCONNECT_PRINT_MSG)
68+
69+
70+
###########################################################
71+
# infinite loop that waits for event
72+
###########################################################
73+
while True:
74+
blynk.run()

test/test_blynk_connection.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,71 @@ def cb(self):
1414

1515
def test_send(self, cb, mocker):
1616
cb._socket = socket.socket()
17-
with mocker.patch('socket.socket.send', return_value=5):
18-
result = cb.send('1234')
19-
assert result == 5
17+
mocker.patch('socket.socket.send', return_value=5)
18+
result = cb.send('1234')
19+
assert result == 5
2020

2121
def test_send_ioerror(self, cb, mocker):
2222
cb._socket = socket.socket()
23-
with mocker.patch('socket.socket.send', side_effect=IOError('IO')):
24-
result = cb.send('1234')
25-
assert result is None
23+
mocker.patch('socket.socket.send', side_effect=IOError('IO'))
24+
result = cb.send('1234')
25+
assert result is None
2626

2727
def test_send_oserror(self, cb, mocker):
2828
cb._socket = socket.socket()
29-
with mocker.patch('socket.socket.send', side_effect=OSError('OS')):
30-
result = cb.send('1234')
31-
assert result is None
29+
mocker.patch('socket.socket.send', side_effect=OSError('OS'))
30+
result = cb.send('1234')
31+
assert result is None
3232

3333
def test_send_socket_timeout(self, cb, mocker):
3434
cb._socket = socket.socket()
35-
with mocker.patch('socket.socket.send', side_effect=socket.timeout()):
36-
result = cb.send('1234')
37-
assert result is None
35+
mocker.patch('socket.socket.send', side_effect=socket.timeout())
36+
result = cb.send('1234')
37+
assert result is None
3838

3939
def test_send_error_retry_count(self, cb, mocker):
4040
cb._socket = socket.socket()
41-
with mocker.patch('socket.socket.send', side_effect=OSError('OS')):
42-
mocker.spy(time, 'sleep')
43-
cb.send('1234')
44-
assert cb._socket.send.call_count == 3
41+
mocker.patch('socket.socket.send', side_effect=OSError('OS'))
42+
mocker.spy(time, 'sleep')
43+
cb.send('1234')
44+
assert cb._socket.send.call_count == 3
4545

4646
def test_receive(self, cb, mocker):
4747
cb._socket = socket.socket()
48-
with mocker.patch('socket.socket.recv', return_value=b'12345'):
49-
result = cb.receive(10, 1)
50-
assert result == b'12345'
48+
mocker.patch('socket.socket.recv', return_value=b'12345')
49+
result = cb.receive(10, 1)
50+
assert result == b'12345'
5151

5252
def test_receive_timeout(self, cb, mocker):
5353
cb._socket = socket.socket()
54-
with mocker.patch('socket.socket.recv', side_effect=OSError('timed out')):
55-
result = cb.receive(10, 1)
56-
assert result == b''
54+
mocker.patch('socket.socket.recv', side_effect=OSError('timed out'))
55+
result = cb.receive(10, 1)
56+
assert result == b''
5757

5858
def test_receive_timeout_2(self, cb, mocker):
5959
cb._socket = socket.socket()
60-
with mocker.patch('socket.socket.recv', side_effect=socket.timeout('timed out')):
61-
result = cb.receive(10, 1)
62-
assert result == b''
60+
mocker.patch('socket.socket.recv', side_effect=socket.timeout('timed out'))
61+
result = cb.receive(10, 1)
62+
assert result == b''
6363

6464
def test_receive_eagain(self, cb, mocker):
6565
cb._socket = socket.socket()
66-
with mocker.patch('socket.socket.recv', side_effect=IOError('[Errno 11]')):
67-
result = cb.receive(10, 1)
68-
assert result == b''
66+
mocker.patch('socket.socket.recv', side_effect=IOError('[Errno 11]'))
67+
result = cb.receive(10, 1)
68+
assert result == b''
6969

7070
def test_receive_etimeout(self, cb, mocker):
7171
cb._socket = socket.socket()
72-
with mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 60]')):
73-
result = cb.receive(10, 1)
74-
assert result == b''
72+
mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 60]'))
73+
result = cb.receive(10, 1)
74+
assert result == b''
7575

7676
def test_receive_raise_other_oserror(self, cb, mocker):
7777
cb._socket = socket.socket()
78-
with mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 13]')):
79-
with pytest.raises(OSError) as os_err:
80-
cb.receive(10, 1)
81-
assert '[Errno 13]' in str(os_err.value)
78+
mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 13]'))
79+
with pytest.raises(OSError) as os_err:
80+
cb.receive(10, 1)
81+
assert '[Errno 13]' in str(os_err.value)
8282

8383
def test_is_server_alive_negative(self, cb):
8484
result = cb.is_server_alive()
@@ -103,17 +103,17 @@ def test_is_server_alive_positive_no_ping_2(self, cb):
103103
assert result is True
104104

105105
def test_get_socket(self, cb, mocker):
106-
with mocker.patch('socket.socket'):
107-
with mocker.patch('socket.getaddrinfo'):
108-
cb._get_socket()
109-
assert cb._state == cb.CONNECTING
106+
mocker.patch('socket.socket')
107+
mocker.patch('socket.getaddrinfo')
108+
cb._get_socket()
109+
assert cb._state == cb.CONNECTING
110110

111111
def test_get_socket_exception(self, cb, mocker):
112-
with mocker.patch('socket.socket'):
113-
with mocker.patch('socket.getaddrinfo', side_effect=BlynkError('BE')):
114-
with pytest.raises(BlynkError) as b_err:
115-
cb._get_socket()
116-
assert 'Connection with the Blynk server failed: BE' in str(b_err.value)
112+
mocker.patch('socket.socket')
113+
mocker.patch('socket.getaddrinfo', side_effect=BlynkError('BE'))
114+
with pytest.raises(BlynkError) as b_err:
115+
cb._get_socket()
116+
assert 'Connection with the Blynk server failed: BE' in str(b_err.value)
117117

118118
def test_authenticate(self, cb, mocker):
119119
mocker.patch.object(cb, 'send', return_value=None)

0 commit comments

Comments
 (0)