diff --git a/homeassistant/components/teslemetry/__init__.py b/homeassistant/components/teslemetry/__init__.py index 950deea69f1d..81a11628001e 100644 --- a/homeassistant/components/teslemetry/__init__.py +++ b/homeassistant/components/teslemetry/__init__.py @@ -37,7 +37,11 @@ from homeassistant.components.application_credentials import ( ClientCredential, async_import_client_credential, ) -from homeassistant.components.bluetooth import async_ble_device_from_address +from homeassistant.components.bluetooth import ( + BluetoothReachabilityIntent, + async_address_reachability_diagnostics, + async_ble_device_from_address, +) from homeassistant.config_entries import ConfigEntry, ConfigEntryState, ConfigSubentry from homeassistant.const import ( CONF_ACCESS_TOKEN, @@ -345,6 +349,13 @@ async def _async_resolve_vehicle_api( """Report whether the vehicle is currently reachable over Bluetooth.""" device = async_ble_device_from_address(hass, address, connectable=True) if device is None: + LOGGER.debug( + "Vehicle %s is not reachable over Bluetooth, using cloud: %s", + vin, + async_address_reachability_diagnostics( + hass, address, BluetoothReachabilityIntent.CONNECTION + ), + ) return False # The library never refreshes the BLE handle, so set it here while it is known fresh. bluetooth_vehicle.set_device(device) diff --git a/tests/components/teslemetry/test_init.py b/tests/components/teslemetry/test_init.py index 7832965711ba..0a353acd7281 100644 --- a/tests/components/teslemetry/test_init.py +++ b/tests/components/teslemetry/test_init.py @@ -47,6 +47,7 @@ from tesla_fleet_api.teslemetry import EnergySite, Vehicle from tesla_protocol.command import vcsec_pb2 from teslemetry_stream import TeslemetryStreamAuthenticationError +from homeassistant.components.bluetooth import BluetoothReachabilityIntent from homeassistant.components.homeassistant import ( DOMAIN as HOMEASSISTANT_DOMAIN, SERVICE_UPDATE_ENTITY, @@ -2449,19 +2450,34 @@ async def _paired_entry( await hass.async_block_till_done() -async def test_vehicle_bluetooth_out_of_range(hass: HomeAssistant) -> None: - """A paired vehicle out of range still gets a router, and skips Bluetooth.""" - async with _paired_entry(hass, MagicMock(return_value=None)) as ( - router, - bluetooth_vehicle, - cloud, - ): - assert isinstance(router, VehicleRouter) +async def test_vehicle_bluetooth_out_of_range( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """A paired vehicle out of range skips Bluetooth and logs why.""" + with patch( + "homeassistant.components.teslemetry.async_address_reachability_diagnostics", + return_value="unknown (never seen by any scanner)", + ) as mock_diagnostics: + async with _paired_entry(hass, MagicMock(return_value=None)) as ( + router, + bluetooth_vehicle, + cloud, + ): + assert isinstance(router, VehicleRouter) - assert await router.flash_lights() == CLOUD_RESULT + with caplog.at_level(logging.DEBUG): + assert await router.flash_lights() == CLOUD_RESULT - cloud.assert_awaited_once() - bluetooth_vehicle.flash_lights.assert_not_called() + cloud.assert_awaited_once() + bluetooth_vehicle.flash_lights.assert_not_called() + + mock_diagnostics.assert_called_with( + hass, ADDRESS, BluetoothReachabilityIntent.CONNECTION + ) + assert ( + f"Vehicle {VIN} is not reachable over Bluetooth, using cloud: " + "unknown (never seen by any scanner)" + ) in caplog.text async def test_vehicle_router_resumes_bluetooth_when_vehicle_returns(