From d278468bab49962caad7901d248b19abe7d1b0c5 Mon Sep 17 00:00:00 2001 From: Steve Herrell <47094394+twrecked@users.noreply.github.com> Date: Sun, 4 Feb 2024 09:19:46 -0500 Subject: [PATCH] Alpha 07 (#35) * Fix setup function call. * Fix config names. * Bump revision. --- changelog | 4 +++ custom_components/momentary/__init__.py | 8 ++--- custom_components/momentary/cfg.py | 39 ++++++++++++---------- custom_components/momentary/config_flow.py | 8 ++--- custom_components/momentary/const.py | 9 +++-- custom_components/momentary/manifest.json | 2 +- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/changelog b/changelog index 3736cc8..316d32c 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,7 @@ +0.7.0a7: + fix upgrade issues + fix out of correct loop message + fix config path 0.7.0a6: add import notice 0.7.0a5: diff --git a/custom_components/momentary/__init__.py b/custom_components/momentary/__init__.py index de58135..07368a7 100644 --- a/custom_components/momentary/__init__.py +++ b/custom_components/momentary/__init__.py @@ -24,12 +24,12 @@ from .cfg import BlendedCfg -__version__ = '0.7.0a6' +__version__ = "0.7.0a7" _LOGGER = logging.getLogger(__name__) -def setup(hass: HomeAssistant, config: ConfigType) -> bool: +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up an momentary component. """ hass.data.setdefault(COMPONENT_DOMAIN, {}) @@ -81,7 +81,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Database of devices group_name = entry.data[ATTR_GROUP_NAME] file_name = entry.data[ATTR_FILE_NAME] - cfg = BlendedCfg(group_name, file_name) + cfg = BlendedCfg(hass, group_name, file_name) cfg.load() # Load and create devices. @@ -113,7 +113,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER.debug(f"unloading it {entry.data[ATTR_GROUP_NAME]}") unload_ok = await hass.config_entries.async_unload_platforms(entry, [Platform.SWITCH]) if unload_ok: - BlendedCfg.delete_group(entry.data[ATTR_GROUP_NAME]) + BlendedCfg.delete_group(hass, entry.data[ATTR_GROUP_NAME]) cfg = hass.data[COMPONENT_DOMAIN].pop(entry.data[ATTR_GROUP_NAME]) for device, name in cfg[ATTR_DEVICES].items(): await _async_delete_momentary_device_from_registry(hass, entry, device, name) diff --git a/custom_components/momentary/cfg.py b/custom_components/momentary/cfg.py index 7de087d..164b0b4 100644 --- a/custom_components/momentary/cfg.py +++ b/custom_components/momentary/cfg.py @@ -33,12 +33,12 @@ 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. """ with DB_LOCK: try: - with open(DB_DEFAULT_SWITCHES_META_FILE, "r") as meta_file: + with open(default_meta_file(hass), "r") as meta_file: meta_data = json.load(meta_file) return ( meta_data.get(ATTR_DEVICES, {}).get(group_name, {}), @@ -49,7 +49,7 @@ def _load_meta_data(group_name: str): return {} -def _save_meta_data(group_name: str, device_meta_data, group_switches): +def _save_meta_data(hass, group_name: str, device_meta_data, group_switches): """Save meta data for a particular group name. """ with DB_LOCK: @@ -58,7 +58,7 @@ def _save_meta_data(group_name: str, device_meta_data, group_switches): switches = {} devices = {} try: - with open(DB_DEFAULT_SWITCHES_META_FILE, "r") as meta_file: + with open(default_meta_file(hass), "r") as meta_file: meta_data = json.load(meta_file) devices = meta_data.get(ATTR_DEVICES, {}) switches = meta_data.get(ATTR_SWITCHES, {}) @@ -79,7 +79,7 @@ def _save_meta_data(group_name: str, device_meta_data, group_switches): # Write it back out. try: - with open(DB_DEFAULT_SWITCHES_META_FILE, "w") as meta_file: + with open(default_meta_file(hass), "w") as meta_file: json.dump({ ATTR_VERSION: 1, ATTR_DEVICES: devices, @@ -89,7 +89,7 @@ def _save_meta_data(group_name: str, device_meta_data, group_switches): _LOGGER.debug(f"couldn't save meta data {str(e)}") -def _delete_meta_data(group_name: str): +def _delete_meta_data(hass, group_name: str): """Save meta data for a particular group name. """ with DB_LOCK: @@ -97,7 +97,7 @@ def _delete_meta_data(group_name: str): # Read in current meta data switches = {} try: - with open(DB_DEFAULT_SWITCHES_META_FILE, "r") as meta_file: + with open(default_meta_file(hass), "r") as meta_file: meta_data = json.load(meta_file) devices = meta_data.get(ATTR_DEVICES, {}) switches = meta_data.get(ATTR_SWITCHES, {}) @@ -114,7 +114,7 @@ def _delete_meta_data(group_name: str): # Write it back out. try: - with open(DB_DEFAULT_SWITCHES_META_FILE, "w") as meta_file: + with open(default_meta_file(hass), "w") as meta_file: json.dump({ ATTR_VERSION: 1, ATTR_DEVICES: devices, @@ -196,15 +196,16 @@ class BlendedCfg: """ Manage the momentary switch database. We have 2 data points: - - DB_DEFAULT_SWITCHES_FILE; where the user configures their switches, we create + - default_config_file(self.hass); where the user configures their switches, we create this the first time the config flow code is run - - DB_DEFAULT_SWITCHES_META_FILE; where we map the user entries to their unique ids + - default_meta_file(hass); where we map the user entries to their unique ids When we load we match the user list against our meta list and update entries as needed. """ - def __init__(self, group_name: str, file: str): + def __init__(self, hass, group_name: str, file: str): + self._hass = hass self._group_name = group_name self._switches_file = file self._changed: bool = False @@ -300,7 +301,7 @@ def load(self) -> None: # Read in the known meta data. We put this into a temporary # variable for now. Anything we find in the user list is moved into # the permanent variable. Anything left is orphaned. - self._dmeta_data_in, self._smeta_data_in = _load_meta_data(self._group_name) + self._dmeta_data_in, self._smeta_data_in = _load_meta_data(self._hass, self._group_name) # Parse out the user data. We have 2 formats: # - `name:` this indicates a device/entity with a one to one mapping @@ -332,7 +333,7 @@ def load(self) -> None: # Make sure changes are kept. if self._changed: - _save_meta_data(self._group_name, self._dmeta_data, self._smeta_data) + _save_meta_data(self._hass, self._group_name, self._dmeta_data, self._smeta_data) self._changed = False self.dump() @@ -362,8 +363,8 @@ def switches(self): return self._switches @staticmethod - def delete_group(group_name: str): - _delete_meta_data(group_name) + def delete_group(hass, group_name: str): + _delete_meta_data(hass, group_name) def dump(self): _LOGGER.debug(f"dump(load):devices={self._devices}") @@ -375,7 +376,8 @@ def dump(self): class UpgradeCfg: - def __init__(self, group_name: str, file: str): + def __init__(self, hass, group_name: str, file: str): + self._hass = hass self._group_name = group_name self._switches_file = file @@ -383,6 +385,9 @@ def __init__(self, group_name: str, file: str): self._switches = {} self._smeta_data = {} + _LOGGER.debug(f"new-config-file={default_config_file(self._hass)}") + _LOGGER.debug(f"new-meta-file={default_meta_file(self._hass)}") + def import_switch(self, switch): """ Import an original YAML entry. """ @@ -425,7 +430,7 @@ def switch_keys(self): def save(self): # Update both database files. - _save_meta_data(self._group_name, self._dmeta_data, self._smeta_data) + _save_meta_data(self._hass, self._group_name, self._dmeta_data, self._smeta_data) _save_user_data(self._switches_file, self._switches) self.dump() diff --git a/custom_components/momentary/config_flow.py b/custom_components/momentary/config_flow.py index e83b026..4f8ed48 100644 --- a/custom_components/momentary/config_flow.py +++ b/custom_components/momentary/config_flow.py @@ -40,7 +40,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo try: info = await self.validate_input(user_input) - cfg = BlendedCfg(user_input[ATTR_GROUP_NAME], user_input[ATTR_FILE_NAME]) + cfg = BlendedCfg(self.hass, user_input[ATTR_GROUP_NAME], user_input[ATTR_FILE_NAME]) cfg.load() return self.async_create_entry(title=info["title"], data={ ATTR_GROUP_NAME: user_input[ATTR_GROUP_NAME], @@ -56,7 +56,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo # Fill in some defaults. user_input = { ATTR_GROUP_NAME: DEFAULT_IMPORTED_NAME, - ATTR_FILE_NAME: DB_DEFAULT_SWITCHES_FILE + ATTR_FILE_NAME: default_config_file(self.hass) } return self.async_show_form( @@ -73,7 +73,7 @@ async def async_step_import(self, import_data): # Extract the momentary devices in the yaml file. The import function # converts it. _LOGGER.info("importing YAML switches into default group") - cfg = UpgradeCfg(DEFAULT_IMPORTED_NAME, DB_DEFAULT_SWITCHES_FILE) + cfg = UpgradeCfg(self.hass, DEFAULT_IMPORTED_NAME, default_config_file(self.hass)) for switch in import_data: if switch[CONF_PLATFORM] == COMPONENT_DOMAIN: cfg.import_switch(switch) @@ -82,7 +82,7 @@ async def async_step_import(self, import_data): # Store keys in momentary config. return self.async_create_entry(title=f"{DEFAULT_IMPORTED_NAME} {COMPONENT_DOMAIN}", data={ ATTR_GROUP_NAME: DEFAULT_IMPORTED_NAME, - ATTR_FILE_NAME: DB_DEFAULT_SWITCHES_FILE, + ATTR_FILE_NAME: default_config_file(self.hass), ATTR_SWITCHES: list(cfg.switch_keys) }) diff --git a/custom_components/momentary/const.py b/custom_components/momentary/const.py index e613de3..93da271 100644 --- a/custom_components/momentary/const.py +++ b/custom_components/momentary/const.py @@ -30,5 +30,10 @@ DEFAULT_MODE = "old" DEFAULT_TOGGLE_FOR = timedelta(seconds=1) -DB_DEFAULT_SWITCHES_FILE = "/config/momentary.yaml" -DB_DEFAULT_SWITCHES_META_FILE = "/config/.storage/momentary.meta.json" + +def default_config_file(hass) -> str: + return hass.config.path("momentary.yaml") + + +def default_meta_file(hass) -> str: + return hass.config.path(".storage/momentary.meta.json") diff --git a/custom_components/momentary/manifest.json b/custom_components/momentary/manifest.json index da6b36b..0f1ff53 100644 --- a/custom_components/momentary/manifest.json +++ b/custom_components/momentary/manifest.json @@ -8,5 +8,5 @@ "iot_class": "local_polling", "issue_tracker": "https://github.com/twrecked/hass-momentary/issues", "requirements": [], - "version": "0.7.0a6" + "version": "0.7.0a7" }