Skip to content

Commit

Permalink
Merge pull request #418 from davidusb-geek/davidusb-geek/debug/ha_con…
Browse files Browse the repository at this point in the history
…fig_index_oor

Debugging issue of list index out of range, solution proposed
  • Loading branch information
davidusb-geek authored Jan 4, 2025
2 parents 938ef73 + fea1488 commit d40a24f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/emhass/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ def update_params_with_ha_config(
if 'unit_system' not in ha_config.keys():
ha_config['unit_system'] = {'temperature': '°C'}

for k in range(params["optim_conf"]["number_of_deferrable_loads"]):
number_of_deferrable_loads = params["optim_conf"]["number_of_deferrable_loads"]
if 'num_def_loads' in params['passed_data'].keys():
number_of_deferrable_loads = params['passed_data']['num_def_loads']
if 'number_of_deferrable_loads' in params['passed_data'].keys():
number_of_deferrable_loads = params['passed_data']['number_of_deferrable_loads']

for k in range(number_of_deferrable_loads):
params['passed_data']['custom_predicted_temperature_id'][k].update(
{"unit_of_measurement": ha_config['unit_system']['temperature']}
)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_command_line_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
emhass_conf["defaults_path"] = emhass_conf["root_path"] / "data/config_defaults.json"
emhass_conf["associations_path"] = emhass_conf["root_path"] / "data/associations.csv"


# create loggerW
# create logger
logger, ch = utils.get_logger(__name__, emhass_conf, save_to_file=False)


Expand Down
100 changes: 93 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,102 @@ def test_update_params_with_ha_config(self):
'currency': 'USD',
'unit_system': {'temperature': '°F'}
}
params_json = utils.update_params_with_ha_config(
params_with_ha_config_json = utils.update_params_with_ha_config(
params,
ha_config,
)
params = json.loads(params_json)
self.assertTrue(params["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F")
self.assertTrue(params["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F")
self.assertTrue(params["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$')
self.assertTrue(params["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh')
self.assertTrue(params["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh')
params_with_ha_config = json.loads(params_with_ha_config_json)
self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F")
self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F")
self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh')

def test_update_params_with_ha_config_special_case(self):
# Test special passed runtime params
runtimeparams = {
'prediction_horizon': 28,
'pv_power_forecast': [523, 873, 1059, 1195, 1291, 1352, 1366, 1327, 1254, 1150, 1004, 813, 589, 372, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 228, 301, 363, 407, 438, 456, 458, 443, 417, 381, 332, 269, 195, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'num_def_loads': 2, 'P_deferrable_nom': [0, 0], 'def_total_hours': [0, 0], 'treat_def_as_semi_cont': [1, 1], 'set_def_constant': [1, 1],
'def_start_timestep': [0, 0], 'def_end_timestep': [0, 0], 'soc_init': 0.64, 'soc_final': 0.9,
'load_cost_forecast': [0.2751, 0.2751, 0.2729, 0.2729, 0.2748, 0.2748, 0.2746, 0.2746, 0.2815, 0.2815, 0.2841, 0.2841, 0.282, 0.282, 0.288, 0.288, 0.29, 0.29, 0.2841, 0.2841, 0.2747, 0.2747, 0.2677, 0.2677, 0.2628, 0.2628, 0.2532, 0.2532],
'prod_price_forecast': [0.1213, 0.1213, 0.1192, 0.1192, 0.121, 0.121, 0.1208, 0.1208, 0.1274, 0.1274, 0.1298, 0.1298, 0.1278, 0.1278, 0.1335, 0.1335, 0.1353, 0.1353, 0.1298, 0.1298, 0.1209, 0.1209, 0.1143, 0.1143, 0.1097, 0.1097, 0.1007, 0.1007],
'alpha': 1, 'beta': 0,
'load_power_forecast': [399, 300, 400, 600, 300, 200, 200, 200, 200, 300, 300, 200, 400, 200, 200, 400, 400, 400, 300, 300, 300, 600, 800, 500, 400, 400, 500, 500, 2400, 2300, 2400, 2400, 2300, 2400, 2400, 2400, 2300, 2400, 2400, 200, 200, 300, 300, 300, 300, 300, 300, 300]}
params_ = json.loads(self.params_json)
params_['passed_data'].update(runtimeparams)

runtimeparams_json = json.dumps(runtimeparams)
params_json = json.dumps(params_)

retrieve_hass_conf, optim_conf, plant_conf = utils.get_yaml_parse(
params_json, logger
)
set_type = "dayahead-optim"
params, retrieve_hass_conf, optim_conf, plant_conf = utils.treat_runtimeparams(
runtimeparams_json,
params_json,
retrieve_hass_conf,
optim_conf,
plant_conf,
set_type,
logger,
emhass_conf,
)
ha_config = {
'currency': 'USD',
'unit_system': {'temperature': '°F'}
}
params_with_ha_config_json = utils.update_params_with_ha_config(
params,
ha_config,
)
params_with_ha_config = json.loads(params_with_ha_config_json)
self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F")
self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F")
self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh')
# Test with 0 deferrable loads
runtimeparams = {
'prediction_horizon': 28,
'pv_power_forecast': [523, 873, 1059, 1195, 1291, 1352, 1366, 1327, 1254, 1150, 1004, 813, 589, 372, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 228, 301, 363, 407, 438, 456, 458, 443, 417, 381, 332, 269, 195, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'num_def_loads': 0,
'def_start_timestep': [0, 0], 'def_end_timestep': [0, 0], 'soc_init': 0.64, 'soc_final': 0.9,
'load_cost_forecast': [0.2751, 0.2751, 0.2729, 0.2729, 0.2748, 0.2748, 0.2746, 0.2746, 0.2815, 0.2815, 0.2841, 0.2841, 0.282, 0.282, 0.288, 0.288, 0.29, 0.29, 0.2841, 0.2841, 0.2747, 0.2747, 0.2677, 0.2677, 0.2628, 0.2628, 0.2532, 0.2532],
'prod_price_forecast': [0.1213, 0.1213, 0.1192, 0.1192, 0.121, 0.121, 0.1208, 0.1208, 0.1274, 0.1274, 0.1298, 0.1298, 0.1278, 0.1278, 0.1335, 0.1335, 0.1353, 0.1353, 0.1298, 0.1298, 0.1209, 0.1209, 0.1143, 0.1143, 0.1097, 0.1097, 0.1007, 0.1007],
'alpha': 1, 'beta': 0,
'load_power_forecast': [399, 300, 400, 600, 300, 200, 200, 200, 200, 300, 300, 200, 400, 200, 200, 400, 400, 400, 300, 300, 300, 600, 800, 500, 400, 400, 500, 500, 2400, 2300, 2400, 2400, 2300, 2400, 2400, 2400, 2300, 2400, 2400, 200, 200, 300, 300, 300, 300, 300, 300, 300]}
params_ = json.loads(self.params_json)
params_['passed_data'].update(runtimeparams)
runtimeparams_json = json.dumps(runtimeparams)
params_json = json.dumps(params_)
retrieve_hass_conf, optim_conf, plant_conf = utils.get_yaml_parse(
params_json, logger
)
set_type = "dayahead-optim"
params, retrieve_hass_conf, optim_conf, plant_conf = utils.treat_runtimeparams(
runtimeparams_json,
params_json,
retrieve_hass_conf,
optim_conf,
plant_conf,
set_type,
logger,
emhass_conf,
)
ha_config = {
'currency': 'USD',
'unit_system': {'temperature': '°F'}
}
params_with_ha_config_json = utils.update_params_with_ha_config(
params,
ha_config,
)
params_with_ha_config = json.loads(params_with_ha_config_json)
self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh')
self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh')

def test_build_secrets(self):
# Test the build_secrets defaults from get_test_params()
Expand Down

0 comments on commit d40a24f

Please sign in to comment.