diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 2641961f5c3..428845031a5 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -95,6 +95,14 @@ def get_arguments(): type=int, default=None, help='Enables daily log rotation and keeps up to the specified days') + parser.add_argument( + '--install-osx', + action='store_true', + help='Installs as a service on OS X and loads on boot.') + parser.add_argument( + '--uninstall-osx', + action='store_true', + help='Uninstalls from OS X.') if os.name != "nt": parser.add_argument( '--daemon', @@ -152,6 +160,46 @@ def write_pid(pid_file): sys.exit(1) +def install_osx(): + """ Setup to run via launchd on OS X """ + with os.popen('which hass') as inp: + hass_path = inp.read().strip() + + with os.popen('whoami') as inp: + user = inp.read().strip() + + cwd = os.path.dirname(__file__) + template_path = os.path.join(cwd, 'startup', 'launchd.plist') + + with open(template_path, 'r', encoding='utf-8') as inp: + plist = inp.read() + + plist = plist.replace("$HASS_PATH$", hass_path) + plist = plist.replace("$USER$", user) + + path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist") + + try: + with open(path, 'w', encoding='utf-8') as outp: + outp.write(plist) + except IOError as err: + print('Unable to write to ' + path, err) + return + + os.popen('launchctl load -w -F ' + path) + + print("Home Assistant has been installed. \ + Open it here: http://localhost:8123") + + +def uninstall_osx(): + """ Unload from launchd on OS X """ + path = os.path.expanduser("~/Library/LaunchAgents/org.homeassistant.plist") + os.popen('launchctl unload ' + path) + + print("Home Assistant has been uninstalled.") + + def main(): """ Starts Home Assistant. """ validate_python() @@ -161,6 +209,14 @@ def main(): config_dir = os.path.join(os.getcwd(), args.config) ensure_config_path(config_dir) + # os x launchd functions + if args.install_osx: + install_osx() + return + if args.uninstall_osx: + uninstall_osx() + return + # daemon functions if args.pid_file: check_pid(args.pid_file) diff --git a/homeassistant/startup/__init__.py b/homeassistant/startup/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/homeassistant/startup/launchd.plist b/homeassistant/startup/launchd.plist new file mode 100644 index 00000000000..50bc18e3e38 --- /dev/null +++ b/homeassistant/startup/launchd.plist @@ -0,0 +1,36 @@ + + + + + Label + org.homeassitant + + EnvironmentVariables + + PATH + /usr/local/bin/:/usr/bin:$PATH + + + Program + $HASS_PATH$ + + AbandonProcessGroup + + + RunAtLoad + + + KeepAlive + + SuccessfulExit + + + + StandardErrorPath + /Users/$USER$/Library/Logs/homeassitant.log + + StandardOutPath + /Users/$USER$/Library/Logs/homeassitant.log + + + diff --git a/setup.py b/setup.py index ce8a75072ac..fde7f9bf898 100755 --- a/setup.py +++ b/setup.py @@ -12,7 +12,8 @@ PACKAGES = find_packages(exclude=['tests', 'tests.*']) PACKAGE_DATA = \ {'homeassistant.components.frontend': ['index.html.template'], 'homeassistant.components.frontend.www_static': ['*.*'], - 'homeassistant.components.frontend.www_static.images': ['*.*']} + 'homeassistant.components.frontend.www_static.images': ['*.*'], + 'homeassistant.startup': ['*.*']} REQUIRES = [ 'requests>=2,<3',