New slot program for Home Assistant entities with aliases

Hi everyone,

I wanted to share my recent work. This post Home Assistant Area slot program caught my eye recently. I was able to use the code this person wrote as a basis for a new entities slot program. This one displays a listing for each possible name in HA for each entity, including ALIASES!! I have been working to get aliases into Rhasspy for some time and this program seems to be working. Take a look and provide some feedback.

#!/bin/sh
''''exec python3 -u -- "$0" ${1+"$@"} # '''
# vi: syntax=python

import asyncio
import json
import os
import sys

try:
  import asyncws
except ImportError:
  print("Trying to Install required module: asyncws\n")
  os.system('python3 -m pip install asyncws')
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():
    tranNum = 10
    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": tranNum, "type": "config/device_registry/list"})
    )
    msg = yield from websocket.recv()
    response = json.loads(msg)
    assert response["success"]
    tranNum = tranNum + 1
    
    device_registry = response["result"]

    yield from websocket.send(
        json.dumps({"id": tranNum, "type": "config/entity_registry/list"})
    )
    msg = yield from websocket.recv()
    response = json.loads(msg)
    assert response["success"]
    tranNum = tranNum + 1
    
    entity_registry = response["result"]
    
    for entity in entity_registry:
        if len(sys.argv) > 1:
            entity_id = entity['entity_id']
            entity_domain = entity_id[:entity_id.find('.')]
            for arg in sys.argv:
                if entity_domain == arg:
                    hadevice = next((device for device in device_registry if device['id'] == entity['device_id']), None)
                    if hadevice == None:
                        print(f"({entity['original_name']}):{entity['entity_id']}")
                        if entity['name'] != None:
                            if len(entity['name']) > 0:
                                print(f"({entity['name']}):{entity['entity_id']}")
                    else:
                        if hadevice['name_by_user'] != None:
                            print(f"({hadevice['name_by_user']}):{entity['entity_id']}")
                        else:
                            print(f"({hadevice['name']}):{entity['entity_id']}")
                        if entity['name'] != None:
                            if len(entity['name']) > 0:
                                print(f"({entity['name']}):{entity['entity_id']}")
                        if entity['original_name'] != None:
                            if len(entity['original_name']) > 0:
                                print(f"({entity['original_name']}):{entity['entity_id']}")
                                
                    yield from websocket.send(
                        json.dumps({"id": tranNum, "type": "config/entity_registry/get", "entity_id": entity['entity_id']})
                    )
                    msg = yield from websocket.recv()
                    response = json.loads(msg)
                    assert response["success"]
                    tranNum = tranNum + 1
                    
                    entity_details = response["result"]

                    if 'aliases' in entity_details.keys():
                        print("aliases")
                        for alias in entity['aliases']:
                            print(f"({alias}):{entity['entity_id']}")
        else:
            print(f"({entity['original_name']}):{entity['entity_id']}")

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

This is exactly what I was looking for, could add areas too.

The original script came from someone who did areas in a separate slot program. I suppose I could combine them and give the object based on command line argument but I felt have 2 slot programs for the 2 distinct objects types was ok.

Here is the areas slot program that @thetic wrote and I based my program off:

If there is a big call for it I can combine the 2.

I’m guessing with HA’s year of the voice this is going to become easier soon. But I fear that may come for Rhasspy3 (maybe not), and I wanted something ASAP so I could start actually using Rhasspy in the home. And with the family we need a way to allow for each person’s nuance speech. The big 0one in my house is I call it the “Living room” and my wife calls it the “Parlor” so we need to be able to identify a light by both “living room light” and “parlor light”. Otherwise Rhasspy would only work for one of us. This one script made the whole thing 10000% times more usable.