Closed pdbogen closed 4 years ago
I see it's the --name
argument on entity rename
; thanks!
no problem :) would love to hear more about your idempotent config-file based management ?
certainly. So, I'm very disheartened by the shift from configuration files to config flow. So I'm looking for ways that home-assistant-cli can help me keep my configuration as code.
My first cut of something for this, at the moment, is a shell script that uses hass-cli to set entity names for a given device:
$ cat files/entity-rename.sh
#!/bin/sh
#/ entity-rename [-d] <device id> <old prefix> <new prefix>
#/ -d -- dry run
usage() {
grep '^#/ ' "$0" | cut -c4- >&2
exit 1
}
DRY=""
while [ "$#" -gt 0 ]; do
case "$1" in
-d) DRY="echo "; shift ;;
-*) usage ;;
*) break ;;
esac
done
[ "$#" = 3 ] || usage
DEVICE_ID="$1"
OLD_PREFIX="$2"
NEW_PREFIX="$3"
set -e
. /etc/hass-cli.sh
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
hass-cli -o json entity list > "$tmp/entities.json"
jq --arg id "$DEVICE_ID" -r '.[] | select(.device_id==$id).entity_id' < "$tmp/entities.json" | while read entity; do
type="${entity%%.*}"
entity_part="${entity#*.}"
without_old="${entity_part#${OLD_PREFIX}}"
without_new="${entity_part#${NEW_PREFIX}}"
if [ "$entity_part" != "$without_old" ]; then
suffix="${without_old#_}"
elif [ "$entity_part" != "$without_new" ]; then
suffix="${without_new#_}"
else
echo "warning: found entity $entity with unknown prefix" >&2
continue
fi
name="$(jq --arg id "$entity" -r '.[] | select(.entity_id == $id).name' < "$tmp/entities.json")"
new_name="$(echo "${NEW_PREFIX}${suffix:+_${suffix}}" | sed 's/_/ /g')"
new_id="${type}.${NEW_PREFIX}${suffix:+_${suffix}}"
if [ "$name" = "$new_name" ] && [ "${entity}" = "${new_id}" ]; then
# nothing to do
continue
fi
$DRY hass-cli entity rename --name "${new_name}" "${entity}" "${new_id}"
done
It's idempotent, meaning that running it multiple times with the same argument won't have any effect (unless the state of homeassistant has drifted.)
Things I'm not happy with, though, are:
_temperature_1
and _temperature_2
etc sensors) ought to be for a given entity, so without knowing what prefix to remove, I can't determine what suffix to add to the new desired prefix.But, those caveats aside, it's working for me, so my ansible playbooks can have a step like:
- name: rename entities
shell: |
entity-rename.sh 5fbd537b0a814e2d82f94a41b7a8272f "shenzhen_neo_electronics_co_ltd_power_plug_12a" "green1"
entity-rename.sh a5d6f2fd039a430895444850962da6e1 "shenzhen_neo_electronics_co_ltd_power_plug_12a" "green2"
…to manage the naming of those specific entities.
Next up on my wishlist is to manage config flow entries directly, so I can more directly replicate the experience of using a config file even with HA's move away from infrastructure-grade architecture.
howdy!
thanks for building this. I'm working on using it to write some idempotent config-file based management to backfill what was ripped out in favor of the entity registry.
I have some code working that will rename a given device ID's entity IDs; but I can't find a way to set the friendly name. Can you advise how/if I can do this?
Thanks!