Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Script to install/uninstall HA into OS X."""
2 
3 import os
4 import time
5 
6 # mypy: allow-untyped-calls, allow-untyped-defs
7 
8 
9 def install_osx():
10  """Set up to run via launchd on OS X."""
11  with os.popen("which hass") as inp:
12  hass_path = inp.read().strip()
13 
14  with os.popen("whoami") as inp:
15  user = inp.read().strip()
16 
17  template_path = os.path.join(os.path.dirname(__file__), "launchd.plist")
18 
19  with open(template_path, encoding="utf-8") as tinp:
20  plist = tinp.read()
21 
22  plist = plist.replace("$HASS_PATH$", hass_path)
23  plist = plist.replace("$USER$", user)
24 
25  path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
26 
27  try:
28  with open(path, "w", encoding="utf-8") as outp:
29  outp.write(plist)
30  except OSError as err:
31  print(f"Unable to write to {path}", err)
32  return
33 
34  os.popen(f"launchctl load -w -F {path}")
35 
36  print("Home Assistant has been installed. Open it here: http://localhost:8123")
37 
38 
40  """Unload from launchd on OS X."""
41  path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist")
42  os.popen(f"launchctl unload {path}")
43 
44  print("Home Assistant has been uninstalled.")
45 
46 
47 def run(args: list[str]) -> int:
48  """Handle OSX commandline script."""
49  commands = "install", "uninstall", "restart"
50  if not args or args[0] not in commands:
51  print("Invalid command. Available commands:", ", ".join(commands))
52  return 1
53 
54  if args[0] == "install":
55  install_osx()
56  return 0
57  if args[0] == "uninstall":
59  return 0
60  if args[0] == "restart":
62  # A small delay is needed on some systems to let the unload finish.
63  time.sleep(0.5)
64  install_osx()
65  return 0
66 
67  raise ValueError(f"Invalid command {args[0]}")
None open(self, **Any kwargs)
Definition: lock.py:86
int run(list[str] args)
Definition: __init__.py:47