Skip to content

HomeAssistant Entity Renamer. A script to bulk rename homeassistant entities using regular expressions and without losing their history.

Notifications You must be signed in to change notification settings

danielgoepp/homeassistant-entity-renamer

 
 

Repository files navigation

HomeAssistant Entity Renamer

This project provides a Python script that allows you to list and rename entities in HomeAssistant based on regular expressions. It leverages the HomeAssistant API and WebSockets to interact with the HomeAssistant server.

The homeassistant-entity-renamer.py script provides the following functionality:

  • List entities: You can retrieve a list of entities in HomeAssistant and display their friendly names and entity IDs. You can optionally filter entities using a regular expression.
  • Rename entities: You can rename entities by specifying a search regular expression and a replace regular expression (see Python's re.sub()). The script will display a table with the current entity IDs, new entity IDs, and friendly names. It will ask for confirmation before renaming the entities.
  • Update entities from CSV: You can upload a CSV file containing entity IDs and new friendly names to update entity friendly names in bulk.
  • Search output written to CSV: The script now also writes the search output to a CSV file named output.csv in addition to printing it in the terminal.

Preserves the history of renamed entities since it uses the same code path for renaming as the HomeAssistant UI (which preserves history since the release 2022.4). See this websocket callback.

Tested on HomeAssistant 2023.7.2.

Requirements

  • Python 3.6 or above
  • Packages listed in requirements.txt

Usage

  1. Install the required packages by running the following command:

    pip install -r requirements.txt
    
  2. Rename config.py.example to config.py and modify the configuration variables according to your HomeAssistant setup.

  3. Run the script with the desired options:

    ./homeassistant-entity-renamer.py --search <search_regex> --replace <replace_regex>
    

    Replace <search_regex> with the regular expression that matches the entities you want to rename. Replace <replace_regex> with the regular expression used to rename the entities. Note that you can use all the regex magic that Python's re.sub() function allows.

  4. Check the output and confirm the renaming process if desired.

Usage (with docker)

  1. Rename config.py.example to config.py and modify the configuration variables according to your HomeAssistant setup.
  2. docker build -t homeassistant-renamer .
  3. docker run homeassistant-renamer --search sensor

Examples

$ ./homeassistant-entity-renamer.py --search "interesting"
| Friendly Name              | Entity ID                               |
|----------------------------|-----------------------------------------|
| Interesting Testbutton 1   | input_button.interesting_testbutton_1   |
| Interesting Testdropdown 1 | input_select.interesting_testdropdown_1 |
| Interesting Testentity 1   | input_button.interesting_testentity_1   |
| Interesting testnumber 1   | input_number.interesting_testnumber_1   |
| interesting testtext 1     |   input_text.interesting_testtext_1     |
$ ./homeassistant-entity-renamer.py --search "interesting_test(.*)_1" --replace "just_another_\1"
| Friendly Name              | Current Entity ID                       | New Entity ID                      |
|----------------------------|-----------------------------------------|------------------------------------|
| Interesting Testbutton 1   | input_button.interesting_testbutton_1   | input_button.just_another_button   |
| Interesting Testdropdown 1 | input_select.interesting_testdropdown_1 | input_select.just_another_dropdown |
| Interesting Testentity 1   | input_button.interesting_testentity_1   | input_button.just_another_entity   |
| Interesting testnumber 1   | input_number.interesting_testnumber_1   | input_number.just_another_number   |
| interesting testtext 1     |   input_text.interesting_testtext_1     |   input_text.just_another_text     |

Do you want to proceed with renaming the entities? (y/N): 
Renaming process aborted.
$ ./homeassistant-entity-renamer.py --search "interesting_test(.*)_1" --replace "just_another_\1"
| Friendly Name              | Current Entity ID                       | New Entity ID                      |
|----------------------------|-----------------------------------------|------------------------------------|
| Interesting Testbutton 1   | input_button.interesting_testbutton_1   | input_button.just_another_button   |
| Interesting Testdropdown 1 | input_select.interesting_testdropdown_1 | input_select.just_another_dropdown |
| Interesting Testentity 1   | input_button.interesting_testentity_1   | input_button.just_another_entity   |
| Interesting testnumber 1   | input_number.interesting_testnumber_1   | input_number.just_another_number   |
| interesting testtext 1     |   input_text.interesting_testtext_1     |   input_text.just_another_text     |

Do you want to proceed with renaming the entities? (y/N): y
Entity 'input_button.interesting_testbutton_1' renamed to 'input_button.just_another_button' successfully!
Entity 'input_select.interesting_testdropdown_1' renamed to 'input_select.just_another_dropdown' successfully!
Entity 'input_button.interesting_testentity_1' renamed to 'input_button.just_another_entity' successfully!
Entity 'input_number.interesting_testnumber_1' renamed to 'input_number.just_another_number' successfully!
Entity 'input_text.interesting_testtext_1' renamed to 'input_text.just_another_text' successfully!

Acknowledgements

This project was developed in cooperation with ChatGPT, a large language model trained by OpenAI, based on the GPT-3.5 architecture.

Feel free to explore and modify the script to suit your specific needs. If you encounter any issues or have suggestions for improvements, please submit them to the project's issue tracker.

About

HomeAssistant Entity Renamer. A script to bulk rename homeassistant entities using regular expressions and without losing their history.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 97.9%
  • Dockerfile 2.1%