7
7
import zigpy .exceptions
8
8
9
9
from zigpy_xbee import api as xbee_api , types as t , uart
10
+ import zigpy_xbee .config
10
11
from zigpy_xbee .zigbee .application import ControllerApplication
11
12
13
+ DEVICE_CONFIG = zigpy_xbee .config .SCHEMA_DEVICE (
14
+ {zigpy_xbee .config .CONF_DEVICE_PATH : "/dev/null" }
15
+ )
16
+
12
17
13
18
@pytest .fixture
14
19
def api ():
15
- api = xbee_api .XBee ()
20
+ api = xbee_api .XBee (DEVICE_CONFIG )
16
21
api ._uart = mock .MagicMock ()
17
22
return api
18
23
19
24
20
25
@pytest .mark .asyncio
21
26
async def test_connect (monkeypatch ):
22
- api = xbee_api .XBee ()
23
- dev = mock .MagicMock ()
24
- monkeypatch .setattr (
25
- uart , "connect" , mock .MagicMock (side_effect = asyncio .coroutine (mock .MagicMock ()))
26
- )
27
- await api .connect (dev , 115200 )
27
+ api = xbee_api .XBee (DEVICE_CONFIG )
28
+ monkeypatch .setattr (uart , "connect" , CoroutineMock ())
29
+ await api .connect ()
28
30
29
31
30
32
def test_close (api ):
@@ -542,14 +544,13 @@ def test_handle_many_to_one_rri(api):
542
544
543
545
@pytest .mark .asyncio
544
546
async def test_reconnect_multiple_disconnects (monkeypatch , caplog ):
545
- api = xbee_api .XBee ()
546
- dev = mock .sentinel .uart
547
+ api = xbee_api .XBee (DEVICE_CONFIG )
547
548
connect_mock = CoroutineMock ()
548
549
connect_mock .return_value = asyncio .Future ()
549
550
connect_mock .return_value .set_result (True )
550
551
monkeypatch .setattr (uart , "connect" , connect_mock )
551
552
552
- await api .connect (dev , 115200 )
553
+ await api .connect ()
553
554
554
555
caplog .set_level (logging .DEBUG )
555
556
connected = asyncio .Future ()
@@ -568,14 +569,13 @@ async def test_reconnect_multiple_disconnects(monkeypatch, caplog):
568
569
569
570
@pytest .mark .asyncio
570
571
async def test_reconnect_multiple_attempts (monkeypatch , caplog ):
571
- api = xbee_api .XBee ()
572
- dev = mock .sentinel .uart
572
+ api = xbee_api .XBee (DEVICE_CONFIG )
573
573
connect_mock = CoroutineMock ()
574
574
connect_mock .return_value = asyncio .Future ()
575
575
connect_mock .return_value .set_result (True )
576
576
monkeypatch .setattr (uart , "connect" , connect_mock )
577
577
578
- await api .connect (dev , 115200 )
578
+ await api .connect ()
579
579
580
580
caplog .set_level (logging .DEBUG )
581
581
connected = asyncio .Future ()
@@ -597,11 +597,11 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
597
597
async def test_probe_success (mock_connect , mock_at_cmd ):
598
598
"""Test device probing."""
599
599
600
- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
600
+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
601
601
assert res is True
602
602
assert mock_connect .call_count == 1
603
603
assert mock_connect .await_count == 1
604
- assert mock_connect .call_args [0 ][0 ] is mock . sentinel . uart
604
+ assert mock_connect .call_args [0 ][0 ] == DEVICE_CONFIG
605
605
assert mock_at_cmd .call_count == 1
606
606
assert mock_connect .return_value .close .call_count == 1
607
607
@@ -613,11 +613,11 @@ async def test_probe_success(mock_connect, mock_at_cmd):
613
613
async def test_probe_success_api_mode (mock_connect , mock_at_cmd , mock_api_mode ):
614
614
"""Test device probing."""
615
615
616
- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
616
+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
617
617
assert res is True
618
618
assert mock_connect .call_count == 1
619
619
assert mock_connect .await_count == 1
620
- assert mock_connect .call_args [0 ][0 ] is mock . sentinel . uart
620
+ assert mock_connect .call_args [0 ][0 ] == DEVICE_CONFIG
621
621
assert mock_at_cmd .call_count == 1
622
622
assert mock_api_mode .call_count == 1
623
623
assert mock_connect .return_value .close .call_count == 1
@@ -638,11 +638,11 @@ async def test_probe_fail(mock_connect, mock_at_cmd, mock_api_mode, exception):
638
638
mock_api_mode .reset_mock ()
639
639
mock_at_cmd .reset_mock ()
640
640
mock_connect .reset_mock ()
641
- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
641
+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
642
642
assert res is False
643
643
assert mock_connect .call_count == 1
644
644
assert mock_connect .await_count == 1
645
- assert mock_connect .call_args [0 ][0 ] is mock . sentinel . uart
645
+ assert mock_connect .call_args [0 ][0 ] == DEVICE_CONFIG
646
646
assert mock_at_cmd .call_count == 1
647
647
assert mock_api_mode .call_count == 1
648
648
assert mock_connect .return_value .close .call_count == 1
@@ -658,11 +658,21 @@ async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
658
658
mock_api_mode .reset_mock ()
659
659
mock_at_cmd .reset_mock ()
660
660
mock_connect .reset_mock ()
661
- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
661
+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
662
662
assert res is False
663
663
assert mock_connect .call_count == 1
664
664
assert mock_connect .await_count == 1
665
- assert mock_connect .call_args [0 ][0 ] is mock . sentinel . uart
665
+ assert mock_connect .call_args [0 ][0 ] == DEVICE_CONFIG
666
666
assert mock_at_cmd .call_count == 1
667
667
assert mock_api_mode .call_count == 1
668
668
assert mock_connect .return_value .close .call_count == 1
669
+
670
+
671
+ @pytest .mark .asyncio
672
+ @mock .patch .object (xbee_api .XBee , "connect" )
673
+ async def test_xbee_new (conn_mck ):
674
+ """Test new class method."""
675
+ api = await xbee_api .XBee .new (mock .sentinel .application , DEVICE_CONFIG )
676
+ assert isinstance (api , xbee_api .XBee )
677
+ assert conn_mck .call_count == 1
678
+ assert conn_mck .await_count == 1
0 commit comments