BMW: Remove deprecated refresh from cloud button (#97864)

* Remove deprecated refresh from cloud button

* Clean up strings.json
This commit is contained in:
Richard Kroegel 2023-08-05 22:28:24 +02:00 committed by GitHub
parent c478a81deb
commit 74d02a1574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 66 deletions

View File

@ -26,14 +26,17 @@ _LOGGER = logging.getLogger(__name__)
@dataclass @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.""" """Class describing BMW button entities."""
enabled_when_read_only: bool = False 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 is_available: Callable[[MyBMWVehicle], bool] = lambda _: True
@ -69,13 +72,6 @@ BUTTON_TYPES: tuple[BMWButtonEntityDescription, ...] = (
icon="mdi:crosshairs-question", icon="mdi:crosshairs-question",
remote_function=lambda vehicle: vehicle.remote_services.trigger_remote_vehicle_finder(), 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: async def async_press(self) -> None:
"""Press the button.""" """Press the button."""
if self.entity_description.remote_function: try:
try: await self.entity_description.remote_function(self.vehicle)
await self.entity_description.remote_function(self.vehicle) except MyBMWAPIError as ex:
except MyBMWAPIError as ex: raise HomeAssistantError(ex) from 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
self.coordinator.async_update_listeners() self.coordinator.async_update_listeners()

View File

@ -66,9 +66,6 @@
}, },
"find_vehicle": { "find_vehicle": {
"name": "Find vehicle" "name": "Find vehicle"
},
"refresh": {
"name": "Refresh from cloud"
} }
}, },
"lock": { "lock": {

View File

@ -61,18 +61,6 @@
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'unknown', 'state': 'unknown',
}), }),
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by MyBMW',
'friendly_name': 'i4 eDrive40 Refresh from cloud',
'icon': 'mdi:refresh',
}),
'context': <ANY>,
'entity_id': 'button.i4_edrive40_refresh_from_cloud',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
}),
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'attribution': 'Data provided by MyBMW', 'attribution': 'Data provided by MyBMW',
@ -121,17 +109,5 @@
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'unknown', 'state': 'unknown',
}), }),
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by MyBMW',
'friendly_name': 'i3 (+ REX) Refresh from cloud',
'icon': 'mdi:refresh',
}),
'context': <ANY>,
'entity_id': 'button.i3_rex_refresh_from_cloud',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
}),
]) ])
# --- # ---

View File

@ -1,4 +1,7 @@
"""Test BMW buttons.""" """Test BMW buttons."""
from unittest.mock import AsyncMock
from bimmer_connected.models import MyBMWRemoteServiceError
from bimmer_connected.vehicle.remote_services import RemoteServices from bimmer_connected.vehicle.remote_services import RemoteServices
import pytest import pytest
import respx import respx
@ -8,6 +11,7 @@ from homeassistant.components.bmw_connected_drive.coordinator import (
BMWDataUpdateCoordinator, BMWDataUpdateCoordinator,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from . import setup_mocked_integration from . import setup_mocked_integration
@ -58,22 +62,31 @@ async def test_update_triggers_success(
assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 1 assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 1
async def test_refresh_from_cloud( async def test_update_failed(
hass: HomeAssistant, hass: HomeAssistant,
bmw_fixture: respx.Router, bmw_fixture: respx.Router,
monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
"""Test button press for deprecated service.""" """Test button press."""
# Setup component # Setup component
assert await setup_mocked_integration(hass) assert await setup_mocked_integration(hass)
BMWDataUpdateCoordinator.async_update_listeners.reset_mock() BMWDataUpdateCoordinator.async_update_listeners.reset_mock()
# Test # Setup exception
await hass.services.async_call( monkeypatch.setattr(
"button", RemoteServices,
"press", "trigger_remote_service",
blocking=True, AsyncMock(side_effect=MyBMWRemoteServiceError),
target={"entity_id": "button.i4_edrive40_refresh_from_cloud"},
) )
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