Skip to content

Commit

Permalink
Alpha 07 (#35)
Browse files Browse the repository at this point in the history
* Fix setup function call.

* Fix config names.

* Bump revision.
  • Loading branch information
twrecked authored Feb 4, 2024
1 parent 15fa385 commit d278468
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
4 changes: 4 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/momentary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
39 changes: 22 additions & 17 deletions custom_components/momentary/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {}),
Expand All @@ -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:
Expand All @@ -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, {})
Expand All @@ -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,
Expand All @@ -89,15 +89,15 @@ 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:

# 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, {})
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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}")
Expand All @@ -375,14 +376,18 @@ 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

self._dmeta_data = {}
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.
"""
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions custom_components/momentary/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
})

Expand Down
9 changes: 7 additions & 2 deletions custom_components/momentary/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion custom_components/momentary/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit d278468

Please sign in to comment.