77import zigpy .exceptions
88
99from zigpy_xbee import api as xbee_api , types as t , uart
10+ import zigpy_xbee .config
1011from zigpy_xbee .zigbee .application import ControllerApplication
1112
13+ DEVICE_CONFIG = zigpy_xbee .config .SCHEMA_DEVICE (
14+ {zigpy_xbee .config .CONF_DEVICE_PATH : "/dev/null" }
15+ )
16+
1217
1318@pytest .fixture
1419def api ():
15- api = xbee_api .XBee ()
20+ api = xbee_api .XBee (DEVICE_CONFIG )
1621 api ._uart = mock .MagicMock ()
1722 return api
1823
1924
2025@pytest .mark .asyncio
2126async 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 ()
2830
2931
3032def test_close (api ):
@@ -542,14 +544,13 @@ def test_handle_many_to_one_rri(api):
542544
543545@pytest .mark .asyncio
544546async def test_reconnect_multiple_disconnects (monkeypatch , caplog ):
545- api = xbee_api .XBee ()
546- dev = mock .sentinel .uart
547+ api = xbee_api .XBee (DEVICE_CONFIG )
547548 connect_mock = CoroutineMock ()
548549 connect_mock .return_value = asyncio .Future ()
549550 connect_mock .return_value .set_result (True )
550551 monkeypatch .setattr (uart , "connect" , connect_mock )
551552
552- await api .connect (dev , 115200 )
553+ await api .connect ()
553554
554555 caplog .set_level (logging .DEBUG )
555556 connected = asyncio .Future ()
@@ -568,14 +569,13 @@ async def test_reconnect_multiple_disconnects(monkeypatch, caplog):
568569
569570@pytest .mark .asyncio
570571async def test_reconnect_multiple_attempts (monkeypatch , caplog ):
571- api = xbee_api .XBee ()
572- dev = mock .sentinel .uart
572+ api = xbee_api .XBee (DEVICE_CONFIG )
573573 connect_mock = CoroutineMock ()
574574 connect_mock .return_value = asyncio .Future ()
575575 connect_mock .return_value .set_result (True )
576576 monkeypatch .setattr (uart , "connect" , connect_mock )
577577
578- await api .connect (dev , 115200 )
578+ await api .connect ()
579579
580580 caplog .set_level (logging .DEBUG )
581581 connected = asyncio .Future ()
@@ -597,11 +597,11 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
597597async def test_probe_success (mock_connect , mock_at_cmd ):
598598 """Test device probing."""
599599
600- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
600+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
601601 assert res is True
602602 assert mock_connect .call_count == 1
603603 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
605605 assert mock_at_cmd .call_count == 1
606606 assert mock_connect .return_value .close .call_count == 1
607607
@@ -613,11 +613,11 @@ async def test_probe_success(mock_connect, mock_at_cmd):
613613async def test_probe_success_api_mode (mock_connect , mock_at_cmd , mock_api_mode ):
614614 """Test device probing."""
615615
616- res = await xbee_api .XBee .probe (mock . sentinel . uart , mock . sentinel . baud )
616+ res = await xbee_api .XBee .probe (DEVICE_CONFIG )
617617 assert res is True
618618 assert mock_connect .call_count == 1
619619 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
621621 assert mock_at_cmd .call_count == 1
622622 assert mock_api_mode .call_count == 1
623623 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):
638638 mock_api_mode .reset_mock ()
639639 mock_at_cmd .reset_mock ()
640640 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 )
642642 assert res is False
643643 assert mock_connect .call_count == 1
644644 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
646646 assert mock_at_cmd .call_count == 1
647647 assert mock_api_mode .call_count == 1
648648 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):
658658 mock_api_mode .reset_mock ()
659659 mock_at_cmd .reset_mock ()
660660 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 )
662662 assert res is False
663663 assert mock_connect .call_count == 1
664664 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
666666 assert mock_at_cmd .call_count == 1
667667 assert mock_api_mode .call_count == 1
668668 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