Home Assistant Unofficial Reference 2024.12.1
convert_config.py
Go to the documentation of this file.
1 """Convert the HA config to the dynalite config."""
2 
3 from __future__ import annotations
4 
5 from types import MappingProxyType
6 from typing import Any
7 
8 from dynalite_devices_lib import const as dyn_const
9 
10 from homeassistant.const import (
11  CONF_DEFAULT,
12  CONF_HOST,
13  CONF_NAME,
14  CONF_PORT,
15  CONF_ROOM,
16  CONF_TYPE,
17 )
18 
19 from .const import (
20  ACTIVE_INIT,
21  ACTIVE_OFF,
22  ACTIVE_ON,
23  CONF_ACTIVE,
24  CONF_AREA,
25  CONF_AUTO_DISCOVER,
26  CONF_CHANNEL,
27  CONF_CHANNEL_COVER,
28  CONF_CLOSE_PRESET,
29  CONF_DEVICE_CLASS,
30  CONF_DURATION,
31  CONF_FADE,
32  CONF_LEVEL,
33  CONF_NO_DEFAULT,
34  CONF_OPEN_PRESET,
35  CONF_POLL_TIMER,
36  CONF_PRESET,
37  CONF_ROOM_OFF,
38  CONF_ROOM_ON,
39  CONF_STOP_PRESET,
40  CONF_TEMPLATE,
41  CONF_TILT_TIME,
42  CONF_TIME_COVER,
43 )
44 
45 ACTIVE_MAP = {
46  ACTIVE_INIT: dyn_const.ACTIVE_INIT,
47  False: dyn_const.ACTIVE_OFF,
48  ACTIVE_OFF: dyn_const.ACTIVE_OFF,
49  ACTIVE_ON: dyn_const.ACTIVE_ON,
50  True: dyn_const.ACTIVE_ON,
51 }
52 
53 TEMPLATE_MAP = {
54  CONF_ROOM: dyn_const.CONF_ROOM,
55  CONF_TIME_COVER: dyn_const.CONF_TIME_COVER,
56 }
57 
58 
59 def convert_with_map(config, conf_map):
60  """Create the initial converted map with just the basic key:value pairs updated."""
61  result = {}
62  for conf in conf_map:
63  if conf in config:
64  result[conf_map[conf]] = config[conf]
65  return result
66 
67 
68 def convert_channel(config: dict[str, Any]) -> dict[str, Any]:
69  """Convert the config for a channel."""
70  my_map = {
71  CONF_NAME: dyn_const.CONF_NAME,
72  CONF_FADE: dyn_const.CONF_FADE,
73  CONF_TYPE: dyn_const.CONF_CHANNEL_TYPE,
74  }
75  return convert_with_map(config, my_map)
76 
77 
78 def convert_preset(config: dict[str, Any]) -> dict[str, Any]:
79  """Convert the config for a preset."""
80  my_map = {
81  CONF_NAME: dyn_const.CONF_NAME,
82  CONF_FADE: dyn_const.CONF_FADE,
83  CONF_LEVEL: dyn_const.CONF_LEVEL,
84  }
85  return convert_with_map(config, my_map)
86 
87 
88 def convert_area(config: dict[str, Any]) -> dict[str, Any]:
89  """Convert the config for an area."""
90  my_map = {
91  CONF_NAME: dyn_const.CONF_NAME,
92  CONF_FADE: dyn_const.CONF_FADE,
93  CONF_NO_DEFAULT: dyn_const.CONF_NO_DEFAULT,
94  CONF_ROOM_ON: dyn_const.CONF_ROOM_ON,
95  CONF_ROOM_OFF: dyn_const.CONF_ROOM_OFF,
96  CONF_CHANNEL_COVER: dyn_const.CONF_CHANNEL_COVER,
97  CONF_DEVICE_CLASS: dyn_const.CONF_DEVICE_CLASS,
98  CONF_OPEN_PRESET: dyn_const.CONF_OPEN_PRESET,
99  CONF_CLOSE_PRESET: dyn_const.CONF_CLOSE_PRESET,
100  CONF_STOP_PRESET: dyn_const.CONF_STOP_PRESET,
101  CONF_DURATION: dyn_const.CONF_DURATION,
102  CONF_TILT_TIME: dyn_const.CONF_TILT_TIME,
103  }
104  result = convert_with_map(config, my_map)
105  if CONF_CHANNEL in config:
106  result[dyn_const.CONF_CHANNEL] = {
107  channel: convert_channel(channel_conf)
108  for (channel, channel_conf) in config[CONF_CHANNEL].items()
109  }
110  if CONF_PRESET in config:
111  result[dyn_const.CONF_PRESET] = {
112  preset: convert_preset(preset_conf)
113  for (preset, preset_conf) in config[CONF_PRESET].items()
114  }
115  if CONF_TEMPLATE in config:
116  result[dyn_const.CONF_TEMPLATE] = TEMPLATE_MAP[config[CONF_TEMPLATE]]
117  return result
118 
119 
120 def convert_default(config: dict[str, Any]) -> dict[str, Any]:
121  """Convert the config for the platform defaults."""
122  return convert_with_map(config, {CONF_FADE: dyn_const.CONF_FADE})
123 
124 
125 def convert_template(config: dict[str, Any]) -> dict[str, Any]:
126  """Convert the config for a template."""
127  my_map = {
128  CONF_ROOM_ON: dyn_const.CONF_ROOM_ON,
129  CONF_ROOM_OFF: dyn_const.CONF_ROOM_OFF,
130  CONF_CHANNEL_COVER: dyn_const.CONF_CHANNEL_COVER,
131  CONF_DEVICE_CLASS: dyn_const.CONF_DEVICE_CLASS,
132  CONF_OPEN_PRESET: dyn_const.CONF_OPEN_PRESET,
133  CONF_CLOSE_PRESET: dyn_const.CONF_CLOSE_PRESET,
134  CONF_STOP_PRESET: dyn_const.CONF_STOP_PRESET,
135  CONF_DURATION: dyn_const.CONF_DURATION,
136  CONF_TILT_TIME: dyn_const.CONF_TILT_TIME,
137  }
138  return convert_with_map(config, my_map)
139 
140 
142  config: dict[str, Any] | MappingProxyType[str, Any],
143 ) -> dict[str, Any]:
144  """Convert a config dict by replacing component consts with library consts."""
145  my_map = {
146  CONF_NAME: dyn_const.CONF_NAME,
147  CONF_HOST: dyn_const.CONF_HOST,
148  CONF_PORT: dyn_const.CONF_PORT,
149  CONF_AUTO_DISCOVER: dyn_const.CONF_AUTO_DISCOVER,
150  CONF_POLL_TIMER: dyn_const.CONF_POLL_TIMER,
151  }
152  result = convert_with_map(config, my_map)
153  if CONF_AREA in config:
154  result[dyn_const.CONF_AREA] = {
155  area: convert_area(area_conf)
156  for (area, area_conf) in config[CONF_AREA].items()
157  }
158  if CONF_DEFAULT in config:
159  result[dyn_const.CONF_DEFAULT] = convert_default(config[CONF_DEFAULT])
160  if CONF_ACTIVE in config:
161  result[dyn_const.CONF_ACTIVE] = ACTIVE_MAP[config[CONF_ACTIVE]]
162  if CONF_PRESET in config:
163  result[dyn_const.CONF_PRESET] = {
164  preset: convert_preset(preset_conf)
165  for (preset, preset_conf) in config[CONF_PRESET].items()
166  }
167  if CONF_TEMPLATE in config:
168  result[dyn_const.CONF_TEMPLATE] = {
169  TEMPLATE_MAP[template]: convert_template(template_conf)
170  for (template, template_conf) in config[CONF_TEMPLATE].items()
171  }
172  return result
dict[str, Any] convert_area(dict[str, Any] config)
dict[str, Any] convert_channel(dict[str, Any] config)
dict[str, Any] convert_default(dict[str, Any] config)
dict[str, Any] convert_preset(dict[str, Any] config)
dict[str, Any] convert_config(dict[str, Any]|MappingProxyType[str, Any] config)
dict[str, Any] convert_template(dict[str, Any] config)