From 704c9041ea32378bb54e2a1b82f78b1c7026cb7a Mon Sep 17 00:00:00 2001 From: Martin Ziegler Date: Mon, 25 Nov 2024 15:01:52 +0100 Subject: [PATCH] Fixed typo in async def async_setup_entry --- .../bluelab_guardian/__init__.py | 70 +++++++------------ 1 file changed, 26 insertions(+), 44 deletions(-) diff --git a/custom_components/bluelab_guardian/__init__.py b/custom_components/bluelab_guardian/__init__.py index b46200a..c8feff9 100644 --- a/custom_components/bluelab_guardian/__init__.py +++ b/custom_components/bluelab_guardian/__init__.py @@ -13,50 +13,32 @@ _LOGGER = logging.getLogger(__name__) -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Set up Bluelab Guardian from a config entry.""" - - # Copy static files asynchronously - await copy_static_files(hass) - - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = {} - - api_token = entry.data[CONF_API_TOKEN] - organization_id = entry.data.get("organization_id") - headers = {"Authorization": api_token} - - device_list_url = f"{DEVICE_LIST_URL}{organization_id}" - try: - response = await hass.async_add_executor_job(lambda: requests.get(device_list_url, headers=headers)) - response.raise_for_status() - devices = response.json() - _LOGGER.info("Devices fetched: %s", devices) - hass.data[DOMAIN][entry.entry_id]["devices"] = devices - except requests.RequestException as err: - _LOGGER.error("Failed to fetch devices: %s", err) - return False - - # Forward entry setup to sensor and binary_sensor platforms - await hass.config_entries.async_forward_entry_setups(entry, ["sensor", "binary_sensor"]) - - # Trigger initial telemetry and attribute updates - await async_update_telemetry(hass, entry) # Immediate telemetry update - await async_update_device_attributes(hass, entry) # Immediate attributes update - - # Schedule telemetry and attribute updates with event-loop-safe scheduling - async def schedule_telemetry_update(now): - await async_update_telemetry(hass, entry) - - async def schedule_attribute_update(now): - await async_update_device_attributes(hass, entry) - - # Use event-loop-safe scheduling - async_track_time_interval(hass, schedule_telemetry_update, TELEMETRY_UPDATE_INTERVAL) - async_track_time_interval(hass, schedule_attribute_update, ATTRIBUTE_UPDATE_INTERVAL) - - return True - +async def copy_static_files(hass: HomeAssistant): + """Copy static assets to the www directory.""" + src_dir = os.path.join(os.path.dirname(__file__)) + dst_dir = os.path.join(hass.config.path("www"), "custom_components", "bluelab_guardian") + + _LOGGER.debug("Source directory: %s", src_dir) + _LOGGER.debug("Destination directory: %s", dst_dir) + + # Ensure the destination directory exists + os.makedirs(dst_dir, exist_ok=True) + + # Copy the icon and logo files + for file_name in ["icon.png", "logo.png"]: + src_file = os.path.join(src_dir, file_name) + dst_file = os.path.join(dst_dir, file_name) + + _LOGGER.debug("Copying %s to %s", src_file, dst_file) + + if os.path.exists(src_file): + async with aiofiles.open(src_file, "rb") as src: + async with aiofiles.open(dst_file, "wb") as dst: + while chunk := await src.read(1024 * 1024): # Read in chunks + await dst.write(chunk) + else: + _LOGGER.error("File %s does not exist", src_file) + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Bluelab Guardian from a config entry."""