Skip to content

Commit

Permalink
Fixed startup issue. (#96)
Browse files Browse the repository at this point in the history
Use functions to find config location not hard coded values.
  • Loading branch information
twrecked authored Feb 4, 2024
1 parent ff17801 commit 63d77d9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.9.0a8:
fix upgrade issue #2, use config functions not hard coded directory locations
0.9.0a7:
remove deprecated constants
fix upgrade issue
Expand Down
4 changes: 2 additions & 2 deletions custom_components/virtual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

# Get the config.
_LOGGER.debug(f"creating new cfg")
vcfg = BlendedCfg(entry.data)
vcfg = BlendedCfg(hass, entry.data)
vcfg.load()

# create the devices.
Expand Down Expand Up @@ -155,7 +155,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# _LOGGER.debug(f"before hass={hass.data[COMPONENT_DOMAIN]}")
unload_ok = await hass.config_entries.async_unload_platforms(entry, VIRTUAL_PLATFORMS)
if unload_ok:
bcfg = BlendedCfg(entry.data)
bcfg = BlendedCfg(hass, entry.data)
bcfg.delete()
hass.data[COMPONENT_DOMAIN].pop(entry.data[ATTR_GROUP_NAME])
# _LOGGER.debug(f"after hass={hass.data[COMPONENT_DOMAIN]}")
Expand Down
44 changes: 24 additions & 20 deletions custom_components/virtual/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ def _fix_value(value):
return value


def _load_meta_data(group_name: str):
def _load_meta_data(hass, group_name: str):
"""Read in meta data for a particular group.
"""
devices = {}
with DB_LOCK:
try:
with open(META_JSON_FILE, 'r') as meta_file:
with open(default_meta_file(hass), 'r') as meta_file:
devices = json.load(meta_file).get(ATTR_DEVICES, {})
except Exception as e:
_LOGGER.debug(f"no meta data yet {str(e)}")
return devices.get(group_name, {})


def _save_meta_data(group_name, meta_data):
def _save_meta_data(hass, group_name, meta_data):
"""Save meta data for a particular group name.
"""
with DB_LOCK:

# Read in current meta data
devices = {}
try:
with open(META_JSON_FILE, 'r') as meta_file:
with open(default_meta_file(hass), 'r') as meta_file:
devices = json.load(meta_file).get(ATTR_DEVICES, {})
except Exception as e:
_LOGGER.debug(f"no meta data yet {str(e)}")
Expand All @@ -95,7 +95,7 @@ def _save_meta_data(group_name, meta_data):

# Write it back out.
try:
with open(META_JSON_FILE, 'w') as meta_file:
with open(default_meta_file(hass), 'w') as meta_file:
json.dump({
ATTR_VERSION: 1,
ATTR_DEVICES: devices
Expand All @@ -104,15 +104,15 @@ def _save_meta_data(group_name, meta_data):
_LOGGER.debug(f"couldn't save meta data {str(e)}")


def _delete_meta_data(group_name):
def _delete_meta_data(hass, group_name):
"""Save meta data for a particular group name.
"""
with DB_LOCK:

# Read in current meta data
devices = {}
try:
with open(META_JSON_FILE, 'r') as meta_file:
with open(default_meta_file(hass), 'r') as meta_file:
devices = json.load(meta_file).get(ATTR_DEVICES, {})
except Exception as e:
_LOGGER.debug(f"no meta data yet {str(e)}")
Expand All @@ -124,13 +124,13 @@ def _delete_meta_data(group_name):

# Write it back out.
try:
with open(META_JSON_FILE, 'w') as meta_file:
with open(default_meta_file(hass), 'w') as meta_file:
json.dump({
ATTR_VERSION: 1,
ATTR_DEVICES: devices
}, meta_file, indent=4)
except Exception as e:
_LOGGER.debug(f"couldn't save meta data {str(e)}")
_LOGGER.error(f"couldn't save meta data {str(e)}")


def _save_user_data(file_name, devices):
Expand All @@ -140,15 +140,15 @@ def _save_user_data(file_name, devices):
ATTR_DEVICES: devices
})
except Exception as e:
_LOGGER.debug(f"couldn't save user data {str(e)}")
_LOGGER.error(f"couldn't save user data {str(e)}")


def _load_user_data(file_name):
entities = {}
try:
entities = load_yaml(file_name).get(ATTR_DEVICES, [])
except Exception as e:
_LOGGER.debug(f"failed to read virtual file {str(e)}")
_LOGGER.error(f"failed to read virtual file {str(e)}")
return entities


Expand Down Expand Up @@ -254,7 +254,8 @@ class BlendedCfg(object):
them with flow data and options.
"""

def __init__(self, flow_data):
def __init__(self, hass, flow_data):
self._hass = hass
self._group_name = flow_data[ATTR_GROUP_NAME]
self._file_name = flow_data[ATTR_FILE_NAME]
self._changed: bool = False
Expand All @@ -265,10 +266,10 @@ def __init__(self, flow_data):
self._entities = {}

def _load_meta_data(self):
return _load_meta_data(self._group_name)
return _load_meta_data(self._hass, self._group_name)

def _save_meta_data(self):
_save_meta_data(self._group_name, self._meta_data)
_save_meta_data(self._hass, self._group_name, self._meta_data)
self._changed = False

def _load_user_data(self):
Expand Down Expand Up @@ -359,7 +360,7 @@ def load(self):

def delete(self):
_LOGGER.debug(f"deleting {self._group_name}")
_delete_meta_data(self._group_name)
_delete_meta_data(self._hass, self._group_name)

@property
def devices(self):
Expand Down Expand Up @@ -391,7 +392,7 @@ class UpgradeCfg(object):
"""

@staticmethod
def import_yaml(config):
def import_yaml(hass, config):
""" Take the current virtual config and make the new yaml file.
Virtual needs a lot of fine tuning so rather than get rid of the
Expand Down Expand Up @@ -441,14 +442,17 @@ def import_yaml(config):

_LOGGER.debug(f"devices-meta-data={devices_meta_data}")

_save_user_data(IMPORTED_YAML_FILE, devices)
_save_meta_data(IMPORTED_GROUP_NAME, devices_meta_data)
_save_user_data(default_config_file(hass), devices)
_save_meta_data(hass, IMPORTED_GROUP_NAME, devices_meta_data)

@staticmethod
def create_flow_data(_config):
def create_flow_data(hass, _config):
""" Take the current aarlo config and make the new flow configuration.
"""
_LOGGER.debug(f"new-config-file={default_config_file(hass)}")
_LOGGER.debug(f"new-meta-file={default_meta_file(hass)}")

return {
ATTR_GROUP_NAME: IMPORTED_GROUP_NAME,
ATTR_FILE_NAME: IMPORTED_YAML_FILE
ATTR_FILE_NAME: default_config_file(hass)
}
6 changes: 3 additions & 3 deletions custom_components/virtual/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def async_step_user(self, user_input):
# Fill in some defaults.
user_input = {
ATTR_GROUP_NAME: IMPORTED_GROUP_NAME,
ATTR_FILE_NAME: IMPORTED_YAML_FILE
ATTR_FILE_NAME: default_config_file(self.hass)
}

return self.async_show_form(
Expand All @@ -66,8 +66,8 @@ async def async_step_import(self, import_data):
"""Import momentary config from configuration.yaml."""

_LOGGER.debug(f"importing aarlo YAML {import_data}")
UpgradeCfg.import_yaml(import_data)
data = UpgradeCfg.create_flow_data(import_data)
UpgradeCfg.import_yaml(self.hass, import_data)
data = UpgradeCfg.create_flow_data(self.hass, import_data)

return self.async_create_entry(
title=f"Imported Virtual Group",
Expand Down
10 changes: 8 additions & 2 deletions custom_components/virtual/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,11 @@
DEFAULT_PERSISTENT = True

IMPORTED_GROUP_NAME = "imported"
IMPORTED_YAML_FILE = "/config/virtual.yaml"
META_JSON_FILE = "/config/.storage/virtual.meta.json"


def default_config_file(hass) -> str:
return hass.config.path("virtual.yaml")


def default_meta_file(hass) -> str:
return hass.config.path(".storage/virtual.meta.json")

0 comments on commit 63d77d9

Please sign in to comment.