Last active
January 28, 2025 10:34
-
-
Save rnorth/3bbce8595e48e555eb9c5e18ed16b05a to your computer and use it in GitHub Desktop.
Home Assistant pyscript to mass-update device areas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :/