Python, WebSockets, command line client, help me understand!

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()

I don’t know all the details of what you’re using, but I don’t think it’s the library.

You’re creating the socket context twice (two with statements). Keep the context and do everything within that context. Also check whether there isn’t some session object you need to use to maintain state.

I have created it twice, if I don’t then the second send() results in

websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)

However, I’m not having success with websocket-client and can now actually get data from Home Assistant.

My next hurdle is working out how I’m supposed to associate anything to anywhere.
For example I can get a list of areas and/or a list of panels. These have no way to list what is in them.

— Some time later! —

Whoever decided on this structure doesn’t seem to like making things easy!

I have 112 lines of code and all it does is pull down all the areas, pull down all the devices, match them to areas, pull down all the entities, match those to devices, pull down all states, match those to entities.

Show me all the areas, with as count of devices, let me pick an area, and then show me all the devices in that area, with all of their entities, and the status of each entity - if it has one.

e.g.

	Vindriktning-02
		Living Room Particulate Matter 2.5µm Concentration 73µg/m³
		Living Room CO2 494ppm
		Living Room Temperature 22.61°C
		Living Room Humidity 54.90%