mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
Migrate islamic prayer times sensor unique_id to include entry_id (#100814)
* Migrate sensor unique_id to include entry_id * Apply suggestion
This commit is contained in:
parent
b3b235cbb7
commit
473d20712c
@ -3,8 +3,8 @@ from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv, entity_registry as er
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import IslamicPrayerDataUpdateCoordinator
|
||||
@ -16,6 +16,19 @@ CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
"""Set up the Islamic Prayer Component."""
|
||||
|
||||
@callback
|
||||
def update_unique_id(
|
||||
entity_entry: er.RegistryEntry,
|
||||
) -> dict[str, str] | None:
|
||||
"""Update unique ID of entity entry."""
|
||||
if not entity_entry.unique_id.startswith(f"{config_entry.entry_id}-"):
|
||||
new_unique_id = f"{config_entry.entry_id}-{entity_entry.unique_id}"
|
||||
return {"new_unique_id": new_unique_id}
|
||||
return None
|
||||
|
||||
await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
|
||||
|
||||
coordinator = IslamicPrayerDataUpdateCoordinator(hass)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
|
@ -9,7 +9,9 @@ import pytest
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import islamic_prayer_times
|
||||
from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import (
|
||||
NEW_PRAYER_TIMES,
|
||||
@ -145,3 +147,46 @@ async def test_update(hass: HomeAssistant) -> None:
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
assert pt_data.data == NEW_PRAYER_TIMES_TIMESTAMPS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("object_id", "old_unique_id"),
|
||||
[
|
||||
(
|
||||
"fajer_prayer",
|
||||
"Fajr",
|
||||
),
|
||||
(
|
||||
"dhuhr_prayer",
|
||||
"Dhuhr",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_migrate_unique_id(
|
||||
hass: HomeAssistant, object_id: str, old_unique_id: str
|
||||
) -> None:
|
||||
"""Test unique id migration."""
|
||||
entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
ent_reg = er.async_get(hass)
|
||||
|
||||
entity: er.RegistryEntry = ent_reg.async_get_or_create(
|
||||
suggested_object_id=object_id,
|
||||
domain=SENSOR_DOMAIN,
|
||||
platform=islamic_prayer_times.DOMAIN,
|
||||
unique_id=old_unique_id,
|
||||
config_entry=entry,
|
||||
)
|
||||
assert entity.unique_id == old_unique_id
|
||||
|
||||
with patch(
|
||||
"prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times",
|
||||
return_value=PRAYER_TIMES,
|
||||
), freeze_time(NOW):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_migrated = ent_reg.async_get(entity.entity_id)
|
||||
assert entity_migrated
|
||||
assert entity_migrated.unique_id == f"{entry.entry_id}-{old_unique_id}"
|
||||
|
@ -44,7 +44,6 @@ async def test_islamic_prayer_times_sensors(
|
||||
), freeze_time(NOW):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
hass.states.get(sensor_name).state
|
||||
== PRAYER_TIMES_TIMESTAMPS[key].astimezone(dt_util.UTC).isoformat()
|
||||
|
Loading…
x
Reference in New Issue
Block a user