Skip to content

Instantly share code, notes, and snippets.

@rnorth
Last active May 2, 2024 05:14
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnorth/3bbce8595e48e555eb9c5e18ed16b05a to your computer and use it in GitHub Desktop.
Save rnorth/3bbce8595e48e555eb9c5e18ed16b05a to your computer and use it in GitHub Desktop.
Home Assistant pyscript to mass-update device areas
import homeassistant
@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]
log.debug(f"Area: {entry.id} {entry.name}")
area_ids.append(entry.id)
for d in devreg.devices.items():
id = d[0]
entry = d[1]
log.debug(f"Device: {entry.name} {entry.area_id}")
for area_id in area_ids:
if area_id in entry.name and entry.area_id == None:
log.info(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)
@marbon87
Copy link

Thanks for sharing that script!
I am new to Home Assistant and have problems finding the logs. I do not find any output in the log under settings -> system -> logs.
Info log should be logged without special configuration, shouldn't it?

@rnorth
Copy link
Author

rnorth commented Apr 19, 2023 via email

@marbon87
Copy link

Thanks for your help. I do not see any log messages from the script in the config/homeassistant.log, even if i change the logging to warn in your script :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment