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!