2025-09-22 21:45:51 +03:00

60 lines
2.1 KiB
Python

import asyncio
import argparse
import json
from enclave_shelly import ShellyBase
async def main():
parser = argparse.ArgumentParser(description="Queries the status of a Shelly device.")
parser.add_argument("url", type=str, help="The IP or hostname of the Shelly device.")
subparsers = parser.add_subparsers(title="commands")
list_parser = subparsers.add_parser("list", help="List all scripts.")
list_parser.set_defaults(command="list")
create_parser = subparsers.add_parser("create", help="Create a script.")
create_parser.add_argument("name", type=str, help="Name of the script.")
create_parser.set_defaults(command="create")
delete_parser = subparsers.add_parser("delete", help="Delete a script.")
delete_parser.add_argument("id", type=int, help="ID of the script.")
delete_parser.set_defaults(command="delete")
get_code = subparsers.add_parser("code", help="Get the code of a script.")
get_code.add_argument("id", type=int, help="ID of the script.")
get_code.set_defaults(command="code")
upload_code = subparsers.add_parser("upload", help="Upload a file to the script.")
upload_code.add_argument("id", type=int, help="ID of the script.")
upload_code.add_argument("path", type=str, help="Path to the file to upload.")
upload_code.set_defaults(command="upload")
args = parser.parse_args()
shelly = ShellyBase(args.url)
if args.command == "list":
scripts = await shelly.get_scripts()
print(scripts)
elif args.command == "create":
script = await shelly.create_script(args.name)
print(script)
elif args.command == "delete":
result = await shelly.delete_script(args.id)
print(json.dumps(result, indent=4))
elif args.command == "code":
script = await shelly.get_script(args.id)
code = await script.get_code()
print(code)
elif args.command == "upload":
script = await shelly.get_script(args.id)
with open(args.path) as f:
code = f.read()
await script.put_code(code)
print("Upload successful.")
if __name__ == "__main__":
asyncio.run(main())