Advertisement
techblog

populate catch all

Jul 12th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. ignore = data.get("domains_to_ignore","zone,group,automation,script,zwave")
  2. domains_to_ignore=ignore.split(",")
  3. target_group=data.get("target_group","group.catchall")
  4.  
  5. logger.info("ignoring {} domain(s)".format(len(domains_to_ignore)))
  6. logger.info("Targetting group {}".format(target_group))
  7.  
  8. def scan_for_new_entities(hass, logger, domains_to_ignore, target_group):
  9.   entity_ids=[]
  10.   groups=[]
  11.   catchall=hass.states.get(target_group)
  12.  
  13.   if (catchall is None):
  14.     attribs={"view":True, "friendly_name": "Ungrouped Items", "icon": "mdi:magnify"}
  15.     hass.states.set(target_group, "", attribs)
  16.     catchall=hass.states.get(target_group)
  17.  
  18.   for state in hass.states.all():
  19.     domain = state.entity_id.split(".")[0]
  20.     if (domain not in domains_to_ignore):
  21.       entity_ids.append(state.entity_id)
  22.     if (domain == "group") and (state.entity_id != target_group):
  23.       groups.append(state.entity_id)
  24.    
  25.  
  26.   logger.info("==== Entity count ====")
  27.   logger.info("{0} entities".format(len(entity_ids)))
  28.   logger.info("{0} groups".format(len(groups)))
  29.  
  30.   for groupname in groups:
  31.     group = hass.states.get(groupname)
  32.     for a in group.attributes["entity_id"]:
  33.       if a in entity_ids:
  34.         entity_ids.remove(a)
  35.  
  36.   attrs={}
  37.   for a in catchall.attributes:
  38.     if a != "entity_id":
  39.       attrs[a] = catchall.attributes[a]
  40.  
  41.   entity_ids.insert(0,"script.scan_for_new_devices")
  42.   attrs["entity_id"]=entity_ids
  43.   if len(entity_ids) > 1:
  44.     hass.states.set("group.catchall", catchall.state, attrs)
  45.  
  46. scan_for_new_entities(hass, logger, domains_to_ignore, target_group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement