Yes all the memes are true! Once you start using Emacs (and especially once you get a taste of Emacs Lisp) you start wanting to do “everything” from Emacs!
I realize there are a lot of people here who prefer GUI. However from perspective of keyboard driven user like myself (tiling window manager, Emacs, etc.) it’s a hassle to reach for the mouse, switch to browser and use GUI. I prefer few keypresses in completing-read
style interface (without needing to leave comfort of Emacs, where I spend a lot of my computer time) and have cobbled together interfaces for almost everything this way by now. And the other day, I did for OpenHAB, too. So I wanted to share it.
There is only one function, which works via OpenHAB’s REST interface, so make sure you have enabled that. It’s a very simple function which operates on a list of Items you provide to it and allows you to change them to whatever states you define. No querying for state or anything fancy like that (because most of times I simply want to turn ON/OFF some light or similar…).
First define our variables and one function:
(defvar my-openhab-command-list '()
"List of possible commands to issue.
These will be used by the function `my-openhab-command' to select
a command to send.")
(defvar my-openhab-item-list '()
"List of item names to control.
These will be used by the function `my-openhab-command' to select
an item to command.")
(defvar my-openhab-base-url nil
"Full (base) URL of OpenHAB server.
Must include protocol (default is http) and port (default is
8080), e.g.:
http://192.168.1.20:8080
However do not include any parts of REST path after port.
Used by function `my-openhab-command' for its URL argument.")
(defun my-openhab-command (&optional item command)
"Send COMMAND to ITEM in OpenHAB via REST interface."
(interactive)
(let ((_item (or item
(car
(split-string
(completing-read "Item: "
my-openhab-item-list)))))
(_command (or command
(car
(split-string
(completing-read "Command: "
my-openhab-command-list))))))
(shell-command-to-string
(concat "curl -X POST "
"--header 'Content-Type: text/plain' "
"--header 'Accept: application/json' "
"-d '" _command "' "
my-openhab-base-url "/rest/items/" _item))))
Execute those forms above in the normal way (Emacs users should already know, but let me know if anyone needs help with this).
Then add the following settings to your init file, and execute them, too.
Now obviously these are some of my own items, I include them as examples but substitute your own.
(setq my-openhab-command-list '("ON" "OFF"))
(setq my-openhab-item-list '("i433_remote_button_1of5 Bedroom light string"
"i433_remote_button_2of5 Living room light string"
"i433_remote_button_3of5 Christmas palm tree light string"
"i433_remote_button_4of5 Front window seasonal light string"
"i433_remote_button_5of5 Dining room main light"))
(setq my-openhab-base-url "http://192.168.1.20:8080")
One important note about the format of the item list. For each item in list, the first “word” should be your actual item name in OpenHAB. Then (optionally) one or more spaces, followed by some disambiguation terms, reminders for yourself or friendly names, rooms or whatever you like. In completing-read
style interface (which above function implements), you just start typing whatever including for example “living room” and the selection list will get narrowed to this and you press RETURN. The function will take the whole line and then pick off the first contiguous “word” (space separated) and send that to OpenHAB along with your command (ON/OFF or whatever you have defined/selected).
Please do let me know if you end up using this. Mostly I am wondering if I am the only Emacs user here at OpenHAB.