Skip to content

Commit

Permalink
Fix: Resolve Blocking File Operations in move_emojis_directory and …
Browse files Browse the repository at this point in the history
…Code Improvements (#454)

* Fix: Resolve Blocking File Operations in `move_emojis_directory` and Code Improvements

This pull request addresses an issue with blocking file operations (`os.scandir` in `shutil.rmtree`) inside the event loop and introduces several code optimizations for the `xplora_watch` integration.

Signed-off-by: Ludy87 <Ludy87@users.noreply.github.com>

* Update entity.py

Signed-off-by: Ludy87 <Ludy87@users.noreply.github.com>

---------

Signed-off-by: Ludy87 <Ludy87@users.noreply.github.com>
  • Loading branch information
Ludy87 authored Jan 5, 2025
1 parent c217b8c commit 7e032f3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion custom_components/xplora_watch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
watches = await coordinator.controller.setDevices()

await create_www_directory(hass)
move_emojis_directory(hass)
await move_emojis_directory(hass)
await create_service_yaml_file(hass, entry, watches)

# Create a copy and remove Platform.NOTIFY
Expand Down
16 changes: 9 additions & 7 deletions custom_components/xplora_watch/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ def __init__(
super().__init__(coordinator)
if description is not None:
self.entity_description = description
self._config_entry = config_entry
self._data = config_entry.data
self._options = config_entry.options

self.watch_uid = wuid
self._unsub_dispatchers: list[Callable[[], None]] = []

is_admin = " (Admin)-" if coordinator.is_admin.get(coordinator.user_id + config_entry.entry_id, None) else "-"
self.is_admin = " (Admin)-" if coordinator.is_admin.get(coordinator.user_id + config_entry.entry_id, None) else "-"

watch_name = self.coordinator.controller.getWatchUserNames(wuid=wuid)
self.watch_name = self.coordinator.controller.getWatchUserNames(wuid=self.watch_uid)

self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, f"{config_entry.unique_id}_{wuid}")},
identifiers={(DOMAIN, f"{self._config_entry.unique_id}_{self.watch_uid}")},
manufacturer=MANUFACTURER,
model=coordinator.data[wuid].get("model", DEVICE_NAME),
name=f"{coordinator.username}{is_admin}{watch_name} ({wuid})",
model=coordinator.data[self.watch_uid].get("model", DEVICE_NAME),
name=f"{coordinator.username}{self.is_admin}{self.watch_name} ({self.watch_uid})",
sw_version=coordinator.os_version,
via_device=(DOMAIN, f"{config_entry.unique_id}_{coordinator.username}"),
configuration_url="https://github.com/Ludy87/xplora_watch/blob/main/README.md",
)

Expand All @@ -65,6 +65,8 @@ def _states(self, status) -> bool:
async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()

# Restore state if available
if state := await self.async_get_last_state():
self._state = state.state
self._unsub_dispatchers.append(async_dispatcher_connect(self.hass, TRACKER_UPDATE_STR, self._async_receive_data))
Expand All @@ -74,7 +76,7 @@ async def async_will_remove_from_hass(self) -> None:
await super().async_will_remove_from_hass()
for unsub in self._unsub_dispatchers:
unsub()
_LOGGER.debug("When entity is remove on hass")
_LOGGER.debug("When entity is removed from hass")
self._unsub_dispatchers = []

@callback
Expand Down
15 changes: 10 additions & 5 deletions custom_components/xplora_watch/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,19 @@ def mkdir() -> None:
await hass.async_add_executor_job(mkdir)


def move_emojis_directory(hass: HomeAssistant):
async def move_emojis_directory(hass: HomeAssistant):
"""Move emojis directory to www directory."""
src_path = hass.config.path(f"{DATA_CUSTOM_COMPONENTS}/{DOMAIN}/emojis")
dst_path = hass.config.path(f"www/{DOMAIN}")
if os.path.exists(src_path):
if os.path.exists(f"{dst_path}/emojis"):
shutil.rmtree(f"{dst_path}/emojis")
shutil.move(src_path, dst_path)

def move_directories():
if os.path.exists(src_path):
if os.path.exists(f"{dst_path}/emojis"):
shutil.rmtree(f"{dst_path}/emojis")
shutil.move(src_path, dst_path)

# Use executor to run blocking operations
await hass.async_add_executor_job(move_directories)


async def load_yaml(fname: str | os.PathLike[str], secrets: Secrets | None = None) -> JSON_TYPE | None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xplora_watch/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"marshmallow-enum",
"security>=1.2"
],
"version": "v2.14.0"
"version": "v2.14.1"
}
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"zip_release": true,
"render_readme": true,
"homeassistant": "2025.1.0",
"hacs": "2.0.1",
"hacs": "2.0.2",
"filename": "xplora_watch.zip"
}

0 comments on commit 7e032f3

Please sign in to comment.