Skip to content

Commit

Permalink
Merge pull request #411 from GeoDerp/retrieve_hass_fix
Browse files Browse the repository at this point in the history
get_ha_config add error handling
  • Loading branch information
davidusb-geek authored Dec 30, 2024
2 parents a157048 + c44d0be commit c85d1c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/emhass/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def set_input_data_dict(
retrieve_hass_conf, optim_conf, plant_conf = utils.get_yaml_parse(params, logger)
if type(retrieve_hass_conf) is bool:
return False

# Treat runtimeparams
params, retrieve_hass_conf, optim_conf, plant_conf = utils.treat_runtimeparams(
runtimeparams,
Expand All @@ -81,7 +81,7 @@ def set_input_data_dict(
logger,
emhass_conf,
)

# Define the data retrieve object
rh = RetrieveHass(
retrieve_hass_conf["hass_url"],
Expand All @@ -93,20 +93,22 @@ def set_input_data_dict(
logger,
get_data_from_file=get_data_from_file,
)

# Retrieve basic configuration data from hass
if get_data_from_file:
with open(emhass_conf["data_path"] / "test_df_final.pkl", "rb") as inp:
_, _, _, rh.ha_config = pickle.load(inp)
else:
rh.get_ha_config()

response = rh.get_ha_config()
if type(response) is bool:
return False

# Update the params dict using data from the HA configuration
params = utils.update_params_with_ha_config(
params,
rh.ha_config,
)

# Define the forecast and optimization objects
fcst = Forecast(
retrieve_hass_conf,
Expand Down
20 changes: 17 additions & 3 deletions src/emhass/retrieve_hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,23 @@ def get_ha_config(self):
"Authorization": "Bearer " + self.long_lived_token,
"content-type": "application/json",
}
url = self.hass_url + "api/config"
response_config = get(url, headers=headers)
self.ha_config = response_config.json()
if self.hass_url == "http://supervisor/core/api":
url = self.hass_url + "/config"
else:
url = self.hass_url + "api/config"

try:
response_config = get(url, headers=headers)
except Exception:
self.logger.error("Unable to access Home Assistance instance, check URL")
self.logger.error("If using addon, try setting url and token to 'empty'")
return False

try:
self.ha_config = response_config.json()
except Exception:
self.logger.error("EMHASS was unable to obtain configuration data from HA")
return False

def get_data(
self,
Expand Down

0 comments on commit c85d1c7

Please sign in to comment.