Hi all,
One thing that irritated me when I set up my own tabs, is that there was no way to catch all the other entities in the system that haven’t been grouped yet.
I’ve been working on this for a short while, and wanted to post it to get some feedback. I’m new to HA, and haven’t done anything in Python for years. Currently, this works on my system, but that is no guarantee it will work on anyone else’s system.
python_scripts/populate_catchall_group.py
ignore = data.get("domains_to_ignore","zone,group,automation,script,zwave")
domains_to_ignore=ignore.split(",")
target_group=data.get("target_group","group.catchall")
logger.info("ignoring {} domain(s)".format(len(domains_to_ignore)))
logger.info("Targetting group {}".format(target_group))
def scan_for_new_entities(hass, logger, domains_to_ignore, target_group):
entity_ids=[]
groups=[]
catchall=hass.states.get(target_group)
if (catchall is None):
logger.error("Target group {} doesn't exist".format(target_group))
return
for state in hass.states.all():
domain = state.entity_id.split(".")[0]
if (domain not in domains_to_ignore):
entity_ids.append(state.entity_id)
if (domain == "group") and (state.entity_id != target_group):
groups.append(state.entity_id)
logger.info("==== Entity count ====")
logger.info("{0} entities".format(len(entity_ids)))
logger.info("{0} groups".format(len(groups)))
for groupname in groups:
group = hass.states.get(groupname)
for a in group.attributes["entity_id"]:
if a in entity_ids:
entity_ids.remove(a)
attrs={}
for a in catchall.attributes:
if a != "entity_id":
attrs[a] = catchall.attributes[a]
entity_ids.insert(0,"script.scan_for_new_devices")
attrs["entity_id"]=entity_ids
hass.states.set("group.catchall", catchall.state, attrs)
scan_for_new_entities(hass, logger, domains_to_ignore, target_group)
In order for this to run on startup, I added a automation trigger
- alias: Get Ungrouped Entities On Start
hide_entity: true
trigger:
platform: homeassistant
event: start
action:
service: python_script.populate_catchall_group
data:
domains_to_ignore: "zone,group,automation,script,zwave"
target_group: "group.catchall"
There is also a regular script that calls the python script.
If the “catchall” group (or whatever you call it) is defined as a view, you actually end up with the sensor badges displayed across the top
Any feedback? questions? Is this something really basic you had all figured out yourselves already?