Change of acmeda element unique_id (#124963)
* Update base.py Change unique_id to be explicitly a string. * Update __init__.py Add unique id migration * unique_id migration unit tests * Update __init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update __init__.py Fixed ruff formatting issue * Update __init__.py * Update __init__.py * In tests, load entity registries as test fixtures * Fix * Fix --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> Co-authored-by: Joostlek <joostlek@outlook.com>pull/125501/head
parent
bfe19e82ff
commit
3fa24f87c0
|
@ -3,6 +3,7 @@
|
|||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.entity_registry as er
|
||||
|
||||
from .hub import PulseHub
|
||||
|
||||
|
@ -17,6 +18,9 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, config_entry: AcmedaConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Rollease Acmeda Automate hub from a config entry."""
|
||||
|
||||
await _migrate_unique_ids(hass, config_entry)
|
||||
|
||||
hub = PulseHub(hass, config_entry)
|
||||
|
||||
if not await hub.async_setup():
|
||||
|
@ -28,6 +32,19 @@ async def async_setup_entry(
|
|||
return True
|
||||
|
||||
|
||||
async def _migrate_unique_ids(hass: HomeAssistant, entry: AcmedaConfigEntry) -> None:
|
||||
"""Migrate pre-config flow unique ids."""
|
||||
entity_registry = er.async_get(hass)
|
||||
registry_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, entry.entry_id
|
||||
)
|
||||
for reg_entry in registry_entries:
|
||||
if isinstance(reg_entry.unique_id, int): # type: ignore[unreachable]
|
||||
entity_registry.async_update_entity( # type: ignore[unreachable]
|
||||
reg_entry.entity_id, new_unique_id=str(reg_entry.unique_id)
|
||||
)
|
||||
|
||||
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, config_entry: AcmedaConfigEntry
|
||||
) -> bool:
|
||||
|
|
|
@ -67,7 +67,7 @@ class AcmedaBase(entity.Entity):
|
|||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique ID of this roller."""
|
||||
return self.roller.id # type: ignore[no-any-return]
|
||||
return str(self.roller.id)
|
||||
|
||||
@property
|
||||
def device_id(self) -> str:
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
"""Define fixtures available for all Acmeda tests."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.acmeda.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
mock_config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_HOST: "127.0.0.1"},
|
||||
)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
return mock_config_entry
|
|
@ -0,0 +1,28 @@
|
|||
"""Define tests for the Acmeda config flow."""
|
||||
|
||||
from homeassistant.components.acmeda.const import DOMAIN
|
||||
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_cover_id_migration(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test migrating unique id."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
entity_registry.async_get_or_create(
|
||||
COVER_DOMAIN, DOMAIN, 1234567890123, config_entry=mock_config_entry
|
||||
)
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
entities = er.async_entries_for_config_entry(
|
||||
entity_registry, mock_config_entry.entry_id
|
||||
)
|
||||
assert len(entities) == 1
|
||||
assert entities[0].unique_id == "1234567890123"
|
|
@ -0,0 +1,39 @@
|
|||
"""Define tests for the Acmeda config flow."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.acmeda.const import DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
|
||||
"""Return the default mocked config entry."""
|
||||
mock_config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"host": "127.0.0.1"},
|
||||
)
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
return mock_config_entry
|
||||
|
||||
|
||||
async def test_sensor_id_migration(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test migrating unique id."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
entity_registry.async_get_or_create(
|
||||
SENSOR_DOMAIN, DOMAIN, 1234567890123, config_entry=mock_config_entry
|
||||
)
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
entities = er.async_entries_for_config_entry(
|
||||
entity_registry, mock_config_entry.entry_id
|
||||
)
|
||||
assert len(entities) == 1
|
||||
assert entities[0].unique_id == "1234567890123"
|
Loading…
Reference in New Issue