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