I want to share the following 6 routines that I created to help manage Devices and Entities at their source level by allowing complete flexibility to Rename, Delete, and Move both Entities and Devices along with their underlying registry entries (in .storage
) and state, statistics, and metadata in home-assistant_v2.db
This allows for cleaner, more flexible, and more complete editing of devices and entities than is possible through the GUI. This also saves manual and error-prone messing with JSON registries and SQL databases.
All routines by default (optionally) backup any changed files or registries before writing to them. Also, they by default (optionally) stop ha core
before accessing or changing files. I also added a fair bit of error checking to prevent db and registry corruption and to avoid bone-headed mistakes.
CAUTION: These routines should only be used by āpower usersā who know what they are doing, are comfortable taking risks in manipulating core registry and database code, and want to make changes beyond what is possible with the non-technical GUI interface
Despite all the error checking, backups, and flow embedded in my code, there are certain to be bugs in my code and ways to cause damage to your databases that may require you in the worst case to restore from a full backup.
Please look through the code before using it so you know what you are doing. And of course use at your own risk! (though bug reports and suggestions are always welcome)
NOTE: Several use cases for this code are shared in subsequent postings
delete_device+entity
Delete devices and/or their corresponding entities completely from homeassistant registries & databases
- Delete devices ā.storageā info from:
core.device_registry - Delete entity ā.storageā info from:
core.entity_registry
core.restore_state
homeassistant.exposed_entities - Delete corresponding entity data from the following home-assistant_v2.db tables:
states_meta
states
statistics_meta
statistics
statistics_short_term
state_attributes
rename_device
Rename device from OLD_DEVICE_NAME to NEW_DEVICE_NAME and optionally rename name_by_user
to NEW_NAME_BY_USER
- Makes changes to device ā.storageā info in:
core.device_registry
rename_entity
Rename entity_id name from OLD_ENTITY_ID to NEW_ENTITY_ID and optionally:
- Renames the corresponding āunique_idā to NEW_UNIQUE_ID (in core.entity_register)
- Renames āfriendly_nameā to NEW_FRIENDLY_NAME (in core.restore_state and if present in HA db)
- Renames designated entities mentioned in configurations, automations, scripts (See variable: YAML_FILES_BASIC and YAML_FILES_ADDED) [USE carefully]
- Updates or adds a new attribute for the NEW_FRIENDLY_NAME (if given) and assigns to relevant states
-
Changes āentity_idā name in the following registries:
core.entity_register (also optionally changes āunique_idā)
core.restore_state (also optionally changes āfriendly_nameā)
homeassistant.exposed_entities
lovelace.dashboard_*
(plus designated yaml config files) -
Changes the following tables in home-assistant_v2.db:
states_meta (entity_id column)
statistics_meta (statistic_id column)
state_attributes (if NEW_FRIENDLY_NAME given)
states (if NEW_FRIENDLY_NAME given)
rename_device+entities
Rename device and associated entities from OLD_DEVICE_NAME to NEW_DEVICE_NAME
and optionally:
- Rename: name_by_user (for device ā see script ārename_deviceā for details)
- Rename: friendly_name (for entity ā see script ārename_entityā for details)
- Republish: MQTT discovery topic (and delete old one)
Note: Calls the scripts ārename_deviceā and ārename_entityā that are assumed to be in the same directory as this script (unless otherwise configured)
Note: There are a fair number of heuristics used here to make the MQTT publishing work and to update various derived device and entity entries based on the old and new device names. I wrote the heuristics based on the common MQTT sensors I have but YMMV and the heuristics may not work for you. If so, either modify the code or make any missing changes manually.
move_states
Move state metadata_ids from: (X1, X2,ā¦, Xn) ā (Y1, Y2,ā¦, Yn)
- Changes metadata_ids in tables:
states_meta
states
move_statistics
Move statistic metadata_idās from: (X1, X2,ā¦, Xn) ā (Y1, Y2,ā¦, Yn)
- Moves statistic metadata_ids in tables:
statistics_meta (id)
statistics (metadata_id)
statistics_short_term (metadata_id)