13
13
UNIT_PERCENT ,
14
14
)
15
15
from esphome .core import (
16
- CORE ,
16
+ CORE ,
17
17
Lambda
18
18
)
19
19
50
50
51
51
CONF_DEVICE_ID = "samsung_ac_device_id"
52
52
CONF_DEVICE_ADDRESS = "address"
53
- CONF_CAPABILITIES = "capabilities"
54
- CONF_HORIZONTAL_SWING = "horizontal_swing"
55
- CONF_VERTICAL_SWING = "vertical_swing"
56
- CONF_PRESETS = "presets"
57
- CONF_PRESET_NAME = "name"
58
- CONF_PRESET_ENABLED = "enabled"
59
- CONF_PRESET_VALUE = "value"
60
53
CONF_DEVICE_ROOM_TEMPERATURE = "room_temperature"
61
54
CONF_DEVICE_TARGET_TEMPERATURE = "target_temperature"
62
55
CONF_DEVICE_OUTDOOR_TEMPERATURE = "outdoor_temperature"
63
56
CONF_DEVICE_POWER = "power"
64
57
CONF_DEVICE_MODE = "mode"
65
58
CONF_DEVICE_CLIMATE = "climate"
66
- CONF_DEVICE_CUSTOM = "custom_sensor"
67
- CONF_RAW_FILTERS = "raw_filters"
68
- CONF_DEVICE_MESSAGE = "message"
69
-
70
59
CONF_DEVICE_ROOM_HUMIDITY = "room_humidity"
71
60
CONF_DEVICE_WATER_TEMPERATURE = "water_temperature"
61
+ CONF_DEVICE_CUSTOM = "custom_sensor"
62
+ CONF_DEVICE_CUSTOM_MESSAGE = "message"
63
+ CONF_DEVICE_CUSTOM_RAW_FILTERS = "raw_filters"
64
+
65
+ CONF_CAPABILITIES = "capabilities"
66
+ CONF_CAPABILITIES_HORIZONTAL_SWING = "horizontal_swing"
67
+ CONF_CAPABILITIES_VERTICAL_SWING = "vertical_swing"
68
+
69
+ CONF_PRESETS = "presets"
70
+ CONF_PRESET_NAME = "name"
71
+ CONF_PRESET_ENABLED = "enabled"
72
+ CONF_PRESET_VALUE = "value"
73
+
72
74
73
75
def preset_entry (
74
- name : str ,
75
- value : int ,
76
- displayName : str
76
+ name : str ,
77
+ value : int ,
78
+ displayName : str
77
79
): return (
78
80
cv .Optional (name , default = False ), cv .Any (cv .boolean , cv .All ({
79
81
cv .Optional (CONF_PRESET_ENABLED , default = False ): cv .boolean ,
@@ -82,6 +84,7 @@ def preset_entry(
82
84
}))
83
85
)
84
86
87
+
85
88
PRESETS = {
86
89
"sleep" : {"value" : 1 , "displayName" : "Sleep" },
87
90
"quiet" : {"value" : 2 , "displayName" : "Quiet" },
@@ -92,18 +95,20 @@ def preset_entry(
92
95
93
96
CAPABILITIES_SCHEMA = (
94
97
cv .Schema ({
95
- cv .Optional (CONF_HORIZONTAL_SWING , default = False ): cv .boolean ,
96
- cv .Optional (CONF_VERTICAL_SWING , default = False ): cv .boolean ,
98
+ cv .Optional (CONF_CAPABILITIES_HORIZONTAL_SWING , default = False ): cv .boolean ,
99
+ cv .Optional (CONF_CAPABILITIES_VERTICAL_SWING , default = False ): cv .boolean ,
97
100
cv .Optional (CONF_PRESETS ): cv .Schema (dict (
98
- [preset_entry (name , PRESETS [name ]["value" ], PRESETS [name ]["displayName" ]) for name in PRESETS ]
101
+ [preset_entry (name , PRESETS [name ]["value" ],
102
+ PRESETS [name ]["displayName" ]) for name in PRESETS ]
99
103
))
100
104
})
101
105
)
102
106
103
107
CUSTOM_SENSOR_SCHEMA = sensor .sensor_schema ().extend ({
104
- cv .Required (CONF_DEVICE_MESSAGE ): cv .hex_int ,
108
+ cv .Required (CONF_DEVICE_CUSTOM_MESSAGE ): cv .hex_int ,
105
109
})
106
110
111
+
107
112
def custom_sensor_schema (
108
113
message : int ,
109
114
unit_of_measurement : str = sensor ._UNDEF ,
@@ -112,7 +117,7 @@ def custom_sensor_schema(
112
117
device_class : str = sensor ._UNDEF ,
113
118
state_class : str = sensor ._UNDEF ,
114
119
entity_category : str = sensor ._UNDEF ,
115
- raw_filters = []
120
+ raw_filters = []
116
121
):
117
122
return sensor .sensor_schema (
118
123
unit_of_measurement = unit_of_measurement ,
@@ -122,10 +127,11 @@ def custom_sensor_schema(
122
127
state_class = state_class ,
123
128
entity_category = entity_category ,
124
129
).extend ({
125
- cv .Optional (CONF_DEVICE_MESSAGE , default = message ): cv .hex_int ,
126
- cv .Optional (CONF_RAW_FILTERS , default = raw_filters ): sensor .validate_filters
130
+ cv .Optional (CONF_DEVICE_CUSTOM_MESSAGE , default = message ): cv .hex_int ,
131
+ cv .Optional (CONF_DEVICE_CUSTOM_RAW_FILTERS , default = raw_filters ): sensor .validate_filters
127
132
})
128
133
134
+
129
135
def temperature_sensor_schema (message : int ):
130
136
return custom_sensor_schema (
131
137
message = message ,
@@ -134,11 +140,12 @@ def temperature_sensor_schema(message: int):
134
140
device_class = DEVICE_CLASS_TEMPERATURE ,
135
141
state_class = STATE_CLASS_MEASUREMENT ,
136
142
raw_filters = [
137
- { "lambda" : Lambda ("return (int16_t)x;" ) },
138
- { "multiply" : 0.1 }
143
+ {"lambda" : Lambda ("return (int16_t)x;" )},
144
+ {"multiply" : 0.1 }
139
145
],
140
146
)
141
147
148
+
142
149
def humidity_sensor_schema (message : int ):
143
150
return custom_sensor_schema (
144
151
message = message ,
@@ -148,6 +155,7 @@ def humidity_sensor_schema(message: int):
148
155
state_class = STATE_CLASS_MEASUREMENT ,
149
156
)
150
157
158
+
151
159
DEVICE_SCHEMA = (
152
160
cv .Schema (
153
161
{
@@ -226,28 +234,31 @@ async def to_code(config):
226
234
device [CONF_DEVICE_ID ], device [CONF_DEVICE_ADDRESS ], var )
227
235
228
236
# setup capabilities
229
- if CONF_CAPABILITIES in device and CONF_VERTICAL_SWING in device [CONF_CAPABILITIES ]:
230
- cg .add (var_dev .set_supports_vertical_swing (device [CONF_CAPABILITIES ][CONF_VERTICAL_SWING ]))
231
- elif CONF_CAPABILITIES in config and CONF_VERTICAL_SWING in config [CONF_CAPABILITIES ]:
232
- cg .add (var_dev .set_supports_vertical_swing (config [CONF_CAPABILITIES ][CONF_VERTICAL_SWING ]))
233
-
234
- if CONF_CAPABILITIES in device and CONF_HORIZONTAL_SWING in device [CONF_CAPABILITIES ]:
235
- cg .add (var_dev .set_supports_horizontal_swing (device [CONF_CAPABILITIES ][CONF_HORIZONTAL_SWING ]))
236
- elif CONF_CAPABILITIES in config and CONF_HORIZONTAL_SWING in config [CONF_CAPABILITIES ]:
237
- cg .add (var_dev .set_supports_horizontal_swing (config [CONF_CAPABILITIES ][CONF_HORIZONTAL_SWING ]))
238
-
237
+ if CONF_CAPABILITIES in device and CONF_CAPABILITIES_VERTICAL_SWING in device [CONF_CAPABILITIES ]:
238
+ cg .add (var_dev .set_supports_vertical_swing (
239
+ device [CONF_CAPABILITIES ][CONF_CAPABILITIES_VERTICAL_SWING ]))
240
+ elif CONF_CAPABILITIES in config and CONF_CAPABILITIES_VERTICAL_SWING in config [CONF_CAPABILITIES ]:
241
+ cg .add (var_dev .set_supports_vertical_swing (
242
+ config [CONF_CAPABILITIES ][CONF_CAPABILITIES_VERTICAL_SWING ]))
243
+
244
+ if CONF_CAPABILITIES in device and CONF_CAPABILITIES_HORIZONTAL_SWING in device [CONF_CAPABILITIES ]:
245
+ cg .add (var_dev .set_supports_horizontal_swing (
246
+ device [CONF_CAPABILITIES ][CONF_CAPABILITIES_HORIZONTAL_SWING ]))
247
+ elif CONF_CAPABILITIES in config and CONF_CAPABILITIES_HORIZONTAL_SWING in config [CONF_CAPABILITIES ]:
248
+ cg .add (var_dev .set_supports_horizontal_swing (
249
+ config [CONF_CAPABILITIES ][CONF_CAPABILITIES_HORIZONTAL_SWING ]))
239
250
240
251
none_added = False
241
252
for preset in PRESETS :
242
253
device_preset_conf = device [CONF_CAPABILITIES ][CONF_PRESETS ][preset ] if (
243
- CONF_CAPABILITIES in device
244
- and CONF_PRESETS in device [CONF_CAPABILITIES ]
254
+ CONF_CAPABILITIES in device
255
+ and CONF_PRESETS in device [CONF_CAPABILITIES ]
245
256
and preset in device [CONF_CAPABILITIES ][CONF_PRESETS ]) else None
246
257
global_preset_conf = config [CONF_CAPABILITIES ][CONF_PRESETS ][preset ] if (
247
- CONF_CAPABILITIES in config
248
- and CONF_PRESETS in config [CONF_CAPABILITIES ]
258
+ CONF_CAPABILITIES in config
259
+ and CONF_PRESETS in config [CONF_CAPABILITIES ]
249
260
and preset in config [CONF_CAPABILITIES ][CONF_PRESETS ]) else None
250
-
261
+
251
262
preset_conf = global_preset_conf if device_preset_conf is None else device_preset_conf
252
263
preset_dict = isinstance (preset_conf , dict )
253
264
if preset_conf == True or (preset_dict and preset_conf [CONF_PRESET_ENABLED ] == True ):
@@ -256,7 +267,7 @@ async def to_code(config):
256
267
cg .add (var_dev .add_alt_mode ("None" , 0 ))
257
268
258
269
cg .add (var_dev .add_alt_mode (
259
- preset_conf [CONF_PRESET_NAME ] if preset_dict and CONF_PRESET_NAME in preset_conf else PRESETS [preset ]["displayName" ],
270
+ preset_conf [CONF_PRESET_NAME ] if preset_dict and CONF_PRESET_NAME in preset_conf else PRESETS [preset ]["displayName" ],
260
271
preset_conf [CONF_PRESET_VALUE ] if preset_dict and CONF_PRESET_VALUE in preset_conf else PRESETS [preset ]["value" ]
261
272
))
262
273
@@ -268,7 +279,7 @@ async def to_code(config):
268
279
# cg.add(var_dev.add_alt_mode("None", 0))
269
280
# for alt in config[CONF_CAPABILITIES][CONF_ALT_MODES]:
270
281
# cg.add(var_dev.add_alt_mode(alt[CONF_ALT_MODE_NAME], alt[CONF_ALT_MODE_VALUE]))
271
-
282
+
272
283
if CONF_DEVICE_POWER in device :
273
284
conf = device [CONF_DEVICE_POWER ]
274
285
sens = await switch .new_switch (conf )
@@ -305,20 +316,23 @@ async def to_code(config):
305
316
var_cli = cg .new_Pvariable (conf [CONF_ID ])
306
317
await climate .register_climate (var_cli , conf )
307
318
cg .add (var_dev .set_climate (var_cli ))
308
-
319
+
309
320
if CONF_DEVICE_CUSTOM in device :
310
321
for cust_sens in device [CONF_DEVICE_CUSTOM ]:
311
322
sens = await sensor .new_sensor (cust_sens )
312
- cg .add (var_dev .add_custom_sensor (cust_sens [CONF_DEVICE_MESSAGE ], sens ))
313
-
323
+ cg .add (var_dev .add_custom_sensor (
324
+ cust_sens [CONF_DEVICE_CUSTOM_MESSAGE ], sens ))
325
+
314
326
for key in CUSTOM_SENSOR_KEYS :
315
327
if key in device :
316
328
conf = device [key ]
317
329
# combine raw filters with any user-defined filters
318
330
conf_copy = conf .copy ()
319
- conf_copy [CONF_FILTERS ] = (conf [CONF_RAW_FILTERS ] if CONF_RAW_FILTERS in conf else []) + (conf [CONF_FILTERS ] if CONF_FILTERS in conf else [])
331
+ conf_copy [CONF_FILTERS ] = (conf [CONF_DEVICE_CUSTOM_RAW_FILTERS ] if CONF_DEVICE_CUSTOM_RAW_FILTERS in conf else [
332
+ ]) + (conf [CONF_FILTERS ] if CONF_FILTERS in conf else [])
320
333
sens = await sensor .new_sensor (conf_copy )
321
- cg .add (var_dev .add_custom_sensor (conf [CONF_DEVICE_MESSAGE ], sens ))
334
+ cg .add (var_dev .add_custom_sensor (
335
+ conf [CONF_DEVICE_CUSTOM_MESSAGE ], sens ))
322
336
323
337
cg .add (var .register_device (var_dev ))
324
338
0 commit comments