Skip to content

Commit b6c5a24

Browse files
committed
Namespace py objects a little bit
1 parent f67f0bc commit b6c5a24

File tree

1 file changed

+60
-46
lines changed

1 file changed

+60
-46
lines changed

components/samsung_ac/__init__.py

+60-46
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
UNIT_PERCENT,
1414
)
1515
from esphome.core import (
16-
CORE,
16+
CORE,
1717
Lambda
1818
)
1919

@@ -50,30 +50,32 @@
5050

5151
CONF_DEVICE_ID = "samsung_ac_device_id"
5252
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"
6053
CONF_DEVICE_ROOM_TEMPERATURE = "room_temperature"
6154
CONF_DEVICE_TARGET_TEMPERATURE = "target_temperature"
6255
CONF_DEVICE_OUTDOOR_TEMPERATURE = "outdoor_temperature"
6356
CONF_DEVICE_POWER = "power"
6457
CONF_DEVICE_MODE = "mode"
6558
CONF_DEVICE_CLIMATE = "climate"
66-
CONF_DEVICE_CUSTOM = "custom_sensor"
67-
CONF_RAW_FILTERS = "raw_filters"
68-
CONF_DEVICE_MESSAGE = "message"
69-
7059
CONF_DEVICE_ROOM_HUMIDITY = "room_humidity"
7160
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+
7274

7375
def preset_entry(
74-
name: str,
75-
value: int,
76-
displayName: str
76+
name: str,
77+
value: int,
78+
displayName: str
7779
): return (
7880
cv.Optional(name, default=False), cv.Any(cv.boolean, cv.All({
7981
cv.Optional(CONF_PRESET_ENABLED, default=False): cv.boolean,
@@ -82,6 +84,7 @@ def preset_entry(
8284
}))
8385
)
8486

87+
8588
PRESETS = {
8689
"sleep": {"value": 1, "displayName": "Sleep"},
8790
"quiet": {"value": 2, "displayName": "Quiet"},
@@ -92,18 +95,20 @@ def preset_entry(
9295

9396
CAPABILITIES_SCHEMA = (
9497
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,
97100
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]
99103
))
100104
})
101105
)
102106

103107
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,
105109
})
106110

111+
107112
def custom_sensor_schema(
108113
message: int,
109114
unit_of_measurement: str = sensor._UNDEF,
@@ -112,7 +117,7 @@ def custom_sensor_schema(
112117
device_class: str = sensor._UNDEF,
113118
state_class: str = sensor._UNDEF,
114119
entity_category: str = sensor._UNDEF,
115-
raw_filters = []
120+
raw_filters=[]
116121
):
117122
return sensor.sensor_schema(
118123
unit_of_measurement=unit_of_measurement,
@@ -122,10 +127,11 @@ def custom_sensor_schema(
122127
state_class=state_class,
123128
entity_category=entity_category,
124129
).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
127132
})
128133

134+
129135
def temperature_sensor_schema(message: int):
130136
return custom_sensor_schema(
131137
message=message,
@@ -134,11 +140,12 @@ def temperature_sensor_schema(message: int):
134140
device_class=DEVICE_CLASS_TEMPERATURE,
135141
state_class=STATE_CLASS_MEASUREMENT,
136142
raw_filters=[
137-
{ "lambda": Lambda("return (int16_t)x;") },
138-
{ "multiply": 0.1 }
143+
{"lambda": Lambda("return (int16_t)x;")},
144+
{"multiply": 0.1}
139145
],
140146
)
141147

148+
142149
def humidity_sensor_schema(message: int):
143150
return custom_sensor_schema(
144151
message=message,
@@ -148,6 +155,7 @@ def humidity_sensor_schema(message: int):
148155
state_class=STATE_CLASS_MEASUREMENT,
149156
)
150157

158+
151159
DEVICE_SCHEMA = (
152160
cv.Schema(
153161
{
@@ -226,28 +234,31 @@ async def to_code(config):
226234
device[CONF_DEVICE_ID], device[CONF_DEVICE_ADDRESS], var)
227235

228236
# 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]))
239250

240251
none_added = False
241252
for preset in PRESETS:
242253
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]
245256
and preset in device[CONF_CAPABILITIES][CONF_PRESETS]) else None
246257
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]
249260
and preset in config[CONF_CAPABILITIES][CONF_PRESETS]) else None
250-
261+
251262
preset_conf = global_preset_conf if device_preset_conf is None else device_preset_conf
252263
preset_dict = isinstance(preset_conf, dict)
253264
if preset_conf == True or (preset_dict and preset_conf[CONF_PRESET_ENABLED] == True):
@@ -256,7 +267,7 @@ async def to_code(config):
256267
cg.add(var_dev.add_alt_mode("None", 0))
257268

258269
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"],
260271
preset_conf[CONF_PRESET_VALUE] if preset_dict and CONF_PRESET_VALUE in preset_conf else PRESETS[preset]["value"]
261272
))
262273

@@ -268,7 +279,7 @@ async def to_code(config):
268279
# cg.add(var_dev.add_alt_mode("None", 0))
269280
# for alt in config[CONF_CAPABILITIES][CONF_ALT_MODES]:
270281
# cg.add(var_dev.add_alt_mode(alt[CONF_ALT_MODE_NAME], alt[CONF_ALT_MODE_VALUE]))
271-
282+
272283
if CONF_DEVICE_POWER in device:
273284
conf = device[CONF_DEVICE_POWER]
274285
sens = await switch.new_switch(conf)
@@ -305,20 +316,23 @@ async def to_code(config):
305316
var_cli = cg.new_Pvariable(conf[CONF_ID])
306317
await climate.register_climate(var_cli, conf)
307318
cg.add(var_dev.set_climate(var_cli))
308-
319+
309320
if CONF_DEVICE_CUSTOM in device:
310321
for cust_sens in device[CONF_DEVICE_CUSTOM]:
311322
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+
314326
for key in CUSTOM_SENSOR_KEYS:
315327
if key in device:
316328
conf = device[key]
317329
# combine raw filters with any user-defined filters
318330
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 [])
320333
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))
322336

323337
cg.add(var.register_device(var_dev))
324338

0 commit comments

Comments
 (0)