Description
I would like to get the area of an entity and all entities belonging to one area, but if I have not missed something there is no possibility to do this through pyscript directly.
This functionality would allow to create very universal automations, e.g. a window sensor could turn of all heaters in the same room when the window is opened and turn them back on when the window is closed and all other windows in the same room are closed as well. For this the area_id of entities needs to be accessible and entities need to be selected based on area_id and device type (domain, class).
I found some information in the dev docs of homeassistant and with the hass object I was able to retrieve this info:
import homeassistant
ent_reg = homeassistant.helpers.entity_registry.async_get(hass)
ent=ent_reg.async_get('<entity>')
area = ent_reg.async_get(ent).area_id
Unfortunately this is typically None, if entities inherit the area from their device. Therefore, first the device is needed:
import homeassistant
ent_reg = homeassistant.helpers.entity_registry.async_get(hass)
dev_reg = homeassistant.helpers.device_registry.async_get(hass)
ent=ent_reg.async_get('<entity>')
dev=ent.device_id
area = dev_reg.async_get(dev).area_id
Getting all devices of an area: homeassistant.helpers.device_registry.async_entries_for_area(dev_reg, area)
and from this all entities could be fetched and somehow filtered for domain/class.
That said, I have a couple of questions:
- Is this the correct way of doing it or is there a better way? (I am new to pyscipt and have never worked with hass internals before)
- could this be integrated into pyscript?
- If not I would probably write some helper functions which return the area_id for an entity or return entities based on area/domain/class
- Other thoughts?
(#138 might be related)