Home Assistant Unofficial Reference 2024.12.1
models.py
Go to the documentation of this file.
1 """The ISY/IoX integration data models."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import cast
7 
8 from pyisy import ISY
9 from pyisy.constants import PROTO_INSTEON
10 from pyisy.networking import NetworkCommand
11 from pyisy.nodes import Group, Node
12 from pyisy.programs import Program
13 from pyisy.variables import Variable
14 
15 from homeassistant.const import Platform
16 from homeassistant.helpers.device_registry import DeviceInfo
17 
18 from .const import (
19  CONF_NETWORK,
20  NODE_AUX_PROP_PLATFORMS,
21  NODE_PLATFORMS,
22  PROGRAM_PLATFORMS,
23  ROOT_NODE_PLATFORMS,
24  VARIABLE_PLATFORMS,
25 )
26 
27 
28 @dataclass
29 class IsyData:
30  """Data for the ISY/IoX integration."""
31 
32  root: ISY
33  nodes: dict[Platform, list[Node | Group]]
34  root_nodes: dict[Platform, list[Node]]
35  variables: dict[Platform, list[Variable]]
36  programs: dict[Platform, list[tuple[str, Program, Program]]]
37  net_resources: list[NetworkCommand]
38  devices: dict[str, DeviceInfo]
39  aux_properties: dict[Platform, list[tuple[Node, str]]]
40 
41  def __init__(self) -> None:
42  """Initialize an empty ISY data class."""
43  self.nodesnodes = {p: [] for p in NODE_PLATFORMS}
44  self.root_nodesroot_nodes = {p: [] for p in ROOT_NODE_PLATFORMS}
45  self.aux_propertiesaux_properties = {p: [] for p in NODE_AUX_PROP_PLATFORMS}
46  self.programsprograms = {p: [] for p in PROGRAM_PLATFORMS}
47  self.variablesvariables = {p: [] for p in VARIABLE_PLATFORMS}
48  self.net_resourcesnet_resources = []
49  self.devicesdevices = {}
50 
51  @property
52  def uuid(self) -> str:
53  """Return the ISY UUID identification."""
54  return cast(str, self.root.uuid)
55 
56  def uid_base(self, node: Node | Group | Variable | Program | NetworkCommand) -> str:
57  """Return the unique id base string for a given node."""
58  if isinstance(node, NetworkCommand):
59  return f"{self.uuid}_{CONF_NETWORK}_{node.address}"
60  return f"{self.uuid}_{node.address}"
61 
62  @property
63  def unique_ids(self) -> set[tuple[Platform, str]]:
64  """Return all the unique ids for a config entry id."""
65  current_unique_ids: set[tuple[Platform, str]] = {
66  (Platform.BUTTON, f"{self.uuid}_query")
67  }
68 
69  # Structure and prefixes here must match what's added in __init__ and helpers
70  for platform in NODE_PLATFORMS:
71  for node in self.nodesnodes[platform]:
72  current_unique_ids.add((platform, self.uid_baseuid_base(node)))
73 
74  for platform in NODE_AUX_PROP_PLATFORMS:
75  for node, control in self.aux_propertiesaux_properties[platform]:
76  current_unique_ids.add((platform, f"{self.uid_base(node)}_{control}"))
77 
78  for platform in PROGRAM_PLATFORMS:
79  for _, node, _ in self.programsprograms[platform]:
80  current_unique_ids.add((platform, self.uid_baseuid_base(node)))
81 
82  for platform in VARIABLE_PLATFORMS:
83  for node in self.variablesvariables[platform]:
84  current_unique_ids.add((platform, self.uid_baseuid_base(node)))
85  if platform == Platform.NUMBER:
86  current_unique_ids.add((platform, f"{self.uid_base(node)}_init"))
87 
88  for platform in ROOT_NODE_PLATFORMS:
89  for node in self.root_nodesroot_nodes[platform]:
90  current_unique_ids.add((platform, f"{self.uid_base(node)}_query"))
91  if platform == Platform.BUTTON and node.protocol == PROTO_INSTEON:
92  current_unique_ids.add((platform, f"{self.uid_base(node)}_beep"))
93 
94  for node in self.net_resourcesnet_resources:
95  current_unique_ids.add((Platform.BUTTON, self.uid_baseuid_base(node)))
96 
97  return current_unique_ids
str uid_base(self, Node|Group|Variable|Program|NetworkCommand node)
Definition: models.py:56
set[tuple[Platform, str]] unique_ids(self)
Definition: models.py:63