Skip to content

Commit 96c933b

Browse files
committed
Create and move one more helper-function
1 parent c4e863b commit 96c933b

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

plugwise/helper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ATTR_STATE,
2424
ATTR_TYPE,
2525
ATTR_UNIT_OF_MEASUREMENT,
26+
BINARY_SENSORS,
2627
COOLING_ICON,
2728
DEVICE_MEASUREMENTS,
2829
DEVICE_STATE,
@@ -37,6 +38,8 @@
3738
LOCATIONS,
3839
POWER_WATT,
3940
PW_NOTIFICATION,
41+
SENSORS,
42+
SWITCHES,
4043
SWITCH_GROUP_TYPES,
4144
THERMOSTAT_CLASSES,
4245
)
@@ -1084,6 +1087,37 @@ def pw_notification_updater(self, d_id, d_dict):
10841087
self.notifications != {}
10851088
)
10861089

1090+
def create_lists_from_data(self, data, bs_list, s_list, sw_list):
1091+
"""Helper-function for all_device_data().
1092+
Create lists of binary_sensors, sensors, switches from the relevant data.
1093+
"""
1094+
for key, value in list(data.items()):
1095+
for item in BINARY_SENSORS:
1096+
try:
1097+
data.pop(item[ATTR_ID])
1098+
except KeyError:
1099+
pass
1100+
else:
1101+
if self.active_device_present:
1102+
item[ATTR_STATE] = value
1103+
bs_list.append(item)
1104+
for item in SENSORS:
1105+
try:
1106+
data.pop(item[ATTR_ID])
1107+
except KeyError:
1108+
pass
1109+
else:
1110+
item[ATTR_STATE] = value
1111+
s_list.append(item)
1112+
for item in SWITCHES:
1113+
try:
1114+
data.pop(item[ATTR_ID])
1115+
except KeyError:
1116+
pass
1117+
else:
1118+
item[ATTR_STATE] = value
1119+
sw_list.append(item)
1120+
10871121
def append_special(self, data, d_id, bs_list, s_list):
10881122
"""Helper-function for all_device_data().
10891123
When the conditions are met, it appends the plugwise_notification binary_sensor and/or the device_state sensor.

plugwise/smile.py

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
APPLIANCES,
1616
ATTR_ID,
1717
ATTR_STATE,
18-
BINARY_SENSORS,
1918
DEFAULT_PORT,
2019
DEFAULT_TIMEOUT,
2120
DEFAULT_USERNAME,
@@ -24,11 +23,9 @@
2423
MODULES,
2524
NOTIFICATIONS,
2625
RULES,
27-
SENSORS,
2826
SMILES,
2927
STATUS,
3028
SWITCH_GROUP_TYPES,
31-
SWITCHES,
3229
SYSTEM,
3330
THERMOSTAT_CLASSES,
3431
)
@@ -238,45 +235,21 @@ def all_device_data(self):
238235
dev_and_data_list = []
239236
for dev_id, dev_dict in self.devices.items():
240237
dev_and_data = dev_dict
241-
temp_b_sensor_list = []
242-
temp_sensor_list = []
243-
temp_switch_list = []
238+
temp_bs_list = []
239+
temp_s_list = []
240+
temp_sw_list = []
244241
data = self.get_device_data(dev_id)
245242

246-
self.append_special(data, dev_id, temp_b_sensor_list, temp_sensor_list)
247-
for key, value in list(data.items()):
248-
for item in BINARY_SENSORS:
249-
try:
250-
data.pop(item[ATTR_ID])
251-
except KeyError:
252-
pass
253-
else:
254-
if self.active_device_present:
255-
item[ATTR_STATE] = value
256-
temp_b_sensor_list.append(item)
257-
for item in SENSORS:
258-
try:
259-
data.pop(item[ATTR_ID])
260-
except KeyError:
261-
pass
262-
else:
263-
item[ATTR_STATE] = value
264-
temp_sensor_list.append(item)
265-
for item in SWITCHES:
266-
try:
267-
data.pop(item[ATTR_ID])
268-
except KeyError:
269-
pass
270-
else:
271-
item[ATTR_STATE] = value
272-
temp_switch_list.append(item)
243+
self.create_lists_from_data(data, temp_bs_list, temp_s_list, temp_sw_list)
244+
self.append_special(data, dev_id, temp_bs_list, temp_s_list)
245+
273246
dev_and_data.update(data)
274-
if temp_b_sensor_list != []:
275-
dev_and_data["binary_sensors"] = temp_b_sensor_list
276-
if temp_sensor_list != []:
277-
dev_and_data["sensors"] = temp_sensor_list
278-
if temp_switch_list != []:
279-
dev_and_data["switches"] = temp_switch_list
247+
if temp_bs_list != []:
248+
dev_and_data["binary_sensors"] = temp_bs_list
249+
if temp_s_list != []:
250+
dev_and_data["sensors"] = temp_s_list
251+
if temp_sw_list != []:
252+
dev_and_data["switches"] = temp_sw_list
280253
dev_id_list.append(dev_id)
281254
dev_and_data_list.append(copy.deepcopy(dev_and_data))
282255

0 commit comments

Comments
 (0)