Home Assistant Area slot program

Has anyone figured out a way to query areas and area ids from Home Assistant? I have a slot program based on the example tweaked to output (Friendly name):domain.id and would like similar behavior for areas.

After banging on this for a while, I finally found a simple-ish way forward. I had to add the asyncws library to the virtualenvironment in my Docker image:

FROM rhasspy/rhasspy
RUN ${APPDIR}/.venv/bin/python -m pip install asyncws

Here’s the script.


import asyncio
import json
import os

import asyncws


with open(os.environ["RHASSPY_PROFILE_DIR"] + "/profile.json", "r") as f:
    config = json.load(f)["home_assistant"]

token = config["access_token"]
url = config["url"].replace("http:", "ws:") + "/api/websocket"


@asyncio.coroutine
def work():
    websocket = yield from asyncws.connect(url)

    msg = yield from websocket.recv()
    assert json.loads(msg)["type"] == "auth_required"

    yield from websocket.send(
        json.dumps({"type": "auth", "access_token": token})
    )
    msg = yield from websocket.recv()
    assert json.loads(msg)["type"] == "auth_ok"

    yield from websocket.send(
        json.dumps({"id": 19, "type": "config/area_registry/list"})
    )
    msg = yield from websocket.recv()
    response = json.loads(msg)
    assert response["success"]

    for area in response["result"]:
        print(f"({area['name']}):{area['area_id']}")


asyncio.get_event_loop().run_until_complete(work())
asyncio.get_event_loop().close()

This is nice since Home Assistant now allows services to target areas as well as entities.

1 Like

Hi, total noob here. Can you please specify how to do this? Even the example you linked is misterious for me.

Thank you in advance!

This is awesome! I am pretty sure I can use this to build a slot program for entities with entity aliases. I have been looking for a way to pull entity aliases from the API for weeks now. Looks like i can use this with a call to “config/entity_registry/list” to get the entity list with aliases. Super kool!

I have to ask, where did you find that you could call this “type” in the WS call? I have been reading through the HA documentation and can’t find anything about this.

@thetic

I hope you don’t mind. I used your areas script and turned it into a shell script that can pull an entity list including entity aliases. The shell script works perfectly as slot_program in Rhasspy and allows you to define a list of names/aliases for all your devices in HA and control each device using any name.