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)