| 432 | """Save a file.""" |
| 433 | |
| 434 | def _write_file(): |
| 435 | with open( |
| 436 | file_path, |
| 437 | mode="w" if isinstance(content, str) else "wb", |
| 438 | encoding="utf-8" if isinstance(content, str) else None, |
| 439 | errors="ignore" if isinstance(content, str) else None, |
| 440 | ) as file_handler: |
| 441 | file_handler.write(content) |
| 442 | |
| 443 | # Create gz for .js files |
| 444 | if os.path.isfile(file_path): |
| 445 | if file_path.endswith(".js"): |
| 446 | with open(file_path, "rb") as f_in: |
| 447 | with gzip.open(file_path + ".gz", "wb") as f_out: |
| 448 | shutil.copyfileobj(f_in, f_out) |
| 449 | |
| 450 | # LEGACY! Remove with 2.0 |
| 451 | if "themes" in file_path and file_path.endswith(".yaml"): |
| 452 | filename = file_path.split("/")[-1] |
| 453 | base = file_path.split("/themes/")[0] |
| 454 | combined = f"{base}/themes/{filename}" |
| 455 | if os.path.exists(combined): |
| 456 | self.log.info("Removing old theme file %s", combined) |
| 457 | os.remove(combined) |
| 458 | |
| 459 | try: |
| 460 | await self.hass.async_add_executor_job(_write_file) |