Make Sonos alarm `unique_id` unique with multiple households (#62645)
parent
cdd54b365a
commit
2172679f41
|
@ -91,6 +91,9 @@ async def async_setup_entry(
|
|||
"""Set up Sonos from a config entry."""
|
||||
|
||||
async def _async_create_alarms(speaker: SonosSpeaker, alarm_ids: list[str]) -> None:
|
||||
async_migrate_alarm_unique_ids(
|
||||
hass, config_entry, speaker.household_id, alarm_ids
|
||||
)
|
||||
entities = []
|
||||
created_alarms = (
|
||||
hass.data[DATA_SONOS].alarms[speaker.household_id].created_alarm_ids
|
||||
|
@ -211,7 +214,7 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
|
|||
def __init__(self, alarm_id: str, speaker: SonosSpeaker) -> None:
|
||||
"""Initialize the switch."""
|
||||
super().__init__(speaker)
|
||||
self._attr_unique_id = f"{SONOS_DOMAIN}-{alarm_id}"
|
||||
self._attr_unique_id = f"alarm-{speaker.household_id}:{alarm_id}"
|
||||
self.alarm_id = alarm_id
|
||||
self.household_id = speaker.household_id
|
||||
self.entity_id = ENTITY_ID_FORMAT.format(f"sonos_alarm_{self.alarm_id}")
|
||||
|
@ -357,6 +360,43 @@ class SonosAlarmEntity(SonosEntity, SwitchEntity):
|
|||
_LOGGER.error("Could not update %s: %s", self.entity_id, exc)
|
||||
|
||||
|
||||
@callback
|
||||
def async_migrate_alarm_unique_ids(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
household_id: str,
|
||||
alarm_ids: list[str],
|
||||
) -> None:
|
||||
"""Migrate alarm switch unique_ids in the entity registry to the new format."""
|
||||
entity_registry = er.async_get(hass)
|
||||
registry_entries = er.async_entries_for_config_entry(
|
||||
entity_registry, config_entry.entry_id
|
||||
)
|
||||
|
||||
alarm_entries = [
|
||||
(entry.unique_id, entry)
|
||||
for entry in registry_entries
|
||||
if entry.domain == Platform.SWITCH and entry.original_icon == "mdi:alarm"
|
||||
]
|
||||
|
||||
for old_unique_id, alarm_entry in alarm_entries:
|
||||
if ":" in old_unique_id:
|
||||
continue
|
||||
|
||||
entry_alarm_id = old_unique_id.split("-")[-1]
|
||||
if entry_alarm_id in alarm_ids:
|
||||
new_unique_id = f"alarm-{household_id}:{entry_alarm_id}"
|
||||
_LOGGER.debug(
|
||||
"Migrating unique_id for %s from %s to %s",
|
||||
alarm_entry.entity_id,
|
||||
old_unique_id,
|
||||
new_unique_id,
|
||||
)
|
||||
entity_registry.async_update_entity(
|
||||
alarm_entry.entity_id, new_unique_id=new_unique_id
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_migrate_speech_enhancement_entity_unique_id(
|
||||
hass: HomeAssistant,
|
||||
|
|
Loading…
Reference in New Issue