API for changing entities

I’m looking into writing a python script for bulk renaming entities to make it easier to switch back and forth between OpenZWave and Z-Wave JS. It doesn’t look like you get this level of control with the built-in python_script system. I’m wondering if anyone could point me to an API or interface that could do this programmatically.

Ideally this would remain a simple single script and not a whole custom component but I’m open to all options.

One idea I have is to log the requests made by the web interface and write a script to pretend to be coming from the browser. But ideally if there’s a more direct python api that’d be nice.

Are you suggesting there’s a REST API for changing entity names? I might be missing it, but I don’t see it in the documentation.

The frontend uses a websocket message of type config/entity_registry/update from what I see in the Chrome inspector:

{
  "type": "config/entity_registry/update",
  "entity_id": "sensor.basement_motion_temperature",
  "name": null,
  "icon": null,
  "area_id": null,
  "new_entity_id": "sensor.basement_motion_temperature_2",
  "id": 29
}

I don’t see that documented but seems straightforward enough to reverse engineer.

Here’s a proof of concept script that just renames a single entity.

#!/usr/bin/python3

import asyncio
import json
import asyncws
import os

ACCESS_TOKEN = os.environ.get('HA_ACCESS_TOKEN')

async def main():
    websocket = await asyncws.connect('ws://homeassistant.local:8123/api/websocket')

    await websocket.send(json.dumps(
        {'type': 'auth',
         'access_token': ACCESS_TOKEN}
    ))

    await websocket.send(json.dumps(
        {
            'id': 29,
            "type": "config/entity_registry/update",
            "entity_id": "switch.basement_light",
            "new_entity_id": "switch.basement_light_2",
        }
    ))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()