Add unique id migration to Geniushub (#122330)
parent
1f3b06a9bd
commit
7c58476af9
|
@ -155,8 +155,22 @@ type GeniusHubConfigEntry = ConfigEntry[GeniusBroker]
|
|||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: GeniusHubConfigEntry) -> bool:
|
||||
"""Create a Genius Hub system."""
|
||||
if CONF_TOKEN in entry.data and CONF_MAC in entry.data:
|
||||
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 reg_entry.unique_id.startswith(entry.data[CONF_MAC]):
|
||||
entity_registry.async_update_entity(
|
||||
reg_entry.entity_id,
|
||||
new_unique_id=reg_entry.unique_id.replace(
|
||||
entry.data[CONF_MAC], entry.entry_id
|
||||
),
|
||||
)
|
||||
|
||||
session = async_get_clientsession(hass)
|
||||
unique_id: str
|
||||
if CONF_HOST in entry.data:
|
||||
client = GeniusHub(
|
||||
entry.data[CONF_HOST],
|
||||
|
@ -164,14 +178,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: GeniusHubConfigEntry) ->
|
|||
password=entry.data[CONF_PASSWORD],
|
||||
session=session,
|
||||
)
|
||||
unique_id = entry.data[CONF_MAC]
|
||||
else:
|
||||
client = GeniusHub(entry.data[CONF_TOKEN], session=session)
|
||||
unique_id = entry.entry_id
|
||||
|
||||
unique_id = entry.unique_id or entry.entry_id
|
||||
|
||||
broker = entry.runtime_data = GeniusBroker(
|
||||
hass, client, entry.data.get(CONF_MAC, unique_id)
|
||||
)
|
||||
broker = entry.runtime_data = GeniusBroker(hass, client, unique_id)
|
||||
|
||||
try:
|
||||
await client.update()
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
"""Tests for the Genius Hub component."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from homeassistant.components.geniushub import DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import CONF_MAC, CONF_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_cloud_unique_id_migration(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_geniushub_cloud: AsyncMock,
|
||||
) -> None:
|
||||
"""Test that the cloud unique ID is migrated to the entry_id."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="Genius hub",
|
||||
data={
|
||||
CONF_TOKEN: "abcdef",
|
||||
CONF_MAC: "aa:bb:cc:dd:ee:ff",
|
||||
},
|
||||
entry_id="1234",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
entity_registry.async_get_or_create(
|
||||
SENSOR_DOMAIN, DOMAIN, "aa:bb:cc:dd:ee:ff_device_78", config_entry=entry
|
||||
)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("sensor.geniushub_aa_bb_cc_dd_ee_ff_device_78")
|
||||
entity_entry = entity_registry.async_get(
|
||||
"sensor.geniushub_aa_bb_cc_dd_ee_ff_device_78"
|
||||
)
|
||||
assert entity_entry.unique_id == "1234_device_78"
|
Loading…
Reference in New Issue