|
9 | 9 | from syrupy.assertion import SnapshotAssertion |
10 | 10 |
|
11 | 11 | from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN |
12 | | -from homeassistant.components.shelly.const import UPDATE_PERIOD_MULTIPLIER |
| 12 | +from homeassistant.components.shelly.const import DOMAIN, UPDATE_PERIOD_MULTIPLIER |
13 | 13 | from homeassistant.const import ( |
14 | 14 | STATE_OFF, |
15 | 15 | STATE_ON, |
|
22 | 22 | from homeassistant.helpers.entity_registry import EntityRegistry |
23 | 23 |
|
24 | 24 | from . import ( |
| 25 | + MOCK_MAC, |
25 | 26 | init_integration, |
26 | 27 | mock_rest_update, |
27 | 28 | mutate_rpc_device_status, |
@@ -670,3 +671,57 @@ async def test_rpc_presencezone_component( |
670 | 671 |
|
671 | 672 | assert (state := hass.states.get(entity_id)) |
672 | 673 | assert state.state == STATE_UNAVAILABLE |
| 674 | + |
| 675 | + |
| 676 | +@pytest.mark.parametrize( |
| 677 | + ("old_id", "new_id", "role"), |
| 678 | + [ |
| 679 | + ("boolean", "boolean_generic", None), |
| 680 | + ("boolean", "boolean_has_power", "has_power"), |
| 681 | + ("input", "input", None), # negative test, input is not a virtual component |
| 682 | + ], |
| 683 | +) |
| 684 | +async def test_migrate_unique_id_virtual_components_roles( |
| 685 | + hass: HomeAssistant, |
| 686 | + mock_rpc_device: Mock, |
| 687 | + entity_registry: EntityRegistry, |
| 688 | + caplog: pytest.LogCaptureFixture, |
| 689 | + monkeypatch: pytest.MonkeyPatch, |
| 690 | + old_id: str, |
| 691 | + new_id: str, |
| 692 | + role: str | None, |
| 693 | +) -> None: |
| 694 | + """Test migration of unique_id for virtual components to include role.""" |
| 695 | + entry = await init_integration(hass, 3, skip_setup=True) |
| 696 | + unique_base = f"{MOCK_MAC}-{old_id}:200" |
| 697 | + old_unique_id = f"{unique_base}-{old_id}" |
| 698 | + new_unique_id = f"{unique_base}-{new_id}" |
| 699 | + config = deepcopy(mock_rpc_device.config) |
| 700 | + if role: |
| 701 | + config[f"{old_id}:200"] = { |
| 702 | + "role": role, |
| 703 | + } |
| 704 | + else: |
| 705 | + config[f"{old_id}:200"] = {} |
| 706 | + monkeypatch.setattr(mock_rpc_device, "config", config) |
| 707 | + |
| 708 | + entity = entity_registry.async_get_or_create( |
| 709 | + suggested_object_id="test_name_test_sensor", |
| 710 | + disabled_by=None, |
| 711 | + domain=BINARY_SENSOR_DOMAIN, |
| 712 | + platform=DOMAIN, |
| 713 | + unique_id=old_unique_id, |
| 714 | + config_entry=entry, |
| 715 | + ) |
| 716 | + assert entity.unique_id == old_unique_id |
| 717 | + |
| 718 | + await hass.config_entries.async_setup(entry.entry_id) |
| 719 | + await hass.async_block_till_done() |
| 720 | + |
| 721 | + entity_entry = entity_registry.async_get("binary_sensor.test_name_test_sensor") |
| 722 | + assert entity_entry |
| 723 | + assert entity_entry.unique_id == new_unique_id |
| 724 | + |
| 725 | + assert ( |
| 726 | + "Migrating unique_id for binary_sensor.test_name_test_sensor" in caplog.text |
| 727 | + ) == (old_id != new_id) |
0 commit comments