diff --git a/custom_components/opendisplay/update.py b/custom_components/opendisplay/update.py index 53b8544..e7c2c42 100644 --- a/custom_components/opendisplay/update.py +++ b/custom_components/opendisplay/update.py @@ -31,6 +31,7 @@ AuthenticationFailedError, AuthenticationRequiredError, BLEConnectionError, + BLETimeoutError, OTAError, ) from opendisplay.models.enums import ICType @@ -372,10 +373,16 @@ def _on_log(msg: str) -> None: # Clear the (now fresh) app-mode GATT from the proxy cache so # the post-reboot AppLoader connection re-discovers the OTA # service instead of these app-firmware handles. - cleared = await device.clear_gatt_cache() - _on_log(f"Proxy GATT cache clear requested: {cleared}") + # Best effort: a proxy that is slow to answer must not + # abort a flash that may well succeed without the clear. + try: + cleared = await device.clear_gatt_cache() + except TimeoutError as err: + _on_log(f"Proxy GATT cache clear timed out: {err}") + else: + _on_log(f"Proxy GATT cache clear requested: {cleared}") await device.trigger_dfu_bootloader() - except BLEConnectionError as err: + except (BLEConnectionError, BLETimeoutError) as err: _on_log( f"App-mode connect failed ({err}); device is likely already " "in the AppLoader — attempting OTA directly." @@ -434,7 +441,7 @@ def _on_log(msg: str) -> None: translation_domain=DOMAIN, translation_key="authentication_error", ) from err - except (OTAError, BLEConnectionError) as err: + except (OTAError, BLEConnectionError, BLETimeoutError) as err: raise HomeAssistantError(f"Firmware update failed: {err}") from err finally: self._installing = False diff --git a/tests/test_update.py b/tests/test_update.py index 97f7a64..b28213f 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -18,6 +18,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_component import async_update_entity from opendisplay import AuthenticationFailedError, BLEConnectionError +from opendisplay.exceptions import BLETimeoutError from opendisplay.ota import OTAError import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry @@ -385,3 +386,56 @@ async def test_install_refused_while_the_device_sleeps( assert err.value.translation_key == "device_sleeping_ota" mock_ota.assert_not_awaited() + + +async def test_install_continues_when_the_proxy_cache_clear_times_out( + hass: HomeAssistant, + setup_seen: Callable[[], Awaitable[None]], + mock_ota: MagicMock, + mock_opendisplay_device: MagicMock, +) -> None: + """Clearing the proxy cache is best effort, so a slow proxy cannot abort the flash.""" + mock_opendisplay_device.clear_gatt_cache = AsyncMock( + side_effect=TimeoutError( + "Timeout waiting for BluetoothDeviceClearCacheResponse" + ) + ) + await setup_seen() + + await _install(hass) + + mock_opendisplay_device.trigger_dfu_bootloader.assert_awaited_once() + mock_ota.assert_awaited_once() + assert hass.states.get(ENTITY).attributes[ATTR_INSTALLED_VERSION] == LATEST + + +async def test_install_flashes_directly_when_app_mode_stops_responding( + hass: HomeAssistant, + setup_seen: Callable[[], Awaitable[None]], + mock_ota: MagicMock, + mock_opendisplay_device: MagicMock, +) -> None: + """No response in app mode is handled like a failed connect: try the AppLoader.""" + mock_opendisplay_device.trigger_dfu_bootloader = AsyncMock( + side_effect=BLETimeoutError("No response received within 10.0s") + ) + await setup_seen() + + await _install(hass) + + mock_ota.assert_awaited_once() + + +async def test_install_reports_a_flash_timeout_as_an_update_failure( + hass: HomeAssistant, + setup_seen: Callable[[], Awaitable[None]], + mock_ota: MagicMock, +) -> None: + """A device going silent mid-flash is a failed update, not an unexpected error.""" + mock_ota.side_effect = BLETimeoutError("No response received within 10.0s") + await setup_seen() + + with pytest.raises(HomeAssistantError, match="Firmware update failed"): + await _install(hass) + + assert hass.states.get(ENTITY).attributes[ATTR_IN_PROGRESS] is False