@@ -1954,3 +1954,93 @@ async def connect_device(address: str) -> tuple[str, str | None]:
19541954 cancel1 ()
19551955 cancel2 ()
19561956 cancel3 ()
1957+
1958+
1959+ @pytest .mark .asyncio
1960+ async def test_backend_name_from_tuple (
1961+ enable_bluetooth : None ,
1962+ install_bleak_catcher : None ,
1963+ mock_platform_client : None ,
1964+ ) -> None :
1965+ """Test that backend name is extracted from tuple (bleak 2.0.0+)."""
1966+ manager = _get_manager ()
1967+ hci0_device_advs , cancel_hci0 , _ = _generate_scanners_with_fake_devices ()
1968+
1969+ with patch (
1970+ "habluetooth.wrappers.get_platform_client_backend_type" ,
1971+ return_value = (FakeBleakClient , "TestBackend" ),
1972+ ):
1973+ ble_device = hci0_device_advs ["00:00:00:00:00:01" ][0 ]
1974+ client = bleak .BleakClient (ble_device )
1975+
1976+ # Access the wrapped backend through the client
1977+ # The backend name should be extracted from the tuple
1978+ wrapped_backend = client ._async_get_best_available_backend_and_device (manager )
1979+ assert wrapped_backend .backend_name == "TestBackend"
1980+ assert wrapped_backend .client == FakeBleakClient
1981+
1982+ cancel_hci0 ()
1983+
1984+
1985+ @pytest .mark .asyncio
1986+ async def test_backend_name_from_class (
1987+ enable_bluetooth : None ,
1988+ install_bleak_catcher : None ,
1989+ mock_platform_client : None ,
1990+ ) -> None :
1991+ """Test that backend name is derived from class name (pre-bleak 2.0.0)."""
1992+ manager = _get_manager ()
1993+ hci0_device_advs , cancel_hci0 , _ = _generate_scanners_with_fake_devices ()
1994+
1995+ with patch (
1996+ "habluetooth.wrappers.get_platform_client_backend_type" ,
1997+ return_value = FakeBleakClient ,
1998+ ):
1999+ ble_device = hci0_device_advs ["00:00:00:00:00:01" ][0 ]
2000+ client = bleak .BleakClient (ble_device )
2001+
2002+ # Access the wrapped backend through the client
2003+ # The backend name should be derived from the class name
2004+ wrapped_backend = client ._async_get_best_available_backend_and_device (manager )
2005+ assert wrapped_backend .backend_name == "type"
2006+ assert wrapped_backend .client == FakeBleakClient
2007+
2008+ cancel_hci0 ()
2009+
2010+
2011+ @pytest .mark .asyncio
2012+ async def test_backend_name_in_logs (
2013+ enable_bluetooth : None ,
2014+ install_bleak_catcher : None ,
2015+ mock_platform_client : None ,
2016+ caplog : pytest .LogCaptureFixture ,
2017+ ) -> None :
2018+ """Test that backend name appears in debug logs."""
2019+ caplog .set_level (logging .DEBUG )
2020+
2021+ hci0_device_advs , cancel_hci0 , _ = _generate_scanners_with_fake_devices ()
2022+
2023+ with patch (
2024+ "habluetooth.wrappers.get_platform_client_backend_type" ,
2025+ return_value = (FakeBleakClient , "TestBackend" ),
2026+ ):
2027+ ble_device = hci0_device_advs ["00:00:00:00:00:01" ][0 ]
2028+ with patch .object (FakeBleakClient , "is_connected" , return_value = True ):
2029+ client = bleak .BleakClient (ble_device )
2030+ await client .connect ()
2031+
2032+ # Check that the backend name appears in the logs
2033+ assert any (
2034+ "[TestBackend]" in record .message
2035+ for record in caplog .records
2036+ if "Connecting via" in record .message
2037+ ), f"Backend name not found in logs: { [r .message for r in caplog .records ]} "
2038+ assert any (
2039+ "[TestBackend]" in record .message
2040+ for record in caplog .records
2041+ if "Connected via" in record .message
2042+ ), f"Backend name not found in logs: { [r .message for r in caplog .records ]} "
2043+
2044+ await client .disconnect ()
2045+
2046+ cancel_hci0 ()
0 commit comments