Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """Utility functions for the ScreenLogic integration."""
2 
3 import logging
4 
5 from screenlogicpy.const.data import SHARED_VALUES
6 
7 from homeassistant.helpers import entity_registry as er
8 
9 from .const import DOMAIN as SL_DOMAIN, SL_UNIT_TO_HA_UNIT, ScreenLogicDataPath
10 from .coordinator import ScreenlogicDataUpdateCoordinator
11 
12 _LOGGER = logging.getLogger(__name__)
13 
14 
15 def generate_unique_id(*args: str | int | None) -> str:
16  """Generate new unique_id for a screenlogic entity from specified parameters."""
17  _LOGGER.debug("gen_uid called with %s", args)
18  if len(args) == 3:
19  if args[2] in SHARED_VALUES:
20  if args[1] is not None and (isinstance(args[1], int) or args[1].isdigit()):
21  return f"{args[0]}_{args[1]}_{args[2]}"
22  return f"{args[0]}_{args[2]}"
23  return f"{args[2]}"
24  return f"{args[1]}"
25 
26 
27 def get_ha_unit(sl_unit) -> str:
28  """Return equivalent Home Assistant unit of measurement if exists."""
29  if (ha_unit := SL_UNIT_TO_HA_UNIT.get(sl_unit)) is not None:
30  return ha_unit
31  return sl_unit
32 
33 
35  coordinator: ScreenlogicDataUpdateCoordinator,
36  platform_domain: str,
37  data_path: ScreenLogicDataPath,
38 ) -> None:
39  """Remove excluded entity if it exists."""
40  assert coordinator.config_entry
41  entity_registry = er.async_get(coordinator.hass)
42  unique_id = f"{coordinator.config_entry.unique_id}_{generate_unique_id(*data_path)}"
43  if entity_id := entity_registry.async_get_entity_id(
44  platform_domain, SL_DOMAIN, unique_id
45  ):
46  _LOGGER.debug(
47  "Removing existing entity '%s' per data inclusion rule", entity_id
48  )
49  entity_registry.async_remove(entity_id)
str generate_unique_id(*str|int|None args)
Definition: util.py:15
None cleanup_excluded_entity(ScreenlogicDataUpdateCoordinator coordinator, str platform_domain, ScreenLogicDataPath data_path)
Definition: util.py:38