Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Small utility functions for the dlna_dms integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.core import HomeAssistant
6 from homeassistant.util import slugify
7 
8 from .const import CONF_SOURCE_ID, DOMAIN
9 
10 
11 def generate_source_id(hass: HomeAssistant, name: str) -> str:
12  """Generate a unique source ID."""
13  other_entries = hass.config_entries.async_entries(DOMAIN)
14  other_source_ids: set[str] = {
15  other_source_id
16  for entry in other_entries
17  if (other_source_id := entry.data.get(CONF_SOURCE_ID))
18  }
19 
20  source_id_base = slugify(name)
21  if source_id_base not in other_source_ids:
22  return source_id_base
23 
24  tries = 1
25  while (suggested_source_id := f"{source_id_base}_{tries}") in other_source_ids:
26  tries += 1
27 
28  return suggested_source_id
str generate_source_id(HomeAssistant hass, str name)
Definition: util.py:11