Home Assistant Unofficial Reference 2024.12.1
auto_setup.py
Go to the documentation of this file.
1 """Handle auto setup of IHC products from the ihc project file."""
2 
3 import logging
4 import os.path
5 
6 from defusedxml import ElementTree
7 import voluptuous as vol
8 
9 from homeassistant.config import load_yaml_config_file
10 from homeassistant.const import CONF_TYPE, CONF_UNIT_OF_MEASUREMENT, UnitOfTemperature
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import discovery
14 
15 from .const import (
16  AUTO_SETUP_YAML,
17  CONF_BINARY_SENSOR,
18  CONF_DIMMABLE,
19  CONF_INVERTING,
20  CONF_LIGHT,
21  CONF_NODE,
22  CONF_SENSOR,
23  CONF_SWITCH,
24  CONF_XPATH,
25  DOMAIN,
26  IHC_PLATFORMS,
27 )
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 
32 AUTO_SETUP_SCHEMA = vol.Schema(
33  {
34  vol.Optional(CONF_BINARY_SENSOR, default=[]): vol.All(
35  cv.ensure_list,
36  [
37  vol.All(
38  {
39  vol.Required(CONF_NODE): cv.string,
40  vol.Required(CONF_XPATH): cv.string,
41  vol.Optional(CONF_INVERTING, default=False): cv.boolean,
42  vol.Optional(CONF_TYPE): cv.string,
43  }
44  )
45  ],
46  ),
47  vol.Optional(CONF_LIGHT, default=[]): vol.All(
48  cv.ensure_list,
49  [
50  vol.All(
51  {
52  vol.Required(CONF_NODE): cv.string,
53  vol.Required(CONF_XPATH): cv.string,
54  vol.Optional(CONF_DIMMABLE, default=False): cv.boolean,
55  }
56  )
57  ],
58  ),
59  vol.Optional(CONF_SENSOR, default=[]): vol.All(
60  cv.ensure_list,
61  [
62  vol.All(
63  {
64  vol.Required(CONF_NODE): cv.string,
65  vol.Required(CONF_XPATH): cv.string,
66  vol.Optional(
67  CONF_UNIT_OF_MEASUREMENT, default=UnitOfTemperature.CELSIUS
68  ): cv.string,
69  }
70  )
71  ],
72  ),
73  vol.Optional(CONF_SWITCH, default=[]): vol.All(
74  cv.ensure_list,
75  [
76  vol.All(
77  {
78  vol.Required(CONF_NODE): cv.string,
79  vol.Required(CONF_XPATH): cv.string,
80  }
81  )
82  ],
83  ),
84  }
85 )
86 
87 
88 def autosetup_ihc_products(hass: HomeAssistant, config, ihc_controller, controller_id):
89  """Auto setup of IHC products from the IHC project file."""
90  if not (project_xml := ihc_controller.get_project()):
91  _LOGGER.error("Unable to read project from IHC controller")
92  return False
93  project = ElementTree.fromstring(project_xml)
94 
95  # If an auto setup file exist in the configuration it will override
96  yaml_path = hass.config.path(AUTO_SETUP_YAML)
97  if not os.path.isfile(yaml_path):
98  yaml_path = os.path.join(os.path.dirname(__file__), AUTO_SETUP_YAML)
99  yaml = load_yaml_config_file(yaml_path)
100  try:
101  auto_setup_conf = AUTO_SETUP_SCHEMA(yaml)
102  except vol.Invalid as exception:
103  _LOGGER.error("Invalid IHC auto setup data: %s", exception)
104  return False
105 
106  groups = project.findall(".//group")
107  for platform in IHC_PLATFORMS:
108  platform_setup = auto_setup_conf[platform]
109  discovery_info = get_discovery_info(platform_setup, groups, controller_id)
110  if discovery_info:
111  discovery.load_platform(hass, platform, DOMAIN, discovery_info, config)
112 
113  return True
114 
115 
116 def get_discovery_info(platform_setup, groups, controller_id):
117  """Get discovery info for specified IHC platform."""
118  discovery_data = {}
119  for group in groups:
120  groupname = group.attrib["name"]
121  for product_cfg in platform_setup:
122  products = group.findall(product_cfg[CONF_XPATH])
123  for product in products:
124  product_id = int(product.attrib["id"].strip("_"), 0)
125  nodes = product.findall(product_cfg[CONF_NODE])
126  for node in nodes:
127  if "setting" in node.attrib and node.attrib["setting"] == "yes":
128  continue
129  ihc_id = int(node.attrib["id"].strip("_"), 0)
130  name = f"{groupname}_{ihc_id}"
131  # make the model number look a bit nicer - strip leading _
132  model = product.get("product_identifier", "").lstrip("_")
133  device = {
134  "ihc_id": ihc_id,
135  "ctrl_id": controller_id,
136  "product": {
137  "id": product_id,
138  "name": product.get("name") or "",
139  "note": product.get("note") or "",
140  "position": product.get("position") or "",
141  "model": model,
142  "group": groupname,
143  },
144  "product_cfg": product_cfg,
145  }
146  discovery_data[name] = device
147  return discovery_data
def get_discovery_info(platform_setup, groups, controller_id)
Definition: auto_setup.py:116
def autosetup_ihc_products(HomeAssistant hass, config, ihc_controller, controller_id)
Definition: auto_setup.py:88
dict[Any, Any] load_yaml_config_file(str config_path, Secrets|None secrets=None)
Definition: config.py:268