Home Assistant Unofficial Reference 2024.12.1
helpers.py
Go to the documentation of this file.
1 """Helper functions to relate sensors keys and unique ids."""
2 
3 from aiopvpc.const import (
4  ALL_SENSORS,
5  KEY_INJECTION,
6  KEY_MAG,
7  KEY_OMIE,
8  KEY_PVPC,
9  TARIFFS,
10 )
11 
12 from homeassistant.helpers.entity_registry import RegistryEntry
13 
14 _ha_uniqueid_to_sensor_key = {
15  TARIFFS[0]: KEY_PVPC,
16  TARIFFS[1]: KEY_PVPC,
17  f"{TARIFFS[0]}_{KEY_INJECTION}": KEY_INJECTION,
18  f"{TARIFFS[1]}_{KEY_INJECTION}": KEY_INJECTION,
19  f"{TARIFFS[0]}_{KEY_MAG}": KEY_MAG,
20  f"{TARIFFS[1]}_{KEY_MAG}": KEY_MAG,
21  f"{TARIFFS[0]}_{KEY_OMIE}": KEY_OMIE,
22  f"{TARIFFS[1]}_{KEY_OMIE}": KEY_OMIE,
23 }
24 
25 
27  using_private_api: bool, entries: list[RegistryEntry]
28 ) -> set[str]:
29  """Get enabled API indicators."""
30  if not using_private_api:
31  return {KEY_PVPC}
32  if len(entries) > 1:
33  # activate only enabled sensors
34  return {
35  _ha_uniqueid_to_sensor_key[sensor.unique_id]
36  for sensor in entries
37  if not sensor.disabled
38  }
39  # default sensors when enabling token access
40  return {KEY_PVPC, KEY_INJECTION}
41 
42 
43 def make_sensor_unique_id(config_entry_id: str | None, sensor_key: str) -> str:
44  """Generate unique_id for each sensor kind and config entry."""
45  assert sensor_key in ALL_SENSORS
46  assert config_entry_id is not None
47  if sensor_key == KEY_PVPC:
48  # for old compatibility
49  return config_entry_id
50  return f"{config_entry_id}_{sensor_key}"
set[str] get_enabled_sensor_keys(bool using_private_api, list[RegistryEntry] entries)
Definition: helpers.py:28
str make_sensor_unique_id(str|None config_entry_id, str sensor_key)
Definition: helpers.py:43