Migrate SleepIQ unique IDs that are using sleeper name instead of sleeper ID (#68062)
This commit is contained in:
parent
a902f0ee53
commit
e59bf4f3af
5 changed files with 138 additions and 15 deletions
|
@ -5,14 +5,35 @@ from asyncsleepiq import (
|
|||
SleepIQTimeoutException,
|
||||
)
|
||||
|
||||
from homeassistant.components.sleepiq.const import DOMAIN
|
||||
from homeassistant.components.sleepiq.const import (
|
||||
DOMAIN,
|
||||
IS_IN_BED,
|
||||
PRESSURE,
|
||||
SLEEP_NUMBER,
|
||||
)
|
||||
from homeassistant.components.sleepiq.coordinator import UPDATE_INTERVAL
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.components.sleepiq.conftest import setup_platform
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, mock_registry
|
||||
from tests.components.sleepiq.conftest import (
|
||||
BED_ID,
|
||||
SLEEPER_L_ID,
|
||||
SLEEPER_L_NAME,
|
||||
SLEEPER_L_NAME_LOWER,
|
||||
SLEEPIQ_CONFIG,
|
||||
setup_platform,
|
||||
)
|
||||
|
||||
ENTITY_IS_IN_BED = f"sensor.sleepnumber_{BED_ID}_{SLEEPER_L_NAME_LOWER}_{IS_IN_BED}"
|
||||
ENTITY_PRESSURE = f"sensor.sleepnumber_{BED_ID}_{SLEEPER_L_NAME_LOWER}_{PRESSURE}"
|
||||
ENTITY_SLEEP_NUMBER = (
|
||||
f"sensor.sleepnumber_{BED_ID}_{SLEEPER_L_NAME_LOWER}_{SLEEP_NUMBER}"
|
||||
)
|
||||
|
||||
|
||||
async def test_unload_entry(hass: HomeAssistant, mock_asyncsleepiq) -> None:
|
||||
|
@ -64,3 +85,52 @@ async def test_api_timeout(hass: HomeAssistant, mock_asyncsleepiq) -> None:
|
|||
mock_asyncsleepiq.init_beds.side_effect = SleepIQTimeoutException
|
||||
entry = await setup_platform(hass, None)
|
||||
assert not await hass.config_entries.async_setup(entry.entry_id)
|
||||
|
||||
|
||||
async def test_unique_id_migration(hass: HomeAssistant, mock_asyncsleepiq) -> None:
|
||||
"""Test migration of sensor unique IDs."""
|
||||
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=SLEEPIQ_CONFIG,
|
||||
unique_id=SLEEPIQ_CONFIG[CONF_USERNAME].lower(),
|
||||
)
|
||||
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
mock_registry(
|
||||
hass,
|
||||
{
|
||||
ENTITY_IS_IN_BED: er.RegistryEntry(
|
||||
entity_id=ENTITY_IS_IN_BED,
|
||||
unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{IS_IN_BED}",
|
||||
platform=DOMAIN,
|
||||
config_entry_id=mock_entry.entry_id,
|
||||
),
|
||||
ENTITY_PRESSURE: er.RegistryEntry(
|
||||
entity_id=ENTITY_PRESSURE,
|
||||
unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{PRESSURE}",
|
||||
platform=DOMAIN,
|
||||
config_entry_id=mock_entry.entry_id,
|
||||
),
|
||||
ENTITY_SLEEP_NUMBER: er.RegistryEntry(
|
||||
entity_id=ENTITY_SLEEP_NUMBER,
|
||||
unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{SLEEP_NUMBER}",
|
||||
platform=DOMAIN,
|
||||
config_entry_id=mock_entry.entry_id,
|
||||
),
|
||||
},
|
||||
)
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
ent_reg = er.async_get(hass)
|
||||
|
||||
sensor_is_in_bed = ent_reg.async_get(ENTITY_IS_IN_BED)
|
||||
assert sensor_is_in_bed.unique_id == f"{SLEEPER_L_ID}_{IS_IN_BED}"
|
||||
|
||||
sensor_pressure = ent_reg.async_get(ENTITY_PRESSURE)
|
||||
assert sensor_pressure.unique_id == f"{SLEEPER_L_ID}_{PRESSURE}"
|
||||
|
||||
sensor_sleep_number = ent_reg.async_get(ENTITY_SLEEP_NUMBER)
|
||||
assert sensor_sleep_number.unique_id == f"{SLEEPER_L_ID}_{SLEEP_NUMBER}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue