Home Assistant Unofficial Reference 2024.12.1
const.py
Go to the documentation of this file.
1 """MySensors constants."""
2 
3 from __future__ import annotations
4 
5 from collections import defaultdict
6 from typing import Final, Literal, TypedDict
7 
8 from homeassistant.const import Platform
9 
10 ATTR_DEVICES: Final = "devices"
11 ATTR_GATEWAY_ID: Final = "gateway_id"
12 ATTR_NODE_ID: Final = "node_id"
13 
14 CONF_BAUD_RATE: Final = "baud_rate"
15 CONF_PERSISTENCE_FILE: Final = "persistence_file"
16 CONF_RETAIN: Final = "retain"
17 CONF_TCP_PORT: Final = "tcp_port"
18 CONF_TOPIC_IN_PREFIX: Final = "topic_in_prefix"
19 CONF_TOPIC_OUT_PREFIX: Final = "topic_out_prefix"
20 CONF_VERSION: Final = "version"
21 CONF_GATEWAY_TYPE: Final = "gateway_type"
22 type ConfGatewayType = Literal["Serial", "TCP", "MQTT"]
23 CONF_GATEWAY_TYPE_SERIAL: ConfGatewayType = "Serial"
24 CONF_GATEWAY_TYPE_TCP: ConfGatewayType = "TCP"
25 CONF_GATEWAY_TYPE_MQTT: ConfGatewayType = "MQTT"
26 
27 DOMAIN: Final = "mysensors"
28 MYSENSORS_GATEWAY_START_TASK: str = "mysensors_gateway_start_task_{}"
29 MYSENSORS_GATEWAYS: Final = "mysensors_gateways"
30 MYSENSORS_DISCOVERED_NODES: Final = "mysensors_discovered_nodes_{}"
31 PLATFORM: Final = "platform"
32 SCHEMA: Final = "schema"
33 CHILD_CALLBACK: str = "mysensors_child_callback_{}_{}_{}_{}"
34 NODE_CALLBACK: str = "mysensors_node_callback_{}_{}"
35 MYSENSORS_DISCOVERY: str = "mysensors_discovery_{}_{}"
36 MYSENSORS_NODE_DISCOVERY: str = "mysensors_node_discovery"
37 MYSENSORS_ON_UNLOAD: str = "mysensors_on_unload_{}"
38 TYPE: Final = "type"
39 UPDATE_DELAY: float = 0.1
40 
41 
42 class DiscoveryInfo(TypedDict):
43  """Represent the discovery info type for mysensors platforms."""
44 
45  devices: list[DevId]
46  gateway_id: GatewayId
47 
48 
49 class NodeDiscoveryInfo(TypedDict):
50  """Represent discovered mysensors node."""
51 
52  gateway_id: GatewayId
53  node_id: int
54 
55 
56 SERVICE_SEND_IR_CODE: Final = "send_ir_code"
57 
58 type SensorType = str
59 # S_DOOR, S_MOTION, S_SMOKE, ...
60 
61 type ValueType = str
62 # V_TRIPPED, V_ARMED, V_STATUS, V_PERCENTAGE, ...
63 
64 type GatewayId = str
65 # a unique id generated by config_flow.py and stored in the ConfigEntry as the entry id.
66 
67 type DevId = tuple[GatewayId, int, int, int]
68 # describes the backend of a hass entity.
69 # Contents are: GatewayId, node_id, child_id, v_type as int
70 #
71 # The string version of v_type can be looked up in the enum gateway.const.SetReq
72 # of the appropriate BaseAsyncGateway
73 # Home Assistant Entities are quite limited and only ever do one thing.
74 # MySensors Nodes have multiple child_ids each with a s_type several associated v_types
75 # The MySensors integration brings these together by creating an entity for every v_type
76 # of every child_id of every node.
77 # The DevId tuple perfectly captures this.
78 
79 BINARY_SENSOR_TYPES: dict[SensorType, set[ValueType]] = {
80  "S_DOOR": {"V_TRIPPED"},
81  "S_MOTION": {"V_TRIPPED"},
82  "S_SMOKE": {"V_TRIPPED"},
83  "S_SPRINKLER": {"V_TRIPPED"},
84  "S_WATER_LEAK": {"V_TRIPPED"},
85  "S_SOUND": {"V_TRIPPED"},
86  "S_VIBRATION": {"V_TRIPPED"},
87  "S_MOISTURE": {"V_TRIPPED"},
88 }
89 
90 CLIMATE_TYPES: dict[SensorType, set[ValueType]] = {"S_HVAC": {"V_HVAC_FLOW_STATE"}}
91 
92 COVER_TYPES: dict[SensorType, set[ValueType]] = {
93  "S_COVER": {"V_DIMMER", "V_PERCENTAGE", "V_LIGHT", "V_STATUS"}
94 }
95 
96 DEVICE_TRACKER_TYPES: dict[SensorType, set[ValueType]] = {"S_GPS": {"V_POSITION"}}
97 
98 LIGHT_TYPES: dict[SensorType, set[ValueType]] = {
99  "S_DIMMER": {"V_DIMMER", "V_PERCENTAGE"},
100  "S_RGB_LIGHT": {"V_RGB"},
101  "S_RGBW_LIGHT": {"V_RGBW"},
102 }
103 
104 REMOTE_TYPES: dict[SensorType, set[ValueType]] = {"S_IR": {"V_IR_SEND"}}
105 
106 SENSOR_TYPES: dict[SensorType, set[ValueType]] = {
107  "S_SOUND": {"V_LEVEL"},
108  "S_VIBRATION": {"V_LEVEL"},
109  "S_MOISTURE": {"V_LEVEL"},
110  "S_INFO": {"V_TEXT"},
111  "S_GPS": {"V_POSITION"},
112  "S_TEMP": {"V_TEMP"},
113  "S_HUM": {"V_HUM"},
114  "S_BARO": {"V_PRESSURE", "V_FORECAST"},
115  "S_WIND": {"V_WIND", "V_GUST", "V_DIRECTION"},
116  "S_RAIN": {"V_RAIN", "V_RAINRATE"},
117  "S_UV": {"V_UV"},
118  "S_WEIGHT": {"V_WEIGHT", "V_IMPEDANCE"},
119  "S_POWER": {"V_WATT", "V_KWH", "V_VAR", "V_VA", "V_POWER_FACTOR"},
120  "S_DISTANCE": {"V_DISTANCE"},
121  "S_LIGHT_LEVEL": {"V_LIGHT_LEVEL", "V_LEVEL"},
122  "S_IR": {"V_IR_RECEIVE", "V_IR_RECORD"},
123  "S_WATER": {"V_FLOW", "V_VOLUME"},
124  "S_CUSTOM": {"V_VAR1", "V_VAR2", "V_VAR3", "V_VAR4", "V_VAR5", "V_CUSTOM"},
125  "S_SCENE_CONTROLLER": {"V_SCENE_ON", "V_SCENE_OFF"},
126  "S_COLOR_SENSOR": {"V_RGB"},
127  "S_MULTIMETER": {"V_VOLTAGE", "V_CURRENT", "V_IMPEDANCE"},
128  "S_GAS": {"V_FLOW", "V_VOLUME"},
129  "S_WATER_QUALITY": {"V_TEMP", "V_PH", "V_ORP", "V_EC"},
130  "S_AIR_QUALITY": {"V_DUST_LEVEL", "V_LEVEL"},
131  "S_DUST": {"V_DUST_LEVEL", "V_LEVEL"},
132 }
133 
134 SWITCH_TYPES: dict[SensorType, set[ValueType]] = {
135  "S_LIGHT": {"V_LIGHT"},
136  "S_BINARY": {"V_STATUS"},
137  "S_DOOR": {"V_ARMED"},
138  "S_MOTION": {"V_ARMED"},
139  "S_SMOKE": {"V_ARMED"},
140  "S_SPRINKLER": {"V_STATUS"},
141  "S_WATER_LEAK": {"V_ARMED"},
142  "S_SOUND": {"V_ARMED"},
143  "S_VIBRATION": {"V_ARMED"},
144  "S_MOISTURE": {"V_ARMED"},
145  "S_LOCK": {"V_LOCK_STATUS"},
146  "S_WATER_QUALITY": {"V_STATUS"},
147 }
148 
149 TEXT_TYPES: dict[SensorType, set[ValueType]] = {"S_INFO": {"V_TEXT"}}
150 
151 PLATFORM_TYPES: dict[Platform, dict[SensorType, set[ValueType]]] = {
152  Platform.BINARY_SENSOR: BINARY_SENSOR_TYPES,
153  Platform.CLIMATE: CLIMATE_TYPES,
154  Platform.COVER: COVER_TYPES,
155  Platform.DEVICE_TRACKER: DEVICE_TRACKER_TYPES,
156  Platform.LIGHT: LIGHT_TYPES,
157  Platform.REMOTE: REMOTE_TYPES,
158  Platform.SENSOR: SENSOR_TYPES,
159  Platform.SWITCH: SWITCH_TYPES,
160  Platform.TEXT: TEXT_TYPES,
161 }
162 
163 FLAT_PLATFORM_TYPES: dict[tuple[str, SensorType], set[ValueType]] = {
164  (platform, s_type_name): v_type_name
165  for platform, platform_types in PLATFORM_TYPES.items()
166  for s_type_name, v_type_name in platform_types.items()
167 }
168 
169 TYPE_TO_PLATFORMS: dict[SensorType, list[Platform]] = defaultdict(list)
170 
171 for platform, platform_types in PLATFORM_TYPES.items():
172  for s_type_name in platform_types:
173  TYPE_TO_PLATFORMS[s_type_name].append(platform)
174 
175 PLATFORMS = tuple(PLATFORM_TYPES)