Skip to content

Commit

Permalink
Fix race writing meta data. (#43)
Browse files Browse the repository at this point in the history
Bump revision...
  • Loading branch information
twrecked authored Jun 29, 2024
1 parent e08f7d1 commit c6c9282
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 46 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0.7.0b11:
Fix race writing meta data
0.7.0b10:
Fix IO in event loop issues
0.7.0b9:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/momentary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .cfg import BlendedCfg


__version__ = "0.7.0b10"
__version__ = "0.7.0b11"

_LOGGER = logging.getLogger(__name__)

Expand Down
97 changes: 53 additions & 44 deletions custom_components/momentary/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import aiofiles
import asyncio
import copy
import logging
import json
Expand All @@ -22,6 +23,8 @@

_LOGGER = logging.getLogger(__name__)

_meta_lock = asyncio.Lock()


def _fix_value(value):
""" If needed, convert value into a type that can be stored in yaml.
Expand Down Expand Up @@ -80,61 +83,67 @@ async def _async_save_yaml(file_name, data):
async def _load_meta_data(hass, group_name: str):
"""Read in meta data for a particular group.
"""
meta_data = await _async_load_json(default_meta_file(hass))
return (
meta_data.get(ATTR_DEVICES, {}).get(group_name, {}),
meta_data.get(ATTR_SWITCHES, {}).get(group_name, {})
)
async with _meta_lock:

meta_data = await _async_load_json(default_meta_file(hass))
return (
meta_data.get(ATTR_DEVICES, {}).get(group_name, {}),
meta_data.get(ATTR_SWITCHES, {}).get(group_name, {})
)


async def _save_meta_data(hass, group_name: str, device_meta_data, group_switches):
"""Save meta data for a particular group name.
"""
# Read in current meta data
meta_data = await _async_load_json(default_meta_file(hass))
devices = meta_data.get(ATTR_DEVICES, {})
switches = meta_data.get(ATTR_SWITCHES, {})

# Update (or add) the group piece.
_LOGGER.debug(f"device meta before {devices}")
_LOGGER.debug(f"switch meta before {switches}")
switches.update({
group_name: group_switches
})
devices.update({
group_name: device_meta_data
})
_LOGGER.debug(f"device meta after {devices}")
_LOGGER.debug(f"switch meta after {switches}")
async with _meta_lock:

# Read in current meta data
meta_data = await _async_load_json(default_meta_file(hass))
devices = meta_data.get(ATTR_DEVICES, {})
switches = meta_data.get(ATTR_SWITCHES, {})

# Update (or add) the group piece.
_LOGGER.debug(f"device meta before {devices}")
_LOGGER.debug(f"switch meta before {switches}")
switches.update({
group_name: group_switches
})
devices.update({
group_name: device_meta_data
})
_LOGGER.debug(f"device meta after {devices}")
_LOGGER.debug(f"switch meta after {switches}")

await _async_save_json(default_meta_file(hass), {
ATTR_VERSION: 1,
ATTR_DEVICES: devices,
ATTR_SWITCHES: switches
})
await _async_save_json(default_meta_file(hass), {
ATTR_VERSION: 1,
ATTR_DEVICES: devices,
ATTR_SWITCHES: switches
})


async def _delete_meta_data(hass, group_name: str):
"""Save meta data for a particular group name.
"""
# Read in current meta data
meta_data = await _async_load_json(default_meta_file(hass))
devices = meta_data.get(ATTR_DEVICES, {})
switches = meta_data.get(ATTR_SWITCHES, {})

# Remove the group.
_LOGGER.debug(f"devices meta before {devices}")
_LOGGER.debug(f"switches meta before {switches}")
devices.pop(group_name)
switches.pop(group_name)
_LOGGER.debug(f"devices meta after {devices}")
_LOGGER.debug(f"switches meta after {switches}")

await _async_save_json(default_meta_file(hass), {
ATTR_VERSION: 1,
ATTR_DEVICES: devices,
ATTR_SWITCHES: switches
})
async with _meta_lock:

# Read in current meta data
meta_data = await _async_load_json(default_meta_file(hass))
devices = meta_data.get(ATTR_DEVICES, {})
switches = meta_data.get(ATTR_SWITCHES, {})

# Remove the group.
_LOGGER.debug(f"devices meta before {devices}")
_LOGGER.debug(f"switches meta before {switches}")
devices.pop(group_name)
switches.pop(group_name)
_LOGGER.debug(f"devices meta after {devices}")
_LOGGER.debug(f"switches meta after {switches}")

await _async_save_json(default_meta_file(hass), {
ATTR_VERSION: 1,
ATTR_DEVICES: devices,
ATTR_SWITCHES: switches
})


async def _load_user_data(switches_file: str):
Expand Down
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.0b10"
"version": "0.7.0b11"
}

0 comments on commit c6c9282

Please sign in to comment.