From 74d02a15748377f95cc8350efcfc7f63dff4e599 Mon Sep 17 00:00:00 2001 From: Richard Kroegel <42204099+rikroe@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:28:24 +0200 Subject: [PATCH] BMW: Remove deprecated refresh from cloud button (#97864) * Remove deprecated refresh from cloud button * Clean up strings.json --- .../components/bmw_connected_drive/button.py | 41 ++++++------------- .../bmw_connected_drive/strings.json | 3 -- .../snapshots/test_button.ambr | 24 ----------- .../bmw_connected_drive/test_button.py | 33 ++++++++++----- 4 files changed, 35 insertions(+), 66 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/button.py b/homeassistant/components/bmw_connected_drive/button.py index 6edb1a3f2ac..c3f066610a9 100644 --- a/homeassistant/components/bmw_connected_drive/button.py +++ b/homeassistant/components/bmw_connected_drive/button.py @@ -26,14 +26,17 @@ _LOGGER = logging.getLogger(__name__) @dataclass -class BMWButtonEntityDescription(ButtonEntityDescription): +class BMWRequiredKeysMixin: + """Mixin for required keys.""" + + remote_function: Callable[[MyBMWVehicle], Coroutine[Any, Any, RemoteServiceStatus]] + + +@dataclass +class BMWButtonEntityDescription(ButtonEntityDescription, BMWRequiredKeysMixin): """Class describing BMW button entities.""" enabled_when_read_only: bool = False - remote_function: Callable[ - [MyBMWVehicle], Coroutine[Any, Any, RemoteServiceStatus] - ] | None = None - account_function: Callable[[BMWDataUpdateCoordinator], Coroutine] | None = None is_available: Callable[[MyBMWVehicle], bool] = lambda _: True @@ -69,13 +72,6 @@ BUTTON_TYPES: tuple[BMWButtonEntityDescription, ...] = ( icon="mdi:crosshairs-question", remote_function=lambda vehicle: vehicle.remote_services.trigger_remote_vehicle_finder(), ), - BMWButtonEntityDescription( - key="refresh", - translation_key="refresh", - icon="mdi:refresh", - account_function=lambda coordinator: coordinator.async_request_refresh(), - enabled_when_read_only=True, - ), ) @@ -120,22 +116,9 @@ class BMWButton(BMWBaseEntity, ButtonEntity): async def async_press(self) -> None: """Press the button.""" - if self.entity_description.remote_function: - try: - await self.entity_description.remote_function(self.vehicle) - except MyBMWAPIError as ex: - raise HomeAssistantError(ex) from ex - elif self.entity_description.account_function: - _LOGGER.warning( - "The 'Refresh from cloud' button is deprecated. Use the" - " 'homeassistant.update_entity' service with any BMW entity for a full" - " reload. See" - " https://www.home-assistant.io/integrations/bmw_connected_drive/#update-the-state--refresh-from-api" - " for details" - ) - try: - await self.entity_description.account_function(self.coordinator) - except MyBMWAPIError as ex: - raise HomeAssistantError(ex) from ex + try: + await self.entity_description.remote_function(self.vehicle) + except MyBMWAPIError as ex: + raise HomeAssistantError(ex) from ex self.coordinator.async_update_listeners() diff --git a/homeassistant/components/bmw_connected_drive/strings.json b/homeassistant/components/bmw_connected_drive/strings.json index af73417b1a9..69abd97ddfe 100644 --- a/homeassistant/components/bmw_connected_drive/strings.json +++ b/homeassistant/components/bmw_connected_drive/strings.json @@ -66,9 +66,6 @@ }, "find_vehicle": { "name": "Find vehicle" - }, - "refresh": { - "name": "Refresh from cloud" } }, "lock": { diff --git a/tests/components/bmw_connected_drive/snapshots/test_button.ambr b/tests/components/bmw_connected_drive/snapshots/test_button.ambr index a7520a6bce0..af43f118a77 100644 --- a/tests/components/bmw_connected_drive/snapshots/test_button.ambr +++ b/tests/components/bmw_connected_drive/snapshots/test_button.ambr @@ -61,18 +61,6 @@ 'last_updated': , 'state': 'unknown', }), - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'attribution': 'Data provided by MyBMW', - 'friendly_name': 'i4 eDrive40 Refresh from cloud', - 'icon': 'mdi:refresh', - }), - 'context': , - 'entity_id': 'button.i4_edrive40_refresh_from_cloud', - 'last_changed': , - 'last_updated': , - 'state': 'unknown', - }), StateSnapshot({ 'attributes': ReadOnlyDict({ 'attribution': 'Data provided by MyBMW', @@ -121,17 +109,5 @@ 'last_updated': , 'state': 'unknown', }), - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'attribution': 'Data provided by MyBMW', - 'friendly_name': 'i3 (+ REX) Refresh from cloud', - 'icon': 'mdi:refresh', - }), - 'context': , - 'entity_id': 'button.i3_rex_refresh_from_cloud', - 'last_changed': , - 'last_updated': , - 'state': 'unknown', - }), ]) # --- diff --git a/tests/components/bmw_connected_drive/test_button.py b/tests/components/bmw_connected_drive/test_button.py index 236dd76ce9f..16803756702 100644 --- a/tests/components/bmw_connected_drive/test_button.py +++ b/tests/components/bmw_connected_drive/test_button.py @@ -1,4 +1,7 @@ """Test BMW buttons.""" +from unittest.mock import AsyncMock + +from bimmer_connected.models import MyBMWRemoteServiceError from bimmer_connected.vehicle.remote_services import RemoteServices import pytest import respx @@ -8,6 +11,7 @@ from homeassistant.components.bmw_connected_drive.coordinator import ( BMWDataUpdateCoordinator, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from . import setup_mocked_integration @@ -58,22 +62,31 @@ async def test_update_triggers_success( assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 1 -async def test_refresh_from_cloud( +async def test_update_failed( hass: HomeAssistant, bmw_fixture: respx.Router, + monkeypatch: pytest.MonkeyPatch, ) -> None: - """Test button press for deprecated service.""" + """Test button press.""" # Setup component assert await setup_mocked_integration(hass) BMWDataUpdateCoordinator.async_update_listeners.reset_mock() - # Test - await hass.services.async_call( - "button", - "press", - blocking=True, - target={"entity_id": "button.i4_edrive40_refresh_from_cloud"}, + # Setup exception + monkeypatch.setattr( + RemoteServices, + "trigger_remote_service", + AsyncMock(side_effect=MyBMWRemoteServiceError), ) - assert RemoteServices.trigger_remote_service.call_count == 0 - assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 2 + + # Test + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + "button", + "press", + blocking=True, + target={"entity_id": "button.i4_edrive40_flash_lights"}, + ) + assert RemoteServices.trigger_remote_service.call_count == 1 + assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 0