I’ve got a Beepy, I’m very happy with it and want to access and control my home from the command line.
Obviously the HTTP interface doesn’t work in lynx, and I can’t use an “app” and no command line clients seems to exist.
Fine, let’s write my own.
I know python well enough to do this.
But it seems the RestFul API doesn’t cover everything, e.g. you can’t find out areas
So I’m forced to use WebSockets, which I’m not familiar with.
I’m using the WebSockets python library.
The Home Assistant WebSocket API says I should connect and get “auth required”, which I do!
However, the library closes the connection, and when I start a new one and send the auth data I get the same “auth required” reply,
I have searched but not found anything that explains how to do this
(This guide doesn’t work, some library issues as far as I can tell, possibly something changed in the two years)
Can anyone explain how to do this??
My code so far is:
# Find our configuration
from pathlib import Path
configfile = Path.home().joinpath( '.config', 'hacli', 'config.ini')
# read it
import configparser
config = configparser.ConfigParser()
config.read(configfile)
import json
import asyncio
from websockets.sync.client import connect
def hello():
with connect(config['ha']['URL']) as websocket:
websocket.send("Hello world!")
message = websocket.recv()
print(f"Received: {message}")
with connect(config['ha']['URL']) as websocket:
websocket.send(
json.dumps({
"type": "auth",
"access_token": config['ha']['TOKEN']
})
)
anotherMessage = websocket.recv()
print(f"Received: {anotherMessage}")
hello()