Skip to content

Commit

Permalink
Ensure updated HA state after remote service
Browse files Browse the repository at this point in the history
  • Loading branch information
rikroe committed May 29, 2023
1 parent 4193a27 commit daa21c5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
8 changes: 8 additions & 0 deletions homeassistant/components/bmw_connected_drive/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,17 @@ async def async_turn_on(self, **kwargs: Any) -> None:
except MyBMWAPIError as ex:
raise HomeAssistantError(ex) from ex

# BMW remote services that change the vehicle's state update the local object
# when executing the service, so only the HA state machine needs further updates.
self.coordinator.async_update_listeners()

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
try:
await self.entity_description.remote_service_off(self.vehicle)
except MyBMWAPIError as ex:
raise HomeAssistantError(ex) from ex

# BMW remote services that change the vehicle's state update the local object
# when executing the service, so only the HA state machine needs further updates.
self.coordinator.async_update_listeners()
12 changes: 11 additions & 1 deletion tests/components/bmw_connected_drive/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Fixtures for BMW tests."""

from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, Mock

from bimmer_connected.api.authentication import MyBMWAuthentication
from bimmer_connected.vehicle.remote_services import RemoteServices, RemoteServiceStatus
import pytest

from homeassistant.components.bmw_connected_drive.coordinator import (
BMWDataUpdateCoordinator,
)

from . import mock_login, mock_vehicles


Expand All @@ -20,5 +24,11 @@ async def bmw_fixture(monkeypatch):
AsyncMock(return_value=RemoteServiceStatus({"eventStatus": "EXECUTED"})),
)

monkeypatch.setattr(
BMWDataUpdateCoordinator,
"async_update_listeners",
Mock(),
)

with mock_vehicles():
yield mock_vehicles
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by MyBMW',
'friendly_name': 'i4 eDrive40 Charge',
'friendly_name': 'i4 eDrive40 Charging',
'icon': 'mdi:ev-station',
}),
'context': <ANY>,
'entity_id': 'switch.i4_edrive40_charge',
'entity_id': 'switch.i4_edrive40_charging',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'on',
Expand Down
9 changes: 7 additions & 2 deletions tests/components/bmw_connected_drive/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import respx
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.bmw_connected_drive.coordinator import (
BMWDataUpdateCoordinator,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError

Expand All @@ -32,8 +35,8 @@ async def test_entity_state_attrs(
[
("switch.i4_edrive40_climate", "ON"),
("switch.i4_edrive40_climate", "OFF"),
("switch.i4_edrive40_charge", "ON"),
("switch.i4_edrive40_charge", "OFF"),
("switch.i4_edrive40_charging", "ON"),
("switch.i4_edrive40_charging", "OFF"),
],
)
async def test_update_triggers_success(
Expand All @@ -46,6 +49,7 @@ async def test_update_triggers_success(

# Setup component
assert await setup_mocked_integration(hass)
BMWDataUpdateCoordinator.async_update_listeners.reset_mock()

# Test
await hass.services.async_call(
Expand All @@ -55,6 +59,7 @@ async def test_update_triggers_success(
target={"entity_id": entity_id},
)
assert RemoteServices.trigger_remote_service.call_count == 1
assert BMWDataUpdateCoordinator.async_update_listeners.call_count == 1


@pytest.mark.parametrize(
Expand Down

0 comments on commit daa21c5

Please sign in to comment.