Hello everyone,
I have a whole bunch of entities for which it is forbidden to set the entity via yaml (e.g. MQTT entities).
Rather than going through all of them 1 by 1, I was wondering if it is possible to set their area via python script?
All of these entities have their area in their entity_id anyway, so it is easy to find them in the UI, but it is a pain to set them all via the UI.
I thought that a script would solve this in milliseconds but I am not sure if HA allows this and if so, how it is done.
If you want to hack this, you can just edit the entity registry file in your .storage directory (do backup before!). But I wouldn’t think it’s worth the hassle and possible errors for that one-time job.
Well, it is not a one-time job. Everytime I add a new Shelly or other MQTT device, I have to do it.
Everytime I wipe my HA setup (or setup a new test system), I have to do it.
I don’t know why it is not allowed to simply do this in the MQTT yaml file, but it is simply not allowed. And manually editing the .storage directory is too risky and far more work than having a python script run through all entity_ids and then setting the area according to the id.
I’m looking for exactly the same thing and for the same reasons. The name of the area is often in the name of the device and it would therefore be easy to deduce via pyscript to which area to assign it… so if you find a lead, I’m all ears.
Easily changed areas for devices with this pyscript viewed like a service in homeassistant.
Fill free to test an view log in home-assistant.log.
When ok, uncomment the line commented to apply.
Regards
import homeassistant
import re
@service
def update_device_areas():
devreg = homeassistant.helpers.device_registry.async_get(hass)
entreg = homeassistant.helpers.entity_registry.async_get(hass)
areareg = homeassistant.helpers.area_registry.async_get(hass)
area_ids = []
for a in areareg.areas.items():
id = a[0]
entry = a[1]
area_ids.append(entry.id)
for d in devreg.devices.items():
id = d[0]
entry = d[1]
for area_id in area_ids:
if entry.area_id == None:
words=entry.name.lower().replace('-',' ').replace('_',' ').split(' ')
if 'kitchen' in words:words.append('cuisine')
if 'nursery' in words:words.append('nurserie')
if 'cellar' in words:words.append('cellier')
if 'bathroom' in words:words.append('salle de bain')
if 'marie' in words:words.append('chambre de marie')
if 'jules' in words:words.append('chambre de jules')
if area_id.lower() in words:
log.warning(f"======> Will put device {entry.name} into area {area_id}")
# Uncomment the line below when you're happy that the script will make the correct changes
devreg.async_update_device(device_id=id, area_id=area_id)
else:
log.error(f"Don't know where is {entry.name}, not in {area_id}")