Skip to content

Add search integration#30511

Merged
balloob merged 7 commits into
devfrom
add-search
Jan 10, 2020
Merged

Add search integration#30511
balloob merged 7 commits into
devfrom
add-search

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Jan 5, 2020

Description:

Our data is interconnected, making it a graph. This means that we can also search it as a graph. This integration can give you related items based on any node in the graph. Currently implemented areas, devices, entities, config entry, scene. Leaving script/automation for a future PR.

image

Goal of this integration will be to provide related data in the UI. Example use cases:

  • Show scenes that impact a specific area
  • List automations that use a specific input_boolean

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Comment thread homeassistant/components/search/__init__.py Outdated
Comment thread homeassistant/components/search/__init__.py Outdated
@balloob
Copy link
Copy Markdown
Member Author

balloob commented Jan 6, 2020

I've added support for scenes and config entries.

When we find a config entry, scene, automation or script (the outputs!), we will no longer further resolve it into subtypes. Because if I query for a device, I don't want to return all devices that share a scene or config entry with the device that I am querying for.

It's still possible to pick any of those as an entry point, in which case they will be resolved. So these types are search type only.

Support for script/automation should move to a new PR to no longer further complicate this.

@balloob balloob requested a review from a team as a code owner January 6, 2020 18:46
@balloob
Copy link
Copy Markdown
Member Author

balloob commented Jan 6, 2020

This is ready for approval/merging.

Comment thread homeassistant/components/search/__init__.py Outdated
Comment thread homeassistant/components/search/__init__.py Outdated
@awarecan
Copy link
Copy Markdown
Contributor

awarecan commented Jan 7, 2020

The result is not a graph, it only contains all vertices, but missing the edges.

We can extend the search methods to return a tuple (vertices, edges).

self.results = defaultdict(set)  #vertices
self.edges = defaultdict(set)

# in Searcher class
def search(self, item_type, item_id):
  self.results[item_type].add(item_id)
  self._to_resolve.add((item_type, item_id))

  while self._to_resolve:
    search_type, search_id = self._to_resolve.pop()
    await getattr(self, f"_resolve_{search_type}")(search_id)

  return (self.results, self.edges)

# for example device
def _resolve_device(self, device_id) -> None:
  device_entry = self._device_reg.async_get(device_id)
  if device_entry is not None:
    if device_entry.area_id:
      self._add_or_resolve("area", device_entry.area_id)
      # add edge
      self.edges[("device", device_id)].add(("area", device_entry.area_id))
    for config_entry_id in device_entry.config_entries:
      self._add_or_resolve("config_entry", config_entry_id)
      # add edge
      self.edges[("device", device_id)].add(("config_entry", config_entry_id))

  for entity_entry in entity_registry.async_entries_for_device(
    self._entity_reg, device_id
  ):
    self._add_or_resolve("entity", entity_entry.entity_id)
    # add edge
    self.edges[("device", device_id)].add(("entity", entity_entry.entity_id))

self.results["entity"] -= self.results["automation"]

# Remove entry into graph from search results.
self.results[item_type].remove(item_id)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep it in vertices if we return a graph

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Jan 7, 2020

Well I wonder if we should return it as a graph? The user just wants to see related data. It doesn't care much about the relationship maybe?

Comment thread homeassistant/components/homeassistant/scene.py Outdated
@Jc2k
Copy link
Copy Markdown
Member

Jc2k commented Jan 7, 2020

Nice!

Will this gain other ways of searching in the future or is it just for finding related objects? (Wondering if there is a more descriptive name if its just for finding related objects).

@balloob
Copy link
Copy Markdown
Member Author

balloob commented Jan 7, 2020

@Jc2k for now it's just related items. I'll update the websocket endpoint.

@balloob balloob merged commit 3348f4f into dev Jan 10, 2020
@delete-merged-branch delete-merged-branch Bot deleted the add-search branch January 10, 2020 18:57
@lock lock Bot locked and limited conversation to collaborators Jan 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants