mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 12:21:04 -04:00
Adapt bosch_shc to in-place entity ID change (#185906)
This commit is contained in:
@@ -10,7 +10,7 @@ from homeassistant.components.alarm_control_panel import (
|
||||
AlarmControlPanelState,
|
||||
CodeFormat,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
@@ -73,6 +73,17 @@ class IntrusionSystemAlarmControlPanel(AlarmControlPanelEntity):
|
||||
device_info["via_device_id"] = hub.id
|
||||
self._attr_device_info = device_info
|
||||
|
||||
@callback
|
||||
@override
|
||||
def async_entity_id_changed(self, old_entity_id: str) -> None:
|
||||
"""Opt in to in-place entity_id changes.
|
||||
|
||||
Can be removed when backwards compatibility is disabled for core
|
||||
integrations, i.e. once all core integrations have been migrated
|
||||
to in-place entity ID change.
|
||||
"""
|
||||
super().async_entity_id_changed(old_entity_id)
|
||||
|
||||
@override
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe to SHC events."""
|
||||
@@ -81,13 +92,13 @@ class IntrusionSystemAlarmControlPanel(AlarmControlPanelEntity):
|
||||
def on_state_changed() -> None:
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
self._device.subscribe_callback(self.entity_id, on_state_changed)
|
||||
self._device.subscribe_callback(self.unique_id, on_state_changed)
|
||||
|
||||
@override
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Unsubscribe from SHC events."""
|
||||
await super().async_will_remove_from_hass()
|
||||
self._device.unsubscribe_callback(self.entity_id)
|
||||
self._device.unsubscribe_callback(self.unique_id)
|
||||
|
||||
@property
|
||||
@override
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Tests for the bosch_shc alarm control panel platform."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
from collections.abc import Callable, Generator
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
from boschshcpy import SHCIntrusionSystem
|
||||
import pytest
|
||||
@@ -19,12 +19,15 @@ from homeassistant.const import (
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from .conftest import intrusion_system_device, setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
ENTITY_ID = "alarm_control_panel.alarm_system"
|
||||
NEW_ENTITY_ID = "alarm_control_panel.renamed_alarm_system"
|
||||
UNIQUE_ID = "test-mac_com.bosch.tt.intrusion.system"
|
||||
ArmingState = SHCIntrusionSystem.ArmingState
|
||||
AlarmState = SHCIntrusionSystem.AlarmState
|
||||
Profile = SHCIntrusionSystem.Profile
|
||||
@@ -160,3 +163,49 @@ async def test_push_update(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(ENTITY_ID).state == AlarmControlPanelState.TRIGGERED
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"remove_entity",
|
||||
[
|
||||
pytest.param(
|
||||
lambda entity_registry: entity_registry.async_remove(NEW_ENTITY_ID),
|
||||
id="remove",
|
||||
),
|
||||
pytest.param(
|
||||
lambda entity_registry: entity_registry.async_update_entity(
|
||||
NEW_ENTITY_ID, disabled_by=er.RegistryEntryDisabler.USER
|
||||
),
|
||||
id="disable",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_entity_id_change_in_place(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_session: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
remove_entity: Callable[[er.EntityRegistry], object],
|
||||
) -> None:
|
||||
"""The SHC subscription survives an entity_id change and is released on removal."""
|
||||
device = intrusion_system_device()
|
||||
mock_session.intrusion_system = device
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
entity_registry.async_update_entity(ENTITY_ID, new_entity_id=NEW_ENTITY_ID)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Renamed in place, not removed and re-added.
|
||||
device.subscribe_callback.assert_called_once_with(UNIQUE_ID, ANY)
|
||||
device.unsubscribe_callback.assert_not_called()
|
||||
|
||||
device.alarm_state = AlarmState.ALARM_ON
|
||||
device.subscribe_callback.call_args.args[1]()
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(NEW_ENTITY_ID).state == AlarmControlPanelState.TRIGGERED
|
||||
|
||||
remove_entity(entity_registry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(NEW_ENTITY_ID) is None
|
||||
device.unsubscribe_callback.assert_called_once_with(UNIQUE_ID)
|
||||
|
||||
Reference in New Issue
Block a user