Skip to content

Commit

Permalink
Refactor multiple thermostat code
Browse files Browse the repository at this point in the history
  • Loading branch information
craigmaloney committed Jul 23, 2020
1 parent f00cc48 commit 3cb8813
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions thermostat/multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,17 @@ def multiple_thermostat_calculate_epa_field_savings_metrics(thermostats):

metrics_dict = {}
for output in results:
try:
if len(output) > 0:
thermostat_id = output[0]['ct_identifier']
metrics_dict[thermostat_id] = []
for individual_output in output:
metrics_dict[thermostat_id].append(individual_output)
except IndexError:
warnings.warn("Missing thermostat output")
metrics_dict[thermostat_id] = output

# Get the order of the thermostats from the original input so the output
# matches the order that was sent in
thermostat_ids = \
[thermostat.thermostat_id for thermostat in thermostats_list]
metrics = []
for thermostat_id in thermostat_ids:
try:
for metric in metrics_dict[thermostat_id]:
metrics.append(metric)
# Prevent duplicate thermostat IDs from being double-counted
metrics_dict.pop(thermostat_id, None)
# Trap for missing keys
except KeyError:
pass
for thermostat in thermostats_list:
for metric in metrics_dict.get(thermostat.thermostat_id, []):
metrics.append(metric)
# Prevent duplicate thermostat IDs from being double-counted
metrics_dict.pop(thermostat_id, None)

return metrics

0 comments on commit 3cb8813

Please sign in to comment.