diff --git a/custom_components/bahmcloud_store/views.py b/custom_components/bahmcloud_store/views.py index fecd2f9..42e38c8 100644 --- a/custom_components/bahmcloud_store/views.py +++ b/custom_components/bahmcloud_store/views.py @@ -159,6 +159,27 @@ class BCSApiView(HomeAssistantView): self.core = core async def get(self, request: web.Request) -> web.Response: + # Manual refresh via query ?refresh=1 + refresh = request.query.get("refresh") + if refresh in ("1", "true", "yes"): + try: + # Prefer async method if available + if hasattr(self.core, "async_refresh_now"): + await self.core.async_refresh_now() + # Fallbacks for older cores + elif hasattr(self.core, "refresh_now"): + await self.core.hass.async_add_executor_job(self.core.refresh_now) + elif hasattr(self.core, "load_index"): + await self.core.hass.async_add_executor_job(self.core.load_index) + else: + _LOGGER.warning("Core has no refresh method; returning cached data.") + except Exception as e: + _LOGGER.exception("Manual refresh failed: %s", e) + return web.json_response( + {"ok": False, "message": f"Refresh failed: {e}"}, + status=500, + ) + return web.json_response( {"ok": True, "version": self.core.version, "repos": self.core.list_repos_public()} ) @@ -179,39 +200,6 @@ class BCSApiView(HomeAssistantView): return web.json_response({"ok": False, "message": "Unknown operation"}, status=400) -class BCSRefreshView(HomeAssistantView): - """ - Manual refresh endpoint to reload store.yaml/bcs.yaml immediately. - This avoids waiting for the auto-refresh interval. - """ - url = "/api/bcs/refresh" - name = "api:bcs_refresh" - requires_auth = True - - def __init__(self, core: Any) -> None: - self.core = core - - async def post(self, request: web.Request) -> web.Response: - try: - # We intentionally call a dedicated core method if present. - # Fallbacks are used to keep compatibility with older core versions. - if hasattr(self.core, "async_refresh_now"): - await self.core.async_refresh_now() - elif hasattr(self.core, "refresh_now"): - await self.core.hass.async_add_executor_job(self.core.refresh_now) - elif hasattr(self.core, "load_index"): - await self.core.hass.async_add_executor_job(self.core.load_index) - else: - _LOGGER.warning("No refresh method found on core; returning current cached data.") - except Exception as e: - _LOGGER.exception("Manual refresh failed: %s", e) - return web.json_response({"ok": False, "message": f"Refresh failed: {e}"}, status=500) - - return web.json_response( - {"ok": True, "repos": self.core.list_repos_public()} - ) - - class BCSCustomRepoView(HomeAssistantView): url = "/api/bcs/custom_repo" name = "api:bcs_custom_repo"